diff --git a/android_class_shadowing_scanner/androzoo.py b/android_class_shadowing_scanner/androzoo.py index 8073bef..08340cf 100644 --- a/android_class_shadowing_scanner/androzoo.py +++ b/android_class_shadowing_scanner/androzoo.py @@ -1,12 +1,16 @@ 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: - while True: +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}") @@ -15,6 +19,16 @@ def download_apk(sha256: str, api_key: str, logfile: Path | None = None) -> byte 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" @@ -24,3 +38,10 @@ def download_apk(sha256: str, api_key: str, logfile: Path | None = None) -> byte 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