Last active 1587394359

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

Revision c6cb942bc789b27d6ac151bd46fd5d8eec2f8a0c

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 " doesn't work "
27 term = input("Enter search term")
28 results = []
29 for row in filelist.findAll("tr")[1:]:
30 dest_fn = row.find("td")
31 if ".zip" in dest_fn.text:
32 if term.lower() in dest_fn.text.split(".zip")[0].lower():
33 results += (dest_fn.text.split(".zip")[0], dest_fn.find("a")["href"])
34 search_menu = CursesMenu("Search", "")
35 for title, url in results:
36 item = FunctionItem(title, download_file, [url])
37 search_menu.append_item(item)
38 search_menu.show()
39
40def generate_submenu(filename):
41 filelist = Soup(requests.get(archive_path + filename).text, "html.parser").find("table", {"class": "archext"})
42 system_menu = CursesMenu(filename, "Select a file to download")
43# system_menu.append_item(FunctionItem("Search...", search_submenu, [filelist]))
44 for row in filelist.findAll("tr")[1:]:
45 dest_fn = row.find("td")
46 if ".zip" in dest_fn.text:
47 item = FunctionItem(dest_fn.text.split(".zip")[0], download_file, [dest_fn.find("a")["href"]])
48 system_menu.append_item(item)
49 system_menu.show()
50
51def main():
52 systems_menu = CursesMenu("System Menu", "Select the system to browse roms")
53 for system in system_data["files"]["file"]:
54 if system["@name"][-4:] == ".zip":
55 item = FunctionItem(system["@name"], generate_submenu, [system["@name"]])
56 systems_menu.append_item(item)
57 systems_menu.show()
58
59if __name__ == "__main__":
60 main()