Last active
August 29, 2015 14:06
-
-
Save foozmeat/e650388a4407591a8978 to your computer and use it in GitHub Desktop.
Batch downloading twitter avatars from a CSV file containing handles
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/env ruby | |
require 'twitter' | |
require 'csv' | |
require 'fileutils' | |
require 'open-uri' | |
client = Twitter::REST::Client.new do |config| | |
config.consumer_key = "YOUR_CONSUMER_KEY" | |
config.consumer_secret = "YOUR_CONSUMER_SECRET" | |
config.access_token = "YOUR_ACCESS_TOKEN" | |
config.access_token_secret = "YOUR_ACCESS_SECRET" | |
end | |
FileUtils.mkdir_p('avatars') | |
CSV.foreach("query_result.csv") do |row| | |
handle = row[1] | |
existing_files = Dir.glob("avatars/#{handle}.*") | |
next if existing_files.count > 0 | |
puts "Looking up #{handle}" | |
begin | |
user = client.user(handle) | |
rescue Twitter::Error::TooManyRequests => error | |
puts "Sleeping for #{error.rate_limit.reset_in}" | |
sleep error.rate_limit.reset_in | |
retry | |
rescue Twitter::Error::NotFound | |
puts "Not found" | |
next | |
rescue Twitter::Error::Forbidden | |
puts "Account Suspended" | |
next | |
end | |
if user.profile_image_uri? | |
url = user.profile_image_uri(:original) | |
extension = File.extname(url) | |
filename = "avatars/#{handle}#{extension}" | |
puts "Downloading #{filename}" | |
File.open(filename, "wb") do |saved_file| | |
open(url, "rb") do |read_file| | |
saved_file.write(read_file.read) | |
end | |
end | |
end | |
sleep 1 # Slow things down | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment