Last active 1587394359

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

Revision 8c1bbd440249d6cf265bdec1c3881ee3fb14a7e9

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
11
12system_data = xmltodict.parse(requests.get("https://ia801407.us.archive.org/23/items/no-intro-rom-sets/no-intro-rom-sets_files.xml").text)
13archive_path = "https://ia801407.us.archive.org/view_archive.php?archive=/23/items/no-intro-rom-sets/"
14
15def download_file(url):
16 if url[0] != "h":
17 url = "https:" + url
18 r = requests.get(url, stream=True)
19 if r.status_code == 200:
20 with open(os.path.basename(unquote(urlparse(url).path)), "wb") as f:
21 for chunk in r:
22 print("*")
23 f.write(chunk)
24
25def search_submenu(filelist):
26 term = input("Enter search term")
27 results = []
28 for row in filelist.findAll("tr")[1:]:
29 dest_fn = row.find("td")
30 if ".zip" in dest_fn.text:
31 if term.lower() in dest_fn.text.split(".zip")[0].lower():
32 results += (dest_fn.text.split(".zip")[0], dest_fn.find("a")["href"])
33 search_menu = CursesMenu("Search", "")
34 for title, url in results:
35 item = FunctionItem(title, download_file, [url])
36 search_menu.append_item(item)
37 search_menu.show()
38
39def generate_submenu(filename):
40 filelist = Soup(requests.get(archive_path + filename).text, "html.parser").find("table", {"class": "archext"})
41 system_menu = CursesMenu(filename, "Select a file to download")
42# system_menu.append_item(FunctionItem("Search...", search_submenu, [filelist]))
43 for row in filelist.findAll("tr")[1:]:
44 dest_fn = row.find("td")
45 if ".zip" in dest_fn.text:
46 item = FunctionItem(dest_fn.text.split(".zip")[0], download_file, [dest_fn.find("a")["href"]])
47 system_menu.append_item(item)
48 system_menu.show()
49
50def main():
51 systems_menu = CursesMenu("System Menu", "Select the system to browse roms")
52 for system in system_data["files"]["file"]:
53 if system["@name"][-4:] == ".zip":
54 item = FunctionItem(system["@name"], generate_submenu, [system["@name"]])
55 systems_menu.append_item(item)
56 systems_menu.show()
57
58if __name__ == "__main__":
59 main()