Created
November 16, 2009 15:46
-
-
Save lackac/236071 to your computer and use it in GitHub Desktop.
Some old code where the script would monitor a newsstream called "Percről percre" and tweet any new items through a specific twitter account.
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
#!/usr/bin/env ruby | |
$KCODE = 'u' | |
require 'jcode' | |
require 'rubygems' | |
require 'open-uri' | |
require 'hpricot' | |
require 'twitter' | |
DEBUG = true | |
class PPTwit | |
def initialize url, user, password, interval = 30 | |
@url = url | |
@user = user | |
@interval = interval.to_i > 15 ? interval.to_i : 15 # in seconds | |
@last = false | |
if File.exists?(".#{@user}_last") | |
@last = File.read(".#{@user}_last").strip | |
end | |
@twit = Twitter::Base.new(@user, password) | |
puts "initialized: #{user} #{password} for #{url}" if DEBUG | |
end | |
def start_update | |
puts "updating every #{@interval} seconds" if DEBUG | |
loop do | |
update | |
sleep(@interval) | |
end | |
end | |
def update | |
puts "Updating..." if DEBUG | |
doc = nil | |
while doc.nil? | |
begin | |
doc = Hpricot(open(@url)) | |
rescue SocketError, SystemCallError => e | |
puts "Error: #{e}" | |
puts "Waiting 15 seconds and trying again" | |
sleep(15) | |
end | |
end | |
puts "url fetched" if DEBUG | |
pp = doc % "div.pp_post:last" | |
posting = false | |
while pp | |
if pp["class"] =~ /pp_post/ | |
title = (pp % "h3/:last").inner_text | |
time = (pp % "span.post_time").inner_text | |
post = (pp % "p/:first").inner_text | |
if posting or not @last | |
@last = "#{time} #{title}".strip | |
if DEBUG | |
puts "Posting:" | |
puts "title: #{title}" | |
puts "time: #{time}" | |
puts "post: #{post.inspect}" | |
puts | |
puts "twits: #{post.length <= 140 ? post : split_to_twits(post).inspect}" | |
puts | |
end | |
if post != "" and post.length <= 140 | |
@twit.update(post) | |
elsif post != "" | |
split_to_twits(post).each do |t| | |
@twit.update(t) | |
sleep(2) | |
end | |
end | |
end | |
posting = true if @last == "#{time} #{title}".strip | |
end | |
#if time =~ /19:45$/ | |
# break | |
#end | |
pp = pp.previous_sibling | |
end | |
f = File.new(".#{@user}_last", "w") | |
f.write(@last) | |
f.close | |
end | |
private | |
def split_to_twits s | |
twits = [] | |
words = s.split(" ") | |
while words.join(" ").length > 140 | |
first = "" | |
while first.length + words.first.length <= 136 | |
first << words.shift + " " | |
end | |
first << "..." | |
twits << first | |
end | |
twits << words.join(" ") | |
end | |
end | |
case $ARGV.size | |
when 3 | |
ppt = PPTwit.new $ARGV[0], $ARGV[1], $ARGV[2] | |
when 4 | |
ppt = PPTwit.new $ARGV[0], $ARGV[1], $ARGV[2], $ARGV[3] | |
else | |
puts "USAGE: pptwit.rb url user password [update_interval]" | |
exit 1 | |
end | |
ppt.start_update | |
# vim: sw=2 ts=2 et |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment