Created
May 24, 2013 07:05
-
-
Save chelsea/5641777 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
<li data-row="1" data-col="1" data-sizex="1" data-sizey="1"> | |
<div data-id="random_aww" data-view="RandomAww"></div> | |
</li> | |
<li data-row="1" data-col="1" data-sizex="1" data-sizey="1"> | |
<div data-id="animalsbeingjerks" data-view="RandomAww"></div> | |
</li> |
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
require 'net/http' | |
require 'json' | |
placeholder = '/assets/nyantocat.gif' | |
reddits = { 'random_aww' => 'awww', 'animalsbeingjerks' => 'animalsbeingjerks' } | |
reddits.each do |title, reddit| | |
SCHEDULER.every '60s', first_in: 0 do |job| | |
http = Net::HTTP.new('www.reddit.com') | |
response = http.request(Net::HTTP::Get.new("/r/#{reddit}.json")) | |
json = JSON.parse(response.body) | |
if json['data']['children'].count <= 0 | |
send_event(title, image: placeholder) | |
else | |
urls = json['data']['children'].map{|child| child['data']['url'] } | |
# Ensure we're linking directly to an image, not a gallery etc. | |
send_event(title, image: urls.select{|url| url.downcase.end_with?('png', 'gif', 'jpg', 'jpeg') }.sample(1).first) | |
end | |
end | |
end |
Seems like a decent solution, since the server is just pushing data to the client it would be tricky to do anything else.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For this:
key = data-id in the dashboard html, value is the /r/name of the subreddit you want to parse from