Return to Snippet

Revision: 17325
at September 1, 2009 05:02 by beandr


Initial Code
#! /usr/bin/ruby

require 'rubygems'
require 'json'
require 'net/http'
require 'uri'

class TwitterBackup
  
  def backup(username)
    url = URI::parse('http://twitter.com')

    page = 1
    loop do
      req = Net::HTTP::Get.new("/statuses/user_timeline.json?screen_name=#{username}&count=200&page=#{page}")
      res = Net::HTTP.start(url.host, url.port) {|http| http.request(req) }

      if res.body.length > 2
        process_response(JSON.parse(res.body))
      else 
        break
      end
      
      page += 1   
    end
  end
  
  protected
    def process_response(response_json)
        response_json.each do |tweet|
          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']}"
        end
    end
end

TwitterBackup.new.backup(ARGV[0])

Initial URL
http://twitter.com

Initial Description
To run, save as twitterbackup.rb:

ruby twitterbackup.rb yourtwittername

Initial Title
Backup your Twitter Tweets

Initial Tags
script, backup, ruby, twitter

Initial Language
Ruby