import os import requests import zipfile from io import BytesIO import shutil def download_and_extract_latest_release(): # GitHub API URL for repository releases repo_api_url = "https://api.github.com/repos/nixietab/pds/releases/latest" try: # Get the latest release data response = requests.get(repo_api_url) response.raise_for_status() release_data = response.json() # Find the PDS.zip asset in the release assets pds_zip_url = None for asset in release_data.get("assets", []): if asset["name"].lower() == "pds.zip": pds_zip_url = asset["browser_download_url"] break if not pds_zip_url: print("Error: PDS.zip not found in the latest release.") return # Download the PDS.zip file print(f"Downloading PDS.zip from {pds_zip_url}...") zip_response = requests.get(pds_zip_url) zip_response.raise_for_status() # Extract the contents of the ZIP file with zipfile.ZipFile(BytesIO(zip_response.content)) as zip_file: process_folder = "progress" os.makedirs(process_folder, exist_ok=True) print(f"Extracting files to '{process_folder}'...") zip_file.extractall(process_folder) print("Download and extraction complete.") except requests.exceptions.RequestException as e: print(f"Network error: {e}") except zipfile.BadZipFile: print("Error: The downloaded file is not a valid ZIP file.") except Exception as e: print(f"An error occurred: {e}") def download_and_extract_repository(repo_url, destination_folder): try: # GitHub ZIP URL for the repository's main branch repo_zip_url = f"{repo_url}/archive/refs/heads/main.zip" print(f"Downloading repository ZIP from {repo_zip_url}...") response = requests.get(repo_zip_url) response.raise_for_status() # Extract the ZIP contents with zipfile.ZipFile(BytesIO(response.content)) as zip_file: temp_folder = "temp_repo" os.makedirs(temp_folder, exist_ok=True) print(f"Extracting files to temporary folder '{temp_folder}'...") zip_file.extractall(temp_folder) # Move extracted files to the destination folder print(f"Moving files to '{destination_folder}'...") if os.path.exists(destination_folder): shutil.rmtree(destination_folder) # Remove existing folder os.makedirs(destination_folder, exist_ok=True) extracted_folder = os.path.join(temp_folder, os.listdir(temp_folder)[0]) for item in os.listdir(extracted_folder): shutil.move(os.path.join(extracted_folder, item), destination_folder) # Clean up temporary folder shutil.rmtree(temp_folder) print(f"Repository downloaded and extracted to '{destination_folder}'.") except requests.exceptions.RequestException as e: print(f"Network error: {e}") except zipfile.BadZipFile: print("Error: The downloaded file is not a valid ZIP file.") except Exception as e: print(f"An error occurred: {e}") if __name__ == "__main__": download_and_extract_latest_release() download_and_extract_repository("https://github.com/nixietab/picodulce", "progress") download_and_extract_repository("https://github.com/nixietab/2hsu", ".")