1st commit
This commit is contained in:
60
scripts/download_helpers.py
Normal file
60
scripts/download_helpers.py
Normal file
@@ -0,0 +1,60 @@
|
||||
import os
|
||||
import requests
|
||||
import tarfile
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
|
||||
def download_maxmind(license_key: str, db_name: str, dest_path: str, url_template: str):
|
||||
url = url_template.format(DBNAME=db_name, LICENSE_KEY=license_key)
|
||||
tmp = Path("/tmp") / "maxmind_download.tar.gz"
|
||||
r = requests.get(url, stream=True, timeout=60)
|
||||
r.raise_for_status()
|
||||
with open(tmp, "wb") as f:
|
||||
for chunk in r.iter_content(chunk_size=8192):
|
||||
if chunk:
|
||||
f.write(chunk)
|
||||
with tarfile.open(tmp, "r:gz") as tar:
|
||||
for member in tar.getmembers():
|
||||
if member.name.endswith(".mmdb"):
|
||||
member_f = tar.extractfile(member)
|
||||
if member_f is None:
|
||||
continue
|
||||
dest = Path(dest_path)
|
||||
dest.parent.mkdir(parents=True, exist_ok=True)
|
||||
with open(dest, "wb") as out_f:
|
||||
shutil.copyfileobj(member_f, out_f)
|
||||
return str(dest)
|
||||
raise RuntimeError("Nie znaleziono pliku .mmdb w archiwum")
|
||||
|
||||
def download_file(url: str, dest_path: str):
|
||||
r = requests.get(url, stream=True, timeout=60)
|
||||
r.raise_for_status()
|
||||
Path(dest_path).parent.mkdir(parents=True, exist_ok=True)
|
||||
with open(dest_path, "wb") as f:
|
||||
for chunk in r.iter_content(chunk_size=8192):
|
||||
if chunk:
|
||||
f.write(chunk)
|
||||
return dest_path
|
||||
|
||||
def github_latest_mmdb(repo: str, token: str | None = None) -> str | None:
|
||||
"""
|
||||
Zwraca URL do najnowszego assetu .mmdb z releases/latest dla repo (owner/name).
|
||||
Preferencja: City -> Country -> ASN.
|
||||
"""
|
||||
api = f"https://api.github.com/repos/{repo}/releases/latest"
|
||||
headers = {"Accept": "application/vnd.github+json"}
|
||||
if token:
|
||||
headers["Authorization"] = f"Bearer {token}"
|
||||
r = requests.get(api, headers=headers, timeout=30)
|
||||
r.raise_for_status()
|
||||
data = r.json()
|
||||
assets = data.get("assets", [])
|
||||
urls = [a.get("browser_download_url") for a in assets if (a.get("browser_download_url") or "").lower().endswith(".mmdb")]
|
||||
if not urls:
|
||||
return None
|
||||
lower = [u.lower() for u in urls]
|
||||
for key in ("city", "country", "asn"):
|
||||
for i, u in enumerate(lower):
|
||||
if key in u:
|
||||
return urls[i]
|
||||
return urls[0]
|
Reference in New Issue
Block a user