forked from WordPress/openverse
/
get-additional-sources.ts
297 lines (286 loc) · 7.16 KB
/
get-additional-sources.ts
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
import type { MediaType } from "~/constants/media"
import type { PaginatedSearchQuery } from "~/types/search"
import { MODEL_3D } from "~/constants/media"
/**
* Describes the query format used by the URL builder functions of additional
* sources. This includes the query string and the usage filters.
*/
interface AdditionalSearchQuery {
q: string
}
/**
* Convert the ApiQueryParams object from the store to a format used by all the
* URL builder functions of all additional sources.
*
* @param query - the ApiQueryParams object
* @returns the query and filters in the format used by the URL builders
*/
const transformSearchQuery = (
query: PaginatedSearchQuery
): AdditionalSearchQuery => ({
q: query.q ?? "",
})
type SearchFunctions = {
[k in MediaType]?: (search: AdditionalSearchQuery) => {
queryParams: Record<string, string | string[]>
url: string
}
}
/**
* Describes an additional source builder, which contains a display name,
* whether the source supports use filters and a mapping of media type to a URL
* builder function that leads to the search results for that media type.
*/
interface AdditionalSourceBuilder extends SearchFunctions {
name: string
}
/**
* Describes an additional source, consisting of the source name and the
* pre-populated URL.
*/
export interface AdditionalSource {
name: string
url: string
}
/**
* Maps each additional source with search URL builder functions for each
* content type.
*
* @see {@link https://github.com/creativecommons/cccatalog-frontend/issues/315}
*/
const additionalSourceBuilders: AdditionalSourceBuilder[] = [
{
name: "Centre For Ageing Better",
image: (search) => ({
url: "https://ageingbetter.resourcespace.com/pages/search.php",
queryParams: {
search: search.q,
},
}),
},
{
name: "EDU images",
image: (search) => ({
url: "https://images.all4ed.org",
queryParams: {
s: search.q,
},
}),
},
{
name: "Google Images",
image: (search) => ({
url: "https://www.google.com/search",
queryParams: {
q: search.q,
tbm: "isch", // this means 'search images'
tbs: "il:cl",
},
}),
},
{
name: "Images of Empowerment",
image: (search) => ({
url: "https://www.imagesofempowerment.org/",
queryParams: {
s: search.q,
},
}),
},
{
name: "Open Clip Art Library",
image: (search) => ({
url: "http://www.openclipart.org/search/",
queryParams: {
query: search.q,
},
}),
},
{
name: "Nappy",
image: (search) => ({
url: "https://www.nappy.co/",
queryParams: {
s: search.q,
},
}),
},
{
name: "The Greats",
image: (search) => ({
url: "https://www.thegreats.co/artworks/",
queryParams: {
theme: "0",
search: search.q,
},
}),
},
{
name: "ccMixter",
audio: (search) => ({
// no https :(
url: "http://dig.ccmixter.org/search",
queryParams: {
lic: "open",
searchp: search.q,
},
}),
},
{
name: "SoundCloud",
audio: (search) => {
return {
url: "https://soundcloud.com/search/sounds",
queryParams: {
q: search.q,
"filter.license": "to_share", // @todo: choose which type from the search object
},
}
},
},
{
name: "Europeana",
audio: (search) => {
return {
url: "https://www.europeana.eu/en/search",
queryParams: {
page: "1",
qf: 'TYPE:"SOUND"',
// search cc licensed works
query: `${search.q} AND RIGHTS:*creative*`,
},
}
},
video(search) {
return {
url: "https://www.europeana.eu/en/search",
queryParams: {
page: "1",
qf: 'TYPE:"VIDEO"',
// search cc licensed works
query: `${search.q} AND RIGHTS:*creative*`,
},
}
},
},
{
name: "Vimeo",
video: (search) => ({
url: "https://vimeo.com/search",
queryParams: {
license: "by",
q: search.q,
},
}),
},
{
name: "Wikimedia Commons",
video: (search) => ({
url: "https://commons.wikimedia.org/w/index.php",
queryParams: {
search: `${search.q}`,
title: "Special:MediaSearch",
type: "video",
},
}),
},
{
name: "YouTube",
video: (search) => ({
url: "https://www.youtube.com/results",
queryParams: {
search_query: search.q,
sp: "EgIwAQ%3D%3D", // this interesting line filters by cc license
},
}),
},
{
name: "Sketchfab",
[MODEL_3D](search) {
// TODO: Use actual license from filters
const licenseCodes: string[] = [
"322a749bcfa841b29dff1e8a1bb74b0b", // CC BY
"b9ddc40b93e34cdca1fc152f39b9f375", // CC BY-SA
"72360ff1740d419791934298b8b6d270", // CC BY-ND
"bbfe3f7dbcdd4122b966b85b9786a989", // CC BY-NC
"2628dbe5140a4e9592126c8df566c0b7", // CC BY-NC-SA
"34b725081a6a4184957efaec2cb84ed3", // CC BY-NC-ND
"7c23a1ba438d4306920229c12afcb5f9", // CC0
]
return {
url: "https://sketchfab.com/search",
queryParams: {
q: search.q,
licenses: licenseCodes,
},
}
},
},
{
name: "Thingiverse",
[MODEL_3D](search) {
return {
url: "https://www.thingiverse.com/search",
queryParams: {
type: "things",
q: search.q,
},
}
},
},
{
name: "Free Music Archive",
audio: (search) => ({
url: "https://freemusicarchive.org/search",
queryParams: {
quicksearch: search.q,
},
}),
},
]
/**
* Get a list of source builders for a given media type.
*
* @param mediaType - the media type by which to filter source builders
* @returns a list of additional source builders
*/
export const getAdditionalSourceBuilders = (
mediaType: MediaType
): AdditionalSourceBuilder[] =>
additionalSourceBuilders.filter((source) => source[mediaType])
/**
* Get a list of sources for a given media type with the URL populated to show
* the results of the given query.
*
* @param mediaType - the media type by which to filter source builders
* @param query - the query to show results for in the additional sources
* @returns a list of additional sources with pre-populated URLs
*/
export const getAdditionalSources = (
mediaType: MediaType,
query: PaginatedSearchQuery
): AdditionalSource[] =>
getAdditionalSourceBuilders(mediaType).map((source) => {
const urlFunc = source[mediaType]
// type-guard, never occurs
if (!urlFunc) {
throw new Error(
`Invalid media type ${mediaType} for additional source ${source.name}`
)
}
const urlInfo = urlFunc(transformSearchQuery(query))
const sourceUrl = new URL(urlInfo.url)
for (const [key, value] of Object.entries(urlInfo.queryParams)) {
if (Array.isArray(value)) {
for (const v of value) {
sourceUrl.searchParams.append(key, v)
}
} else {
sourceUrl.searchParams.set(key, value)
}
}
return {
url: sourceUrl.href,
name: source.name,
}
})