forked from stashapp/CommunityScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
defaultDataForPath.js
450 lines (395 loc) · 10.6 KB
/
defaultDataForPath.js
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
var jsonData = [
{
"name": "OPTIONAL NAME - NOT USED IN SCRIPT",
"paths": [
"C:\\Users\\UserName\\Desktop\\NOTPORN\\Brazzers",
"D:\\SecretStorage\\Porn\\Brazzers"
],
"studio": "Brazzers",
"tags": [
"Default_Data_For_Path_Tagged"
]
}
];
function ok() {
return {
output: "ok"
};
}
function main() {
var hookContext = input.Args.hookContext;
var type = hookContext.type;
var ID = hookContext.ID;
if (!type || !ID) {
// just return
return ok();
}
var itemPath;
var name = "";
if (type === 'Scene.Create.Post') {
itemPath = getScenePath(ID);
name = "scene"
} else if (type === 'Gallery.Create.Post') {
itemPath = getGalleryPath(ID);
name = "gallery"
} else if(type === 'Image.Create.Post') {
itemPath = getImagePath(ID);
name = "image"
}
var defaultData = getDefaultData(itemPath)
// create tags
var defaultTags = [];
for(var p=0; p<defaultData.length; p++) {
var tags = defaultData[p].tags;
if(tags) {
for(var t=0; t<tags.length; t++) {
var tag = tags[t]
if(stringNotAlreadyPresent(tag, defaultTags))
defaultTags.push(tag)
}
}
}
// create studio
var addStudio = false;
var defaultStudioId = null;
var defaultStudio;
for(var p=0; p<defaultData.length; p++) {
var studio = defaultData[p].studio;
if(studio) {
var studioId = getStudioId(studio)
if(studioId) {
defaultStudioId = studioId;
addStudio = true;
defaultStudio = studio;
}
}
}
// create performers
var defaultPerformers = [];
for(var p=0; p<defaultData.length; p++) {
var performers = defaultData[p].performers;
if(performers) {
for(var t=0; t<performers.length; t++) {
var performer = performers[t];
if(stringNotAlreadyPresent(performer, defaultPerformers))
defaultPerformers.push(performer)
}
}
}
// convert tags to tagIds
var addTags = false;
var defaultTagIds = [];
if(defaultTags) {
for(var i=defaultTags.length-1; i>=0; i--) {
var tagId = getTagId(defaultTags[i])
tagId ? defaultTagIds.push(tagId) : defaultTags.pop();
}
if(defaultTagIds && defaultTagIds.length != 0) {
addTags = true;
}
}
// convert performers to performerIds
var addPerformers = false;
var defaultPerformerIds = [];
if(defaultPerformers) {
for(var i=defaultPerformers.length-1; i>=0; i--) {
var tagId = getPerformerId(defaultPerformers[i])
tagId ? defaultPerformerIds.push(tagId) : defaultPerformers.pop();
}
if(defaultPerformerIds && defaultPerformerIds.length != 0) {
addPerformers = true;
}
}
// Apply all and log
var tags = addTags ? defaultTagIds : null;
var studio = addStudio ? defaultStudioId : null;
var performers = addPerformers ? defaultPerformerIds : null;
if (type === 'Scene.Create.Post') {
setSceneData(ID, tags, studio, performers)
} else if (type === 'Gallery.Create.Post') {
setGalleryData(ID, tags, studio, performers)
} else if(type === 'Image.Create.Post') {
setImageData(ID, tags, studio, performers)
}
for(var o=0;o<defaultTags.length;o++) {
log.Info("[DefaultDataForPath]: Added tag " + defaultTags[o] + " to " + name + " " + ID);
}
for(var o=0;o<defaultPerformers.length;o++) {
log.Info("[DefaultDataForPath]: Added performer " + defaultPerformers[o] + " to " + name + " " + ID);
}
addStudio ? log.Info("[DefaultDataForPath]: Added studio " + defaultStudio + " to " + name + " " + ID) : "";
}
function getScenePath(ID) {
var query = "\
query findScene($id: ID) {\
findScene(id: $id) {\
path\
}\
}";
var variables = {
id: ID
};
var result = gql.Do(query, variables);
var findScene = result.findScene;
if (!findScene) {
return null;
}
var path = findScene.path;
return path;
}
function getImagePath(ID) {
var query = "\
query findImage($id: ID) {\
findImage(id: $id) {\
path\
}\
}";
var variables = {
id: ID
};
var result = gql.Do(query, variables);
var findImage = result.findImage;
if (!findImage) {
return null;
}
var path = findImage.path;
return path;
}
function getGalleryPath(ID) {
var query = "\
query findGallery($id: ID) {\
findGallery(id: $id) {\
path\
}\
}";
var variables = {
id: ID
};
var result = gql.Do(query, variables);
var findGallery = result.findGallery;
if (!findGallery) {
return null;
}
var path = findGallery.path;
return path;
}
function getTagId(tagName) {
var query = "\
query findTagId($filter: FindFilterType!) {\
findTags(filter: $filter) {\
tags {\
id\
}\
}\
}";
var variables = {
filter: {
q: tagName
}
};
result = gql.Do(query, variables)
if(result.findTags.tags[0]) {
return result.findTags.tags[0].id;
}
else {
log.Error("TAG " + tagName + " DOES NOT EXIST IN STASH!")
return null;
}
}
function getPerformerId(performerName) {
var query = "\
query findPerformerId($filter: FindFilterType!) {\
findPerformers(filter: $filter) {\
performers {\
id\
}\
}\
}";
var variables = {
filter: {
q: performerName
}
};
result = gql.Do(query, variables)
if(result.findPerformers.performers[0]) {
return result.findPerformers.performers[0].id;
}
else {
log.Error("PERFORMER " + performerName + " DOES NOT EXIST IN STASH!")
return null;
}
}
function getStudioId(studioName) {
var query = "\
query findStudioId($filter: FindFilterType!) {\
findStudios(filter: $filter) {\
studios {\
id\
}\
}\
}";
var variables = {
filter: {
q: studioName
}
};
result = gql.Do(query, variables)
if(result.findStudios.studios[0]) {
return result.findStudios.studios[0].id
}
else {
log.Error("STUDIO " + studioName + " DOES NOT EXIST IN STASH!")
return null;
}
}
function stringNotAlreadyPresent(text, itemArray) {
for(var i=0;i < itemArray.length; i++) {
if(itemArray[i] === text) {
return false;
}
}
return true;
}
function addAllData(obj, lowerItemPath, defaultData, pTags, pPerformers, pStudio) {
if(obj) {
if(obj.paths) {
var paths = obj.paths;
if(containsPath(paths, lowerItemPath)) {
// inject data from parent if avail
if(pTags) {
if(!obj.tags) {
obj.tags = [];
}
obj.tags = obj.tags.concat(pTags)
}
if(pPerformers) {
if(!obj.performers) {
obj.performers = [];
}
obj.performers = obj.performers.concat(pPerformers)
}
if(pStudio) {
obj.studio = pStudio
}
defaultData.push(obj)
}
}
else {
// add defaults to children
if(obj.tags) {
if(!pTags) {
pTags = obj.tags;
}
else {
pTags = pTags.concat(obj.tags)
}
}
if(obj.performers) {
if(!pPerformers) {
pPerformers = obj.performers
}
else {
pPerformers = pPerformers.concat(obj.performers)
}
}
if(obj.studio) {
pStudio = obj.studio;
}
}
if(obj.children) {
for(var o=0;o<obj.children.length;o++) {
defaultData = addAllData(obj.children[o], lowerItemPath, defaultData, pTags, pPerformers, pStudio)
}
}
}
return defaultData
}
function getDefaultData(itemPath) {
var defaultData = [];
var lowerItemPath = itemPath.toLowerCase();
for(var i=0;i<jsonData.length;i++) {
var obj = jsonData[i];
defaultData = addAllData(obj, lowerItemPath, defaultData, null, null, null)
}
return defaultData;
}
function containsPath(paths, inputPath) {
for(var p=0;p<paths.length;p++) {
var path = paths[p].toLowerCase() + '';
if(stringContains(inputPath, path)) {
log.Info("[DefaultDataForPath]: " + inputPath + " MATCH " + path)
return true;
}
}
return false;
}
function stringContains(value, searchFor)
{
var v = (value || '').toLowerCase();
var v2 = searchFor;
if (v2) {
v2 = v2.toLowerCase();
}
return v.indexOf(v2) > -1;
}
function containsElem(items, elem) {
for(var i=0;i<items.length;i++) {
var item = items[i].toLowerCase();
if(item.equals(elem)) {
return true;
}
}
return false;
}
function setSceneData(sceneID, tagIDs, studioID, performerIds) {
var mutation = "\
mutation sceneUpdate($input: SceneUpdateInput!) {\
sceneUpdate(input: $input) {\
id\
}\
}";
var variables = {
input: {
id: sceneID,
tag_ids: tagIDs,
studio_id: studioID,
performer_ids: performerIds
}
};
gql.Do(mutation, variables);
}
function setImageData(sceneID, tagIDs, studioID, performerIds) {
var mutation = "\
mutation imageUpdate($input: ImageUpdateInput!) {\
imageUpdate(input: $input) {\
id\
}\
}";
var variables = {
input: {
id: sceneID,
tag_ids: tagIDs,
studio_id: studioID,
performer_ids: performerIds
}
};
gql.Do(mutation, variables);
}
function setGalleryData(sceneID, tagIDs, studioID, performerIds) {
var mutation = "\
mutation galleryUpdate($input: GalleryUpdateInput!) {\
galleryUpdate(input: $input) {\
id\
}\
}";
var variables = {
input: {
id: sceneID,
tag_ids: tagIDs,
studio_id: studioID,
performer_ids: performerIds
}
};
gql.Do(mutation, variables);
}
main();