-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathnxshot.py
244 lines (187 loc) · 6.5 KB
/
nxshot.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import sys
import os
import argparse
import json
from pathlib import Path
from datetime import datetime
from shutil import copy
from Crypto.Cipher import AES
import hashlib
from urllib.request import urlopen, urlretrieve
from urllib.error import URLError
from bs4 import BeautifulSoup
import xml.etree.ElementTree as ET
KEY_HASH = "24e0dc62a15c11d38b622162ea2b4383"
IS_UPDATED = 0
ENCRYPTED_IDS = {}
# Argument parser
parser = argparse.ArgumentParser(
description='Automatically organize and timestamp\
your Nintendo Switch captures.')
parser.add_argument(
'filepath',
metavar='FILEPATH',
type=Path,
help='"Nintendo/Album" folder from your SD card.')
parser.add_argument('-d',
'--download-nswdb',
action='store_true',
help='Download IDs from nswdb.com instead of switchbrew.org\n \
NOTE: Regions may not match')
# If there are arguments, parse them. If not, exit
args = parser.parse_args()
def load_key(file_name, KEY_HASH):
try:
with open(file_name, 'r') as key_file:
key_string = key_file.read(32)
key = bytes.fromhex(key_string)
if(hashlib.md5(key).hexdigest() not in KEY_HASH):
raise ValueError("Keys don't match!")
return key
except FileNotFoundError:
print("Encryption key (key.txt) not found!")
except ValueError:
print("Encryption key (key.txt) doesn't match!")
def encrypt_title_id(key, title_id):
key_cipher = AES.new(key, AES.MODE_ECB)
title_id_bytes = bytearray.fromhex(title_id[:16])
title_id_bytes.reverse()
title_id_bytes = title_id_bytes.ljust(16, b'\0') # Padding
encrypted_title_id = key_cipher.encrypt(title_id_bytes)
screenshot_id = encrypted_title_id.hex().upper()
return screenshot_id
def update_game_ids():
key = load_key('key.txt', KEY_HASH)
if not key:
return -1
if args.download_nswdb:
update_nswdb(key)
else:
update_switchbrew(key)
with open('gameids.json', 'w', encoding='utf-8') as encrypted_ids_file:
json.dump(ENCRYPTED_IDS, encrypted_ids_file,
ensure_ascii=False, indent=4, sort_keys=True)
print("Successfully updated Game IDs")
return 1
def update_switchbrew(key):
wiki = 'http://switchbrew.org/index.php?title=Title_list/Games'
try:
wiki_page = urlopen(wiki)
except URLError:
print("Could not update gameids!")
return -1
wiki_soup = BeautifulSoup(wiki_page, 'html.parser')
game_table = wiki_soup.find('table', {"class": "wikitable sortable"})
game_table_rows = game_table.find_all('tr')
for row in game_table_rows[1:]:
try:
title_id = str(row.contents[1].string)
game_name = str(row.contents[3].string)
region = str(row.contents[5].string)
except IndexError: # At least one cell empty; ignore row
continue
if title_id == 'None' or game_name == 'None' or region == 'None':
continue
if ":" in game_name:
game_name = game_name.replace(":", " -")
#print('TitleID = {}'.format(title_id))
screenshot_id = encrypt_title_id(key, title_id)
#print("ScreenshotID = {}".format(encrypted.hex()))
ENCRYPTED_IDS[screenshot_id] = game_name + ' (' + region + ')'
def update_nswdb(key):
urlretrieve('http://nswdb.com/xml.php', 'db.xml')
tree = ET.parse('db.xml')
root = tree.getroot()
for release in root.findall('release'):
try:
screenshot_id = encrypt_title_id(key, release.find('titleid').text)
region = release.find('region').text
if region == "WLD":
region = "EUR USA"
name = release.find('name').text
name = name.replace(":", " -")
except ValueError:
continue
ENCRYPTED_IDS[screenshot_id] = name + ' (' + region + ')'
os.remove("db.xml")
def check_id(game_id, ENCRYPTED_IDS):
if game_id in ENCRYPTED_IDS:
return ENCRYPTED_IDS[game_id]
else:
return 'Unknown'
def check_folders(file_list):
current = 0
length = len(file_list)
# print(file_list)
for media_path in file_list:
year = media_path.stem[0:4]
month = media_path.stem[4:6]
day = media_path.stem[6:8]
hour = media_path.stem[8:10]
minute = media_path.stem[10:12]
second = media_path.stem[12:14]
game_id = media_path.stem[17:]
try:
time = datetime(
int(year),
int(month),
int(day),
hour=int(hour),
minute=int(minute),
second=int(second))
pass
except ValueError:
print(
'Invalid filename for media {}: {}'.format(
current,
str(media_path.stem)
)
)
continue
posix_timestamp = time.timestamp()
output_folder = args.filepath.joinpath(
'Organized', check_id(game_id, ENCRYPTED_IDS))
output_folder.mkdir(parents=True, exist_ok=True)
new_file = copy(str(media_path), str(output_folder))
os.utime(new_file, (posix_timestamp, posix_timestamp))
current += 1
print('Organized {} of {} files.\r'.format(current, length), end='')
print("")
# Load game ids and their names from external file
try:
with open('gameids.json', 'r', encoding='utf-8') as encrypted_ids_file:
ENCRYPTED_IDS = json.load(encrypted_ids_file)
except FileNotFoundError:
print('Game ID list (gameids.json) not found! Fetching from Switchbrew...')
IS_UPDATED = update_game_ids()
if IS_UPDATED < 0:
sys.exit()
except json.decoder.JSONDecodeError:
print('Invalid JSON format! Fetching from Switchbrew...')
IS_UPDATED = update_game_ids()
if IS_UPDATED < 0:
sys.exit()
if not IS_UPDATED:
update_game_ids()
album_folder = args.filepath
screenshot_list = sorted(
Path(album_folder).glob(
'[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]/*.jpg'
)
)
video_list = sorted(
Path(album_folder).glob(
'[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]/*.mp4'
)
)
if len(screenshot_list) != 0:
print('Organizing screenshots...')
check_folders(screenshot_list)
else:
print('No screenshots found!')
if len(video_list) != 0:
print('\nOrganizing videos...')
check_folders(video_list)
else:
print('\nNo videos found!')
print('Done!')