Last active
August 29, 2015 13:56
-
-
Save tmspzz/9101662 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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(); | |
} |
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
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
Consider using cheerio instead of jsdom: more lightweight and more fault-tolerant to HTML issues. It uses essentially a jQuery API.