Skip to content

Commit f5ff6af

Browse files
authored
[Gallery Date Parser] Fixed issue with trailing dates + added optional title set (#588)
1 parent 88863bc commit f5ff6af

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

plugins/DateParser/date_parser.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import os
12
import sys, json
23
from PythonDepManager import ensure_import
34
ensure_import("dateparser>=1.2.1")
4-
ensure_import("stashapi:stashapp-tools>=0.2.58")
5+
ensure_import("stashapi:stashapp-tools")
56
import stashapi.log as log
67
from stashapi.stashapp import StashInterface
78
import 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

2731
def 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

plugins/DateParser/date_parser.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: Gallery Date Parser
22
# requires: PythonDepManager
33
description: Find date in path or filename and add it to the gallery
4-
version: 1.0.0
4+
version: 1.1.0
55
exec:
66
- python
77
- "{pluginDir}/date_parser.py"
@@ -11,3 +11,8 @@ tasks:
1111
description: Add the date on galleries based on their path
1212
defaultArgs:
1313
mode: gallery
14+
settings:
15+
setTitle:
16+
displayName: Set title ?
17+
description: Set title from folder name minus the found date ? Only applies on galleries without titles
18+
type: BOOLEAN

0 commit comments

Comments
 (0)