Skip to content

Commit

Permalink
Improve release and update info
Browse files Browse the repository at this point in the history
  • Loading branch information
Aareon Sullivan committed Sep 17, 2024
1 parent 6f9a0c2 commit 6f17553
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/update_allCountries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install httpx loguru
pip install httpx loguru tqdm
- name: Run update script
run: python update_allCountries.py
Expand Down
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,7 @@ cython_debug/
allCountries.txt
allCountries.zip

# Auto-generated release notes
release_notes.txt
# Auto-generated release info
release_notes.txt
release_title.txt
update_status.txt
24 changes: 22 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ readme = "README.md"
python = "^3.10"
httpx = "^0.27.2"
loguru = "^0.7.2"
tqdm = "^4.66.5"


[tool.poetry.group.test.dependencies]
Expand Down
77 changes: 58 additions & 19 deletions update_allCountries.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
import re
from datetime import datetime, timezone
from pathlib import Path
import time

from tqdm import tqdm
import httpx
from loguru import logger

GEONAMES_URL = "https://download.geonames.org/export/zip/allCountries.zip"
LOCAL_FILE = Path("allCountries.zip")
EXTRACTED_FILE = Path("allCountries.txt")
GITHUB_API_URL = "https://api.github.com/repos/Aareon/GeoNamesMirror/releases/latest"
GITHUB_API_URL = "https://api.github.com/repos/Aareon/GeoNamesMirror/releases"


async def check_for_updates() -> bool:
if LOCAL_FILE.exists():
Expand All @@ -34,13 +37,31 @@ async def download_file():
async with client.stream("GET", GEONAMES_URL) as response:
response.raise_for_status()
total_size = int(response.headers.get("Content-Length", 0))

with LOCAL_FILE.open("wb") as f:
downloaded = 0
async for chunk in response.aiter_bytes():
f.write(chunk)
downloaded += len(chunk)
logger.info(f"Downloaded {downloaded}/{total_size} bytes")

chunk_size = 1024 * 1024 # 1 MB chunks
downloaded = 0
start_time = time.time()
last_log_time = start_time
log_interval = 5 # Log every 5 seconds

with open(LOCAL_FILE, "wb") as f, tqdm(
total=total_size, unit='iB', unit_scale=True, desc="Downloading"
) as progress_bar:
async for chunk in response.aiter_bytes(chunk_size):
size = f.write(chunk)
downloaded += size
progress_bar.update(size)

current_time = time.time()
if current_time - last_log_time >= log_interval:
elapsed_time = current_time - start_time
speed = downloaded / elapsed_time / 1024 / 1024 # MB/s
percent = (downloaded / total_size) * 100 if total_size > 0 else 0
logger.info(f"Downloaded: {downloaded/1024/1024:.2f} MB / {total_size/1024/1024:.2f} MB "
f"({percent:.2f}%) - Speed: {speed:.2f} MB/s")
last_log_time = current_time

logger.info(f"Download completed. Total size: {total_size/1024/1024:.2f} MB")

def calculate_md5(filename: Path) -> str:
hash_md5 = hashlib.md5()
Expand Down Expand Up @@ -91,19 +112,37 @@ def create_release_notes(stats, is_update):
"""

async def get_previous_checksum():
async with httpx.AsyncClient() as client:
response = await client.get(GITHUB_API_URL)
response.raise_for_status()
release_data = response.json()
body = release_data.get('body', '')

# Extract MD5 checksum from the release notes
match = re.search(r'MD5 Checksum: ([a-fA-F0-9]{32})', body)
if match:
return match.group(1)
try:
async with httpx.AsyncClient() as client:
response = await client.get(GITHUB_API_URL, headers={
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28"
})
response.raise_for_status()
releases = response.json()

if not releases:
logger.info("No previous releases found. This will be the first release.")
return None

latest_release = releases[0]
body = latest_release.get('body', '')

# Extract MD5 checksum from the release notes
match = re.search(r'MD5 Checksum: ([a-fA-F0-9]{32})', body)
if match:
return match.group(1)
else:
logger.warning("MD5 checksum not found in the latest release notes.")
return None
except httpx.HTTPStatusError as e:
logger.error(f"HTTP error occurred: {e}")
except Exception as e:
logger.error(f"An unexpected error occurred while fetching previous releases: {e}")
return None

async def main():
current_checksum = None
try:
if await check_for_updates():
logger.info("Updating Geonames data...")
Expand Down Expand Up @@ -135,7 +174,7 @@ async def main():

logger.info(f"Process complete. Release notes:\n{release_notes}")
else:
logger.info("Geonames data is up to date.")
logger.info(f"Geonames data is up to date.{' Checksum: ' + current_checksum + '.' if current_checksum else ''} Last modified: {datetime.fromtimestamp(LOCAL_FILE.stat().st_mtime)}")
except httpx.HTTPError as e:
logger.error(f"HTTP error occurred: {e}")
except IOError as e:
Expand Down

0 comments on commit 6f17553

Please sign in to comment.