47 lines
1.6 KiB
Python
47 lines
1.6 KiB
Python
import http.client
|
|
|
|
# import requests
|
|
import time
|
|
import random
|
|
from datetime import datetime
|
|
from pathlib import Path
|
|
|
|
|
|
def download_apk(
|
|
sha256: str, api_key: str, logfile: Path | None = None
|
|
) -> bytes | None:
|
|
for _ in range(3):
|
|
try:
|
|
conn = http.client.HTTPSConnection("androzoo.uni.lu")
|
|
conn.request("GET", f"/api/download?apikey={api_key}&sha256={sha256}")
|
|
resp = conn.getresponse()
|
|
if resp.status != 200:
|
|
raise RuntimeError(f"Failled to download APK {sha256}: {resp.reason}")
|
|
data = resp.read()
|
|
return data
|
|
# resp = requests.get(
|
|
# "https://androzoo.uni.lu/api/download",
|
|
# params={
|
|
# b"apikey": api_key.encode("utf-8"),
|
|
# b"sha256": sha256.encode("utf-8"),
|
|
# },
|
|
# )
|
|
# if resp.status_code != 200:
|
|
# raise RuntimeError(f"Failled to download APK {sha256}: {resp.reason}")
|
|
# return resp.content
|
|
except Exception as e:
|
|
delay = random.randint(1, 6)
|
|
log = f"[{datetime.today().strftime('%Y-%m-%d %H:%M:%S')}] Failed to download {sha256}: {e}, retry in {delay}s"
|
|
if logfile:
|
|
with logfile.open("a") as file:
|
|
file.write(f"{log}\n")
|
|
else:
|
|
print(log)
|
|
time.sleep(delay)
|
|
log = f"[{datetime.today().strftime('%Y-%m-%d %H:%M:%S')}] Failed to download {sha256}, abort"
|
|
if logfile:
|
|
with logfile.open("a") as file:
|
|
file.write(f"{log}\n")
|
|
else:
|
|
print(log)
|
|
return None
|