Created
July 21, 2009 18:38
-
-
Save donpdonp/151508 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
#!/usr/bin/env ruby | |
require 'rexml/document' | |
require 'net/http' | |
require 'time' | |
def get_tweets(user,pass,page) | |
Net::HTTP.start('www.twitter.com') {|http| | |
req = Net::HTTP::Get.new("/statuses/user_timeline.xml?count=200?page=#{page}") | |
req.basic_auth user, pass | |
response = http.request(req) | |
} | |
end | |
if ARGV.size == 0 | |
puts "Usage: savetweets.rb <twitter username> <twitter password>" | |
exit | |
end | |
doc = REXML::Document.new | |
statuses = REXML::Element.new("statuses") | |
statuses.attributes["type"] = "array" | |
doc.elements << statuses | |
#http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses-user_timeline | |
#count. Optional. Specifies the number of statuses to retrieve. May not be greater than 200 | |
#Clients may request up to 3,200 statuses via the page and count parameters for timeline REST API methods. | |
page = 0 | |
loop do | |
page += 1 | |
warn "loading page #{page}" | |
page_response = get_tweets(ARGV[0], ARGV[1], page) | |
begin | |
page_doc = REXML::Document.new page_response.body | |
error = page_doc.root.elements["hash/error"] | |
if error | |
raise error.text | |
end | |
statuses = page_doc.root.elements["statuses"] | |
statuses.elements.each do |status| | |
statuses << status | |
end | |
warn "got #{statuses.size} tweets, #{doc.root.elements["statuses"].size} total" | |
rescue REXML::ParseException | |
puts "error parsing #{page_response.body}" | |
break | |
rescue RuntimeError=> e | |
puts "#{e}" | |
break | |
end | |
end | |
statuses = doc.root.elements["statuses"] | |
puts "#{statuses.size} tweets total" | |
if statuses.size > 0 | |
start_date = Time.parse(statuses.elements[1].elements["created_at"].text) | |
end_date = Time.parse(statuses.elements[statuses.elements.size].elements["created_at"].text) | |
filename = "#{start_date.strftime("%Y-%m-%d")}-#{end_date.strftime("%Y-%m-%d")}.twitter.xml" | |
warn "Saving tweets to #{filename}" | |
File.open(filename, "w") do |f| | |
f.write "#{doc}" | |
end | |
else | |
warn "No tweets retrieved." | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment