forked from stg-annon/stashapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstashbox.py
286 lines (235 loc) · 8.57 KB
/
stashbox.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
import re, math, json, copy
from enum import Enum
from requests.structures import CaseInsensitiveDict
from .classes import GQLWrapper
from . import stashbox_gql_fragments
from . import log as StashLogger
class StashboxTarget(Enum):
SCENE = "SCENE"
STUDIO = "STUDIO"
PERFORMER = "PERFORMER"
TAG = "TAG"
class StashBoxInterface(GQLWrapper):
port = None
url = None
headers = {
"Accept-Encoding": "gzip, deflate",
"Content-Type": "application/json",
"Accept": "application/json",
"Connection": "keep-alive",
"DNT": "1"
}
cookies = {}
def __init__(self, conn={}, fragments:list[str]=[stashbox_gql_fragments.DEVELOP]):
conn = CaseInsensitiveDict(conn)
self.log = conn.get("Logger", StashLogger)
self.url = conn.get('endpoint', "https://stashdb.org/graphql")
self.endpoint = self.url
if not self.url:
raise Exception("REQUIRED key 'endpoint' not provided in connection dict")
stash = conn.get("stash")
if stash:
c = stash.get_stashbox_connection(self.url)
api_key = c.get("api_key")
if not api_key:
raise Exception(f"Could not find api_key for '{self.url}' with prorivded stash connection")
else:
api_key = conn.get('api_key', None)
if not api_key:
raise Exception(f"REQUIRED key 'api_key' not provided in connection dict ({conn})")
self.headers['ApiKey'] = api_key
try:
# test query to check connection
r = self._callGraphQL("query Me{me {name email}}")
self.log.debug(f'Connected to "{self.url}" as {r["me"]["name"]} ({r["me"]["email"]})')
except Exception as e:
self.log.exit(f"Could not connect to Stash-Box at {self.url}", e)
self.fragments = {}
for fragment in fragments:
self.parse_fragments(fragment)
def get_scene_last_updated(self, scene_id):
query = """query sceneLastUpdated($id: ID!) {
findScene(id: $id) {
id
updated
deleted
}
}"""
result = self._callGraphQL(query, {"id": scene_id})
return result["findScene"]
def pending_edits_count(self, stash_id, target_type):
query = """query PendingEditsCount($type: TargetTypeEnum!, $id: ID!) {
queryEdits(input: {target_type: $type, target_id: $id, status: PENDING, per_page: 1}) {
count
}
}
"""
variables = {
"type": target_type.name,
"id": stash_id
}
return self._callGraphQL(query, variables)["queryEdits"]["count"]
def get_tag(self, tag_id, fragment=None):
query = """query FindTag($id: ID!) {
findTag(id: $id) {
...TagFragment
}
}"""
if fragment:
query = re.sub(r'\.\.\.TagFragment', fragment, query)
result = self._callGraphQL(query, {"id":tag_id})
return result["findTag"]
def find_tags(self, tag_query, fragment=None, pages=-1, callback=None):
query = """query Tags($input: TagQueryInput!){
queryTags(input: $input){
count
tags{
...TagFragment
}
}
}"""
if fragment:
query = re.sub(r'\.\.\.TagFragment', fragment, query)
tag_query["page"] = tag_query.get("page", 1)
tag_query["per_page"] = 40
return self._paginate_query(query, tag_query, pages, callback)
def find_scene(self, scene_id, fragment=None):
query = """query FindScene($id: ID!) {
findScene(id: $id) {
...SceneFragment
}
}"""
if fragment:
query = re.sub(r'\.\.\.SceneFragment', fragment, query)
result = self._callGraphQL(query, {"id":scene_id})
return result["findScene"]
def find_scenes_count(self, scene_query):
query = """query FindScenes($input: SceneQueryInput!) {
queryScenes(input: $input) {
count
scenes { id }
}
}"""
return self._callGraphQL(query, {"input":scene_query})["queryScenes"]["count"]
def find_scenes(self, scene_query, fragment=None, pages=-1, callback=None):
query = """query FindScenes($input: SceneQueryInput!) {
queryScenes(input: $input) {
count
scenes {
...SceneFragment
}
}
}"""
if fragment:
query = re.sub(r'\.\.\.SceneFragment', fragment, query)
scene_query["page"] = scene_query.get("page", 1)
scene_query["per_page"] = 40
return self._paginate_query(query, scene_query, pages, callback)
# returns items up to specified page, -1 for all pages (default: -1)
def _paginate_query(self, query, type_input, pages=-1, callback=None):
result = self._callGraphQL(query, {"input": type_input})
queryType = list(result.keys())[0]
result = result[queryType]
itemType = list(result.keys())[1]
items = result[itemType]
if callback != None:
callback(items)
if pages == -1: # set to all pages if -1
pages = math.ceil(result["count"] / type_input["per_page"])
self.log.progress(float(type_input["page"])/float(pages))
self.log.debug(f'received page {type_input["page"]}/{pages} for {queryType} query')
if type_input.get("page") < pages:
type_input["page"] = type_input["page"] + 1
next_page = self._paginate_query(query, type_input, pages, callback)
items.extend(next_page)
return items
def find_site_id(self, site_name):
query = """query FindSiteId{ querySites{ count sites{ id name url } } }"""
result = self._callGraphQL(query)
for site in result["querySites"]["sites"]:
if site["name"].upper() == site_name.upper():
return site["id"]
def fetch_scene_edit_details(self, stash_id):
existing = self.find_scene(stash_id, fragment="...SceneEditFragment")
# cast existing meta to SceneEditDetailsInput
existing["studio_id"] = existing["studio"]["id"]
del existing["studio"]
existing["image_ids"] = [ i["id"] for i in existing["images"] ]
del existing["images"]
existing["tag_ids"] = [ t["id"] for t in existing["tags"] ]
del existing["tags"]
for p in existing["performers"]:
p["performer_id"] = p["performer"]["id"]
del p["performer"]
for url in existing["urls"]:
url["site_id"] = url["site"]["id"]
del url["site"]
return existing
def edit_scene(self, stash_id:str, edit:dict, manual_comment:str):
if self.pending_edits_count(stash_id, StashboxTarget.SCENE) > 0:
self.log.warning(f'Edit not submited Scene:{stash_id} has pending edits')
return
comments = []
details = self.fetch_scene_edit_details(stash_id)
if edit.get("tags"):
passed_tag_ids = edit["tags"]["ids"]
mode = edit["tags"]["mode"]
if mode == "SET":
details["tag_ids"] = passed_tag_ids
comments.append("SET Tags")
if mode == "ADD":
details["tag_ids"].extend(passed_tag_ids)
comments.append(f"ADD {len(passed_tag_ids)} Tag(s)")
if mode == "REMOVE":
details["tag_ids"] = [tid for tid in details["tag_ids"] if tid not in passed_tag_ids]
comments.append(f"REMOVE {len(passed_tag_ids)} Tag(s)")
if edit.get("performers"):
passed_performer_appearances = edit["performers"]["appearances"]
mode = edit["performers"]["mode"]
if mode == "SET":
details["performers"] = passed_performer_appearances
comments.append("SET Performers")
if mode == "ADD":
details["performers"].extend(passed_performer_appearances)
comments.append(f"ADD {len(passed_performer_appearances)} Performer(s)")
if mode == "REMOVE":
remove_lookup = [p["performer_id"] for p in passed_performer_appearances]
details["performers"] = [p for p in details["performers"] if p["performer_id"] not in remove_lookup ]
comments.append(f"REMOVE {len(passed_performer_appearances)} Performer(s)")
if edit.get("urls"):
passed_url_edits = edit["urls"]["links"]
mode = edit["urls"]["mode"]
if mode == "SET":
details["urls"] = passed_url_edits
comments.append("SET Url(s)")
if mode == "ADD":
details["urls"].extend(passed_url_edits)
comments.append(f"ADD {len(passed_url_edits)} Url(s)")
if mode == "REMOVE":
remove_lookup = [url["url"] for url in passed_url_edits]
details["urls"] = [url for url in details["urls"] if url["url"] not in remove_lookup ]
comments.append(f"REMOVE {len(passed_url_edits)} Url(s)")
if mode == "REPLACE":
for url_edit in passed_url_edits:
for url in details["urls"]:
if url["url"] == url_edit["target_url"]:
url["url"] = url_edit["url"]
comments.append(f'REPLACE {url_edit["target_url"]} with {url_edit["url"]}')
for attr in ["code","date","details","director","duration","studio_id","title"]:
if edit.get(attr):
details[attr] = edit[attr]
comments.append(f"update/correct `{attr}`")
comments = [f"* {c}" for c in comments]
comments.insert(0, manual_comment)
comment = "\n".join(comments)
query = """mutation SceneEdit($sceneData: SceneEditInput!) { sceneEdit(input: $sceneData) { id } }"""
input = {
"sceneData":{
"edit": {"bot":True, "id":stash_id, "operation":"MODIFY", "comment":comment},
"details": details
}
}
result = self._callGraphQL(query, input)
return result["sceneEdit"]
def callGQL(self, q, v):
return self._callGraphQL(q, v)