1+ import os
12import sys , json
23from PythonDepManager import ensure_import
34ensure_import ("dateparser>=1.2.1" )
4- ensure_import ("stashapi:stashapp-tools>=0.2.58 " )
5+ ensure_import ("stashapi:stashapp-tools" )
56import stashapi .log as log
67from stashapi .stashapp import StashInterface
78import re
@@ -13,30 +14,38 @@ def main():
1314 global pattern
1415
1516 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* "
1718 )
1819 json_input = json .loads (sys .stdin .read ())
1920 mode_arg = json_input ["args" ]["mode" ]
2021
2122 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" ])
2327 if mode_arg == "gallery" :
24- find_date_for_galleries ()
28+ find_date_for_galleries (settings )
2529
2630
2731def parse_date_candidate (string ):
2832 result = None
2933 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 )
3338 temp = parse (g1 + " " + g2 + " " + g3 )
3439 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 ]
3645 return result
3746
3847
39- def find_date_for_galleries ():
48+ def find_date_for_galleries (settings ):
4049
4150 galleries = stash .find_galleries (f = {"is_missing" : "date" })
4251
@@ -62,9 +71,11 @@ def find_date_for_galleries():
6271 "Gallery ID ("
6372 + gallery .get ("id" )
6473 + ") has matched the date : "
65- + acceptableDate
74+ + acceptableDate [ 0 ]
6675 )
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 ]
6879 stash .update_gallery (updateObject )
6980
7081
0 commit comments