Skip to content

Instantly share code, notes, and snippets.

@kueda
Created May 6, 2014 18:11
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 kueda/937d12c280dc5ae8d1af to your computer and use it in GitHub Desktop.
Save kueda/937d12c280dc5ae8d1af to your computer and use it in GitHub Desktop.
Simple (too simple) script to retrieve info about social media for Nerds for Nature Monitor Change project
# This is a pretty dumb script that gets pics matching some tags from social
# media services and writes info about them to a local CSV file. Yes, it could
# be much better. Right now it only downloads *recent* data, not all data, so
# it's designed to run frequently. Improvements could include
# * writing to a single file, w/ de-duping
# * writing to a Google Spreadsheet
# * retrieval of all matches for Flickr and Instagram (impossible w/ Twitter)
require 'rubygems'
require 'twitter'
require 'pp'
require 'active_support/inflector'
require 'ostruct'
require 'flickraw'
require 'instagram'
require 'csv'
## CHANGE THESE ###############################################
TWITTER_KEY = 'xxx'
TWITTER_SECRET = 'xxx'
FLICKR_KEY = 'xxx'
FLICKR_SECRET = 'xxx'
INSTAGRAM_KEY = 'xxx'
###############################################################
class TwitterProvider
def self.method_missing(method, *args, &block)
@@instance ||= self.new
@@instance.send(method, *args, &block)
end
def search(q, &block)
client.search(q, :result_type => "recent").each do |tweet|
next unless tweet.media.first
block.yield(photo_from_api_response(tweet))
end
end
def client
return @client if @client
@client = Twitter::REST::Client.new do |config|
config.consumer_key = TWITTER_KEY
config.consumer_secret = TWITTER_SECRET
end
end
def photo_from_api_response(tweet)
return nil unless p = tweet.media.first
max_size = if p.sizes[:large] then "large"
elsif p.sizes[:medium] then "medium"
elsif p.sizes[:small] then "small"
end
return nil if max_size.nil? # skip if there's no reasonable image url
OpenStruct.new(
:response => tweet,
:image_url => "#{p.media_url}:#{max_size}",
:username => tweet.user.name,
:url => tweet.url,
:datetime => tweet.created_at
)
end
end
class FlickrProvider
def self.method_missing(method, *args, &block)
@@instance ||= self.new
@@instance.send(method, *args, &block)
end
def search(q, &block)
client.photos.search(:tags => [q], :per_page => 500, :extras => 'url_o,url_l,url_m,owner_name,date_taken').each do |photo|
block.yield(OpenStruct.new(
:response => photo,
:image_url => photo.to_hash["url_o"],
:username => photo.ownername,
:url => "http://flickr.com/photos/#{photo.owner}/#{photo.id}",
:datetime => Time.parse(photo.datetaken)
))
end
end
def client
if FlickRaw.api_key.nil?
FlickRaw.api_key = FLICKR_KEY
FlickRaw.shared_secret = FLICKR_SECRET
end
flickr
end
end
class InstagramProvider
def self.method_missing(method, *args, &block)
@@instance ||= self.new
@@instance.send(method, *args, &block)
end
def search(q, &block)
client.tag_recent_media(q).each do |photo|
block.yield(OpenStruct.new(
:response => photo,
:image_url => photo.images.standard_resolution.url,
:username => photo.user.username,
:url => photo.link,
:datetime => Time.at(photo.created_time.to_i)
))
end
end
def client
return @client if @client
Instagram.configure do |config|
config.client_id = INSTAGRAM_KEY
end
@client = Instagram
end
end
providers = [TwitterProvider, FlickrProvider, InstagramProvider]
tags = %w(morganfire01 morganfire02 morganfire03 morganfire04)
now = Time.now
path = "morganfire-photos-#{now.strftime('%Y-%m-%d')}-#{now.to_i}.csv"
CSV.open(path, 'w') do |csv|
csv << %w(provider tag datetime username image_url url)
providers.each do |provider|
provider_name = provider.name.underscore.split('_').first.capitalize
puts provider_name
tags.each do |tag|
provider.search(tag) do |photo|
next unless photo
puts "#{tag}\t#{photo.datetime}\t#{photo.username}\t#{photo.image_url}\t#{photo.url}"
csv << [provider_name, tag, photo.datetime.iso8601, photo.username, photo.image_url, photo.url]
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment