Last active 1587394359

A rom downloader for the no-intro rom collection on internet archive

romdl.py Raw
1# pip3 install curses-menu xmltodict requests bs4
2# supports openemu automatic import on macos
3# python 3.7 yo
4from cursesmenu import CursesMenu
5from cursesmenu.items import SubmenuItem, FunctionItem
6import requests
7import xmltodict
8from bs4 import BeautifulSoup as Soup
9from sys import stderr
10import os
11from urllib.parse import urlparse, unquote
12from getpass import getuser
13from subprocess import Popen
14
15system_data = xmltodict.parse(requests.get("https://ia801407.us.archive.org/23/items/no-intro-rom-sets/no-intro-rom-sets_files.xml").text)
16archive_path = "https://ia801407.us.archive.org/view_archive.php?archive=/23/items/no-intro-rom-sets/"
17
18# openemu support
19directory = os.path.join("/Users", getuser(), "Library", "Application Support", "OpenEmu", "Game Library", "roms", "Automatically Import")
20if not os.path.exists(directory):
21 directory = ""
22
23def download_file(url):
24 if url[0] != "h":
25 url = "https:" + url
26 r = requests.get(url, stream=True)
27 if r.status_code == 200:
28 with open(os.path.join(directory, os.path.basename(unquote(urlparse(url).path))), "wb") as f:
29 for chunk in r:
30 print("*")
31 f.write(chunk)
32
33def generate_submenu(filename):
34 filelist = Soup(requests.get(archive_path + filename).text, "html.parser").find("table", {"class": "archext"})
35 system_menu = CursesMenu(filename, "Select a file to download")
36 for row in filelist.findAll("tr")[1:]:
37 dest_fn = row.find("td")
38 if ".zip" in dest_fn.text:
39 display_fn = dest_fn.text.split(".zip")[0]
40 if "/" in display_fn:
41 display_fn = display_fn.split("/",1)[1]
42 item = FunctionItem(display_fn, download_file, [dest_fn.find("a")["href"]])
43 system_menu.append_item(item)
44 system_menu.show()
45
46def main():
47 systems_menu = CursesMenu("System Menu", "Select the system to browse roms")
48 for system in system_data["files"]["file"]:
49 if system["@name"][-4:] == ".zip":
50 item = FunctionItem(system["@name"], generate_submenu, [system["@name"]], should_exit=True)
51 systems_menu.append_item(item)
52 systems_menu.show()
53
54if __name__ == "__main__":
55 main()