Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Batch downloading twitter avatars from a CSV file containing handles
#!/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
You can’t perform that action at this time.