Skip to content

Instantly share code, notes, and snippets.

@Brawl345
Last active January 19, 2019 21:58
Show Gist options
  • Save Brawl345/51ea0b5c877392cc1877baaadb324040 to your computer and use it in GitHub Desktop.
Save Brawl345/51ea0b5c877392cc1877baaadb324040 to your computer and use it in GitHub Desktop.
Python-Schnipsel
# Byte-compiled / optimized / DLL files / pip
__pycache__/
*.py[cod]
*$py.class
src/
# IDE
.idea/
*.txt
*.dat
*.csv
*.bat
#!/usr/bin/env python3
from requests import get
SYSTEM = "ctr"
def main():
print("Hole Titel-Liste...")
"""req = get("https://yls8.mtheall.com/ninupdates/titlelist.php?sys={0}&csv=1".format(SYSTEM))
if req.status_code != 200:
print("HTTP-Fehler {0}".format(req.status_code))
return
titlelist = req.text"""
with open("{0}.csv".format(SYSTEM), "r") as csv:
titlelist = csv.read()
print("Splitte Titel-Liste...")
lines = titlelist.split("\n")
titles = []
print("Splitte Titel-Liste noch mal...")
for line in lines:
if line == "":
continue
titles.append(line.split(","))
print("Erstelle BAT...")
with open("{0}.bat".format(SYSTEM), "w") as batfile:
batfile.write("@echo off\n")
for title in titles[1:]:
titleid = title[0]
versions = title[2].replace("v", "").split(" ")
for version in versions:
text = "python 3DS-NUSD.py --nopack {0} {1}\n".format(titleid, version)
batfile.write(text)
batfile.write("pause")
if __name__ == "__main__":
main()
#!/usr/bin/env python3
import hashlib
import os
import zlib
from glob import glob
BUF_SIZE = 65536
# https://stackoverflow.com/a/5061842
class crc32Digest():
name = 'crc32'
digest_size = 4
block_size = 1
def __init__(self, arg=b''):
self.__digest = 0
self.update(arg)
def copy(self):
copy = super(self.__class__, self).__new__(self.__class__)
copy.__digest = self.__digest
return copy
def digest(self):
return self.__digest
def hexdigest(self):
return '{:08x}'.format(self.__digest)
def update(self, arg):
self.__digest = zlib.crc32(arg, self.__digest) & 0xffffffff
hashlib.crc32 = crc32Digest
hashlib.algorithms_available.add("crc32")
with open("info.txt", "w") as textfile:
for ipsw_file in glob("*.ipsw"):
print(os.path.basename(ipsw_file))
size = os.path.getsize(ipsw_file)
print(" Size: {0}".format(size))
with open(ipsw_file, 'rb') as f:
# Generate hashes by using buffer to not trash memory: https://stackoverflow.com/a/22058673
md5 = hashlib.md5()
sha1 = hashlib.sha1()
crc32 = crc32Digest()
while True:
data = f.read(BUF_SIZE)
if not data:
break
md5.update(data)
sha1.update(data)
crc32.update(data)
print(" MD5: {0}".format(md5.hexdigest()))
print(" SHA1: {0}".format(sha1.hexdigest()))
print(" CRC32: {0}".format(crc32.hexdigest()))
textfile.write('<rom name="{name}" size="{size}" crc="{crc}" md5="{md5}" sha1="{sha1}"/>\n'.format(
name=os.path.basename(ipsw_file),
size=size,
crc=crc32.hexdigest(),
md5=md5.hexdigest(),
sha1=sha1.hexdigest()
))
print("")
print("Operation completed.")
#!/usr/bin/env python3
import hashlib
import os
import re
import sys
import zlib
from datetime import datetime
from glob import glob
import lxml.builder
import lxml.etree
BUF_SIZE = 65536
XML = lxml.builder.ElementMaker()
# https://stackoverflow.com/a/5061842
class crc32Digest():
name = 'crc32'
digest_size = 4
block_size = 1
def __init__(self, arg=b''):
self.__digest = 0
self.update(arg)
def copy(self):
copy = super(self.__class__, self).__new__(self.__class__)
copy.__digest = self.__digest
return copy
def digest(self):
return self.__digest
def hexdigest(self):
return '{:08x}'.format(self.__digest)
def update(self, arg):
self.__digest = zlib.crc32(arg, self.__digest) & 0xffffffff
hashlib.crc32 = crc32Digest
hashlib.algorithms_available.add("crc32")
def header(device):
"""Konstruiert <header>"""
return XML.header(
XML.name(device),
XML.description(device),
XML.version(datetime.now().strftime("%Y%m%d-%H%M%S")),
XML.author("Akamaru"),
XML.homepage("Aka's Blog"),
XML.url("https://akamaru.de")
)
def calculate_hashes(ipsw):
"""Generiert Hashes und gibt ein Dict zurück (nutzt Buffer: https://stackoverflow.com/a/22058673)"""
with open(ipsw, 'rb') as f:
md5 = hashlib.md5()
sha1 = hashlib.sha1()
crc32 = crc32Digest()
while True:
data = f.read(BUF_SIZE)
if not data:
break
crc32.update(data)
md5.update(data)
sha1.update(data)
return {"crc32": crc32.hexdigest(),
"md5": md5.hexdigest(),
"sha1": sha1.hexdigest()}
def game(ipsw, device):
"""Konstruiert <game>"""
basename = os.path.basename(ipsw)
found = re.match("\w+\d,\d_(.+)_(.+)_Restore\.ipsw", basename).groups()
if not found:
raise ValueError("Invalider IPSW-Name: {0}".format(basename))
hashes = calculate_hashes(ipsw)
return XML.game(
XML.description("{0} Restore IPSW ({1}) ({2})".format(device, found[0], found[1])),
XML.rom(
name=basename,
size=str(os.path.getsize(ipsw)),
crc=hashes["crc32"],
md5=hashes["md5"],
sha1=hashes["sha1"]
),
name="{0} Restore IPSW ({1}) ({2})".format(device, found[0], found[1])
)
def main():
if len(sys.argv) != 3:
print('BENUTZUNG: python {0} "Gerätename" "DAT-Datei"'.format(sys.argv[0]))
return
device = sys.argv[1]
datname = sys.argv[2]
finished_dat = XML.datafile(header(device))
for ipsw in sorted(glob("*.ipsw"), reverse=True):
finished_dat.append(game(ipsw, device))
with open("{0}".format(datname), "wb") as dat:
dat.write(b'<?xml version="1.0"?>\n')
dat.write(
b'<!DOCTYPE datafile PUBLIC "-//Logiqx//DTD ROM Management Datafile//EN" "http://www.logiqx.com/Dats/datafile.dtd">\n')
dat.write(lxml.etree.tostring(finished_dat, pretty_print=True))
if __name__ == "__main__":
main()
#!/usr/bin/env python3
from operator import itemgetter
from json import loads, dumps
with open("ProductAndServiceUsage_OfficeApps.json", "r") as json_file:
data = loads(json_file.read())
with open("sorted_office.json", "w") as new_file:
new_data = sorted(data, key=itemgetter("time"))
new_file.write(dumps(new_data, indent=2, sort_keys=True))
#!/usr/bin/env python3
import re
import PyNUSD
import xmltodict
from requests import get
# Change to
# https://raw.githubusercontent.com/WiiDatabase/NUS-Database/master/vwii-database.xml
# for vWii titles
DB_URL = "https://raw.githubusercontent.com/WiiDatabase/NUS-Database/master/database.xml"
def get_datbaase():
"""We want this dict:
titles = {
'000000010000003A': [6175, 6176],
'000000010000001F': [1037, 1039, 1040, 2576, 3088, 3092, 3349, 3607, 3608]
}
"""
print('Downloading database.xml...')
req = get(DB_URL)
if req.status_code != 200:
print("Couldn't download database.xml.")
return
database = xmltodict.parse(req.text)['database']
titles = {}
print('Building database...')
for title in database['SYS']:
titleid = title['titleID']
if "region" in title:
regions = title['region'].split(',')
for region in regions:
regioncode = re.search(r'\w+', database['REGIONS']['region'][int(region)]['#text']).group()
new_titleid = titleid.replace('XX', regioncode)
versions = title['version'].split(',')
titles[new_titleid] = []
for version in versions:
titleversion = int(re.search(r'\d+', version).group())
titles[new_titleid].append(titleversion)
else:
versions = title['version'].split(',')
titles[titleid] = []
for version in versions:
titleversion = int(re.search(r'\d+', version).group())
titles[titleid].append(titleversion)
for title in database['IOS']:
titleid = title['titleID']
versions = title['version'].split(',')
titles[titleid] = []
for version in versions:
titleversion = int(re.search(r'\d+', version).group())
titles[titleid].append(titleversion)
return titles
def download_titles():
print('Starting downloads...')
cur = 1
total = len(titles)
for titleid, version_table in titles.items():
print('======================================================')
print('{cur} of {total}'.format(cur=cur, total=total))
print('======================================================')
for version in version_table:
PyNUSD.main(
titleid=titleid,
titlever=version,
pack_as_wad=True,
keepcontents=True,
decryptcontents=False,
localuse=True,
cdndir=True,
)
print("")
cur += 1
if __name__ == "__main__":
titles = get_datbaase()
if titles:
download_titles()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment