Skip to content

Commit

Permalink
Merge pull request #7 from jeremiah-k/fix-dl-oldversions
Browse files Browse the repository at this point in the history
Use base pattern matching for assets
  • Loading branch information
jeremiah-k authored Oct 15, 2024
2 parents 8f4d8bf + 422851d commit 22c3b19
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 41 deletions.
30 changes: 18 additions & 12 deletions app/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def main():
auto_extract = config.get("AUTO_EXTRACT", False)
extract_patterns = config.get("EXTRACT_PATTERNS", [])

selected_apk_assets = config.get('SELECTED_APK_ASSETS', [])
selected_firmware_assets = config.get('SELECTED_FIRMWARE_ASSETS', [])
selected_apk_patterns = config.get('SELECTED_APK_ASSETS', [])
selected_firmware_patterns = config.get('SELECTED_FIRMWARE_ASSETS', [])

download_dir = config.get('DOWNLOAD_DIR', os.path.join(os.path.expanduser("~"), "storage", "downloads", "Meshtastic"))
firmware_dir = os.path.join(download_dir, "firmware")
Expand Down Expand Up @@ -136,7 +136,7 @@ def cleanup_old_versions(directory, keep_count):
log_message(f"Removed directory: {version}")

# Function to check for missing releases and download them if necessary
def check_and_download(releases, latest_release_file, release_type, download_dir, versions_to_keep, extract_patterns, selected_assets=None):
def check_and_download(releases, latest_release_file, release_type, download_dir, versions_to_keep, extract_patterns, selected_patterns=None):
downloaded_versions = []

if not os.path.exists(download_dir):
Expand All @@ -161,9 +161,15 @@ def check_and_download(releases, latest_release_file, release_type, download_dir
log_message(f"Downloading new version: {release_tag}")
for asset in release['assets']:
file_name = asset['name']
if selected_assets:
if file_name not in selected_assets:
continue
# Modify the matching logic here
if selected_patterns:
matched = False
for pattern in selected_patterns:
if pattern in file_name:
matched = True
break
if not matched:
continue # Skip this asset
download_path = os.path.join(release_dir, file_name)
download_file(asset['browser_download_url'], download_path)
if auto_extract and file_name.endswith('.zip') and release_type == "Firmware":
Expand Down Expand Up @@ -195,7 +201,7 @@ def check_and_download(releases, latest_release_file, release_type, download_dir
latest_firmware_releases = []
latest_android_releases = []

if save_firmware and selected_firmware_assets:
if save_firmware and selected_firmware_patterns:
versions_to_download = firmware_versions_to_keep
latest_firmware_releases = get_latest_releases(firmware_releases_url, versions_to_download, releases_to_scan)
downloaded_firmwares = check_and_download(
Expand All @@ -205,13 +211,13 @@ def check_and_download(releases, latest_release_file, release_type, download_dir
firmware_dir,
firmware_versions_to_keep,
extract_patterns,
selected_assets=selected_firmware_assets
selected_patterns=selected_firmware_patterns
)
log_message(f"Latest Firmware releases: {', '.join(release['tag_name'] for release in latest_firmware_releases)}")
elif not selected_firmware_assets:
elif not selected_firmware_patterns:
log_message("No firmware assets selected. Skipping firmware download.")

if save_apks and selected_apk_assets:
if save_apks and selected_apk_patterns:
versions_to_download = android_versions_to_keep
latest_android_releases = get_latest_releases(android_releases_url, versions_to_download, releases_to_scan)
downloaded_apks = check_and_download(
Expand All @@ -221,10 +227,10 @@ def check_and_download(releases, latest_release_file, release_type, download_dir
apks_dir,
android_versions_to_keep,
extract_patterns,
selected_assets=selected_apk_assets
selected_patterns=selected_apk_patterns
)
log_message(f"Latest Android APK releases: {', '.join(release['tag_name'] for release in latest_android_releases)}")
elif not selected_apk_assets:
elif not selected_apk_patterns:
log_message("No APK assets selected. Skipping APK download.")

end_time = time.time()
Expand Down
23 changes: 18 additions & 5 deletions app/menu_apk.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# fetchtastic/menu_apk.py
# app/menu_apk.py

import re
import requests
Expand All @@ -15,6 +15,13 @@ def fetch_apk_assets():
asset_names = [asset['name'] for asset in assets if asset['name'].endswith('.apk')]
return asset_names

def extract_base_name(filename):
# Remove version numbers and extensions from filename to get base pattern
# Example: 'fdroidRelease-2.5.1.apk' -> 'fdroidRelease-'
base_name = re.sub(r'-\d+\.\d+\.\d+.*', '-', filename)
base_name = re.sub(r'\.apk$', '', base_name)
return base_name

def select_assets(assets):
title = '''Select the APK files you want to download (press SPACE to select, ENTER to confirm):
Note: These are files from the latest release. Version numbers may change in other releases.'''
Expand All @@ -24,16 +31,22 @@ def select_assets(assets):
if not selected_assets:
print("No APK files selected. APKs will not be downloaded.")
return None
return selected_assets

# Extract base patterns from selected filenames
base_patterns = []
for asset_name in selected_assets:
pattern = extract_base_name(asset_name)
base_patterns.append(pattern)
return base_patterns

def run_menu():
try:
assets = fetch_apk_assets()
selected_assets = select_assets(assets)
if selected_assets is None:
selected_patterns = select_assets(assets)
if selected_patterns is None:
return None
return {
'selected_assets': selected_assets
'selected_assets': selected_patterns
}
except Exception as e:
print(f"An error occurred: {e}")
Expand Down
22 changes: 18 additions & 4 deletions app/menu_firmware.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# app/menu_firmware.py

import re
import requests
from pick import pick

Expand All @@ -14,6 +15,13 @@ def fetch_firmware_assets():
asset_names = [asset['name'] for asset in assets]
return asset_names

def extract_base_name(filename):
# Remove version numbers and extensions from filename to get base pattern
# Example: 'firmware-esp32-2.5.6.d55c08d.zip' -> 'firmware-esp32-'
base_name = re.sub(r'-\d+\.\d+\.\d+.*', '-', filename)
base_name = re.sub(r'\.zip$', '', base_name)
return base_name

def select_assets(assets):
title = '''Select the firmware files you want to download (press SPACE to select, ENTER to confirm):
Note: These are files from the latest release. Version numbers may change in other releases.'''
Expand All @@ -23,16 +31,22 @@ def select_assets(assets):
if not selected_assets:
print("No firmware files selected. Firmware will not be downloaded.")
return None
return selected_assets

# Extract base patterns from selected filenames
base_patterns = []
for asset_name in selected_assets:
pattern = extract_base_name(asset_name)
base_patterns.append(pattern)
return base_patterns

def run_menu():
try:
assets = fetch_firmware_assets()
selected_assets = select_assets(assets)
if selected_assets is None:
selected_patterns = select_assets(assets)
if selected_patterns is None:
return None
return {
'selected_assets': selected_assets
'selected_assets': selected_patterns
}
except Exception as e:
print(f"An error occurred: {e}")
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = fetchtastic
version = 0.1.0
version = 0.1.1
author = Jeremiah K
author_email = [email protected]
description = Meshtastic Firmware and APK Downloader
Expand Down
19 changes: 0 additions & 19 deletions setup.py

This file was deleted.

0 comments on commit 22c3b19

Please sign in to comment.