-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-photos.js
More file actions
40 lines (30 loc) · 1.04 KB
/
Copy pathget-photos.js
File metadata and controls
40 lines (30 loc) · 1.04 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
const got = require('got');
const INSTAGRAM_BASE = 'https://api.instagram.com';
const INSTAGRAM_PATH = '/v1/users/{user_id}/media/recent';
const INSTAGRAM_QUERY = '?access_token={access_token}';
const PHOTOS_ENDPOINT = `${INSTAGRAM_BASE}${INSTAGRAM_PATH}${INSTAGRAM_QUERY}`;
const flatten = list => list.reduce((a, b) => a.concat(Array.isArray(b) ? flatten(b) : b), []);
const getPhotos = async (accessToken, userId, count = 3) => {
let url = PHOTOS_ENDPOINT
.replace(/{access_token}/g, accessToken)
.replace(/{user_id}/g, userId);
const acc = {pages: [], urls: []};
let response;
/* eslint-disable no-await-in-loop */
while (url && acc.pages.length <= count && !acc.urls.includes(url)) {
acc.urls.push(url);
response = await got(url);
const {
data = [],
pagination = {}
} = JSON.parse(response.body);
url = pagination.next_url;
acc.pages.push(data);
}
/* eslint-enable no-await-in-loop */
const {pages: photos} = acc;
return {
photos: flatten(photos)
};
};
module.exports = getPhotos;