admin commited on
Commit ·
1b614cd
1
Parent(s): 79f0fcf
add timeout
Browse files
app.py
CHANGED
|
@@ -7,6 +7,9 @@ from datetime import datetime
|
|
| 7 |
from zoneinfo import ZoneInfo
|
| 8 |
from tzlocal import get_localzone
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
def timestamp(naive_time: datetime = None, target_tz=ZoneInfo("Asia/Shanghai")):
|
| 12 |
if not naive_time:
|
|
@@ -23,7 +26,7 @@ def download_file(url, video_id, cache_dir="./__pycache__"):
|
|
| 23 |
|
| 24 |
os.makedirs(cache_dir)
|
| 25 |
local_file = f"{cache_dir}/{video_id}.mp4"
|
| 26 |
-
response = requests.get(url, stream=True)
|
| 27 |
if response.status_code == 200:
|
| 28 |
with open(local_file, "wb") as file:
|
| 29 |
for chunk in response.iter_content(chunk_size=8192):
|
|
@@ -54,7 +57,7 @@ def get_real_url(short_url):
|
|
| 54 |
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36"
|
| 55 |
},
|
| 56 |
allow_redirects=True,
|
| 57 |
-
timeout=
|
| 58 |
).url.split("/?")[0]
|
| 59 |
|
| 60 |
|
|
@@ -69,7 +72,7 @@ def infer(video_url: str, p: int):
|
|
| 69 |
if "b23.tv" in video_url:
|
| 70 |
video_url = get_real_url(video_url)
|
| 71 |
|
| 72 |
-
response = requests.get(
|
| 73 |
response_json = response.json()
|
| 74 |
retcode = response_json["code"]
|
| 75 |
if retcode == 1:
|
|
|
|
| 7 |
from zoneinfo import ZoneInfo
|
| 8 |
from tzlocal import get_localzone
|
| 9 |
|
| 10 |
+
TIMEOUT = 10
|
| 11 |
+
API = os.getenv("api_bili")
|
| 12 |
+
|
| 13 |
|
| 14 |
def timestamp(naive_time: datetime = None, target_tz=ZoneInfo("Asia/Shanghai")):
|
| 15 |
if not naive_time:
|
|
|
|
| 26 |
|
| 27 |
os.makedirs(cache_dir)
|
| 28 |
local_file = f"{cache_dir}/{video_id}.mp4"
|
| 29 |
+
response = requests.get(url, stream=True, timeout=TIMEOUT)
|
| 30 |
if response.status_code == 200:
|
| 31 |
with open(local_file, "wb") as file:
|
| 32 |
for chunk in response.iter_content(chunk_size=8192):
|
|
|
|
| 57 |
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36"
|
| 58 |
},
|
| 59 |
allow_redirects=True,
|
| 60 |
+
timeout=TIMEOUT,
|
| 61 |
).url.split("/?")[0]
|
| 62 |
|
| 63 |
|
|
|
|
| 72 |
if "b23.tv" in video_url:
|
| 73 |
video_url = get_real_url(video_url)
|
| 74 |
|
| 75 |
+
response = requests.get(API, params={"url": video_url}, timeout=TIMEOUT)
|
| 76 |
response_json = response.json()
|
| 77 |
retcode = response_json["code"]
|
| 78 |
if retcode == 1:
|