romdl.py
· 2.1 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
from getpass import getuser
from subprocess import Popen
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/"
# openemu support
directory = os.path.join("/Users", getuser(), "Library", "Application Support", "OpenEmu", "Game Library", "roms", "Automatically Import")
if not os.path.exists(directory):
directory = ""
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.join(directory, os.path.basename(unquote(urlparse(url).path))), "wb") as f:
for chunk in r:
print("*")
f.write(chunk)
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")
for row in filelist.findAll("tr")[1:]:
dest_fn = row.find("td")
if ".zip" in dest_fn.text:
display_fn = dest_fn.text.split(".zip")[0]
if "/" in display_fn:
display_fn = display_fn.split("/",1)[1]
item = FunctionItem(display_fn, 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"]], should_exit=True)
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 | from getpass import getuser |
| 12 | from subprocess import Popen |
| 13 | |
| 14 | system_data = xmltodict.parse(requests.get("https://ia801407.us.archive.org/23/items/no-intro-rom-sets/no-intro-rom-sets_files.xml").text) |
| 15 | archive_path = "https://ia801407.us.archive.org/view_archive.php?archive=/23/items/no-intro-rom-sets/" |
| 16 | |
| 17 | # openemu support |
| 18 | directory = os.path.join("/Users", getuser(), "Library", "Application Support", "OpenEmu", "Game Library", "roms", "Automatically Import") |
| 19 | if not os.path.exists(directory): |
| 20 | directory = "" |
| 21 | |
| 22 | def 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 | |
| 32 | def 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 | |
| 45 | def 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 | |
| 53 | if __name__ == "__main__": |
| 54 | main() |