2022-12-10 14:10:01 +02:00

69 lines
1.6 KiB
Python

import sys, json
from pathlib import Path
import stashapi.log as log
from stashapi.stashapp import StashInterface, stashapp_gql_fragments
import re
from dateparser import parse
from datetime import datetime
def main():
global stash
global pattern
pattern = re.compile(r"\D(\d{4}|\d{1,2})[\._\- /\\](\d{1,2}|[a-zA-Z]{3,}\.*)[\._\- /\\](\d{4}|\d{1,2})\D")
json_input = json.loads(sys.stdin.read())
mode_arg = json_input['args']['mode']
stash = StashInterface(json_input["server_connection"],fragments=[stashapp_gql_fragments.DEVELOP])
match mode_arg:
case "gallery":
find_date_for_galleries()
#log.exit("ok")
def find_date_for_galleries():
galleries = stash.find_galleries(f={
"is_missing": "date",
"path": {
"modifier": "MATCHES_REGEX",
"value": ".zip$"
},
"file_count": {
"modifier": "EQUALS",
"value": 1
}
})
total = len(galleries)
log.info(f"Found {total} galleries")
for i, gallery in enumerate(galleries):
log.progress(i/total)
acceptableDate = None
for file in gallery.get("files", []):
for match in pattern.finditer(file["path"]):
g1 = match.group(1)
g2 = match.group(2)
g3 =match.group(3)
temp = parse(g1+" "+g2+" "+g3)
if temp:
acceptableDate = temp.strftime("%Y-%m-%d")
if acceptableDate:
log.info("Gallery ID ("+gallery.get("id") + ") has matched the date : "+acceptableDate)
updateObject = {
"id":gallery.get("id"),
"date":acceptableDate
}
stash.update_gallery(updateObject)
if __name__ == '__main__':
main()