-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbooru.service.ts
More file actions
161 lines (132 loc) · 4.66 KB
/
booru.service.ts
File metadata and controls
161 lines (132 loc) · 4.66 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
import { BadRequestException, Injectable, ServiceUnavailableException } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import {
BooruTypes,
BooruTypesStringEnum,
Danbooru,
Danbooru2,
E621Net,
Gelbooru,
GelbooruCom,
IBooruEndpoints,
IBooruOptions,
IBooruQueryIdentifiers,
Moebooru,
RealBooruCom,
Rule34PahealNet,
Rule34Xxx
} from '@alejandroakbal/universal-booru-wrapper'
import * as UBW from '@alejandroakbal/universal-booru-wrapper'
import { booruQueriesDTO } from './dto/booru-queries.dto'
import { BooruEndpointParamsDTO, SupportedBooruType } from './dto/request-booru.dto'
import { BooruAuthManagerService } from './services/booru-auth-manager.service'
@Injectable()
export class BooruService {
constructor(
private readonly configService: ConfigService,
private readonly authManager: BooruAuthManagerService
) {}
public buildApiClass(params: BooruEndpointParamsDTO, queries: booruQueriesDTO): BooruTypes {
const booruClass = this.getApiClassByType(params.booruType)
const endpoints: IBooruEndpoints = {
base: queries.baseEndpoint,
posts: queries.postsEndpoint,
randomPosts: queries.randomPostsEndpoint,
singlePost: queries.singlePostEndpoint,
tags: queries.tagsEndpoint
}
const defaultQueryIdentifiers: IBooruQueryIdentifiers = {
posts: {
limit: queries.defaultQueryIdentifiersPostsLimit,
pageID: queries.defaultQueryIdentifiersPostsPageID,
tags: queries.defaultQueryIdentifiersPostsTags,
rating: queries.defaultQueryIdentifiersPostsRating,
score: queries.defaultQueryIdentifiersPostsScore,
order: queries.defaultQueryIdentifiersPostsOrder
},
randomPosts: {
limit: queries.defaultQueryIdentifiersRandomPostsLimit,
pageID: queries.defaultQueryIdentifiersRandomPostsPageID,
tags: queries.defaultQueryIdentifiersRandomPostsTags,
rating: queries.defaultQueryIdentifiersRandomPostsRating,
score: queries.defaultQueryIdentifiersRandomPostsScore,
order: queries.defaultQueryIdentifiersRandomPostsOrder
},
singlePost: {
id: queries.defaultQueryIdentifiersSinglePostID
},
tags: {
tag: queries.defaultQueryIdentifiersTagsTag,
tagEnding: queries.defaultQueryIdentifiersTagsTagEnding,
limit: queries.defaultQueryIdentifiersTagsLimit,
pageID: queries.defaultQueryIdentifiersTagsPageID,
order: queries.defaultQueryIdentifiersTagsOrder
}
}
// No default QueryValues are needed
// Resolve authentication credentials
const authCredentials = this.resolveAuthCredentials(queries)
const options: IBooruOptions = {
HTTPScheme: queries.httpScheme,
...authCredentials
}
const Api = new booruClass(endpoints, defaultQueryIdentifiers, undefined, options)
return Api
}
private resolveAuthCredentials(queries: booruQueriesDTO): { auth?: { username: string; apiKey: string } } {
// Priority 1: Query parameters
if (queries.auth_user && queries.auth_pass) {
return {
auth: {
username: queries.auth_user,
apiKey: queries.auth_pass
}
}
}
// Priority 2: Environment variables through auth manager
const envCredentials = this.authManager.getAvailableCredential(queries.baseEndpoint)
if (envCredentials) {
return {
auth: {
username: envCredentials.user,
apiKey: envCredentials.password
}
}
}
// Priority 3: No authentication
return {}
}
private getApiClassByType(booruType: SupportedBooruType) {
switch (booruType) {
case BooruTypesStringEnum.DANBOORU:
return Danbooru
case BooruTypesStringEnum.DANBOORU2:
return Danbooru2
case BooruTypesStringEnum.MOEBOORU:
return Moebooru
case BooruTypesStringEnum.GELBOORU:
return Gelbooru
case BooruTypesStringEnum.RULE_34_XXX:
return Rule34Xxx
case BooruTypesStringEnum.RULE34_PAHEAL_NET:
return Rule34PahealNet
case BooruTypesStringEnum.GELBOORU_COM:
return GelbooruCom
case BooruTypesStringEnum.E621_NET:
return E621Net
case BooruTypesStringEnum.REALBOORU_COM:
return RealBooruCom
case 'kemono': {
const maybeKemonoClass = (UBW as any).Kemono
if (!maybeKemonoClass) {
throw new ServiceUnavailableException(
'Kemono booru type requires @alejandroakbal/universal-booru-wrapper version that exports Kemono'
)
}
return maybeKemonoClass
}
default:
throw new BadRequestException(`Unsupported booru type: ${booruType}`)
}
}
}