Skip to content

Instantly share code, notes, and snippets.

@paulvictor
Created July 12, 2012 11:15
Show Gist options
  • Save paulvictor/3097515 to your computer and use it in GitHub Desktop.
Save paulvictor/3097515 to your computer and use it in GitHub Desktop.
Tv show pages from twitter
#!/usr/bin/env ruby
require 'csv'
require 'json'
require 'oauth'
class Credentials
def self.next
@@read_creds ||= CSV.foreach("./creds.csv") do |row|
@@vals ||= []
@@vals << {:consumer_key => row[0], :consumer_secret => row[1], :token => row[2], :secret => row[3]}
end
@@idx ||= 0
ret_val = @@vals[@@idx]
@@idx = (@@idx + 1 >= @@vals.size) ? 0 : (@@idx + 1)
ret_val
end
end
#show name is the pattern
def match_all_words(pattern, str)
all_words = pattern.split(/[^\w]/)
all_words.reduce(true){|memo, w| memo && str.match(Regexp.new(w, Regexp::IGNORECASE))}
end
CSV.foreach("./tv_shows.csv") do |row|
begin
show_name = row[1]
puts show_name
creds = Credentials.next
at = OAuth::AccessToken.new(OAuth::Consumer.new(creds[:consumer_key], creds[:consumer_secret], :site => "https://api.twitter.com"), creds[:token], creds[:secret])
resp = at.get("/1/users/search.json?q=#{URI.encode(show_name)}&include_entities=true", {})
results = JSON.parse(resp.body)
results.select!{|res| res['description'] && res['description'].match(/official/i) && match_all_words(show_name, res['description']) && res['url'] && match_all_words(show_name, res['url'])}
#results.each{|res| puts res['url']}
the_result = results.inject{|memo, res| memo['followers_count'].to_i > res['followers_count'].to_i ? memo : res}
puts "#{show_name} : twitter.com/#{the_result['screen_name']}"
rescue SignalException => e
raise e
rescue Exception => xcp
puts xcp.class
puts xcp.message
puts xcp.backtrace
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment