This repository has been archived by the owner on Nov 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #108 from christianbundy/master
4/24
- Loading branch information
Showing
6 changed files
with
267 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,53 @@ | ||
//var queue = new PowerQueue(); | ||
var hn = Meteor.require('hacker-news-api'); | ||
|
||
var readHn = function () { | ||
var readHn = function (before) { | ||
"use strict"; | ||
console.log('Reading last hour, 12 hours, 24 hours, and week of Hacker News'); | ||
var now = Date.now() / 1000; | ||
var querylist = []; | ||
|
||
// past hour | ||
querylist.push('search?tags=story&numericFilters=created_at_i>' + | ||
(now - 60 * 60) + ',created_at_i<' + | ||
now); | ||
|
||
// past 12 hours | ||
querylist.push('search?tags=story&numericFilters=created_at_i>' + | ||
(now - 60 * 60 * 12) + ',created_at_i<' + | ||
now); | ||
|
||
// past 24 hours | ||
querylist.push('search?tags=story&numericFilters=created_at_i>' + | ||
(now - 60 * 60 * 24) + ',created_at_i<' + | ||
now); | ||
|
||
// past week | ||
querylist.push('search?tags=story&numericFilters=created_at_i>' + | ||
(now - 60 * 60 * 24 * 7) + ',created_at_i<' + | ||
now); | ||
|
||
// past month | ||
querylist.push('search?tags=story&numericFilters=created_at_i>' + | ||
(now - 60 * 60 * 24 * 30) + ',created_at_i<' + | ||
now); | ||
|
||
// past year | ||
querylist.push('search?tags=story&numericFilters=created_at_i>' + | ||
(now - 60 * 60 * 24 * 365) + ',created_at_i<' + | ||
now); | ||
|
||
// past ever | ||
querylist.push('search?tags=story&numericFilters=created_at_i>' + | ||
0 + ',created_at_i<' + | ||
now); | ||
|
||
_.forEach(querylist, function (query) { | ||
hn.call(query, Meteor.bindEnvironment( | ||
function (error, data) { | ||
if (error) { | ||
throw error; | ||
} | ||
_(data.hits).forEach(function (item) { | ||
/*jshint camelcase: false */ | ||
var obj = { | ||
oldId: parseInt(item.objectID, 10), | ||
oldPoints: parseInt(item.points, 10), | ||
createdAt: new Date(item.created_at), | ||
site: 'hn', | ||
author: item.author, | ||
title: item.title, | ||
url: item.url, | ||
oldComments: parseInt(item.num_comments, 10), | ||
}; | ||
|
||
Posts.upsert({ | ||
oldId: obj.oldId | ||
},{ | ||
$set: obj | ||
}); | ||
|
||
}); | ||
}, function (error) { | ||
console.log('Reading the past ' + before + ' seconds of Hacker News'); | ||
var now = Math.floor(Date.now() / 1000); | ||
var query = 'search?tags=story&numericFilters=created_at_i>'; | ||
query += (now - before) + ',created_at_i<' + now; | ||
|
||
hn.call(query, Meteor.bindEnvironment( | ||
function (error, data) { | ||
if (error) { | ||
throw error; | ||
}) | ||
); | ||
}); | ||
} | ||
_(data.hits).forEach(function (item) { | ||
/*jshint camelcase: false */ | ||
var obj = { | ||
oldId: parseInt(item.objectID, 10), | ||
oldPoints: parseInt(item.points, 10), | ||
createdAt: new Date(item.created_at), | ||
site: 'hn', | ||
author: item.author, | ||
title: item.title, | ||
url: item.url, | ||
oldComments: parseInt(item.num_comments, 10), | ||
}; | ||
|
||
Posts.upsert({ | ||
oldId: obj.oldId | ||
},{ | ||
$set: obj | ||
}); | ||
|
||
}); | ||
}, function (error) { | ||
throw error; | ||
}) | ||
); | ||
}; | ||
|
||
// read last 24 hours of hn | ||
readHn(); | ||
Meteor.setInterval(function () { | ||
"use strict"; | ||
readHn(60); // top for the minute | ||
readHn(60 * 60); // top for the hour | ||
readHn(24 * 60 * 60); // top for the day | ||
readHn(7 * 24 * 60 * 60); // top for the week | ||
}, 60 * 1000); // every minute | ||
|
||
// reread hn every half hour | ||
Meteor.setInterval(readHn, 1000 * 60 * 30); | ||
Meteor.setInterval(function () { | ||
"use strict"; | ||
readHn(Math.floor(Date.now() / 1000)); // top ever | ||
readHn(7 * 24 * 60 * 60); // top for the week | ||
}, 60 * 60 * 1000); // every hour |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.