-
Notifications
You must be signed in to change notification settings - Fork 1
/
feedbin.js
147 lines (122 loc) · 3.71 KB
/
feedbin.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const fs = require('fs');
const fetch = require('node-fetch');
const unionBy = require('lodash/unionBy');
// Load .env variables
require('dotenv').config();
const API = 'https://api.feedbin.com';
// FEEDLY_AUTH was a typo in the first version: fixing but still supporting
const AUTH = process.env.FEEDBIN_AUTH || process.env.FEEDLY_AUTH;
const ROUTES = {
stars: '/v2/starred_entries.json',
entries: '/v2/entries.json'
}
const CACHE_FOLDER = './_data'
const CACHE_FILE_PATH = './_data/feedbin.json';
/**************************
* Where the magic happens
\**************************/
module.exports = {
getAllFavorites
}
async function getAllFavorites () {
console.log('>>> Reading from cache...');
let cache = readFromCache();
if (cache.favorites.length) {
console.log(`>>> ${cache.favorites.length} favorites loaded from cache`);
}
// Only fetch new mentions in production, otherwise we just use the cache
if (process.env.NODE_ENV === 'production') {
console.log('>>> Checking for new favorites...');
const feed = await fetchFavoriteList();
const newFavorites = checkForNewFavorites(cache.favorites, feed);
if (newFavorites.length) {
const text = await fetchEntries(newFavorites);
if (text.length) {
const favorites = {
lastFetched: new Date().toISOString(),
favorites: mergeFavorites(cache.favorites, text)
}
writeToCache(favorites);
cache = favorites;
return favorites;
}
}
}
return cache;
}
/**************************
* API access with auth
\**************************/
async function fetchFeedbinFeed(name, route) {
// If we dont have a domain name, token, or route, abort
if (!API || !AUTH || !route) {
console.warn(`>>> unable to fetch ${name}: missing domain or auth`);
return false;
}
let url = `${API}${route}`;
const response = await fetch(url, { headers: { 'Authorization': 'Basic ' + Buffer.from(AUTH).toString('base64') }});
if (response.status === 200) {
const feed = await response.json();
console.log(`>>> ${feed.length} ${name} fetched from ${url}`);
return feed;
}
return null;
}
/************************************\
* Only two routes implemented so far
\************************************/
function fetchFavoriteList() {
return fetchFeedbinFeed('favorites', ROUTES.stars);
}
function fetchEntries(favorites) {
let route = `${ROUTES.entries}?ids=` + favorites.join();
return fetchFeedbinFeed('entries', route);
}
function checkForNewFavorites(oldFavorites, feed) {
// compares the recently fetched list to our cached list, discards any entries previously fetched
var newFavorites = [];
for (var i = feed.length - 1; i >= 0; i--) {
var id = feed[i];
newFavorites.push(id);
oldFavorites.forEach(oldFav => {
if (id === oldFav.id) {
newFavorites.pop();
}
});
}
if (newFavorites.length) {
console.log(`>>> ${newFavorites.length} new favorites found`);
} else {
console.log(`>>> no new favorites found`);
}
return newFavorites;
}
// allows us to merge new entries into our cache, based on ID
function mergeFavorites(a, b) {
return unionBy(a, b, 'id');
}
/************************************\
* Caching utilities
\************************************/
function writeToCache(data) {
const fileContent = JSON.stringify(data, null, 2);
// create cache folder if it doesnt exist already
if (!fs.existsSync(CACHE_FOLDER)) {
fs.mkdirSync(CACHE_FOLDER);
}
// write data to cache json file
fs.writeFile(CACHE_FILE_PATH, fileContent, err => {
if (err) throw err;
console.log(`>>> favorites cached to ${CACHE_FILE_PATH}`);
})
}
function readFromCache() {
if (fs.existsSync(CACHE_FILE_PATH)) {
const cacheFile = fs.readFileSync(CACHE_FILE_PATH);
return JSON.parse(cacheFile);
}
return {
lastFetched: null,
favorites: []
}
}