const FEED_REFRESH_RATE = parseInt(process.env.FEED_REFRESH_RATE || '0');
+function timestamp(obj: any): number {
+ if(obj.getTime) {
+ return Math.floor(obj.getTime()/1000)
+ }
+ else if(obj.toString() === obj) {
+ return Math.floor(new Date(obj).getTime() / 1000)
+ }
+ else if(!isNaN(obj)) {
+ if(obj.toString().length === 10) {
+ return obj;
+ }
+ else {
+ return Math.floor(obj/1000);
+ }
+ }
+}
+
function contentExtractor(feed: FeedSchema, item): string {
let str = '';
switch(feed.title) {
async function queryFeeds() {
const completeFeedList = await db.select<FeedSchema[]>('*').from('feeds').orderBy('created_at');
+ const now = Date.now();
const feedsToQuery = completeFeedList.filter(feed => {
// only update every 5 minutes
- return Math.floor(Date.now()/1000) > (feed.updated_at + (1000 * 60 * FEED_REFRESH_RATE));
+ return timestamp(now) > (timestamp(feed.updated_at) + (60 * FEED_REFRESH_RATE));
});
console.log(`Querying ${feedsToQuery.length} feeds`);
- const now = Date.now();
for(let feed of feedsToQuery) {
console.log(`Querying ${feed.title}(${feed.url})`);
title: item.title,
link: item.link,
feed_id: feed.id,
- pub_date: Math.floor(new Date(item.pubDate).getTime()/1000),
+ pub_date: timestamp(item.pubDate),
author: item.creator || item.author,
- created_at: Math.floor(now/1000),
- updated_at: Math.floor(now/1000),
+ created_at: timestamp(now),
+ updated_at: timestamp(now),
meta: {
comment_link: item.comments,
snippet: contentExtractor(feed, item)
}
await db('feeds').update({
- updated_at: Math.floor(Date.now()/1000)
+ updated_at: timestamp(now)
}).where({
id: feed.id
});
let firstLoad = true;
const freshReadInHours = parseInt(process.env.FRESH_READ_IN_HOURS) || 6;
- let createdAt = Math.floor(Date.now()/1000 - ((60 * 60) * freshReadInHours));
+ let createdAt = Math.floor(timestamp(new Date()) - ((60 * 60) * freshReadInHours));
if(!displayedItems) {
const temp = parseInt(header);