-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
36 lines (27 loc) · 903 Bytes
/
index.js
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
'use strict';
const got = require('got');
const isUrl = require('is-url');
const parseUrl = getUrl => {
return getUrl.split('?')[0] + '?__a=1';
};
module.exports = url => {
if (typeof url !== 'string' || isUrl(url) === false) {
throw new Error('Please provide a valid url');
}
const parsedUrl = parseUrl(url);
return got(parsedUrl, {json: true}).then(res => {
const data = res.body.graphql.shortcode_media.edge_sidecar_to_children.edges;
const list = {media: []};
for (let j = 0; j < data.length; j++) {
const videoUrl = data[j].node.video_url;
const edge = data[j].node.display_resources[2].src;
videoUrl === undefined ? list.media.push(edge) : list.media.push(videoUrl); // eslint-disable-line no-unused-expressions
}
return {data: list};
}).catch(err => {
if (err) {
err.message = 'the url does not contain multiple images';
}
return err.message;
});
};