-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathwikimedia-commons.js
More file actions
81 lines (70 loc) · 2.71 KB
/
wikimedia-commons.js
File metadata and controls
81 lines (70 loc) · 2.71 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
const axios = require('axios');
const requestConfigTemplate = {
baseURL: 'https://commons.wikimedia.org/',
url: '/w/api.php',
method: 'get',
timeout: 10000,
params: {
action: 'query',
prop: 'imageinfo',
iiurlwidth: 400,
iiurlheight: 400,
redirects: 'resolve',
iiprop: 'user|url|extmetadata',
format: 'json',
},
};
module.exports = {
async getImagesFromCommonsWithTitle(topic, commonsCategory) {
let requestConfig = requestConfigTemplate;
if (commonsCategory !== undefined) {
requestConfig.params.generator = 'categorymembers';
requestConfig.params.gcmtype = 'file';
requestConfig.params.gcmtitle = 'Category:' + commonsCategory;
requestConfig.params.gcmlimit = 30;
} else {
requestConfig.params.generator = 'search';
requestConfig.params.gsrsearch = topic;
requestConfig.params.gsrlimit = 30;
requestConfig.params.gsrnamespace = 6;
}
const response = await axios.request(requestConfig);
if (!response.data.query || !response.data.query.pages) {
return [];
}
return Object.keys(response.data.query.pages).map(p => {
const page = response.data.query.pages[p];
let image = {
id: page.pageid,
source: 'Wikimedia Commons',
imageURL: page.imageinfo[0].url,
thumbURL: page.imageinfo[0].thumburl,
title: [],
creators: [],
uploader: page.imageinfo[0].user,
institutions: [],
infoURL: page.imageinfo[0].descriptionurl,
location: null,
geoLocations: [],
year: null,
license: '',
};
const extMetadata = page.imageinfo[0].extmetadata;
image.title.push(page.title.replace('File:', '').replace(/\.[^/.]+$/, ''));
if (extMetadata.GPSLatitude !== undefined && extMetadata.GPSLongitude !== undefined) {
image.geoLocations.push('POINT(' + extMetadata.GPSLongitude.value + ' ' + extMetadata.GPSLatitude.value + ')')
}
if (extMetadata.DateTimeOriginal !== undefined) {
const dateString = extMetadata.DateTimeOriginal.value;
const year = parseInt(dateString.substr(0, 4), 10);
if (year !== NaN) {
image.year = year;
}
}
if (extMetadata.LicenseShortName !== undefined) {
image.license = extMetadata.LicenseShortName.value;
}
return image;
});
}
};