romdl.py
· 2.3 KiB · Python
Raw
# pip3 install curses-menu xmltodict requests bs4
# python 3.7 yo
from cursesmenu import CursesMenu
from cursesmenu.items import SubmenuItem, FunctionItem
import requests
import xmltodict
from bs4 import BeautifulSoup as Soup
from sys import stderr
import os
from urllib.parse import urlparse, unquote
system_data = xmltodict.parse(requests.get("https://ia801407.us.archive.org/23/items/no-intro-rom-sets/no-intro-rom-sets_files.xml").text)
archive_path = "https://ia801407.us.archive.org/view_archive.php?archive=/23/items/no-intro-rom-sets/"
def download_file(url):
if url[0] != "h":
url = "https:" + url
r = requests.get(url, stream=True)
if r.status_code == 200:
with open(os.path.basename(unquote(urlparse(url).path)), "wb") as f:
for chunk in r:
print("*")
f.write(chunk)
def search_submenu(filelist):
term = input("Enter search term")
results = []
for row in filelist.findAll("tr")[1:]:
dest_fn = row.find("td")
if ".zip" in dest_fn.text:
if term.lower() in dest_fn.text.split(".zip")[0].lower():
results += (dest_fn.text.split(".zip")[0], dest_fn.find("a")["href"])
search_menu = CursesMenu("Search", "")
for title, url in results:
item = FunctionItem(title, download_file, [url])
search_menu.append_item(item)
search_menu.show()
def generate_submenu(filename):
filelist = Soup(requests.get(archive_path + filename).text, "html.parser").find("table", {"class": "archext"})
system_menu = CursesMenu(filename, "Select a file to download")
# system_menu.append_item(FunctionItem("Search...", search_submenu, [filelist]))
for row in filelist.findAll("tr")[1:]:
dest_fn = row.find("td")
if ".zip" in dest_fn.text:
item = FunctionItem(dest_fn.text.split(".zip")[0], download_file, [dest_fn.find("a")["href"]])
system_menu.append_item(item)
system_menu.show()
def main():
systems_menu = CursesMenu("System Menu", "Select the system to browse roms")
for system in system_data["files"]["file"]:
if system["@name"][-4:] == ".zip":
item = FunctionItem(system["@name"], generate_submenu, [system["@name"]])
systems_menu.append_item(item)
systems_menu.show()
if __name__ == "__main__":
main()
| 1 | # pip3 install curses-menu xmltodict requests bs4 |
| 2 | # python 3.7 yo |
| 3 | from cursesmenu import CursesMenu |
| 4 | from cursesmenu.items import SubmenuItem, FunctionItem |
| 5 | import requests |
| 6 | import xmltodict |
| 7 | from bs4 import BeautifulSoup as Soup |
| 8 | from sys import stderr |
| 9 | import os |
| 10 | from urllib.parse import urlparse, unquote |
| 11 | |
| 12 | system_data = xmltodict.parse(requests.get("https://ia801407.us.archive.org/23/items/no-intro-rom-sets/no-intro-rom-sets_files.xml").text) |
| 13 | archive_path = "https://ia801407.us.archive.org/view_archive.php?archive=/23/items/no-intro-rom-sets/" |
| 14 | |
| 15 | def 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 | |
| 25 | def 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 | |
| 39 | def 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 | |
| 50 | def 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 | |
| 58 | if __name__ == "__main__": |
| 59 | main() |