-
-
Save rmetzler/419655 to your computer and use it in GitHub Desktop.
This file contains 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
require 'time' | |
require 'json/pure' # require 'json/ext' segfaults for me | |
require 'twitter' | |
# There’s probably a way to get the path out of RubyGems itself, but I don’t know how, and the RubyGems | |
# documentation is a clusterfuck. )’= | |
require '/Users/elliottcable/.rvm/gems/ruby-1.9.2-preview1/gems/twitter-0.9.7/examples/helpers/config_store.rb' | |
######################################################################################################## Welcome! | |
# This file is ugly as fuck. Get over it, I was tired… not to mention that I hadn’t written any Ruby in, like, a | |
# year. C will do this to your head rather fast. | |
# | |
# Anyway. Usage is pretty simple… give it a status ID as an argument, and it will spit out the (ordered) JSON for | |
# all the tweets and retweets you’ve made since that particular status ID. Mind you, it spits out the JSON on | |
# STDOUT, but *does* require interaction on STDERR and STDIN: don’t redirect them, and don’t `cron` this. | |
# (Unless you want to scrape Twitter.com for the OAuth key, which is too much work for me. Sorry!) | |
# | |
# Oh, also, you have to have a ~/.twitter file (YAML) with your OAuth `token` and `secret`. You can create them: | |
# <http://twitter.com/apps/new> | |
# | |
# Suggested use: accumulatively write the JSON to a local backup file. You can sync this file up to S3 or Dropbox | |
# or whatever the hell you want. | |
# $ ruby ItBacksUpYourFuckingTweets.rb `\ | |
# tail -n 1 "$HOME/Documents/tweets.json" |\ | |
# awk -F':' '{ print $1 }'` \ | |
# 1>>"$HOME/Documents/tweets.json" | |
################################################################################################################# | |
STDERR.sync = true | |
config = ConfigStore.new("#{ENV['HOME']}/.twitter") | |
oauth = Twitter::OAuth.new(config['token'], config['secret']) | |
config.delete('rtoken', 'rsecret', 'atoken', 'asecret') | |
begin | |
# Fuck you, OAuth. All of this is necessary, just to log in… /-: | |
until config['atoken'] && config['asecret'] | |
if config['rtoken'] && config['rsecret'] | |
`open #{oauth.request_token.authorize_url}` | |
STDERR.puts "You're being redirected to your browser to authorize me." | |
STDERR.puts "Please type the PIN that Twitter gives you: " | |
pin = STDIN.gets.chomp | |
oauth.authorize_from_request(config['rtoken'], config['rsecret'], pin) | |
config.update({ | |
'atoken' => oauth.access_token.token, | |
'asecret' => oauth.access_token.secret, | |
}).delete('rtoken', 'rsecret') | |
else | |
config.update({ | |
'rtoken' => oauth.request_token.token, | |
'rsecret' => oauth.request_token.secret, | |
}) | |
end | |
end | |
twitter = Twitter::Base.new(oauth) | |
STDERR.print " Tweets (per 200): " | |
results = Array.new; request = Array.new; page = 1 | |
loop do; begin | |
request = twitter.user_timeline(count: 200, page: page, since_id: ARGV[0]) | |
results += request; page += 1 | |
request.empty? ? break : STDERR.print(page + 1 % 5 == 0 ? '!' : '.') | |
rescue Twitter::RateLimitExceeded | |
minutes = (Time.parse(twitter.rate_limit_status['reset_time']) - Time.now) / 60 | |
STDERR.print "\nYou're out of requests. Try again in #{minutes}m." | |
break | |
end; end | |
STDERR.print "\n" | |
STDERR.print "Retweets (per 200): " | |
request = Array.new; page = 1 | |
loop do; begin | |
request = twitter.retweeted_by_me(count: 200, page: page, since_id: ARGV[0]) | |
results += request; page += 1 | |
request.empty? ? break : STDERR.print(page + 1 % 5 == 0 ? '!' : '.') | |
rescue Twitter::RateLimitExceeded | |
minutes = (Time.parse(twitter.rate_limit_status['reset_time']) - Time.now) / 60 | |
STDERR.print "\nYou're out of requests. Try again in #{minutes}m." | |
break | |
end; end | |
STDERR.print "\n" | |
results.sort {|a, b| a["id"].to_i <=> b["id"].to_i }.each do |t| | |
# Yes, we *are* round-tripping from Twitter’s JSON, through a Ruby `Hash`, through the Twitter gem’s `Mash`, | |
# then through the `JSON` gem… back to JSON! Yay! It’s worth it for the Twitter gem’s OAuth handling, though. | |
puts "#{t['id']}: #{JSON.generate(t.to_hash)}" | |
end | |
config.delete('rtoken', 'rsecret', 'atoken', 'asecret') | |
# rescue Twitter::Unauthorized, OAuth::Unauthorized | |
# config.delete('rtoken', 'rsecret', 'atoken', 'asecret') and retry if | |
# config['rtoken'] || config['rsecret'] || config['atoken'] || config['asecret'] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment