no use to redirect from hf.co/spaces/pup-py/fetch
Browse files- fetch.py +13 -14
- pyproject.toml +1 -1
fetch.py
CHANGED
@@ -1,12 +1,11 @@
|
|
1 |
from apscheduler.executors.asyncio import AsyncIOExecutor
|
2 |
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
3 |
from contextlib import asynccontextmanager
|
4 |
-
from datetime import datetime
|
5 |
from fastapi import FastAPI, Request
|
6 |
from fastapi.responses import (
|
7 |
FileResponse,
|
8 |
PlainTextResponse,
|
9 |
-
RedirectResponse,
|
10 |
Response,
|
11 |
)
|
12 |
from os import environ, getenv
|
@@ -18,6 +17,10 @@ import json
|
|
18 |
import subprocess
|
19 |
|
20 |
LOGFILE = Path.home() / "a.json"
|
|
|
|
|
|
|
|
|
21 |
|
22 |
|
23 |
class PrettyJSONResponse(Response):
|
@@ -49,21 +52,18 @@ def read_root(request: Request) -> Response:
|
|
49 |
"""
|
50 |
|
51 |
query_params = dict(request.query_params)
|
52 |
-
if "ping" in query_params:
|
53 |
-
self_host = getenv("SPACE_HOST", "0.0.0.0:7860")
|
54 |
-
return RedirectResponse(f"http://{self_host}/ping")
|
55 |
|
56 |
# python version
|
57 |
py_ver = query_params.pop("python", "3.12")
|
58 |
if "Windows" in request.headers.get("user-agent"):
|
59 |
-
pup_url =
|
60 |
script = [
|
61 |
f"$pup_ps1 = (iwr -useb {pup_url}).Content",
|
62 |
f"& ([scriptblock]::Create($pup_ps1)) {py_ver}",
|
63 |
]
|
64 |
hint = f"""iex (iwr "{request.url}").Content"""
|
65 |
else:
|
66 |
-
pup_url =
|
67 |
script = [f"curl -fsSL {pup_url} | bash -s {py_ver}"]
|
68 |
hint = f"""curl -fsSL "{request.url}" | bash"""
|
69 |
|
@@ -152,17 +152,16 @@ async def ping():
|
|
152 |
|
153 |
|
154 |
def self_ping():
|
155 |
-
|
156 |
-
|
157 |
with httpx.Client() as client:
|
158 |
-
|
159 |
-
_ = client.get(
|
160 |
|
161 |
|
162 |
-
self_ping()
|
163 |
-
|
164 |
scheduler = AsyncIOScheduler(executors={"default": AsyncIOExecutor()})
|
165 |
-
scheduler.add_job(self_ping,
|
|
|
166 |
|
167 |
|
168 |
if __name__ == "__main__":
|
|
|
1 |
from apscheduler.executors.asyncio import AsyncIOExecutor
|
2 |
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
3 |
from contextlib import asynccontextmanager
|
4 |
+
from datetime import datetime, timedelta
|
5 |
from fastapi import FastAPI, Request
|
6 |
from fastapi.responses import (
|
7 |
FileResponse,
|
8 |
PlainTextResponse,
|
|
|
9 |
Response,
|
10 |
)
|
11 |
from os import environ, getenv
|
|
|
17 |
import subprocess
|
18 |
|
19 |
LOGFILE = Path.home() / "a.json"
|
20 |
+
PUP_URLS = {
|
21 |
+
"Linux": "https://raw.githubusercontent.com/liquidcarbon/puppy/main/pup.sh",
|
22 |
+
"Windows": "https://raw.githubusercontent.com/liquidcarbon/puppy/main/pup.ps1",
|
23 |
+
}
|
24 |
|
25 |
|
26 |
class PrettyJSONResponse(Response):
|
|
|
52 |
"""
|
53 |
|
54 |
query_params = dict(request.query_params)
|
|
|
|
|
|
|
55 |
|
56 |
# python version
|
57 |
py_ver = query_params.pop("python", "3.12")
|
58 |
if "Windows" in request.headers.get("user-agent"):
|
59 |
+
pup_url = PUP_URLS.get("Windows")
|
60 |
script = [
|
61 |
f"$pup_ps1 = (iwr -useb {pup_url}).Content",
|
62 |
f"& ([scriptblock]::Create($pup_ps1)) {py_ver}",
|
63 |
]
|
64 |
hint = f"""iex (iwr "{request.url}").Content"""
|
65 |
else:
|
66 |
+
pup_url = PUP_URLS.get("Linux")
|
67 |
script = [f"curl -fsSL {pup_url} | bash -s {py_ver}"]
|
68 |
hint = f"""curl -fsSL "{request.url}" | bash"""
|
69 |
|
|
|
152 |
|
153 |
|
154 |
def self_ping():
|
155 |
+
self_host1 = getenv("SPACE_HOST", "0.0.0.0:7860")
|
156 |
+
self_host2 = "https://huggingface.co/spaces/pup-py/fetch"
|
157 |
with httpx.Client() as client:
|
158 |
+
_ = client.get(f"http://{self_host1}/ping", follow_redirects=True)
|
159 |
+
_ = client.get(self_host2, follow_redirects=True)
|
160 |
|
161 |
|
|
|
|
|
162 |
scheduler = AsyncIOScheduler(executors={"default": AsyncIOExecutor()})
|
163 |
+
scheduler.add_job(self_ping, next_run_time=datetime.now() + timedelta(seconds=30))
|
164 |
+
scheduler.add_job(self_ping, "interval", minutes=1)
|
165 |
|
166 |
|
167 |
if __name__ == "__main__":
|
pyproject.toml
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
[project]
|
2 |
name = "fetch"
|
3 |
-
version = "0.5.
|
4 |
description = "Puppy Installer"
|
5 |
authors = [
|
6 |
{ name = "Alex Kislukhin" }
|
|
|
1 |
[project]
|
2 |
name = "fetch"
|
3 |
+
version = "0.5.1"
|
4 |
description = "Puppy Installer"
|
5 |
authors = [
|
6 |
{ name = "Alex Kislukhin" }
|