lgdl.py: stall detection + fresh-start wipe + speed guard; MANIFEST queue status

This commit is contained in:
Dmitry Kokorin 2026-09-15 13:23:18 +03:00
parent b155cf2cb1
commit 8b632f1229
2 changed files with 35 additions and 2 deletions

View file

@ -98,13 +98,14 @@ def sniff(data: bytes) -> str:
return "unknown"
def download(f_id, out_dir, name, tries=8):
def download(f_id, out_dir, name, tries=16):
fi = file_info(f_id)
md5, size, ext = fi["md5"], int(fi.get("filesize") or 0), fi.get("extension") or "bin"
if not name.lower().endswith("." + ext.lower()):
name = f"{name}.{ext}"
os.makedirs(out_dir, exist_ok=True)
out = os.path.join(out_dir, name)
stall = 0
for t in range(1, tries + 1):
card = None
for c in range(5): # card fetch with backoff (libgen DB is flaky)
@ -125,13 +126,28 @@ def download(f_id, out_dir, name, tries=8):
continue
url = m.group(1)
tmp = out + ".part"
prev = os.path.getsize(tmp) if os.path.exists(tmp) else 0
try:
rc = curl(url, out=tmp, referer=f"{BASE}/ads.php?md5={md5}", binary=True,
timeout=900, extra=["-C", "-"])
timeout=3600,
# abort hung connections: <500B/s for 300s
extra=["-C", "-", "--speed-time", "300", "--speed-limit", "500"])
except RuntimeError as e:
print(f" try {t}: fetch error ({e}) — back off")
time.sleep(45)
continue
cur = os.path.getsize(tmp) if os.path.exists(tmp) else 0
# resume-hang detection: two consecutive zero-progress resumes at the
# same offset => this CDN offset is wedged; wipe and start fresh
if 0 < prev == cur:
stall += 1
if stall >= 2:
print(f" try {t}: resume stalled 2x at {prev}b — wiping part, fresh start")
os.remove(tmp)
stall = 0
cur = 0
else:
stall = 0
data = open(tmp, "rb").read()
kind = sniff(data)
ok_size = (size == 0) or (len(data) == size)