Created
August 24, 2012 17:19
-
-
Save TurplePurtle/3453092 to your computer and use it in GitHub Desktop.
My Little StreamLogger Bot
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
require 'snoo' | |
bot = Snoo::Client.new 'http://www.reddit.com', 'r/mylittlestreamlog bot' | |
puts 'Logging in...' | |
bot.log_in(ARGV[0], ARGV[1]) | |
def is_stream(item) | |
data = item['data'] | |
if /(?<!main)stream/i.match(data['title']) | |
return true | |
elsif data['is_self'] and /(?<!main)stream/i.match(data['selftext']) | |
return true | |
else | |
return false | |
end | |
end | |
def listing(bot, opts={}) | |
bot.get_listing(opts)['data']['children'] | |
end | |
def created_utc(item) | |
item['data']['created_utc'].to_i | |
end | |
def run(bot) | |
savefile = 'last_time.dat' | |
last_time = File.read(savefile).to_i if File.exists? savefile | |
last_time = nil if last_time == 0 | |
unless last_time | |
puts 'Last link not found. Type "yes" to continue.' | |
return unless $stdin.gets.chomp == 'yes' | |
last_time = created_utc(listing(bot, | |
subreddit: 'mylittlestreamlog', limit: 1, page: 'new', sort: 'new').first) | |
puts "Using #{last_time} as last time." | |
end | |
raise "Last item creation time required" if not last_time | |
listing = listing(bot, subreddit: 'mlplounge', page: 'new', sort: 'new') | |
streams = listing.select {|item| is_stream(item) } | |
streams.select! {|item| created_utc(item) > last_time } | |
streams.reverse_each do |item| | |
data = item['data'] | |
title = "[#{data['author']}] #{data['title']}" | |
url = "http://www.reddit.com" + data['permalink'] | |
puts "-Submitting #{title}" | |
bot.submit title, 'mylittlestreamlog', url: url | |
sleep(3) | |
end | |
File.open(savefile, 'w') do |f| | |
last_time = created_utc(listing.first) | |
f.write(last_time) | |
end | |
end | |
loop do | |
begin | |
puts "-- Running..." | |
run bot | |
rescue | |
puts $! | |
end | |
sleep 5*60 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment