Last active 1587394359

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

Revision 4a2fbb367b5a7895c685abe73cb3280a0c0f3191

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