This repository was archived by the owner on Dec 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
2173 lines (2077 loc) · 87.2 KB
/
index.js
File metadata and controls
2173 lines (2077 loc) · 87.2 KB
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @import HydrusAPI from './types/types.d.ts'
*/
/**
* Takes an options object and convert it to
* an URLSearchParams instance
* making sure that all lists and objects are
* encoded as JSON strings first
* @param {{[key: string]: any}} options
* @returns {URLSearchParams}
*/
const optionsToURLSearchParams = (options) => {
for (const [key, value] of Object.entries(options)) {
if (Array.isArray(value) || (typeof value === "object" && value !== null)) {
options[key] = JSON.stringify(value)
}
}
return new URLSearchParams(options)
}
module.exports = class API{
// region: API
/** @type {boolean} */
debug
/** @type {string} */
access_key
/** @type {string} */
address
/** What API version do we support */
VERSION = 81
/** What version of Hydrus are we testing against */
HYDRUS_TARGET_VERSION = 643
/**
* These are the permissions that the client can have
* @type {HydrusAPI.BASIC_PERMS}
*/
BASIC_PERM = Object.freeze({
MODIFY_URLS: 0,
MODIFY_FILES: 1,
MODIFY_TAGS: 2,
SEARCH_AND_FETCH_FILES: 3,
MANAGE_PAGES: 4,
MANAGE_COOKIES_AND_HEADERS: 5,
MANAGE_DATABASE: 6,
EDIT_NOTES: 7,
EDIT_FILE_RELATIONSHIPS: 8,
EDIT_FILE_RATINGS: 9,
MANAGE_POPUPS: 10,
EDIT_FILE_TIMES: 11,
COMMIT_PENDING: 12,
SEE_LOCAL_PATHS: 13,
})
/** @type {HydrusAPI.CANVAS_TYPE} */
CANVAS_TYPE = Object.freeze({
/** The normal viewer in hydrus that is its own window */
MEDIA_VIEWER: 0,
/** The box in the bottom-left corner of the Main GUI window */
PREVIEW_VIEWER: 1,
/** Something to represent your own access, if you wish */
API_VIEWER: 4,
})
/** @type {HydrusAPI.TIMESTAMP_TYPE} */
TIMESTAMP_TYPE = Object.freeze({
/** File modified time (web domain) */
MODIFIED_TIME_WEB_DOMAIN: 0,
/** File modified time (on the hard drive) */
MODIFIED_TIME_DISK: 1,
/** File import time */
IMPORTED_TIME: 3,
/** File delete time */
DELETED_TIME: 4,
/** Archived time */
ARCHIVED_TIME: 5,
/** Last viewed */
LAST_VIEWED: 6,
/** File originally imported time */
ORIGINAL_IMPORT_TIME: 7,
})
/** @type {HydrusAPI.SERVICE_TYPE} */
SERVICE_TYPE = Object.freeze({
/** 0 - tag repository */
TAG_REPO: 0,
/** 1 - file repository */
FILE_REPO: 1,
/** 2 - a local file domain like 'my files' */
LOCAL_FILE_DOMAIN: 2,
/** 5 - a local tag domain like 'my tags' */
LOCAL_TAG_DOMAIN: 5,
/** 6 - a 'numerical' rating service with several stars */
RATING_SERVICE_NUMERICAL: 6,
/** 7 - a 'like/dislike' rating service with on/off status */
RATING_SERVICE_BOOLEAN: 7,
/** 10 - all known tags -- a union of all the tag services */
ALL_KNOWN_TAGS: 10,
/** 11 - all known files -- a union of all the file services and files that appear in tag services */
ALL_KNOWN_FILES: 11,
/** 12 - the local booru -- you can ignore this */
LOCAL_BOORU: 12,
/** 13 - IPFS */
IPFS: 13,
/** 14 - trash */
TRASH: 14,
/** 15 - all local files -- all files on hard disk ('all my files' + updates + trash) */
ALL_LOCAL_FILES: 15,
/** 17 - file notes */
FILE_NOTES: 17,
/** 18 - Client API */
CLIENT_API: 18,
/** 19 - deleted from anywhere -- you can ignore this */
DELETED_FROM_ANYWHERE: 19,
/** 20 - local updates -- a file domain to store repository update files in */
LOCAL_UPDATES: 20,
/** 21 - all my files -- union of all local file domains */
ALL_MY_FILES: 21,
/** 22 - a 'inc/dec' rating service with positive integer rating */
RATING_SERVICE_INC_DEC: 22,
/** 99 - server administration */
SERVER_ADMIN: 99,
})
/** @type {HydrusAPI.POTENTIALS_SEARCH_TYPE} */
POTENTIALS_SEARCH_TYPE = Object.freeze({
/** 0 - one file matches search 1 */
A_FILE_MATCHES_SEARCH_ONE: 0,
/** 1 - both files match search 1 */
BOTH_FILES_MATCH_SEARCH_ONE: 1,
/** 2 - one file matches search 1, the other 2 */
EACH_FILE_MATCHES_A_SEPARATE_SEARCH: 2,
})
/** @type {HydrusAPI.PIXEL_DUPLICATES} */
PIXEL_DUPLICATES = Object.freeze({
/** 0 - must be pixel duplicates */
MUST_BE_DUPLICATES: 0,
/** 1 - can be pixel duplicates */
CAN_BE_DUPLICATES: 1,
/** 2 - must not be pixel duplicates */
MUST_NOT_BE_DUPLICATES: 2,
})
/**
* @type {number|undefined}
* Setting this to a HydrusAPI version will allow usage
* when the HydrusAPI API version and Hydrus API version
* mismatches.
*
* !!! This use case will not be supported
*/
api_version_override
/**
* We highly suggest wrapping this class' methods
* in functions over using them directly.
* @param {HydrusAPI.APIOptions} [options={}] Extra options
*/
constructor(options={}) {
this.access_key = options?.access_key ?? ''
this.address = options?.address ?? 'http://127.0.0.1:45869'
this.debug = options?.debug ?? false
this.api_version_override = options?.api_version_override
this._first_successful_versioning = false
}
/**
* Constructs an API call and then calls it,
* returning the result
* @param {HydrusAPI.CallOptions} o
* @returns {Promise<Object|boolean|number|ReadableStream>}
* Object if `o.return_as` is `raw` or `json`;
* boolean if `o.return_as` is `success`;
* number if `o.return_as` is `status`;
* ReadableStream if `o.return_as` is `readable_stream`;
*/
async call(o) {
// region: call
if (this.debug) {
console.log(`ENDPOINT: ${o.endpoint}`)
}
if (o.endpoint !== '/api_version' && !this._first_successful_versioning) {
await this.api_version()
}
o.headers = o.headers ?? new Headers()
o.method = o.method ?? (
o.queries ? 'GET' : (
o.json ? 'POST' : (o.body ? 'POST' : 'GET')
)
)
o.return_as = o.return_as ?? 'json'
if (o.json && !o.headers.has('content-type')) {
o.headers.set('content-type', 'application/json')
}
if (o.queries) {
o.endpoint = o.endpoint + '?' + o.queries.toString()
}
if (this?.access_key !== '') {
if (!o.headers.has('hydrus-client-api-access-key')) {
o.headers.set(
'hydrus-client-api-access-key',
this.access_key
)
}
}
/** @type {{[key: string]: any}} */
const fetch_options = {}
for (const key of Object.keys(o)) {
if (!['endpoint', 'return_as', 'json'].includes(key)) {
fetch_options[key] = o[key]
}
}
fetch_options.body = fetch_options.body ?? (
o.json ? JSON.stringify(o.json) : undefined
)
if (this.debug) {
console.log(fetch_options)
}
return await fetch(
this.address + o.endpoint,
fetch_options
)
.then(async(response) => {
if (this.debug) {
console.log(response)
}
const throw_not_okay = async() => {
if (!response.ok) {
const message = `endpoint '${o.endpoint}' responded with status code '${response.status}' and text '${response.statusText}'. Previous messages may review more details.`
if (!this.debug) {
console.error(response)
}
try {
console.error(await response.json())
} catch {
// do nothing
}
throw new Error(message)
}
}
switch (o.return_as) {
case 'raw':
return response
case 'json':
await throw_not_okay()
try {
return await response.json()
} catch (e) {
if (e instanceof SyntaxError) {
throw new Error(`Invalid json returned from Hydrus! Does this endpoint return json?`)
}
}
break;
case 'status':
return response.status
case 'success':
return response.ok
case 'readable_stream':
await throw_not_okay()
if (!(response?.body instanceof ReadableStream)) {
throw new Error(`response.body doesn't exist or isn't a readable stream`)
}
return response.body
default:
throw new Error(`unhandled return_as value of '${o.return_as}'`)
}
})
}
/**
* Gets the current API version.
*
* GET Endpoint: /api_version
*
* https://github.com/hydrusnetwork/hydrus/blob/master/docs/developer_api.md#get-api_version--idapi_version-
* @param {HydrusAPI.CallOptions['return_as']} [return_as] Optional; Sane default; How do you want the result returned?
* @returns {Promise<HydrusAPI.api_version_response>}
*/
async api_version(return_as) {
// region: api_version
if (
this._first_successful_versioning ||
(return_as && return_as !== 'json')
) {
return /** @type {Promise<HydrusAPI.api_version_response>} */ (await this.call({
endpoint: '/api_version',
return_as: return_as
}))
} else {
const json = /** @type {HydrusAPI.api_version_response} */ (await this.call({
endpoint: '/api_version',
return_as: return_as
}))
if (json.hydrus_version !== this.HYDRUS_TARGET_VERSION) {
console.warn(`This version of HydrusAPI is targetting Hydrus version '${this.HYDRUS_TARGET_VERSION}', but you are currently connected to version '${json.hydrus_version}'.`)
}
if (json.version !== this.VERSION) {
if (!this.api_version_override || this.api_version_override !== json.version) {
throw new Error(
`This version of HydrusAPI is made for Hydrus API version '${this.VERSION}', but you attempted to connect to version '${json.version}'. This is not officially supported. We suggest finding a version of HydrusAPI that matches your Hydrus API version. If you still want to continue then pass 'api_version_override: ${json.version}' in the api options when initializing. NO support will be provided for this use case.`
)
} else {
console.warn(`'api_version_override' is set to '${this.api_version_override}'. No support will be provided for this use case.`)
}
}
this._first_successful_versioning = true
return json
}
}
/**
* Register a new external program with Hydrus.
* This requires the 'add from api request' mini-dialog
* under services->review services to be open,
* otherwise it will 403.
*
* GET Endpoint: /request_new_permissions
*
* https://github.com/hydrusnetwork/hydrus/blob/master/docs/developer_api.md#get-request_new_permissions--idrequest_new_permissions-
* @param {HydrusAPI.request_new_permissions_options} options
* @param {HydrusAPI.CallOptions['return_as']} [return_as] Optional; Sane default; How do you want the result returned?
* @returns {Promise<HydrusAPI.request_new_permissions_response>}
*/
async request_new_permissions(options, return_as) {
// region: request_new_permissions
if (options?.permissions === 'all') {
options.permissions = Object.values(this.BASIC_PERM)
}
return /** @type {Promise<HydrusAPI.request_new_permissions_response>} */ (await this.call({
endpoint: '/request_new_permissions',
queries: optionsToURLSearchParams(options),
return_as: return_as
}))
}
/**
* Gets a session key for the client.
*
* A session key expires after 24 hours of inactivity,
* whenever Hydrus restarts,
* or if the underlying access key is deleted.
* A request on an expired session key returns 419.
*
* GET Endpoint: /session_key
*
* https://github.com/hydrusnetwork/hydrus/blob/master/docs/developer_api.md#get-session_key--idsession_key-
* @param {HydrusAPI.CallOptions['return_as']} [return_as] Optional; Sane default; How do you want the result returned?
* @returns {Promise<HydrusAPI.session_key_response>}
*/
async session_key(return_as) {
// region: session_key
return /** @type {Promise<HydrusAPI.session_key_response>} */ (await this.call({
endpoint: '/session_key',
return_as: return_as
}))
}
/**
* Returns the permissions that the client has
* with its current access key.
*
* GET Endpoint: /verify_access_key
*
* https://github.com/hydrusnetwork/hydrus/blob/master/docs/developer_api.md#get-verify_access_key--idverify_access_key-
* @param {HydrusAPI.CallOptions['return_as']} [return_as] Optional; Sane default; How do you want the result returned?
* @returns {Promise<HydrusAPI.verify_access_key_response>}
*/
async verify_access_key(return_as) {
// region: verify_access_key
return /** @type {Promise<HydrusAPI.verify_access_key_response>} */ (await this.call({
endpoint: '/verify_access_key',
return_as: return_as
}))
}
/**
* Returns info for a given service.
* If the service does not exist, this gives 404.
* It is very unlikely but edge-case possible that
* two services will have the same name,
* in this case you'll get the pseudorandom first.
*
* GET Endpoint: /get_service
*
* https://github.com/hydrusnetwork/hydrus/blob/master/docs/developer_api.md#get-get_service--idget_service-
* @param {{service_name?: string, service_key?: string}} service either service_name or service_key must be provided
* @param {HydrusAPI.CallOptions['return_as']} [return_as] Optional; Sane default; How do you want the result returned?
* @returns {Promise<HydrusAPI.get_service_response>}
*/
async get_service(service, return_as) {
// region: get_service
return /** @type {Promise<HydrusAPI.get_service_response>} */ (await this.call({
endpoint: '/get_service',
queries: optionsToURLSearchParams(service),
return_as: return_as
}))
}
/**
* Returns info for all services
*
* GET Endpoint: /get_services
*
* https://github.com/hydrusnetwork/hydrus/blob/master/docs/developer_api.md#get-get_services--idget_services-
* @param {HydrusAPI.CallOptions['return_as']} [return_as] Optional; Sane default; How do you want the result returned?
* @returns {Promise<HydrusAPI.get_services_response>}
*/
async get_services(return_as) {
// region: get_services
return /** @type {Promise<HydrusAPI.get_services_response>} */ (await this.call({
endpoint: '/get_services',
return_as: return_as
}))
}
/**
* Features:
* * Import files
* * Delete files
* * Undelete files
* * Clear deletion records for deleted files
* * Copy files between file domains
* * Archive files
* * Unarchive files
* * Generate hashes for a file
*
* https://github.com/hydrusnetwork/hydrus/blob/master/docs/developer_api.md#importing-and-deleting-files
*/
get add_files() {
return {
/**
* Tell Hydrus to import a file.
* supply a json with either bytes : *file bytes*
* or path: *file path*
*
* POST Endpoint: /add_files/add_file
*
* https://github.com/hydrusnetwork/hydrus/blob/master/docs/developer_api.md#post-add_filesadd_file--idadd_files_add_file-
* @param {{bytes: *}|{path: string, delete_after_successful_import?: boolean}} options path to the file or the file data
* @param {HydrusAPI.CallOptions['return_as']} [return_as] Optional; Sane default; How do you want the result returned?
* @returns {Promise<HydrusAPI.add_file_response>}
*/
add_file: async(options, return_as) => {
// region: add_files/add_file
/** @type {HydrusAPI.CallOptions} */
const o = {
endpoint: '/add_files/add_file',
return_as: return_as
}
if ('path' in options) {
o.json = options
} else if ('bytes' in options) {
o.headers = new Headers({'content-type': 'application/octet-stream'})
o.body = options.bytes
} else {
throw new Error('path or bytes must be defined in options')
}
return /** @type {Promise<HydrusAPI.add_file_response>} */ (await this.call(o))
},
/**
* Tell Hydrus to delete one or more files
*
* POST Endpoint: /add_files/delete_files
*
* https://github.com/hydrusnetwork/hydrus/blob/master/docs/developer_api.md#post-add_filesdelete_files--idadd_files_delete_files-
* @param {HydrusAPI.delete_files_options} options
* @param {HydrusAPI.CallOptions['return_as']} [return_as] Optional; Sane default; How do you want the result returned?
* @returns {Promise<boolean>} true if successful
*/
delete_files: async(options, return_as) => {
// region: add_files/delete_files
return /** @type {Promise<boolean>} */ (await this.call({
endpoint: '/add_files/delete_files',
json: options,
return_as: return_as ?? 'success'
}))
},
/**
* Tell Hydrus to undelete one or more files
*
* POST Endpoint: /add_files/undelete_files
*
* https://github.com/hydrusnetwork/hydrus/blob/master/docs/developer_api.md#post-add_filesundelete_files--idadd_files_undelete_files-
* @param {HydrusAPI.undelete_files_options} options
* @param {HydrusAPI.CallOptions['return_as']} [return_as] Optional; Sane default; How do you want the result returned?
* @returns {Promise<boolean>} successful if true
*/
undelete_files: async (options, return_as) => {
// region: add_files/undelete_files
return /** @type {Promise<boolean>} */ (await this.call({
endpoint: '/add_files/undelete_files',
json: options,
return_as: return_as ?? 'success'
}))
},
/**
* Tell Hydrus to clear any deletion records it has for a file
*
* POST Endpoint: /add_files/clear_file_deletion_record
*
* https://github.com/hydrusnetwork/hydrus/blob/master/docs/developer_api.md#post-add_filesclear_file_deletion_record--idadd_files_clear_file_deletion_record-
* @param {HydrusAPI.FilesObject} options
* @param {HydrusAPI.CallOptions['return_as']} [return_as] Optional; Sane default; How do you want the result returned?
* @returns {Promise<boolean>} successful if true
*/
clear_file_deletion_record: async(options, return_as) => {
// region: add_files/clear_file_deletion_record
return /** @type {Promise<boolean>} */ (await this.call({
endpoint: '/add_files/clear_file_deletion_record',
json: options,
return_as: return_as ?? 'success'
}))
},
/**
* Add a file to one or more local file services
*
* POST Endpoint: /add_files/migrate_files
*
* https://github.com/hydrusnetwork/hydrus/blob/master/docs/developer_api.md#post-add_filesmigrate_files--idadd_files_migrate_files-
* @param {HydrusAPI.migrate_files_options} options
* @param {HydrusAPI.CallOptions['return_as']} [return_as] Optional; Sane default; How do you want the result returned?
* @returns {Promise<boolean>} successful if true
*/
migrate_files: async(options, return_as) => {
// region: add_files/migrate_files
return /** @type {Promise<boolean>} */ (await this.call({
endpoint: '/add_files/migrate_files',
json: options,
return_as: return_as ?? 'success'
}))
},
/**
* Tell Hydrus to archive inboxed files
*
* POST Endpoint: /add_files/archive_files
*
* https://github.com/hydrusnetwork/hydrus/blob/master/docs/developer_api.md#post-add_filesarchive_files--idadd_files_archive_files-
* @param {HydrusAPI.FilesObject} options
* @param {HydrusAPI.CallOptions['return_as']} [return_as] Optional; Sane default; How do you want the result returned?
* @returns {Promise<boolean>} if true then the file was successfully archived, was already archived, or doesn't exist
*/
archive_files: async(options, return_as) => {
// region: add_files/archive_files
return /** @type {Promise<boolean>} */ (await this.call({
endpoint: '/add_files/archive_files',
json: options,
return_as: return_as ?? 'success'
}))
},
/**
* Tell Hydrus to unarchive files which moves them to the inbox
*
* POST Endpoint: /add_files/unarchive_files
*
* https://github.com/hydrusnetwork/hydrus/blob/master/docs/developer_api.md#post-add_filesunarchive_files--idadd_files_unarchive_files-
* @param {HydrusAPI.FilesObject} options
* @param {HydrusAPI.CallOptions['return_as']} [return_as] Optional; Sane default; How do you want the result returned?
* @returns {Promise<boolean>} if true then the file was successfully unarchived, was already unarchived, or doesn't exist
*/
unarchive_files: async(options, return_as) => {
// region: add_files/unarchive_files
return /** @type {Promise<boolean>} */ (await this.call({
endpoint: '/add_files/unarchive_files',
json: options,
return_as: return_as ?? 'success'
}))
},
/**
* Asks Hydrus to attempt to generate hashes for the given file.
* supply a json with either bytes : *file bytes*
* or path: *file path*
*
* POST Endpoint: /add_files/generate_hashes
*
* https://github.com/hydrusnetwork/hydrus/blob/master/docs/developer_api.md#post-add_filesgenerate_hashes--idadd_files_generate_hashes-
* @param {HydrusAPI.generate_hashes_options} options path to the file or the file data
* @param {HydrusAPI.CallOptions['return_as']} [return_as] Optional; Sane default; How do you want the result returned?
* @returns {Promise<HydrusAPI.generate_hashes_response>}
*/
generate_hashes: async(options, return_as) => {
// region: add_files/generate_hashes
/** @type {HydrusAPI.CallOptions} */
const o = {
endpoint: '/add_files/generate_hashes',
return_as: return_as
}
if ('path' in options) {
o.json = options
} else if ('bytes' in options) {
o.headers = new Headers({'content-type': 'application/octet-stream'})
o.body = options.bytes
} else {
throw new Error('path or bytes must be defined in options')
}
return /** @type {Promise<HydrusAPI.generate_hashes_response>} */ (await this.call(o))
},
}
}
/**
* Features:
* * Get files associated with a url
* * Associate and dissociate files with a url
* * Get info about a url
* * Import a url
*
* https://github.com/hydrusnetwork/hydrus/blob/master/docs/developer_api.md#importing-and-editing-urls
*/
get add_urls() {
return {
/**
* Ask Hydrus about a URL's files.
*
* GET Endpoint: /add_urls/get_url_files
*
* https://github.com/hydrusnetwork/hydrus/blob/master/docs/developer_api.md#get-add_urlsget_url_files--idadd_urls_get_url_files-
* @param {HydrusAPI.get_url_files_options} options
* @param {HydrusAPI.CallOptions['return_as']} [return_as] Optional; Sane default; How do you want the result returned?
* @returns {Promise<HydrusAPI.get_url_files_response>}
*/
get_url_files: async(options, return_as) => {
// region: add_urls/get_url_files
const q = new URLSearchParams()
if (options.doublecheck_file_system) {
q.append('doublecheck_file_system', `${options.doublecheck_file_system}`)
}
q.append('url', options.url)
return /** @type {Promise<HydrusAPI.get_url_files_response>} */ (await this.call({
endpoint: '/add_urls/get_url_files',
queries: q,
return_as: return_as
}))
},
/**
* Ask Hydrus for information about a URL.
*
* GET Endpoint: /add_urls/get_url_info
*
* https://github.com/hydrusnetwork/hydrus/blob/master/docs/developer_api.md#get-add_urlsget_url_info--idadd_urls_get_url_info-
* @param {string} url url you want to check
* @param {HydrusAPI.CallOptions['return_as']} [return_as] Optional; Sane default; How do you want the result returned?
* @returns {Promise<HydrusAPI.get_url_info_response>}
*/
get_url_info: async(url, return_as) => {
// region: add_urls/get_url_info
return /** @type {Promise<HydrusAPI.get_url_info_response>} */ (await this.call({
endpoint: '/add_urls/get_url_info',
queries: optionsToURLSearchParams({url: url}),
return_as: return_as
}))
},
/**
* Tell Hydrus to 'import' a URL.
* This triggers the exact same routine as drag-and-dropping
* a text URL onto the main Hydrus window.
*
* POST Endpoint: /add_urls/add_url
*
* https://github.com/hydrusnetwork/hydrus/blob/master/docs/developer_api.md#post-add_urlsadd_url--idadd_urls_add_url-
* @param {HydrusAPI.add_url_options} options
* @param {HydrusAPI.CallOptions['return_as']} [return_as] Optional; Sane default; How do you want the result returned?
* @returns {Promise<HydrusAPI.add_url_response>}
*/
add_url: async(options, return_as) => {
// region: add_urls/add_url
return /** @type {Promise<HydrusAPI.add_url_response>} */ (await this.call({
endpoint: '/add_urls/add_url',
json: options,
return_as: return_as,
}))
},
/**
* Manage which URLs Hydrus considers to be
* associated with which files.
*
* POST Endpoint: /add_urls/associate_url
*
* https://github.com/hydrusnetwork/hydrus/blob/master/docs/developer_api.md#post-add_urlsassociate_url--idadd_urls_associate_url-
* @param {HydrusAPI.associate_url_options} options
* @param {HydrusAPI.CallOptions['return_as']} [return_as] Optional; Sane default; How do you want the result returned?
* @returns {Promise<boolean>} successful if true
*/
associate_url: async(options, return_as) => {
// region: add_urls/associate_url
return /** @type {Promise<boolean>} */ (await this.call({
endpoint: '/add_urls/associate_url',
json: options,
return_as: return_as ?? 'success'
}))
}
}
}
/**
* Features:
* * Get clean version of a list of tags
* * Get, set, or modify favourite tags
* * Get a tags sibling and parent relationships
* * Search Hydrus for a tag
* * Add or remove tags from files
*
* https://github.com/hydrusnetwork/hydrus/blob/master/docs/developer_api.md#editing-file-tags
*/
get add_tags() {
return {
/**
* Ask Hydrus about how it will see certain tags.
*
* GET Endpoint: /add_tags/clean_tags
*
* https://github.com/hydrusnetwork/hydrus/blob/master/docs/developer_api.md#get-add_tagsclean_tags--idadd_tags_clean_tags-
* @param {string[]} tags
* @param {HydrusAPI.CallOptions['return_as']} [return_as] Optional; Sane default; How do you want the result returned?
* @returns {Promise<HydrusAPI.clean_tags_response>}
*/
clean_tags: async(tags, return_as) => {
// region: add_tags/clean_tags
return /** @type {Promise<HydrusAPI.clean_tags_response>} */ (await this.call({
endpoint: '/add_tags/clean_tags',
queries: optionsToURLSearchParams({tags: tags}),
return_as: return_as
}))
},
/**
* Fetch Hydrus' favourite tags.
* This is the list of tags you see beneath an
* autocomplete input, under the 'favourites' tab.
* This is not the per-service 'most used' tab you see
* in manage tags.
*
* GET Endpoint: /add_tags/get_favourite_tags
*
* https://github.com/hydrusnetwork/hydrus/blob/master/docs/developer_api.md#get-add_tagsget_favourite_tags--idadd_tags_get_favourite_tags-
* @param {HydrusAPI.CallOptions['return_as']} [return_as] Optional; Sane default; How do you want the result returned?
* @returns {Promise<HydrusAPI.get_favourite_tags_response>}
*/
get_favourite_tags: async(return_as) => {
// region add_tags/get_favourite_tags
return /** @type {Promise<HydrusAPI.get_favourite_tags_response>} */ (await this.call({
endpoint: '/add_tags/get_favourite_tags',
return_as: return_as
}))
},
/**
* Ask Hydrus about a tags' sibling and parent relationships.
*
* GET Endpoint: /add_tags/get_siblings_and_parents
*
* https://github.com/hydrusnetwork/hydrus/blob/master/docs/developer_api.md#get-add_tagsget_siblings_and_parents--idadd_tags_get_siblings_and_parents-
* @param {string[]} tags
* @param {HydrusAPI.CallOptions['return_as']} [return_as] Optional; Sane default; How do you want the result returned?
* @returns {Promise<HydrusAPI.get_siblings_and_parents_response>}
*/
get_siblings_and_parents: async(tags, return_as) => {
// region add_tags/get_siblings_and_parents
return /** @type {Promise<HydrusAPI.get_siblings_and_parents_response>} */ (await this.call({
endpoint: '/add_tags/get_siblings_and_parents',
queries: optionsToURLSearchParams({tags: tags}),
return_as: return_as
}))
},
/**
* Search Hydrus for tags.
*
* GET Endpoint: /add_tags/search_tags
*
* https://github.com/hydrusnetwork/hydrus/blob/master/docs/developer_api.md#get-add_tagssearch_tags--idadd_tags_search_tags-
* @param {HydrusAPI.search_tags_options} options
* @param {HydrusAPI.CallOptions['return_as']} [return_as] Optional; Sane default; How do you want the result returned?
* @returns {Promise<HydrusAPI.search_tags_response>}
*/
search_tags: async(options, return_as) => {
// region add_tags/search_tags
return /** @type {Promise<HydrusAPI.search_tags_response>} */ (await this.call({
endpoint: '/add_tags/search_tags',
queries: optionsToURLSearchParams(options),
return_as: return_as
}))
},
/**
* Make changes to the tags that files have.
*
* POST Endpoint: /add_tags/add_tags
*
* https://github.com/hydrusnetwork/hydrus/blob/master/docs/developer_api.md#post-add_tagsadd_tags--idadd_tags_add_tags-
* @param {HydrusAPI.add_tags_options} options
* @param {HydrusAPI.CallOptions['return_as']} [return_as] Optional; Sane default; How do you want the result returned?
* @returns {Promise<boolean>} successful if true
*/
add_tags: async(options, return_as) => {
// region: add_tags/add_tags
return /** @type {Promise<boolean>} */ (await this.call({
endpoint: '/add_tags/add_tags',
json: options,
return_as: return_as ?? 'success'
}))
},
/**
* Edit Hydrus' favourite tags.
* This is the complement to /add_tags/get_favourite_tags.
*
* POST Endpoint: set_favourite_tags
*
* https://github.com/hydrusnetwork/hydrus/blob/master/docs/developer_api.md#post-add_tagsset_favourite_tags--idadd_tags_set_favourite_tags-
* @param {HydrusAPI.set_favourite_tags_options} options
* @param {HydrusAPI.CallOptions['return_as']} [return_as] Optional; Sane default; How do you want the result returned?
* @returns {Promise<HydrusAPI.get_favourite_tags_response>}
*/
set_favourite_tags: async(options, return_as) => {
// region: add_tags/set_favourite_tags
return /** @type {Promise<HydrusAPI.get_favourite_tags_response>} */ (await this.call({
endpoint: '/add_tags/set_favourite_tags',
json: options,
return_as: return_as
}))
},
}
}
/**
* Add or remove ratings associated with a file.
*
* https://github.com/hydrusnetwork/hydrus/blob/master/docs/developer_api.md#editing-file-ratings
*/
get edit_ratings() {
return {
/**
* Add or remove ratings associated with a file.
*
* POST Endpoint: /edit_ratings/set_rating
*
* https://github.com/hydrusnetwork/hydrus/blob/master/docs/developer_api.md#post-edit_ratingsset_rating--idedit_ratings_set_rating-
* @param {HydrusAPI.set_rating_options} options
* @param {HydrusAPI.CallOptions['return_as']} [return_as] Optional; Sane default; How do you want the result returned?
* @returns {Promise<boolean>} successful if true
*/
set_rating: async(options, return_as) => {
// region: edit_ratings/set_rating
return /** @type {Promise<boolean>} */ (await this.call({
endpoint: '/edit_ratings/set_rating',
json: options,
return_as: return_as ?? 'success'
}))
},
}
}
/**
* Set or modify viewtime
*
* Set modified at, imported at,
* deleted at, archived at, last viewed,
* and original import times
*
* https://github.com/hydrusnetwork/hydrus/blob/master/docs/developer_api.md#editing-file-times
*/
get edit_times() {
return {
/**
* Add a file view to the file viewing statistics system.
*
* This increments the number of views stored for
* the file in the file viewing statistics system.
* This system records "last time the file was viewed",
* "total number of views", and "total viewtime"
* for three different `canvas_types`
*
* It doesn't matter much, but in hydrus
* the "last time the file was viewed" is considered
* to be when the user started viewing the file, not ended,
* so if you wish to track that too, you can send it along.
* If you do not include a timestamp, the system will use now,
* which is close enough, assuming you are sending recent
* rather than deferred data.
*
* You can send multiple file identifiers,
* but I imagine you will just be sending one most of the time.
*
* If the user has disabled file viewing statistics tracking
* on their client (under the options), this will 403.
*
* POST Endpoint: /edit_times/increment_file_viewtime
*
* https://github.com/hydrusnetwork/hydrus/blob/master/docs/developer_api.md#post-edit_timesincrement_file_viewtime--idedit_times_increment_file_viewtime-
* @param {HydrusAPI.increment_and_set_file_viewtime_options} options
* @param {HydrusAPI.CallOptions['return_as']} [return_as] Optional; Sane default; How do you want the result returned?
* @returns {Promise<boolean>} Successful if true
*/
increment_file_viewtime: async(options, return_as) => {
// region: edit_times/increment_file_viewtime
return /** @type {Promise<boolean>} */ (await this.call({
endpoint: '/edit_times/increment_file_viewtime',
json: options,
return_as: return_as ?? 'success'
}))
},
/**
* Set fixed values in the file viewing statistics system.
*
* This is an override to set the number of views stored
* for the file in the file viewing statistics system
* to fixed values you specify.
*
* I recommend you only use this call for unusual maintenance,
* migration, or reset situations--stick to the
* `edit_times.increment_file_viewtime()` call for normal use.
*
* The system records "last time the file was viewed",
* "total number of views",
* and "total viewtime" for three different `canvas_types`
*
* The "Client API" viewer was added so you may record
* your views separately if you wish.
* Otherwise you might like to fold them into the normal
* Media viewer count.
*
* If you do not include a timestamp,
* the system will either leave what is currently recorded,
* or, if the file has no viewing data yet, fill in with now.
*
* You can send multiple file identifiers,
* but I imagine you will just be sending one.
*
* If the user has disabled file viewing statistics tracking
* on their client (under the options), this will 403.
*
* POST Endpoint: /edit_times/set_file_viewtime
*
* https://github.com/hydrusnetwork/hydrus/blob/master/docs/developer_api.md#post-edit_timesset_file_viewtime--idedit_times_set_file_viewtime-
* @param {HydrusAPI.increment_and_set_file_viewtime_options} options
* @param {HydrusAPI.CallOptions['return_as']} [return_as] Optional; Sane default; How do you want the result returned?
* @returns {Promise<boolean>} Successful if true
*/
set_file_viewtime: async(options, return_as) => {
// region: edit_times/set_file_viewtime
return /** @type {Promise<boolean>} */ (await this.call({
endpoint: '/edit_times/set_file_viewtime',
json: options,
return_as: return_as ?? 'success'
}))
},
/**
* Add or remove timestamps associated with a file.
*
* This is a copy of the manage times dialog in the program,
* so if you are uncertain about something, check that out.
* The client records timestamps up to millisecond accuracy.
*
* You have to select some files, obviously.
* I'd imagine most uses will be over one file at a time,
* but you can spam 100 or 10,000 if you need to.
*
* Then choose whether you want to work with
* timestamp or timestamp_ms.
* timestamp can be an integer or a float,
* and in the latter case, the API will suck up the three
* most significant digits to be the millisecond data.
* timestamp_ms is an integer of milliseconds,
* simply the timestamp value multiplied by 1,000.
* It doesn't matter which you use--whichever is easiest for you.
*
* If you send null timestamp time,
* then this will instruct to delete the existing value,
* if possible and reasonable.