-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (37 loc) · 1.66 KB
/
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
37
38
39
40
41
42
43
44
require('dotenv').config();
const request = require('request');
const cheerio = require('cheerio');
const fs = require("fs");
main();
async function main() {
let beerFestivals = await getPortfolioCollectionHightCount("beer-festivals");
let hackathons = await getPortfolioCollectionHightCount("hackathons");
let untappdProfileData = await asyncGetRequest(`https://api.untappd.com/v4/user/info/CraftBeerSean?client_id=${process.env.UNTAPPD_CLIENT_ID}&client_secret=${process.env.UNTAPPD_CLIENT_SECRET}`, {});
let date_time_now = (new Date()).toUTCString();
appendValuesToMdFile({
beer_festivals_val: beerFestivals,
hackathons_val: hackathons,
beer_checkins_val: JSON.parse(untappdProfileData).response.user.stats.total_beers,
date_time_now
}, "about.md", "render.md");
}
function appendValuesToMdFile(obj, fileName, newFileName) {
if (newFileName == undefined) newFileName = fileName;
let fileContent = fs.readFileSync(fileName, "utf8");
Object.keys(obj).map(key => {
fileContent = fileContent.replace(key, obj[key]);
}); fs.writeFileSync(newFileName, fileContent);
}
async function getPortfolioCollectionHightCount(collectionName) {
let html = await asyncGetRequest(`https://seanomahoney.com/timeline/collection/${collectionName}`, {});
const $ = cheerio.load(html);
return $("#count").text().replace(" Highlights", "");
}
async function asyncGetRequest(url, headers) {
return new Promise(resolve => {
request({ method: "GET", url, headers }, function (error, response) {
if (error) throw new Error(error);
resolve(response.body);
});
});
}