mirror of
https://github.com/Moepchi/webspeak3.git
synced 2026-12-03 18:02:49 +08:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7d14c3409e |
@@ -47,16 +47,23 @@ def xrpc(method: str, payload: dict | bytes, token: str | None = None, content_t
|
|||||||
return json.load(res)
|
return json.load(res)
|
||||||
|
|
||||||
|
|
||||||
def fetch(url: str, retries: int = 2) -> tuple[bytes, str]:
|
def fetch(url: str, attempt: int = 1) -> tuple[bytes, str]:
|
||||||
req = urllib.request.Request(url, headers={"User-Agent": "webspeak3-release-announcer"})
|
req = urllib.request.Request(url, headers={"User-Agent": "webspeak3-release-announcer"})
|
||||||
try:
|
try:
|
||||||
with urllib.request.urlopen(req, timeout=30) as res:
|
with urllib.request.urlopen(req, timeout=30) as res:
|
||||||
return res.read(), res.headers.get_content_type()
|
return res.read(), res.headers.get_content_type()
|
||||||
except urllib.error.HTTPError as err:
|
except urllib.error.HTTPError as err:
|
||||||
if err.code != 429 or not retries: # GitHub's image service throttles bursts now and then
|
# GitHub's image service throttles right after a release, often for over a minute.
|
||||||
|
# 15+30+45+60 s stays well inside the job's 5-minute timeout.
|
||||||
|
if err.code != 429 or attempt > 4:
|
||||||
raise
|
raise
|
||||||
time.sleep(10)
|
wait = 15 * attempt
|
||||||
return fetch(url, retries - 1)
|
retry_after = err.headers.get("Retry-After", "")
|
||||||
|
if retry_after.isdigit():
|
||||||
|
wait = min(int(retry_after), 60)
|
||||||
|
print(f"Preview image throttled (429), retrying in {wait} s.")
|
||||||
|
time.sleep(wait)
|
||||||
|
return fetch(url, attempt + 1)
|
||||||
|
|
||||||
|
|
||||||
def link_card(release: dict, text: str) -> dict:
|
def link_card(release: dict, text: str) -> dict:
|
||||||
|
|||||||
Reference in new issue
Block a user