Skip to content

Instantly share code, notes, and snippets.

@ninmonkey
Last active April 21, 2019 15:42
Show Gist options
  • Save ninmonkey/52bda9ad3c8802e0baed97a82ad5a6b3 to your computer and use it in GitHub Desktop.
Save ninmonkey/52bda9ad3c8802e0baed97a82ad5a6b3 to your computer and use it in GitHub Desktop.
from xml.etree import ElementTree
import json
import os
import shutil
import requests
"""
todo:
A better solution that manually togglign `FORCE_CACHED`
would be to cache the `xml` file automatically.
Check:
If last update was more than 1 day ago
download new version. save to disk.
else:
load the cached value from disk.
"""
# generate testing files in /input/
# Disable for actual use.
DEBUG_MODE = False
"""
This is set to True for faster debugging.
Web requests are super slow compared to reading local files.
For regular use set to False.
"""
FORCE_CACHED = False
XML_FILENAME = "releases.xml"
def download_xml(url):
filepath = os.path.join("data", XML_FILENAME)
r = requests.get(url)
with open(filepath, mode='w', encoding='utf-8') as f:
f.write(r.text)
def touch_file(path, filename):
# only used when `DEBUG_MODE == True`
# mimics linux `touch`, because actual data doesn't matter for testing.
# This does not destroy already existing files.
filepath = os.path.join(path, filename)
try:
with open(filepath, mode='x'):
pass
except FileExistsError:
pass
def parse_xml():
"""Returns a dict of dicts, example: Keys are `titleid`s
Which is created by
d = {
"id1": { ... properties in XML files ... },
"id2": { ... properties in XML files ... },
}
"""
filepath = os.path.join("data", XML_FILENAME)
tree = ElementTree.parse(filepath)
root = tree.getroot()
release_root = {}
for release in root:
metadata = {}
for stats in release:
key = stats.tag
value = stats.text
metadata[key] = value
key_id = metadata.get('filename')
release_root[key_id] = metadata
return release_root
def main():
for folder in ["data", "input", "output"]:
os.makedirs(folder, exist_ok=True)
if DEBUG_MODE:
# generate data for testing, unless files already exist.
sample_ids = ["hr-arfea.nsp"]
for cur_id in sample_ids:
touch_file("input", cur_id)
if not FORCE_CACHED:
download_xml("http://nswdb.com/xml.php")
metadata = parse_xml()
if not metadata:
raise Exception("Invalid metadata from parse_xml() !")
for full_filename in os.listdir("input"):
name, extension = os.path.splitext(full_filename)
cur_id = name
cur_meta = metadata.get(name)
print(f"Searching for filename = '{name}'")
if not cur_meta:
print(f"No metadata found for name = '{name}'!")
continue
filename_dest = (
"{name} {title} {firmware_version}{ext}"
).format(
name=cur_meta['name'] or "Name Unknown",
title=cur_meta['titleid'] or "TitleId Unknown",
firmware_version=cur_meta['firmware'] or "Firmware Unknown",
ext=extension,
)
filepath_source = os.path.join("input", full_filename)
filepath_destination = os.path.join("output", filename_dest)
message = (
"\nFound:"
"\n\tRelease ID: '{game_id}' "
"\n\tSource: '{filepath_source}' "
"\n\tDestination: '{filepath_destination}' "
).format(
game_id=cur_meta["id"],
filepath_source=filepath_source,
filepath_destination=filepath_destination,
)
print(message)
shutil.move(filepath_source, filepath_destination)
if __name__ == "__main__":
main()
print("Done")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment