-
-
Save 0xnbk/576998 to your computer and use it in GitHub Desktop.
Backup your tweets (Ruby)
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/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]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Backup your tweets (Ruby)
Are you a Ruby programmer who always wanted to be able to keep a backup of his tweets? If yes, you’ll love this snippet.
To run, save as twitterbackup.rb and launch from the command line:
ruby twitterbackup.rb yourtwittername