forked from stashapp/CommunityScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filenameParser.js
397 lines (332 loc) · 9.62 KB
/
filenameParser.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
function ok() {
return {
output: "ok"
};
}
function main() {
var hookContext = input.Args.hookContext;
var type = hookContext.type;
var ID = hookContext.ID;
if (!ID) {
return ok();
}
var filenameFetcher;
var saver;
if (type === 'Scene.Create.Post') {
filenameFetcher = getSceneFilename;
saver = updateScene;
} else if (type === 'Gallery.Create.Post') {
filenameFetcher = getGalleryFilename;
saver = updateGallery;
} else {
return ok();
}
var filename = filenameFetcher(ID);
if (!filename) {
return ok();
}
filename = cleanFilename(filename);
var parseResult = parseFilename(filename);
saver(ID, parseResult);
return ok();
}
function getSceneFilename(sceneID) {
var query = "\
query findScene($id: ID) {\
findScene(id: $id) {\
path\
}\
}";
var variables = {
id: sceneID
};
var result = gql.Do(query, variables);
var findScene = result.findScene;
if (!findScene) {
return null;
}
var path = findScene.path;
return path.substring(path.lastIndexOf('/') + 1);
}
function updateScene(sceneID, parseResult) {
var query = "\
mutation SceneUpdate($input: SceneUpdateInput!) {\
sceneUpdate(input: $input) {\
id\
}\
}";
var variables = {
input: parseResult
};
variables.input.id = sceneID;
gql.Do(query, variables);
}
function getGalleryFilename(galleryID) {
var query = "\
query findGallery($id: ID!) {\
findGallery(id: $id) {\
path\
}\
}";
var variables = {
id: galleryID
};
var result = gql.Do(query, variables);
var findGallery = result.findGallery;
if (!findGallery) {
return null;
}
var path = findGallery.path;
return path.substring(path.lastIndexOf('/') + 1);
}
function updateGallery(galleryID, parseResult) {
var query = "\
mutation GalleryUpdate($input: GalleryUpdateInput!) {\
galleryUpdate(input: $input) {\
id\
}\
}";
var variables = {
input: parseResult
};
variables.input.id = galleryID;
gql.Do(query, variables);
}
function matchNames(parts, name, aliases) {
var names = [name].concat(aliases);
var partRegexes = [];
for (var i = 0; i < parts.length; i++) {
partRegexes[i] = new RegExp('^' + parts[i].toLowerCase() + '[. \\-_]*');
}
var cleanRegex = /[. \-_]/g;
var longestMatch = 0;
for (var i = 0; i < names.length; i++) {
var name = names[i].replace(cleanRegex, '').toLowerCase();
for (var j = 0; j < partRegexes.length; j++) {
if (!partRegexes[j].test(name)) {
break;
}
name = name.replace(partRegexes[j], '');
if (name.length === 0) {
if (j + 1 > longestMatch) {
longestMatch = j + 1;
}
}
}
}
return longestMatch;
}
function cleanFilename(name) {
name = name
// remove imageset-...[rarbg]
.replace(/imageset-[\w\d]+\[rarbg]/i, '')
// replace [...] with just ...
.replace(/\[(.*?)]/g, '$1')
// replace (...) with just ...
.replace(/\((.*?)\)/g, '$1')
// replace {...} with just ...
.replace(/{(.*?)}/g, '$1')
;
var blockList = [
'mp4',
'mov',
'xxx',
'4k',
'4096x2160',
'3840x2160',
'2160p',
'1080p',
'1920x1080',
'60fps',
'30fps',
'repack',
'ktr',
];
var regExp = new RegExp('(_|[^\\w\\d]|^)(' + blockList.join('|') + ')(_|[^\\w\\d]|$)', 'i');
while (regExp.test(name)) {
name = name.replace(regExp, '$1$3');
}
// If name starts with <...>.com remove the .com (sometimes names include studio name as site/domain)
name = name.replace(/^([\w\d-]+?)\.com/, '$1');
name = name
// Remove everything except letters and digits at the start
.replace(/^(_|[^\w\d])+/, '')
// Remove everything except letters and digits at the end
.replace(/(_|[^\w\d])+$/, '')
;
return name;
}
function matchStudio(parts, result) {
var query = "\
query findStudios($studio_filter: StudioFilterType, $filter: FindFilterType!) {\
findStudios(studio_filter: $studio_filter, filter: $filter) {\
studios {\
id\
name\
aliases\
}\
}\
}";
var searchTerm = parts[0].substring(0, 2);
if (parts[0].substring(0, 1) === 'a') {
searchTerm = parts[0].substring(1, 3);
}
var variables = {
filter: {
per_page: -1,
},
studio_filter: {
name: {
modifier: "INCLUDES",
value: searchTerm
},
OR: {
aliases: {
modifier: "INCLUDES",
value: searchTerm
}
}
}
};
var queryResult = gql.Do(query, variables);
var studios = queryResult.findStudios.studios;
if (!studios.length && parts[0].substring(0, 1) === 'a') {
variables.studio_filter.name.value = variables.studio_filter.OR.aliases.value = parts[0].substring(1, 3);
queryResult = gql.Do(query, variables);
studios = queryResult.findStudios.studios;
}
var matchingParts = 0;
for (var i = 0; i < studios.length; i++) {
var studio = studios[i];
matchingParts = matchNames(parts, studio.name, studio.aliases);
if (matchingParts === 0) {
continue;
}
result.studio_id = studio.id;
break;
}
return matchingParts;
}
function matchDate(parts, result) {
if (
parts.length < 3 ||
!/^(\d{2}|\d{4})$/.test(parts[0]) ||
!/^\d{2}$/.test(parts[1]) ||
!/^\d{2}$/.test(parts[2])
) {
return 0;
}
var year = parseInt(parts[0], 10);
var month = parseInt(parts[1], 10);
var day = parseInt(parts[2], 10);
if (year < 100) {
year += 2000;
}
if (
year < 2000 || year > 2100 ||
month < 1 || month > 12 ||
day < 1 || day > 31
) {
return 0;
}
result.date = year + "-" + (month < 10 ? "0" + month : month) + "-" + (day < 10 ? "0" + day : day);
return 3;
}
function matchPerformers(parts, result) {
var query = "\
query findPerformers($performer_filter: PerformerFilterType, $filter: FindFilterType!) {\
findPerformers(performer_filter: $performer_filter, filter: $filter) {\
performers {\
id\
name\
aliases\
}\
}\
}"
var variables = {
filter: {
per_page: -1
},
performer_filter: {
name: {
modifier: "INCLUDES"
},
OR: {
aliases: {
modifier: "INCLUDES"
}
}
}
};
var totalMatchingParts = 0;
result.performer_ids = [];
do {
variables.performer_filter.name.value = variables.performer_filter.OR.aliases.value = parts[0].substring(0, 2);
var queryResult = gql.Do(query, variables);
var performers = queryResult.findPerformers.performers;
if (!performers.length) {
parts.shift();
continue;
}
var maxMatchLength = 0;
var matches = [];
for (var i = 0; i < performers.length; i++) {
var performer = performers[i];
var aliases = performer.aliases ? performer.aliases.split(/\s*[,;]+\s*/) : [];
var matchingParts = matchNames(parts, performer.name, aliases);
if (matchingParts === 0) {
continue;
}
if (matchingParts > maxMatchLength) {
maxMatchLength = matchingParts;
matches = [performer.id];
} else if (matchingParts === maxMatchLength) {
matches.push(performer.id);
}
}
if (maxMatchLength === 0) {
break;
}
result.performer_ids = result.performer_ids.concat(matches);
totalMatchingParts += maxMatchLength;
parts = parts.slice(maxMatchLength);
while (parts.length > 0 && (parts[0].toLowerCase() === 'and' || parts[0] === '&')) {
parts.shift();
totalMatchingParts += 1;
}
} while (parts.length > 0);
return totalMatchingParts;
}
function parseFilename(name) {
var parts = name.split(/[. \-_,]+/);
var matchers = [
matchStudio,
matchDate,
matchPerformers,
];
var result = {};
var hasMatched = false;
for (var matchTries = 0; matchTries < 3 && !hasMatched && parts.length; matchTries++) {
for (var i = 0; i < matchers.length && parts.length > 0; i++) {
var matchedParts = matchers[i](parts, result);
if (matchedParts > 0) {
hasMatched = true;
parts = parts.slice(matchedParts);
}
}
// If no matchers worked remove a part. Maybe the format is correct but studio isn't found? etc
if (!hasMatched) {
parts.shift();
}
}
if (hasMatched && parts.length > 0) {
var title = parts.join(' ');
// Look behind assertions are not supported, so can't use `replace(/(?<=.)([A-Z]/g, ' $1')` so instead have to do a loop. Otherwise for example 'FooABar' will become 'Foo ABar' instead of 'Foo A Bar'
while (/[^\s][A-Z]/.test(title)) {
title = title.replace(/([^\s])([A-Z])/g, '$1 $2');
}
result.title = title.trim();
}
return result;
}
main();