Last active 1587394359

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

Revision 660e81312e3be9ed9828253e6d7ef19b7b99de1a

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