import gradio as gr import os import zipfile from pathlib import Path import shutil import urllib.parse # Define constants ZIP_FILE = "nes.zip" EXTRACTED_FOLDER = "extracted_nes" PUBLIC_FOLDER = "public/roms" # Ensure public ROM folder exists os.makedirs(PUBLIC_FOLDER, exist_ok=True) # Function to extract ROMs from the zip def extract_roms(): try: if os.path.exists(ZIP_FILE): with zipfile.ZipFile(ZIP_FILE, 'r') as zip_ref: zip_ref.extractall(EXTRACTED_FOLDER) print(f"Extracted into {EXTRACTED_FOLDER}") # Move ROMs into the public folder for root, dirs, files in os.walk(EXTRACTED_FOLDER): for file in files: if file.lower().endswith(".nes"): source_path = os.path.join(root, file) target_path = os.path.join(PUBLIC_FOLDER, file) shutil.move(source_path, target_path) print(f"Moved ROM: {file} -> {target_path}") else: raise FileNotFoundError("`nes.zip` not found.") except PermissionError as e: print(f"Permission error: {e}") # Function to list available ROMs def list_roms(): if not os.path.exists(PUBLIC_FOLDER): print("No ROMs found.") return [] roms = [f for f in os.listdir(PUBLIC_FOLDER) if f.endswith(".nes")] print("Available ROMs:", roms) return roms # Function to generate the HTML content for the selected ROM def generate_rom_page(rom_name): rom_url = f"/roms/{urllib.parse.quote(rom_name)}" html_content = f"""