Login   Home   Archives   About  
 
Usando Ruby para seguir tu Twitter
Contributed by Luis M on Friday July 3, 2009 05:14PM
from the vaya-eso-fue-rapido dept. (1013)
Gracias a este artículo de About.com pude escribir un simple cliente en Ruby que crea un archivo con mi último estado en Twitter. Fue simple. Pensé que iba a durar más tiempo escribiendo código innecesario.

Aquí está el script para aquellos que le interese. Sólo tienes que ejecutarlo de la siguiente forma:

./twitter.rb > tweet.html

En mi caso lo puse en un cron como el siguiente, para que ejecute cada 5 minutos:

*/5 * * * * ~/bin/twitter.rb > ~/Sites/my_site/tweet.html

Para instalar lo que necesitas, en Ubuntu solo tienes que hacer:

sudo apt-get install ruby rubygems libhpricot-ruby

#!/usr/bin/env ruby
require 'rubygems'
require 'hpricot'
require 'net/http'

$username = 'user'
$password = 'secret'

def twitter(command, opts={}, type=:get)
# Open an HTTP connection to twitter.com
twitter = Net::HTTP.start('twitter.com')

# Depending on the request type, create either
# an HTTP::Get or HTTP::Post object
case type
when :get
# Append the options to the URL
command << "?" + opts.map{|k,v| "#=#" }.join('&')
req = Net::HTTP::Get.new(command)

when :post
# Set the form data with options
req = Net::HTTP::Post.new(command)
req.set_form_data(opts)
end

# Set up the authentication and
# make the request
req.basic_auth( $username, $password )
res = twitter.request(req)

# Raise an exception unless Twitter
# returned an OK result
unless res.is_a? Net::HTTPOK
doc = Hpricot(res.body)
raise "#: #"
end

# Return the request body
return res.body
end

# Make an API query and parse the output
#'/statuses/friends_timeline.xml',
doc = Hpricot(twitter(
'/statuses/user_timeline/17058142.xml',
{}
))

# If zero statuses were returned, then
# there are no new updates
if (doc/'status').length > 0
# Get the time of the first update
#last_id = (doc/'status id').first.inner_html

# Print in reverse order so newest are
# printed at the bottom of the list
st = (doc/'status').first
#user = (st/'user name').inner_html
text = (st/'text').inner_html
# prints text not directed at somebody else
if text !~ /^[[:blank:]]*@/
puts "
"
puts "#"
puts "

"
end
end
<< Los Mina en desacate | | Firefox 3.5 esta aquí >>

 
Related Links


Most Popular Stories


Movies Already Seen


Movie Queue


Older Stories


This site is powered by phpslash - Copyright © 1998 - 2003 and licensed under the GPL - No warranty is, has, or will be given in any way whatsoever to anyone.

Go 2 Top