1
+ import re
2
+ import sys
3
+ import json
4
+ from pathlib import Path
5
+
6
+ try :
7
+ import stashapi .log as log
8
+ from stashapi .tools import file_to_base64
9
+ except ModuleNotFoundError :
10
+ print (
11
+ "You need to install the stashapi module. (pip install stashapp-tools)" ,
12
+ file = sys .stderr ,
13
+ )
14
+
15
+ image_file_extensions = [".jpg" ,".png" ]
16
+ cover_pattern = r"(?:thumb|poster|cover)\.(?:jpg|png)"
17
+
18
+ def main ():
19
+ mode = sys .argv [1 ]
20
+ fragment = json .loads (sys .stdin .read ())
21
+
22
+ data = None
23
+ if mode == 'scene_fragment' :
24
+ data = scene_fragment (fragment )
25
+
26
+ if data :
27
+ print (json .dumps (data ))
28
+ else :
29
+ print ("null" )
30
+
31
+
32
+ def scene_fragment (fragment ):
33
+ filepath = Path (fragment ["files" ][0 ]["path" ])
34
+ cover = find_cover (filepath )
35
+ if not cover :
36
+ return
37
+ b64img = file_to_base64 (cover )
38
+ if not b64img :
39
+ log .warning (f"Could not parse { filepath } to b64image" )
40
+ return
41
+
42
+ return {"image" :b64img }
43
+
44
+ def find_cover (filepath :Path ):
45
+ for file in filepath .parent .iterdir ():
46
+ if file == filepath :
47
+ continue
48
+ # return image with same name as scene file as long as it has an image suffix
49
+ if file .suffix in image_file_extensions and file .stem == filepath .stem :
50
+ return file
51
+ # return image if it matches pattern
52
+ if re .match (cover_pattern ,file .name , re .IGNORECASE ):
53
+ return file
54
+
55
+
56
+ if __name__ == "__main__" :
57
+ main ()
0 commit comments