-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompleteTheStash.py
337 lines (284 loc) · 12.8 KB
/
CompleteTheStash.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
from dataclasses import dataclass
import json
import os
import sys
from urllib.parse import urlparse
import stashapi.log as logger
from LocalStashClient import LocalStashClient
from MissingStashClient import MissingStashClient
from StashCompleter import StashCompleter
from StashDbClient import StashDbClient
from FansDbClient import FansDbClient
from TpdbClient import TpdbClient
@dataclass
class MissingSceneSource:
stashappAddress: str
stashappApiKey: str
stashboxEndpoint: str
@dataclass
class CompleteTheStashConfiguration:
performerTags: str
sceneExcludeTags: str
stashDbSceneSource: MissingSceneSource
FansDBSceneSource: MissingSceneSource
enableSceneHooks: bool
def parse_url(url):
parsed_url = urlparse(url)
scheme = parsed_url.scheme
host = parsed_url.hostname
port = parsed_url.port or (443 if scheme == "https" else 80)
return scheme, host, port
def check_stash_instances_are_unique(local_configuration, missing_configuration):
local_api_key = local_configuration.get("general", {}).get("apiKey")
missing_api_key = missing_configuration.get("general", {}).get("apiKey")
if local_api_key == missing_api_key:
raise ValueError(
"Local and missing Stash instances have the same API key which indicates both local and missing Stash instances are the same. "
"Please create a different instance for missing Stash."
)
def get_json_input():
if os.getenv("ENABLE_DEV_MODE"):
import dotenv
dotenv.load_dotenv()
fake_input = os.getenv("FAKE_INPUT")
if not fake_input:
raise ValueError("FAKE_INPUT environment variable is not set.")
return json.loads(fake_input)
else:
raw_input = sys.stdin.read()
return json.loads(raw_input)
def get_matching_stashbox_config(
local_configuration, missing_scene_source: MissingSceneSource
):
stashbox_endpoint = missing_scene_source.stashboxEndpoint
stash_boxes = local_configuration.get("general", {}).get("stashBoxes")
if not stash_boxes:
raise ValueError("No stash boxes are configured.")
matching_stashbox = next(
(box for box in stash_boxes if box.get("endpoint") == stashbox_endpoint),
None,
)
if not matching_stashbox:
raise ValueError(
f"Stashbox is not configured for endpoint {stashbox_endpoint}."
)
return matching_stashbox
def get_complete_the_stash_config(local_configuration) -> CompleteTheStashConfiguration:
plugins_configuration = local_configuration.get("plugins", {})
complete_the_stash_config = plugins_configuration.get("CompleteTheStash")
if not complete_the_stash_config:
raise ValueError("CompleteTheStash plugin is not configured.")
if not complete_the_stash_config.get("performerTags"):
raise ValueError("Performer tags are not configured.")
if (
not complete_the_stash_config.get("missingStashAddress")
and not complete_the_stash_config.get("missingStashApiKey")
and not complete_the_stash_config.get("missingFansDBAddress")
and not complete_the_stash_config.get("missingFansDBApiKey")
and not complete_the_stash_config.get("missingStashTpdbAddress")
and not complete_the_stash_config.get("missingStashTpdbApiKey")
):
raise ValueError(
"No missing Stash instances are configured. No missing scenes can be sourced. Are you missing configuration?"
)
if complete_the_stash_config.get(
"missingStashAddress"
) or complete_the_stash_config.get("missingStashApiKey"):
if not complete_the_stash_config.get("missingStashAddress"):
raise ValueError(
"URL for missing Stash for StashDB scenes is not configured. Please set in Plugins configuration."
)
if not complete_the_stash_config.get("missingStashApiKey"):
raise ValueError(
"API Key for missing Stash for StashDB scenes is not configured. Please set in Plugins configuration."
)
if complete_the_stash_config.get(
"missingFansDBAddress"
) or complete_the_stash_config.get("missingFansDBApiKey"):
if not complete_the_stash_config.get("missingFansDBAddress"):
raise ValueError(
"URL for missing Stash for FansDB scenes is not configured. Please set in Plugins configuration."
)
if not complete_the_stash_config.get("missingFansDBApiKey"):
raise ValueError(
"API Key for missing Stash for FansDB scenes is not configured. Please set in Plugins configuration."
)
if complete_the_stash_config.get(
"missingStashTpdbAddress"
) or complete_the_stash_config.get("missingStashTpdbApiKey"):
if not complete_the_stash_config.get("missingStashTpdbAddress"):
raise ValueError(
"URL for missing Stash for TPDB scenes is not configured. Please set in Plugins configuration."
)
if not complete_the_stash_config.get("missingStashTpdbApiKey"):
raise ValueError(
"API Key for missing Stash for TPDB scenes is not configured. Please set in Plugins configuration."
)
performer_tags = complete_the_stash_config.get("performerTags").split(",")
scene_exclude_tags = complete_the_stash_config.get("sceneExcludeTags", "").split(
","
)
stash_db_configuration = None
if complete_the_stash_config.get(
"missingStashAddress"
) and complete_the_stash_config.get("missingStashApiKey"):
stash_db_configuration = MissingSceneSource(
stashappAddress=complete_the_stash_config.get("missingStashAddress"),
stashappApiKey=complete_the_stash_config.get("missingStashApiKey"),
stashboxEndpoint="https://stashdb.org/graphql",
)
FansDB_configuration = None
if complete_the_stash_config.get(
"missingFansDBAddress"
) and complete_the_stash_config.get("missingFansDBApiKey"):
FansDB_configuration = MissingSceneSource(
stashappAddress=complete_the_stash_config.get("missingFansDBAddress"),
stashappApiKey=complete_the_stash_config.get("missingFansDBApiKey"),
stashboxEndpoint="hhttps://fansdb.cc/graphql",
)
tpdb_configuration = None
if complete_the_stash_config.get(
"missingStashTpdbAddress"
) and complete_the_stash_config.get("missingStashTpdbApiKey"):
tpdb_configuration = MissingSceneSource(
stashappAddress=complete_the_stash_config.get("missingStashTpdbAddress"),
stashappApiKey=complete_the_stash_config.get("missingStashTpdbApiKey"),
stashboxEndpoint="https://theporndb.net/graphql",
)
return CompleteTheStashConfiguration(
performerTags=performer_tags,
sceneExcludeTags=scene_exclude_tags,
stashDbSceneSource=stash_db_configuration,
FansDBSceneSource=FansDB_configuration,
tpdbSceneSource=tpdb_configuration,
enableSceneHooks=complete_the_stash_config.get("enableSceneHooks", False),
)
def create_missing_stash_client(missingSceneSource: MissingSceneSource):
scheme, host, port = parse_url(missingSceneSource.stashappAddress)
return MissingStashClient(
scheme,
host,
port,
missingSceneSource.stashappApiKey,
missingSceneSource.stashboxEndpoint,
logger,
)
def process_input(json_input, stash_completer: StashCompleter):
logger.debug(f"Processing input: {json_input}")
event_type = json_input.get("args", {}).get("hookContext", {}).get("type")
if json_input.get("args", {}).get("mode") == "process_performers":
logger.info(f"Processing performers for endpoint {stash_completer.stashbox_client.endpoint}.")
stash_completer.process_performers()
elif event_type in ["Scene.Create.Post", "Scene.Update.Post"]:
try:
scene_id = json_input.get("args", {}).get("hookContext", {}).get("id")
except AttributeError:
scene_id = None
if scene_id is None:
logger.debug(
f"Scene ID is not provided in the input for type {event_type}. Skipping."
)
return
logger.debug(f"Processing scene create type {event_type} for scene {scene_id} for endpoint {stash_completer.stashbox_client.endpoint}.")
stash_completer.process_scene_by_id(scene_id)
else:
logger.error(f"Invalid input: {json_input}")
def execute():
json_input = get_json_input()
logger.debug(f"Input: {json_input}")
local_stash_client = LocalStashClient(json_input["server_connection"], logger)
local_configuration = local_stash_client.get_configuration()
logger.debug(f"Local configuration: {local_configuration}")
complete_the_stash_config = get_complete_the_stash_config(local_configuration)
# Stop processing as soon as possible to improve performance if scene hooks are disabled.
event_type = json_input.get("args", {}).get("hookContext", {}).get("type")
if event_type in ["Scene.Create.Post", "Scene.Update.Post"] and not complete_the_stash_config.enableSceneHooks:
return
# StashDB
if complete_the_stash_config.stashDbSceneSource:
missing_stash_client = create_missing_stash_client(
complete_the_stash_config.stashDbSceneSource
)
missing_configuration = missing_stash_client.get_configuration()
check_stash_instances_are_unique(local_configuration, missing_configuration)
stashbox_config = get_matching_stashbox_config(
local_configuration, complete_the_stash_config.stashDbSceneSource
)
stashbox_client = StashDbClient(
stashbox_config["endpoint"],
stashbox_config["api_key"],
)
config = {
"performerTags": complete_the_stash_config.performerTags,
"stashboxEndpoint": complete_the_stash_config.stashDbSceneSource.stashboxEndpoint,
"sceneExcludeTags": complete_the_stash_config.sceneExcludeTags,
"enableSceneHooks": complete_the_stash_config.enableSceneHooks,
}
stash_completer = StashCompleter(
config,
logger,
stashbox_client,
local_stash_client,
missing_stash_client,
)
process_input(json_input, stash_completer)
# FansDB
if complete_the_stash_config.fansDBSceneSource:
missing_stash_client = create_missing_stash_client(
complete_the_stash_config.stashDbSceneSource
)
missing_configuration = missing_stash_client.get_configuration()
check_stash_instances_are_unique(local_configuration, missing_configuration)
stashbox_config = get_matching_stashbox_config(
local_configuration, complete_the_stash_config.FansDbSceneSource
)
stashbox_client = FansDBClient(
stashbox_config["endpoint"],
stashbox_config["api_key"],
)
config = {
"performerTags": complete_the_stash_config.performerTags,
"stashboxEndpoint": complete_the_stash_config.fansDBSceneSource.stashboxEndpoint,
"sceneExcludeTags": complete_the_stash_config.sceneExcludeTags,
"enableSceneHooks": complete_the_stash_config.enableSceneHooks,
}
stash_completer = StashCompleter(
config,
logger,
stashbox_client,
local_stash_client,
missing_stash_client,
)
process_input(json_input, stash_completer)
# TPDB
if complete_the_stash_config.tpdbSceneSource:
missing_stash_client = create_missing_stash_client(
complete_the_stash_config.tpdbSceneSource
)
missing_configuration = missing_stash_client.get_configuration()
check_stash_instances_are_unique(local_configuration, missing_configuration)
stashbox_config = get_matching_stashbox_config(
local_configuration, complete_the_stash_config.tpdbSceneSource
)
stashbox_client = TpdbClient(
stashbox_config["endpoint"],
stashbox_config["api_key"],
)
config = {
"performerTags": complete_the_stash_config.performerTags,
"stashboxEndpoint": complete_the_stash_config.tpdbSceneSource.stashboxEndpoint,
"sceneExcludeTags": complete_the_stash_config.sceneExcludeTags,
"enableSceneHooks": complete_the_stash_config.enableSceneHooks,
}
stash_completer = StashCompleter(
config,
logger,
stashbox_client,
local_stash_client,
missing_stash_client,
)
process_input(json_input, stash_completer)
if __name__ == "__main__":
script_dir = os.path.dirname(os.path.realpath(__file__))
sys.path.append(script_dir)
execute()