-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
57 lines (45 loc) · 1.91 KB
/
api.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const fetch = require('node-fetch');
const generateQueryUrl = (username, limit, offset = 0) => {
const startDate = '2020-11-01';
const endDate = '2020-12-31';
const queryUrl = `https://api.untappd.com/v4/user/beers/${username}?limit=${limit}&start_date=${startDate}&end_date=${endDate}&offset=${offset}&client_id=${process.env.CLIENT_ID}&client_secret=${process.env.CLIENT_SECRET}`;
return queryUrl;
};
// fetch unique beers of an Untappd user
const userBeerFetch = async (username, limit = 50) => {
const queryUrl = generateQueryUrl(username, limit);
const response = await fetch(queryUrl);
const data = await response.json();
const totalCount = data.response.total_count;
console.log(`total number of results for ${username}: ${totalCount}`);
const numberOfQueries = Math.ceil(totalCount / limit);
const emptyUrls = new Array(numberOfQueries).fill({});
const fetchUrls = emptyUrls.map((_, index) => {
const offset = index * limit;
const newUrl = generateQueryUrl(username, limit, offset);
return newUrl;
});
// convert fetchUrls to requests
const requests = fetchUrls.map(url => fetch(url));
// fetch data for each url, convert to json
// and combine into a flat array of userCheckins
const fullResponse = await Promise.all(requests);
const fullData = await Promise.all(fullResponse.map(j => j.json()));
const userCheckins = fullData.reduce((arr, resp) => {
arr.push(resp.response.beers.items);
return arr;
}, []).flat();
console.log(`userCheckins length for ${username}: ${userCheckins.length}`);
// extract beer id, name and rating into a userBeers object
const userBeers = await userCheckins.reduce((obj, checkin) => {
let beer = {
beer_id: checkin.beer.bid,
beer_name: checkin.beer.beer_name,
beer_rating: checkin.rating_score
};
obj[checkin.beer.bid] = beer;
return obj;
}, {});
return userBeers;
};
module.exports = userBeerFetch;