Skip to content

Instantly share code, notes, and snippets.

@tmspzz
Last active August 29, 2015 13:56
Show Gist options
  • Save tmspzz/9101662 to your computer and use it in GitHub Desktop.
Save tmspzz/9101662 to your computer and use it in GitHub Desktop.
// npm install jsdom
// present a random Aww jpg
// problem: /r/aww/random seems to update the URI on a timer, not per request
function aww_image(title, url, img){
this.title = title;
this.url = url;
this.img = img;
}
var check_img = function (errors, window) {
var $ = window.$;
var imgLinks = $("div[id=siteTable] a");
var url = imgLinks.attr('href');
var matches = url.search(".jpg|.png");
var aww = new aww_image();
if(matches > 0){
aww.title = $('title').text();
aww.url = url;
aww.img = url;
}
else {
aww.title = $ ('title').text();
aww.url = url;
//get_random_link();
}
console.log(JSON.stringify(aww));
}
var get_random_link = function () {
var jsdom = require("jsdom");
var reddit_aww_random = "http://www.reddit.com/r/aww/random";
jsdom.env( reddit_aww_random, ["http://code.jquery.com/jquery.js"], check_img);
}
var main = function(){
get_random_link();
}
if (require.main === module) {
main();
}
@tomayac
Copy link

tomayac commented Feb 19, 2014

Consider using cheerio instead of jsdom: more lightweight and more fault-tolerant to HTML issues. It uses essentially a jQuery API.

@tmspzz
Copy link
Author

tmspzz commented Feb 20, 2014

Will try it. Little update in the mean time. Should catch .png(s) as well.

Now... just have to work around reddit's timed /random/ update

@mhaemmerle
Copy link

The uberlong time between updates sucks. Scrape the whole site? :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment