Skip to content

Instantly share code, notes, and snippets.

@0x263b
Created October 18, 2015 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0x263b/eb109d7f8d15711b3ebb to your computer and use it in GitHub Desktop.
Save 0x263b/eb109d7f8d15711b3ebb to your computer and use it in GitHub Desktop.
A script to download all of a user's tweets into a json file
require 'oauth'
require 'json'
$TWITTER_CONSUMER_KEY = ""
$TWITTER_CONSUMER_SECRET = ""
$TWITTER_ACCESS_TOKEN = ""
$TWITTER_ACCESS_TOKEN_SECRET = ""
def prepare_access_token(oauth_token, oauth_token_secret)
consumer = OAuth::Consumer.new($TWITTER_CONSUMER_KEY, $TWITTER_CONSUMER_SECRET, {:site => "http://api.twitter.com", :scheme => :header })
token_hash = { :oauth_token => oauth_token, :oauth_token_secret => oauth_token_secret }
access_token = OAuth::AccessToken.from_hash(consumer, token_hash)
return access_token
end
def get_all_tweets(screen_name)
access_token = prepare_access_token($TWITTER_ACCESS_TOKEN, $TWITTER_ACCESS_TOKEN_SECRET)
all_tweets = []
response = access_token.request(:get, "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=#{screen_name}&count=200&exclude_replies=true&trim_user=true")
new_tweets = JSON.parse(response.body, {:symbolize_names => true})
all_tweets << new_tweets
all_tweets.flatten!
oldest = all_tweets.last[:id] - 1
while new_tweets.length > 0 do
puts "getting tweets before #{oldest}"
response = access_token.request(:get, "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=#{screen_name}&max_id=#{oldest}&count=200&exclude_replies=true&trim_user=true")
new_tweets = JSON.parse(response.body, {:symbolize_names => true})
all_tweets << new_tweets
all_tweets.flatten!
oldest = all_tweets.last[:id] - 1
puts "...#{all_tweets.length} tweets downloaded so far"
end
File.open("#{screen_name}_archive.json", "w") do |f|
f.write(JSON.pretty_generate(all_tweets))
end
end
get_all_tweets("svbtle")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment