Created
December 4, 2024 18:03
-
-
Save ca0abinary/c54bd4ac21e6277d8636ec524f9b7444 to your computer and use it in GitHub Desktop.
Export ROMM platforms to Anbernic folder structure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import re | |
| import mariadb | |
| from os import environ | |
| from pathlib import Path | |
| from shutil import copyfile | |
| LIBRARY_PATH = Path.cwd().joinpath('library') | |
| RESOURCES_PATH = Path.cwd().joinpath('resources') | |
| EXPORT_PATH = Path.cwd().joinpath('anbernic') | |
| def main(): | |
| global EXPORT_PATH | |
| export_platforms = [] | |
| for platform in get_platforms(): | |
| if get_yes_no(f'Export {platform["name"]} games?') == True: | |
| export_platforms.append(platform) | |
| edit_path = input(f'Path to export games ({EXPORT_PATH}): ') | |
| if edit_path != '' and Path(edit_path).is_dir(): | |
| EXPORT_PATH = Path(edit_path) | |
| for platform in export_platforms: | |
| export_games(platform) | |
| def export_games(platform): | |
| if platform['slug'] not in platform_mapping: | |
| print(f'Platform {platform["name"]} not supported') | |
| return | |
| platform_path = platform_mapping[platform['slug']] | |
| export_rom_path = EXPORT_PATH.joinpath('Roms', platform_path) | |
| export_img_path = export_rom_path.joinpath('Imgs') | |
| export_img_path.mkdir(parents=True, exist_ok=True) | |
| roms = get_roms(platform['id']) | |
| print(f'Exporting {len(roms)} {platform["name"]} game(s) to {export_rom_path}...') | |
| for rom in get_roms(platform['id']): | |
| export_game(rom, export_rom_path, export_img_path) | |
| def export_game(rom, export_rom_path, export_img_path): | |
| src_rom_path = LIBRARY_PATH.joinpath(rom['file_path'], rom['file_name']) | |
| src_img_path = RESOURCES_PATH.joinpath(rom['cover_small_path']) if len(f"{rom['cover_small_path']}".strip()) > 0 else None | |
| rom_path = export_rom_path.joinpath((f"{rom['clean_name']}.{rom['file_extension']}").replace('..', '.')) | |
| img_path = export_img_path.joinpath(f"{rom['clean_name']}.png") | |
| print(f'\t{rom["name"]}...', end='') | |
| copied = 0 | |
| if not rom_path.exists(): | |
| copyfile(src_rom_path, rom_path) | |
| copied += 1 | |
| print(' rom', end='') | |
| if src_img_path is not None and not img_path.exists(): | |
| copyfile(src_img_path, img_path) | |
| copied += 1 | |
| print(' img', end='') | |
| print(' done' if copied > 0 else 'exists') | |
| def get_platforms(): | |
| platforms = [{ | |
| "id": platform['id'], | |
| "slug": platform['slug'] if platform['slug'] != 'arcade' else f'arcade-{platform["fs_slug"]}', | |
| "name": platform['name'] if platform['name'] != 'Arcade' else f'Arcade ({platform["fs_slug"]})', | |
| "fs_slug": platform['fs_slug'], | |
| } for platform in get_table('platforms')] | |
| return sorted(platforms, key=lambda x: x['name']) | |
| def get_roms(platform_id): | |
| roms = [{ | |
| "file_name": rom['file_name'], | |
| "file_path": rom['file_path'], | |
| "file_extension": rom['file_extension'], | |
| "cover_small_path": rom['path_cover_s'], | |
| "name": rom['name'], | |
| "clean_name": re.sub('[^a-zA-Z0-9_]', '_', rom['name']).strip()[:255], | |
| } for rom in get_table('roms', 'platform_id', platform_id)] | |
| return sorted(roms, key=lambda x: x['name']) | |
| def get_table(table_name, key_name = None, key_value = None): | |
| query = (f'SELECT * FROM {table_name}' if key_name is None | |
| else f'SELECT * FROM {table_name} WHERE {key_name} = ?') | |
| with get_db_connection() as conn: | |
| with conn.cursor() as cursor: | |
| cursor.execute(query, (key_value,)) | |
| rows = cursor.fetchall() | |
| return [dict(zip([column[0] for column in cursor.description], row)) for row in rows] | |
| def get_db_connection(): | |
| return mariadb.connect( | |
| user=environ.get('DB_USER'), | |
| password=environ.get('DB_PASSWD'), | |
| host=environ.get('DB_HOST'), | |
| database=environ.get('DB_NAME') | |
| ) | |
| def get_yes_no(question): | |
| answer = input(f'{question} (y/N): ') | |
| return answer in ['y', 'Y'] | |
| platform_mapping = { | |
| 'atari2600': 'A2600', | |
| 'atari5200': 'A5200', | |
| 'atari7800': 'A7800', | |
| 'atari8bit': 'A800', | |
| 'amiga': 'AMIGA', | |
| 'atari': 'ATARI', | |
| 'atari-st': 'ATARIST', | |
| 'c64': 'C64', | |
| 'arcade-cps1': 'CPS1', | |
| 'arcade-cps2': 'CPS2', | |
| 'arcade-cps3': 'CPS3', | |
| 'dos': 'DOS', | |
| 'dc': 'DREAMCAST', | |
| 'arcade-fbneo': 'FBNEO', | |
| 'nes': 'FC', | |
| 'fds': 'FDS', | |
| 'gb': 'GB', | |
| 'gba': 'GBA', | |
| 'gbc': 'GBC', | |
| 'gamegear': 'GG', | |
| 'g-and-w': 'GW', | |
| 'lynx': 'LYNX', | |
| 'arcade-mame': 'MAME', | |
| 'genesis-slash-megadrive': 'MD', | |
| 'segacd': 'MDCD', | |
| 'msx': 'MSX', | |
| 'n64': 'N64', | |
| 'arcade-naomi': 'NAOMI', | |
| 'nds': 'NDS', | |
| 'neo-geo-cd': 'NEOCD', | |
| 'neogeoaes': 'NEOGEO', | |
| 'neo-geo-pocket': 'NGP', | |
| 'neo-geo-pocket-color': 'NGP', | |
| 'supergrafx': 'PCE', | |
| 'pokemon-mini': 'POKE', | |
| 'ps': 'PS', | |
| 'psp': 'PSP', | |
| 'saturn': 'SATURN', | |
| 'sega32': 'SEGA32X', | |
| 'snes': 'SFC', | |
| 'sega-master-system': 'SMS', | |
| 'virtualboy': 'VB', | |
| 'vic-20': 'VIC20', | |
| 'wonderswan': 'WS', | |
| 'wonderswan-color': 'WS', | |
| } | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment