Created
June 10, 2012 19:08
-
-
Save pachacamac/2906952 to your computer and use it in GitHub Desktop.
Talking Reddit Watchdog
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 'restclient' | |
require 'json' | |
subreddits = { | |
'minimalism' => 'http://www.reddit.com/r/minimalism.json?sort=new', | |
'programming' => 'http://www.reddit.com/r/programming.json?sort=top&t=day', | |
'lifeprotips' => 'http://www.reddit.com/r/lifeprotips.json?sort=new', | |
'ruby' => 'http://www.reddit.com/r/ruby.json?sort=new', | |
'linux' => 'http://www.reddit.com/r/linux.json?sort=new', | |
'netsec' => 'http://www.reddit.com/r/netsec.json?sort=new', | |
#'pics' => 'http://www.reddit.com/r/pics.json?sort=new', | |
} | |
def speak(text, lang='en') | |
tmp_file = '/tmp/google_tts.mp3' | |
File.open(tmp_file, 'w') do |f| | |
f.write RestClient.get("http://translate.google.com/translate_tts?tl=#{lang}&q=#{URI.encode(text)}") | |
end | |
`mpg123 -q #{tmp_file}` | |
end | |
at_exit do | |
unless $!.is_a?(Interrupt) | |
speak "Oh no! I'm dying! The reason is: #{$!.message}" | |
end | |
end | |
known_posts = Hash[subreddits.keys.zip(Array.new(subreddits.size){{}})] | |
while true | |
subreddits.each do |subreddit, url| | |
data = JSON.parse(RestClient.get(url))['data']['children'].map{|e| e['data']} | |
posts = Hash[data.map{|e| e['id']}.zip(data)] | |
new_keys = posts.keys - known_posts[subreddit].keys | |
new_posts = posts.select{|k,v| new_keys.include?(k)} | |
puts "#{Time.now} - #{subreddit} - #{new_posts.size} new posts" | |
if !known_posts[subreddit].empty? && !new_posts.empty? | |
new_posts.each{|k,v| puts "#{v['title']} - #{v['domain']}"} | |
speak "I found #{new_posts.count} new post#{new_posts.count==1 ? '':'s'} in #{subreddit}." | |
new_posts.values.sort{|a,b| a['created']<=>b['created']}.each_with_index do |post, i| | |
number = new_posts.size > 1 ? "Number #{i+1}: " : '' | |
domain = post['is_self'] ? '' : ". Domain: #{post['domain']}" | |
speak "#{number}#{post['title']}#{domain}" | |
end | |
end | |
known_posts[subreddit].merge!(new_posts) | |
end | |
sleep 120 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment