Posted By

beandr on 09/01/09


Tagged

script backup ruby twitter tweets


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

aleprieto
nb109


Backup your Twitter Tweets


 / Published in: Ruby
 

URL: http://twitter.com

To run, save as twitterbackup.rb:

ruby twitterbackup.rb yourtwittername

  1. #! /usr/bin/ruby
  2.  
  3. require 'rubygems'
  4. require 'json'
  5. require 'net/http'
  6. require 'uri'
  7.  
  8. class TwitterBackup
  9.  
  10. def backup(username)
  11. url = URI::parse('http://twitter.com')
  12.  
  13. page = 1
  14. loop do
  15. req = Net::HTTP::Get.new("/statuses/user_timeline.json?screen_name=#{username}&count=200&page=#{page}")
  16. res = Net::HTTP.start(url.host, url.port) {|http| http.request(req) }
  17.  
  18. if res.body.length > 2
  19. process_response(JSON.parse(res.body))
  20. else
  21. break
  22. end
  23.  
  24. page += 1
  25. end
  26. end
  27.  
  28. protected
  29. def process_response(response_json)
  30. response_json.each do |tweet|
  31. puts "#{Time.parse(tweet['created_at']).strftime("%A %d %B %Y at %I:%M%p")}, #{tweet['text']}, #{tweet['source']}, #{tweet['in_reply_to_screen_name']}"
  32. end
  33. end
  34. end
  35.  
  36. TwitterBackup.new.backup(ARGV[0])

Report this snippet  

You need to login to post a comment.