1
+ import os
1
2
import sys , json
2
3
from PythonDepManager import ensure_import
3
4
ensure_import ("dateparser>=1.2.1" )
4
- ensure_import ("stashapi:stashapp-tools>=0.2.58 " )
5
+ ensure_import ("stashapi:stashapp-tools" )
5
6
import stashapi .log as log
6
7
from stashapi .stashapp import StashInterface
7
8
import re
@@ -13,30 +14,38 @@ def main():
13
14
global pattern
14
15
15
16
pattern = re .compile (
16
- r"\D(\d{4}|\d{1,2})[\._\- /\\](\d{1,2}|[a-zA-Z]{3,}\.*)[\._\- /\\](\d{4}|\d{1,2})\D "
17
+ r"\D(( \d{4}|\d{1,2})[\._\- /\\](\d{1,2}|[a-zA-Z]{3,}\.*)[\._\- /\\](\d{4}|\d{1,2}))\D* "
17
18
)
18
19
json_input = json .loads (sys .stdin .read ())
19
20
mode_arg = json_input ["args" ]["mode" ]
20
21
21
22
stash = StashInterface (json_input ["server_connection" ])
22
-
23
+ config = stash .get_configuration ()["plugins" ]
24
+ settings = {"setTitle" : False }
25
+ if "date_parser" in config :
26
+ settings .update (config ["date_parser" ])
23
27
if mode_arg == "gallery" :
24
- find_date_for_galleries ()
28
+ find_date_for_galleries (settings )
25
29
26
30
27
31
def parse_date_candidate (string ):
28
32
result = None
29
33
for match in pattern .finditer (string ):
30
- g1 = match .group (1 )
31
- g2 = match .group (2 )
32
- g3 = match .group (3 )
34
+ g0 = match .group (1 )
35
+ g1 = match .group (2 )
36
+ g2 = match .group (3 )
37
+ g3 = match .group (4 )
33
38
temp = parse (g1 + " " + g2 + " " + g3 )
34
39
if temp :
35
- result = temp .strftime ("%Y-%m-%d" )
40
+ potential_title = None
41
+ _ ,ext = os .path .splitext (string )
42
+ if not ext and g0 in os .path .basename (string ):
43
+ potential_title = os .path .basename (string ).replace (g0 , "" ).strip ()
44
+ result = [temp .strftime ("%Y-%m-%d" ), potential_title ]
36
45
return result
37
46
38
47
39
- def find_date_for_galleries ():
48
+ def find_date_for_galleries (settings ):
40
49
41
50
galleries = stash .find_galleries (f = {"is_missing" : "date" })
42
51
@@ -62,9 +71,11 @@ def find_date_for_galleries():
62
71
"Gallery ID ("
63
72
+ gallery .get ("id" )
64
73
+ ") has matched the date : "
65
- + acceptableDate
74
+ + acceptableDate [ 0 ]
66
75
)
67
- updateObject = {"id" : gallery .get ("id" ), "date" : acceptableDate }
76
+ updateObject = {"id" : gallery .get ("id" ), "date" : acceptableDate [0 ]}
77
+ if settings ['setTitle' ] and not gallery .get ("title" ) and acceptableDate [1 ]:
78
+ updateObject ["title" ] = acceptableDate [1 ]
68
79
stash .update_gallery (updateObject )
69
80
70
81
0 commit comments