-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTTS_dumper.py
228 lines (185 loc) · 7.18 KB
/
TTS_dumper.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
# =============================================
# TTS downloader
# Based on https://github.com/theFroh/ttsunhoster
# =============================================
#!/usr/bin/env python3
import os
from json import load
import pprint
from enum import Enum
import requests
import urllib.parse
import concurrent.futures
import urllib.request
MAX_WORKERS = 15
class DataType(Enum):
image = 1
model = 2
pdf = 3
# what do we want to fetch?
MESH_DATA = {
"MeshURL": DataType.model,
"NormalURL": DataType.image,
"DiffuseURL": DataType.image,
"ColliderURL": DataType.model
}
IMAGE_DATA = {
"ImageURL": DataType.image,
"FaceURL": DataType.image,
"BackURL": DataType.image,
"ImageSecondaryURL": DataType.image
}
PDF_DATA = {
"PDFUrl": DataType.pdf
}
# =====================================================
# slugify
# Taken from https://stackoverflow.com/questions/295135/turn-a-string-into-a-valid-filename
# =====================================================
import unicodedata
import re
def slugify(value, allow_unicode=False):
"""
Taken from https://github.com/django/django/blob/master/django/utils/text.py
Convert to ASCII if 'allow_unicode' is False. Convert spaces or repeated
dashes to single dashes. Remove characters that aren't alphanumerics,
underscores, or hyphens. Convert to lowercase. Also strip leading and
trailing whitespace, dashes, and underscores.
"""
value = str(value)
if allow_unicode:
value = unicodedata.normalize('NFKC', value)
else:
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
value = re.sub(r'[^\w\s-]', '', value.lower())
return re.sub(r'[-\s]+', '-', value).strip('-_')
# =====================================================
# load_tts_url
# =====================================================
def load_tts_url(tts_url):
path = None
if tts_url[1] == DataType.image:
path = os.path.join(image_dir, tts_url[2])
elif tts_url[1] == DataType.model:
path = os.path.join(model_dir, tts_url[2])
elif tts_url[1] == DataType.pdf:
path = os.path.join(pdf_dir, tts_url[2])
if path is not None:
if os.path.isfile(path) and not args.replace:
print(f"\tload_tts_url: {path} already exists, skipping.")
return 'skipping'
r = requests.get(tts_url[0], headers={"User-Agent": "XY"})
if r.status_code == requests.codes.ok:
return r.content
else:
print("Error", tts_url[0], r.status_code)
return None
def url_to_tts(url, dtype):
url_path = urllib.parse.urlparse(url).path
url_ext = os.path.splitext(url_path)[1]
if url_ext == '' or url_ext == '.php':
if dtype == DataType.image:
url_ext = '.png'
elif dtype == DataType.model:
url_ext = '.obj'
elif dtype == DataType.pdf:
url_ext = '.pdf'
return "".join([c for c in url if c.isalpha() or c.isdigit()]).rstrip() + url_ext
counter = 0
def json_extract(obj, keys):
"""Recursively fetch values from nested JSON."""
arr = []
def extract(obj, arr, keys):
global counter
"""Recursively search for values of key in JSON tree."""
if isinstance(obj, dict):
for k, url in obj.items():
if isinstance(url, (dict, list)):
extract(url, arr, keys)
elif k in keys and url != '':
if url[:4] != "http":
url = "http://" + url # partly handles these
arr.append((url,keys[k], url_to_tts(url,keys[k])))
elif isinstance(obj, list):
for item in obj:
extract(item, arr, keys)
return arr
values = extract(obj, arr, keys)
return values
def parse_tts_custom_object(workshop_json):
with open(workshop_json, "r", encoding="utf-8") as fp:
save = load(fp)
objects = save["ObjectStates"]
image_urls = json_extract(objects, IMAGE_DATA)
model_urls = json_extract(objects, MESH_DATA)
pdf_urls = json_extract(objects, PDF_DATA)
SaveName = slugify(save['SaveName'])
return image_urls, model_urls, pdf_urls, SaveName
if __name__ == '__main__':
download = False
import argparse
import sys
parser = argparse.ArgumentParser(description="Unhosts Tabletop Simulator workshop custom content.")
parser.add_argument("json_input", help="path to either a WorkshopFileInfos file, or one or more Workshop mod .json files", nargs="+")
parser.add_argument("--output", "-o", help="where to store the Models and Images subdirectories")
parser.add_argument("--replace", "-r", help="replace files already in the output directory")
args = parser.parse_args()
all_urls = []
for json in args.json_input:
print(json)
image_urls, model_urls, pdf_urls, saveName = parse_tts_custom_object(json)
all_urls = image_urls.copy()
all_urls.extend(model_urls)
all_urls.extend(pdf_urls)
if args.output:
output_dir = args.output
else:
output_dir = os.path.join(os.path.dirname(os.path.abspath(args.json_input[0])), 'TTS_' + saveName)
print("Output directory:", output_dir)
if not os.path.exists(output_dir):
os.makedirs(output_dir)
image_dir = os.path.join(output_dir, "Images")
if not os.path.exists(image_dir):
os.makedirs(image_dir)
model_dir = os.path.join(output_dir, "Models")
if not os.path.exists(model_dir):
os.makedirs(model_dir)
pdf_dir = os.path.join(output_dir, "PDF")
if not os.path.exists(pdf_dir):
os.makedirs(pdf_dir)
print("\nURL's to nab:")
# get a list of all urls
# all_urls = all_model_urls
# all_urls.extend(all_image_urls)
pprint.pprint(all_urls)
print("\nGrabbing {} files".format(len(all_urls)))
errorFiles = []
n = 0
with concurrent.futures.ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor:
future_to_url = {executor.submit(load_tts_url, url): url for url in all_urls}
for future in concurrent.futures.as_completed(future_to_url):
url = future_to_url[future]
data = future.result()
print("(%d/%d) %s" % (n, len(all_urls), url[0]))
if data is not None and data != 'skipping':
path = None
if url[1] == DataType.image:
path = os.path.join(image_dir, url[2])
elif url[1] == DataType.model:
path = os.path.join(model_dir, url[2])
elif url[1] == DataType.pdf:
path = os.path.join(pdf_dir, url[2])
if path is not None:
if not os.path.isfile(path) or args.replace: # This verification is still needed because of the concurrency...
with open(path, "wb") as fp:
fp.write(data)
else:
print("\tWritting: Already exists, skipping.")
sys.stdout.flush()
else:
if data is None:
errorFiles.append(url)
n += 1
print("Done!")
print("\n\nERRORS:", len(errorFiles))
print(*errorFiles, sep="\n")