-
Notifications
You must be signed in to change notification settings - Fork 0
/
wiiu_cdndownload.py
executable file
·137 lines (114 loc) · 6.74 KB
/
wiiu_cdndownload.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python3
# usage: wiiu_cdndownload.py <titleid>
import base64
import binascii
import os
import struct
import sys
import zlib
from urllib.request import urlretrieve, urlopen
import aria2p
# initialization, these are the default values
aria2 = aria2p.API(
aria2p.Client(
host="http://localhost",
port=6800,
secret=""
)
)
if len(sys.argv) == 1:
print('wiiu_cdndownload.py <titleid> [version]')
print('Latest version is downloaded, if no version is specified.')
sys.exit(1)
# compressed with zlibm encoded with base64
titlecert = 'eJytkvk/E44fx9GsT58ZsrlvaUmxMJ8RQiTXx50wRRbmWObKkTnTZ5FQxsxNJlfKyvGNCpnJbY7k+Nacc205P+X69H30+Qv0fb5/fr0er8f78eTi5jqCM9Riv24u8iXhx7jVsVIZzqaWhOJ7kuklQk6R8/xbJ6Lb+QXVJ7QnF8iZTxecR31JlPlpX759zbNPH/PGIw4S9Lt0jsTJFIDfjZXCYy+9rP1mKOldKmX8iv1g/s7IsF/ZVURRInZu6M0Io/hiBz1CEqGAvO4aRn57FH6byC7cRnUlhBe08evPdCc8kgs3QN8369giOLrdzAkZ0UtxOqj+dFWG6HDRDyK2a3I/YYhe6pEMrNu9ZhMFmS9KarGVqRtRLTVOTbCBXi6voS63punmDcMfKXdWjbOdaDxipmO35P5SZwyMjS0ag9M9pCKzxwlG7bmyqmfxOVfxtmdFsAHREtXm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for
app_categories = {
'0000', # application
'0002', # demo
'000C', # DLC
}
tid = sys.argv[1].upper()
if len(tid) != 16:
print('Title ID is invalid length')
print('wiiu_cdndownload.py <titleid> [version]')
print('Latest version is downloaded, if no version is specified.')
sys.exit(1)
# http://stackoverflow.com/questions/8866046/python-round-up-integer-to-next-hundred
def roundup(x, base=64):
return x if x % base == 0 else x + base - x % base
# some things used from
# http://stackoverflow.com/questions/13881092/download-progressbar-for-python-3
blocksize = 10 * 1024
def queqe_download(uri, dir_, out):
aria2.add_uris([uri], {'continue': True, 'dir': dir_, 'out': out})
def download(url, printprogress=False, outfile=None, message_prefix='', message_suffix=''):
cn = urlopen(url)
totalsize = int(cn.headers['content-length'])
totalread = 0
if not outfile:
ct = b''
while totalsize > totalread:
toread = min(totalsize - totalread, blocksize)
co = cn.read(toread)
totalread += toread
if printprogress:
percent = min(totalread * 1e2 / totalsize, 1e2)
print('\r{:29} {:>5.1f}% {:>10} / {:>10} {}'.format(message_prefix, percent, totalread, totalsize, message_suffix), end='')
if outfile:
outfile.write(co)
else:
ct += co
if printprogress:
print('')
if not outfile:
return ct
sysbase = 'http://nus.cdn.c.shop.nintendowifi.net/ccs/download/' + tid
appbase = 'http://ccs.cdn.c.shop.nintendowifi.net/ccs/download/' + tid
os.makedirs(tid, exist_ok=True)
base = appbase
if tid[4:8] not in app_categories:
base = sysbase
print('Downloading CETK (title.tik)...')
with open(tid + '/title.tik', 'wb') as f:
download(base + '/cetk', False, f)
print('Downloading TMD (title.tmd)...')
# this is a mess how can i make it better
tmd = download(base + '/tmd{}'.format(('.' + sys.argv[2]) if len(sys.argv) > 2 else ''))
contents = []
count = struct.unpack('>H', tmd[0x1DE:0x1E0])[0]
print('Contents: {}'.format(count))
contentsize = 0
for c in range(count):
contents.append([
# content ID
binascii.hexlify(tmd[0xB04 + (0x30 * c):0xB04 + (0x30 * c) + 0x4]).decode('utf-8'),
# content type
struct.unpack('>H', tmd[0xB0A + (0x30 * c):0xB0A + (0x30 * c) + 0x2])[0],
# content size
struct.unpack('>Q', tmd[0xB0C + (0x30 * c):0xB0C + (0x30 * c) + 0x8])[0],
])
with open(tid + '/title.tmd', 'wb') as f:
f.write(tmd)
total_size = sum(c[2] for c in contents)
print("Total size: 0x{:X} ({} MiB)".format(total_size, total_size / (1024 ** 2)))
print('Writing cert (title.cert)...')
with open(tid + '/title.cert', 'wb') as f:
f.write(zlib.decompress(base64.b64decode(titlecert)))
for c in contents:
uri = os.path.join(base, c[0])
dir_ = os.path.join(os.getcwd(), tid)
print(uri)
print(dir_)
if os.path.isfile(tid + '/' + c[0] + '.app.aria2') and os.path.getsize(tid + '/' + c[0] + '.app') == c[2]:
print('Skipping {}.app due to existing file with proper size'.format(c[0]))
else:
out = c[0] + '.app'
queqe_download(uri, dir_, out)
# with open(tid + '/' + c[0] + '.app', 'wb') as f:
# download(base + '/' + c[0], True, f, 'Downloading: {}.app...'.format(c[0]), '({}) MiB)'.format(c[2] / (1024 ** 2)))
if c[1] & 0x2:
out = c[0] + '.h3'
queqe_download(uri + '.h3', dir_, out)
# with open(tid + '/' + c[0] + '.h3', 'wb') as f:
# download(base + '/' + c[0] + '.h3', True, f, 'Downloading: {}.h3...'.format(c[0]))