From c2bc418ca9fb8dfdf8ab1e883ebf085461919570 Mon Sep 17 00:00:00 2001 From: Jeremiah K <17190268+jeremiah-k@users.noreply.github.com> Date: Mon, 14 Oct 2024 20:03:54 -0500 Subject: [PATCH 1/2] Switch to regex patterns for assets --- app/downloader.py | 30 ++++++++++++++++++------------ app/menu_apk.py | 23 ++++++++++++++++++----- app/menu_firmware.py | 22 ++++++++++++++++++---- 3 files changed, 54 insertions(+), 21 deletions(-) diff --git a/app/downloader.py b/app/downloader.py index 93ad0a1..ef34a51 100644 --- a/app/downloader.py +++ b/app/downloader.py @@ -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") @@ -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): @@ -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": @@ -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( @@ -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( @@ -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() diff --git a/app/menu_apk.py b/app/menu_apk.py index f44c697..ffb8ebf 100644 --- a/app/menu_apk.py +++ b/app/menu_apk.py @@ -1,4 +1,4 @@ -# fetchtastic/menu_apk.py +# app/menu_apk.py import re import requests @@ -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.''' @@ -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}") diff --git a/app/menu_firmware.py b/app/menu_firmware.py index e108445..ce8464d 100644 --- a/app/menu_firmware.py +++ b/app/menu_firmware.py @@ -1,5 +1,6 @@ # app/menu_firmware.py +import re import requests from pick import pick @@ -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.''' @@ -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}") From 422851dddbdbd2caef0d3c22531d464ea0a6573f Mon Sep 17 00:00:00 2001 From: Jeremiah K <17190268+jeremiah-k@users.noreply.github.com> Date: Mon, 14 Oct 2024 20:10:18 -0500 Subject: [PATCH 2/2] Bump version --- setup.cfg | 2 +- setup.py | 19 ------------------- 2 files changed, 1 insertion(+), 20 deletions(-) delete mode 100644 setup.py diff --git a/setup.cfg b/setup.cfg index 0098a0a..09a2d90 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = fetchtastic -version = 0.1.0 +version = 0.1.1 author = Jeremiah K author_email = jeremiahk@gmx.com description = Meshtastic Firmware and APK Downloader diff --git a/setup.py b/setup.py deleted file mode 100644 index bcf05d2..0000000 --- a/setup.py +++ /dev/null @@ -1,19 +0,0 @@ -from setuptools import setup, find_packages - -setup( - name='fetchtastic', - version='0.1.0', - packages=find_packages(), - install_requires=[ - 'requests', - 'pick', - 'PyYAML', - 'urllib3', - ], - entry_points={ - 'console_scripts': [ - 'fetchtastic=app.cli:main', - ], - }, - # Include other metadata as needed -)