const parser = new Parser();
const app = express();
+function contentExtractor(feed: FeedSchema, item): string {
+ let str = '';
+ switch(feed.title) {
+ case 'TechCrunch':
+ str = item.contentSnippet;
+ break;
+ case 'Ars Technica Features':
+ str = item.contentSnippet;
+ break;
+ }
+
+ return str;
+}
+
async function queryFeeds() {
const completeFeedList = await db.select<FeedSchema[]>('*').from('feeds').orderBy('created_at');
const feedsToQuery = completeFeedList.filter(feed => {
// only update every 5 minutes
- return Date.now() > (feed.updated_at + 1000 * 60 * 5);
+ //return Date.now() > (feed.updated_at + 1000 * 60 * 5);
+ return true;
});
console.log(`Querying ${feedsToQuery.length} feeds`);
for(let feed of feedsToQuery) {
console.log(`Querying ${feed.title}(${feed.url})`);
- const data = await parser.parseURL(feed.url);
-
- const items: FeedEntrySchema<any>[] = data.items.map(item => {
- return {
- id: item.guid,
- title: item.title,
- link: item.link,
- feed_id: feed.id,
- pub_date: Math.floor(new Date(item.pubDate).getTime()/1000),
- author: item.creator,
- created_at: Math.floor(Date.now()/1000),
- updated_at: Math.floor(Date.now()/1000),
- meta: {
- comment_link: item.comments
+ try {
+ const data = await parser.parseURL(feed.url);
+
+ const items: FeedEntrySchema<any>[] = data.items.map(item => {
+ return {
+ id: item.guid,
+ title: item.title,
+ link: item.link,
+ feed_id: feed.id,
+ pub_date: Math.floor(new Date(item.pubDate).getTime()/1000),
+ author: item.creator,
+ created_at: Math.floor(Date.now()/1000),
+ updated_at: Math.floor(Date.now()/1000),
+ meta: {
+ comment_link: item.comments,
+ snippet: contentExtractor(feed, item)
+ }
}
+ });
+
+ for(let item of items) {
+ // inserting one at a time so that we can ignore duplicates for now
+ // eventually we'll need to do some kind of merge
+ await db('feed_entry').insert(item).onConflict().ignore();
}
- });
- for(let item of items) {
- // inserting one at a time so that we can ignore duplicates for now
- // eventually we'll need to do some kind of merge
- await db('feed_entry').insert(item).onConflict().ignore();
- }
+ await db('feeds').update({
+ updated_at: new Date()
+ }).where({
+ id: feed.id
+ });
- await db('feeds').update({
- updated_at: new Date()
- }).where({
- id: feed.id
- });
+ }
+ catch(e) {
+ console.log(e);
+ console.log('Continuing..');
+ }
}
setTimeout(queryFeeds, (Number(process.env.FEED_REFRESH_RATE) + 1) * 1000 * 60);
});
res.send(groups.map(group => {
- return `<div class="item">
- ${renderFeedItemHeader(group[0])}
- ${group.map(renderFeedItem).join("\n")}
- </div>`;
- }).join("\n"));
+ return `<div class="item">
+${renderFeedItemHeader(group[0])}
+${group.map(renderFeedItem).join("\n")}
+</div>`;
+ }).join("\n"));
});
app.post('/feeds', async (req, res) => {