Created
October 15, 2015 21:42
-
-
Save toinetoine/a830f994ed292429ea5e to your computer and use it in GitHub Desktop.
Custon Reddit Top Search
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
var startTime = new Date(2015, 5, 1, 0, 0, 0, 0); // june 1st 2015 | |
var endTime = new Date(2015, 8, 1, 0, 0, 0, 0); // sept 1st 2015 | |
searchPages(["microsoft", ".net"], | |
{start: Math.floor(startTime.getTime() / 1000), end: Math.floor(endTime.getTime() / 1000)}, | |
null); | |
var killProcess = false; | |
setTimeout(function() { killProcess = true;}, 300000); | |
function searchPages(searchTerms, timeInterval, redditPageId) { | |
var xmlhttp = new XMLHttpRequest(); | |
var url = "https://www.reddit.com/r/programming/top.json?sort=top&t=year"; | |
if(redditPageId != null) { | |
url += ("&after=" + redditPageId); | |
} | |
xmlhttp.onreadystatechange = function() { | |
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { | |
var redditData = JSON.parse(xmlhttp.responseText); | |
var redditPosts = redditData.data.children; | |
// for each reddit post | |
for(var postIndex = 0; postIndex < redditPosts.length; postIndex++) { | |
var postTitle = redditPosts[postIndex].data.title; | |
var postCreationTime = parseInt(redditPosts[postIndex].data.created_utc); | |
if(postCreationTime >= timeInterval.start && postCreationTime <= timeInterval.end) { | |
var possibleSearchResult = {} | |
possibleSearchResult.title = postTitle; | |
possibleSearchResult.link = "https://www.reddit.com" + redditPosts[postIndex].data.permalink; | |
possibleSearchResult.score = redditPosts[postIndex].data.ups; | |
var tempDate = new Date(0); | |
tempDate.setUTCSeconds(postCreationTime); | |
possibleSearchResult.created = tempDate.toDateString(); | |
// if no terms to match, then just print the results | |
if(searchTerms.length == 0) { | |
console.log(possibleSearchResult); | |
} | |
// print all results that match at least one of the terms | |
else { | |
// for each of the search terms, if found a match, then print it | |
for(var termIndex = 0; termIndex < searchTerms.length; termIndex++) { | |
if(postTitle.toLowerCase().indexOf(searchTerms[termIndex]) > -1) { | |
console.log(possibleSearchResult); | |
break; // go onto the next post if post title matched one of the terms | |
} | |
} | |
} | |
} | |
} | |
if(!killProcess) { | |
setTimeout(function(terms, interval, pageId) { | |
searchPages(terms, interval, pageId) | |
}(searchTerms, timeInterval, redditData.data.after), 2000); | |
} | |
} | |
} | |
xmlhttp.open("GET", url, true); | |
xmlhttp.send(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment