Skip to content

Commit f360419

Browse files
committed
Generic updater. Include tweet updates
1 parent 5be8597 commit f360419

File tree

3 files changed

+29
-18
lines changed

3 files changed

+29
-18
lines changed

public/feeds.mjs

+5
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ class Feed {
4242
}
4343

4444
async function updateFeeds() {
45+
console.info('Updating RSS feeds...');
46+
const tic = Date.now();
47+
4548
const promises = [];
4649

4750
FEEDS.forEach(url => {
@@ -94,6 +97,8 @@ async function updateFeeds() {
9497

9598
FEEDS_CACHE = results;
9699

100+
console.info(`Feed update took ${(Date.now() - tic)/1000}s`);
101+
97102
return results;
98103
}
99104

public/twitter.mjs

+10-3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@ const TWITTER_CREDENTIALS = JSON.parse(fs.readFileSync('./twitter_credentials.js
66
const CACHE = new Map();
77

88
export default class Twitter {
9-
constructor() {
9+
constructor(screenName = null) {
1010
this.client = new TwitterAPI(TWITTER_CREDENTIALS);
11+
this.screenName = screenName;
1112
}
12-
async updateTweets(screenName) {
13+
14+
async updateTweets(screenName = this.screenName) {
15+
console.info('Updating Tweets...');
16+
const tic = Date.now();
17+
1318
let tweets = [];
1419

1520
try {
@@ -43,10 +48,12 @@ export default class Twitter {
4348
// TODO: don't grow cache for every user.
4449
CACHE.set(screenName, tweets);
4550

51+
console.info(`Tweets from ${screenName} update took ${(Date.now() - tic)/1000}s`);
52+
4653
return tweets;
4754
}
4855

49-
async getTweets(screenName) {
56+
async getTweets(screenName = this.screenName) {
5057
if (CACHE.has(screenName)) {
5158
return CACHE.get(screenName);
5259
}

server.mjs

+14-15
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,7 @@ import * as dbHelper from './public/firebaseHelper.mjs';
3434
const PORT = process.env.PORT || 8080;
3535
const RENDER_CACHE = new Map(); // Cache of pre-rendered HTML pages.
3636

37-
function updateRSSFeedsDaily() {
38-
console.info('Updating RSS feeds...');
39-
const dayInMilliseconds = 1000 * 60 * 60 * 24;
40-
const tick = Date.now();
41-
feeds.updateFeeds().then(() => {
42-
console.info(`feed update took ${(Date.now() - tick)/1000}s`);
43-
});
44-
setTimeout(updateRSSFeedsDaily, dayInMilliseconds);
45-
}
37+
const twitter = new Twitter('ChromiumDev');
4638

4739
/**
4840
*
@@ -143,17 +135,15 @@ app.get('/ssr', async (req, res) => {
143135

144136
app.get('/tweets/:username', async (req, res) => {
145137
const username = req.params.username;
146-
const twitter = new Twitter();
147138
res.status(200).json(await twitter.getTweets(username));
148139
});
149140

150-
app.get('/admin/_updaterss', async (req, res) => {
141+
app.get('/admin/update/rss', async (req, res) => {
151142
res.status(200).json(await feeds.updateFeeds());
152143
});
153144

154-
app.get('/admin/_updatetweets', async (req, res) => {
145+
app.get('/admin/update/tweets/:username', async (req, res) => {
155146
const username = req.params.username;
156-
const twitter = new Twitter();
157147
res.status(200).json(await twitter.updateTweets(username));
158148
});
159149

@@ -206,5 +196,14 @@ app.get('/posts/:year?/:month?/:day?', async (req, res) => {
206196
app.listen(PORT, () => {
207197
console.log(`App listening on port ${PORT}`);
208198
console.log('Press Ctrl+C to quit.');
209-
updateRSSFeedsDaily();
210-
});
199+
200+
async function updatePosts(updateFunction, msTimeout) {
201+
await updateFunction.bind(twitter).call();
202+
setTimeout(updatePosts, msTimeout);
203+
}
204+
205+
// Warm the caches.
206+
// TODO: move to cron.
207+
updatePosts(feeds.updateFeeds, 1000 * 60 * 60 * 24); // every 24hrs
208+
updatePosts(twitter.updateTweets, 1000 * 60 * 60 * 1); // every 1hrs
209+
});

0 commit comments

Comments
 (0)