-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileRenamer.py
379 lines (326 loc) · 14.4 KB
/
FileRenamer.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# A simple python script to iterate through files in the directory, then
# rename and create Kodi style sidecar files from Stash queries
# It will create an NFO and background jpg from Stash metadata
# Files will be moved to a subdirectory tree based on Studio parents
# as defined in Stash
# The query is based on filename, so if the file has not been scraped /
# tagged in Stash then it will be skipped. It uses the Studio attribute
# to determine whether to parse or not
# Most code 'borrowed' heavily from WithoutPants' Kodi Helper (https://github.com/stashapp/CommunityScripts/tree/main/scripts/kodi-helper)
# and the TPDB Stash scraper (https://github.com/ThePornDatabase/stash_theporndb_scraper)
import argparse
import os
import re
import argparse
import json
import glob
import requests
import shutil
import logging
import FileRenamerConfig as config
def parseArgs():
parser = argparse.ArgumentParser(description="Rename files from Stash metadata and create accompanying NFO files")
parser.add_argument("--indir", metavar="<input directory>", help="Directory containing files to process (Default to '.')",default="./")
parser.add_argument("--outdir", metavar="<output directory>", help="Generate files in <outdir> (Default to '.')",default="./")
parser.add_argument("--mask", metavar="<filemask>", help="File mask to process. Defaults to '*'", default="*")
parser.add_argument("--extra", help="Also write JPG and NFO files", default=False)
return parser.parse_args()
def main():
args = parseArgs()
if config.use_https:
server = 'https://' + str(config.server_ip) + ':' + str(config.server_port)
else:
server = 'http://' + str(config.server_ip) + ':' + str(config.server_port)
config.server = server
config.auth = setAuth(server)
# Iterate through current directory
# ~ filelist = [f for f in os.listdir('.') if os.path.isfile(f)]
filelist = glob.glob(args.mask.strip())
if filelist:
filelist = [f for f in filelist if os.path.isfile(f)]
for file in filelist:
basename = os.path.splitext(file)[0]
query = config.file_query.replace("<FILENAME>", basename)
jsonresult = callGraphQL(query, config.server, config.auth)
# We only want to process files that have a Studio defined
try:
if jsonresult and not jsonresult['data']['findScenes']['scenes'][0]['studio'] is None:
filedata = {}
filedata['jsondata'] = jsonresult['data']['findScenes']['scenes'][0]
filedata['studiolist'] = get_parental_path(filedata['jsondata']['studio']['id'])
filedata['filename'] = file
filedata['basename'] = os.path.splitext(file)[0]
filedata['fullpathname'] = renamefile(filedata, args)
if 'extras' in args:
if args.extras:
getimage(filedata)
nfodata = generateNFO(filedata['jsondata'], args)
writeFile(filedata['fullpathname'] + ".nfo", nfodata, True)
else:
print(f' *** Scene data not found for {basename}')
except Exception as e:
if not os.path.exists("NotInStash"):
os.makedirs("NotInStash",exist_ok = True)
print(f' Moving file: {basename} into NotInStash/ due to {e}')
shutil.move(file, "NotInStash/" + file)
def callGraphQL(query, server, http_auth_type, retry = True):
graphql_server = server+"/graphql"
json = {}
json['query'] = query
try:
if http_auth_type == "basic":
response = requests.post(graphql_server, json=json, headers=config.headers, auth=(username, password), verify= not config.ignore_ssl_warnings)
elif http_auth_type == "jwt":
response = requests.post(graphql_server, json=json, headers=config.headers, cookies={'session':auth_token}, verify= not config.ignore_ssl_warnings)
else:
response = requests.post(graphql_server, json=json, headers=config.headers, verify= not config.ignore_ssl_warnings)
if response.status_code == 200:
result = response.json()
if result.get("error", None):
for error in result["error"]["errors"]:
logging.error("GraphQL error: {}".format(error), exc_info=debug_mode)
if result.get("data", None):
return result
elif retry and response.status_code == 401 and http_auth_type == "jwt":
jwtAuth()
return callGraphQL(query, variables, False)
else:
logging.error("GraphQL query failed to run by returning code of {}. Query: {}.".format(response.status_code, query))
raise Exception("GraphQL error")
except requests.exceptions.SSLError:
proceed = input("Caught certificate error trying to talk to Stash. Add ignore_ssl_warnings=True to your configuration.py to ignore permanently. Ignore for now? (yes/no):")
if proceed == 'y' or proceed == 'Y' or proceed =='Yes' or proceed =='yes':
ignore_ssl_warnings =True
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
return callGraphQL(query, variables)
else:
print("Exiting.")
sys.exit()
def setAuth(server):
global http_auth_type
r = requests.get(server+"/playground", verify= not config.ignore_ssl_warnings)
if len(r.history)>0 and r.history[-1].status_code == 302:
http_auth_type="jwt"
jwtAuth()
elif r.status_code == 200:
http_auth_type="none"
else:
http_auth_type="basic"
return http_auth_type
def jwtAuth():
response = requests.post(server+"/login", data = {'username':config.username, 'password':config.password}, verify= not config.ignore_ssl_warnings)
auth_token=response.cookies.get('session',None)
if not auth_token:
logging.error("Error authenticating with Stash. Double check your IP, Port, Username, and Password", exc_info=debug_mode)
sys.exit()
def renamefile(filedata, args):
nameformat = config.name_format
# Need to create folder structure if not there
fullpath = args.outdir.strip()
if config.create_parental_path:
for item in reversed(filedata['studiolist']):
studiopath = re.sub(r'[^-a-zA-Z0-9_.() ]+', '', filedata['studiolist'][item])
fullpath = fullpath + studiopath.strip() + "/"
fullpath = fullpath.title()
if not os.path.exists(fullpath):
os.makedirs(fullpath, exist_ok=True)
# Set up filename to use
data = filedata['jsondata']
performers = []
counter = 0
for performer in data['performers']:
if counter < 3:
performers.append(performer['name'])
counter += 1
if performers:
performerstring = ", ".join(performers)
performerstring = f"({performerstring.strip()})"
else:
performerstring = ""
tags = []
for tag in data['tags']:
tags.append(tag['name'])
if tags:
tagstring = ", ".join(tags)
else:
tagstring = ""
targetname = config.name_format
targetname = targetname.replace("<STUDIO>", data['studio']['name'].strip().title())
if not data['studio']['parent_studio'] is None:
parentname = data['studio']['parent_studio']['name'].strip().title()
else:
parentname = data['studio']['name'].strip().title()
dimensions = ""
if re.search(r'\[(\d+p)\]', filedata['filename']):
dimensions = re.search(r'(\[\d+p\])', filedata['filename']).group(1)
else:
if not data['file']['width'] is None and not data['file']['height'] is None:
dimensions = F"[{str(data['file']['width'])}x{str(data['file']['height'])}]"
data['title'] = re.sub(r'[^-a-zA-Z0-9_.()\[\]\' ,]+', ' ', data['title']).title()
if len(data['title']) > 100:
data['title'] = data['title'].strip().title()[0:100]
targetname = targetname.replace("<PARENT>", parentname)
targetname = targetname.replace("<TITLE>", data['title'].strip().title())
targetname = targetname.replace("<ID>", data['id'].strip())
targetname = targetname.replace("<DATE>", data['date'].strip())
targetname = targetname.replace("<PERFORMERS>", performerstring)
targetname = targetname.replace("<TAGS>", tagstring)
targetname = targetname.replace("<DIMENSIONS>", dimensions)
if re.search(r'([\\/])', targetname):
addpath = re.search(r'(.*[\\/])', targetname).group(1)
fullpath = fullpath + addpath
fullpath = re.sub(r'[^-a-zA-Z0-9_\.()\[\]\' ,\\/]+', '', fullpath).title()
if not os.path.exists(fullpath):
os.makedirs(fullpath, exist_ok=True)
targetname = re.search(r'.*[\\/](.*?)$', targetname).group(1)
targetname = re.sub(r'[^-a-zA-Z0-9_\.()\[\]\' ,]+', '', targetname)
# Have to strip possible S##E## for Plex
if re.search(r'([sS]\d{1,3}:?[eE]\d{1,3})', targetname):
targetname = re.sub(r'[sS]\d{1,3}:?[eE]\d{1,3}', '', targetname).title()
# Now move the file
filepathname = fullpath + targetname
filepathname = filepathname.replace(" ", " ")
extension = os.path.splitext(filedata['filename'])[-1]
if len(os.getcwd() + filepathname) > 255:
filepathname = filepathname.replace(performerstring, "")
origfile = filedata['filename']
print(f' Moving file: {origfile} to {filepathname}')
shutil.move(origfile, filepathname + extension)
# ~ shutil.copy(origfile, filepathname + os.path.splitext(filedata['filename'])[-1])
# Return the path and bare filename to be used for NFO and JPG
return filepathname
def getimage(filedata):
if not filedata['jsondata']['paths'] is None:
imagepath = filedata['jsondata']['paths']['screenshot']
filepath = filedata['fullpathname'] + ".jpg"
response = requests.get(imagepath)
imagefile = open(filepath, "wb")
imagefile.write(response.content)
imagefile.close()
else:
filename = filedata['fullpathname']
print(f'No Screenshot found for {filename}')
def addAPIKey(url):
if config.api_key:
return url + "&apikey=" + config.api_key
return url
def getSceneTitle(scene):
if scene["title"] is not None and scene["title"] != "":
return scene["title"]
return basename(scene["path"])
def generateNFO(scene, args):
ret = """<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<movie>
<title>{title}</title>
<userrating>{rating}</userrating>
<plot>{details}</plot>
<uniqueid type="stash">{id}</uniqueid>
{tags}
<premiered>{date}</premiered>
<studio>{studio}</studio>
{performers}
{thumbs}
{fanart}
{genres}
</movie>
"""
# ~ tags = ""
# ~ for t in scene["tags"]:
# ~ tags = tags + """
# ~ <tag>{}</tag>""".format(t["name"])
genres = ""
for t in scene["tags"]:
if t['id'] not in config.ignore_tags and "ambiguous" not in t['name'].lower():
genres = genres + """
<genre>{}</genre>""".format(t["name"])
rating = ""
if scene["rating"] is not None:
rating = str(int(scene["rating"]) * 2)
date = ""
if scene["date"] is not None:
date = scene["date"]
studio = ""
logo = ""
if scene["studio"] is not None:
studio = scene["studio"]["name"]
logo = scene["studio"]["image_path"]
if not logo.endswith("?default=true"):
logo = addAPIKey(logo)
else:
logo = ""
performers = ""
i = 0
for p in scene["performers"]:
thumb = addAPIKey(p["image_path"])
performers = performers + """
<actor>
<name>{}</name>
<role></role>
<order>{}</order>
<thumb>{}</thumb>
</actor>""".format(p["name"], i, thumb)
i += 1
thumbs = [
"""<thumb aspect="poster">{}</thumb>""".format(addAPIKey(scene["paths"]["screenshot"]))
]
fanart = [
"""<thumb>{}</thumb>""".format(addAPIKey(scene["paths"]["screenshot"]))
]
if logo != "":
thumbs.append("""<thumb aspect="clearlogo">{}</thumb>""".format(logo))
fanart.append("""<thumb>{}</thumb>""".format(logo))
fanart = """<fanart>{}</fanart>""".format("\n".join(fanart))
if not scene['studio']['parent_studio'] is None:
parent = scene['studio']['parent_studio']['name']
else:
parent = scene['studio']['name']
if config.create_collection_tags:
tags = '<tag>Site: {}</tag>\n'.format(scene['studio']['name'])
tags += '<tag>Studio: {}</tag>\n'.format(parent)
else:
tags = ""
# ~ genres = []
# ~ if args.genre != None:
# ~ for g in args.genre:
# ~ genres.append("<genre>{}</genre>".format(g))
ret = ret.format(title=getSceneTitle(scene), rating=rating, id=scene["id"], tags=tags, date=date, studio=studio, performers=performers, details=scene["details"] or "", thumbs="\n".join(thumbs), fanart=fanart, genres=genres)
return ret
def writeFile(fn, data, useUTF):
encoding = None
if useUTF:
encoding = "utf-8-sig"
f = open(fn, "w", encoding=encoding)
f.write(data)
f.close()
def get_parental_path(studioid):
basequery = """
query {
findStudio(
id: "<STUDIONUM>"
) {
id
name
parent_studio{
id
}
}
}
"""
query = basequery.replace("<STUDIONUM>", studioid)
jsonresult = callGraphQL(query, config.server, config.auth)
counter = 0
studiolist = {}
studioid = jsonresult['data']['findStudio']['id']
while True:
query = basequery.replace("<STUDIONUM>", studioid)
jsonresult = callGraphQL(query, config.server, config.auth)
studiolist[counter] = jsonresult['data']['findStudio']['name']
if not jsonresult['data']['findStudio']['parent_studio'] is None:
studioid = jsonresult['data']['findStudio']['parent_studio']['id']
jsonresult = {}
counter += 1
else:
break
return studiolist
if __name__ == "__main__":
main()