forked from stashapp/CommunityScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
titleFromFilename.py
74 lines (60 loc) · 1.95 KB
/
titleFromFilename.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import json
import os
import sys
import time
import config
# import log
import graphql
API_VERSION_BF_FILES = 31 # APP/DB Schema version prior to files refactor PR
MAX_RETRY_COUNT = 25
SLEEP_RETRY = 0.5
FRAGMENT = json.loads(sys.stdin.read())
# log.LogDebug(json.dumps(FRAGMENT))
FRAGMENT_SERVER = FRAGMENT["server_connection"]
FRAGMENT_SCENE_ID = FRAGMENT["args"].get("hookContext")
if FRAGMENT_SCENE_ID:
scene_id = FRAGMENT_SCENE_ID["id"]
else:
graphql.exit_plugin("No ID found")
graphql_port = FRAGMENT_SERVER["Port"]
graphql_scheme = FRAGMENT_SERVER["Scheme"]
graphql_session = FRAGMENT_SERVER.get("SessionCookie").get("Value")
system_status = graphql.get_api_version(
port=graphql_port, session=graphql_session, scheme=graphql_scheme
)
api_version = system_status.get("appSchema")
basename = None
if api_version > API_VERSION_BF_FILES: # only needed for versions after files refactor
files_base = graphql.get_scene_base(
scene_id=scene_id,
port=graphql_port,
session=graphql_session,
scheme=graphql_scheme,
)
if len(files_base["files"]) > 0:
basename = files_base["files"][0].get("basename")
else:
graphql.exit_plugin(
f"Stash with API version:{api_version} is not supported. You need at least {API_VERSION_BF_FILES}"
)
if basename is None:
graphql.exit_plugin("No basename found") # file-less scene
if config.STRIP_EXT:
basename = os.path.splitext(basename)[0]
i = MAX_RETRY_COUNT
while i >= 0:
# log.LogDebug(f"TitleFromFilename: Retry attempt {i}")
i -= 1
updated_scene = graphql.update_scene_title(
scene_id,
basename,
port=graphql_port,
session=graphql_session,
scheme=graphql_scheme,
)
if updated_scene:
graphql.exit_plugin(
f"Scene title updated after {MAX_RETRY_COUNT - i} tries. Title:{updated_scene.get('title')}"
)
time.sleep(SLEEP_RETRY)
graphql.exit_plugin("Error updating scene")