-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
37 lines (36 loc) · 1.45 KB
/
index.js
File metadata and controls
37 lines (36 loc) · 1.45 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
'use strict';
const arrayIncludes = require('array-includes');
const got = require('got');
const queryString = require('query-string');
const mediaDefaults = require('./media_defaults.json');
module.exports = (term, type) => {
return new Promise((resolve, reject) => {
const normalizedType = type.toLowerCase();
const validTypes = Object.keys(mediaDefaults);
if (arrayIncludes(validTypes, normalizedType)) {
const media = mediaDefaults[normalizedType].media || '';
const entity = mediaDefaults[normalizedType].entity || '';
const name = mediaDefaults[normalizedType].name || '';
const searchOptions = {term: term, media: media, entity: entity, name: name};
const query = queryString.stringify(searchOptions);
const url = `https://itunes.apple.com/search?${query}`;
got(url).then(response => {
const jsonResponse = JSON.parse(response.body);
const results = jsonResponse.results || [];
let pictureLinks = [];
results.forEach(result => {
const name = result[searchOptions.name];
const thumbnailUrl = result.artworkUrl100;
const imageUrl = thumbnailUrl.replace('100x100', '600x600');
pictureLinks.push({name: name, thumbnailUrl: thumbnailUrl, imageUrl: imageUrl});
});
resolve(pictureLinks);
}).catch(error => {
reject(error);
});
}
else {
reject(`'${normalizedType}' is not a valid type`);
}
});
};