Skip to content

Instantly share code, notes, and snippets.

@JEG2
Created May 7, 2011 22:44
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save JEG2/960922 to your computer and use it in GitHub Desktop.
Save JEG2/960922 to your computer and use it in GitHub Desktop.
require "twitter"
require "sunlight"
data_pos = DATA.pos
last_id = DATA.read.to_s[/\d+/]
last_id = last_id.to_i if last_id
DATA.reopen(__FILE__, "a+")
Twitter.configure do |config|
config.consumer_key = "CONSUMER_KEY"
config.consumer_secret = "CONSUMER_SECRET"
config.oauth_token = "OAUTH_TOKEN"
config.oauth_token_secret = "OAUTH_TOKEN_SECRET"
end
twitter = Twitter::Client.new
Sunlight::Base.api_key = "API_KEY"
loop do
puts "Searching..."
search = Twitter::Search.new.hashtag("whosmycongressperson")
search.since_id(last_id) if last_id
search.each do |tweet|
next unless last_id.nil? or tweet.id > last_id
response = ""
people = nil
if zip = tweet.text[/\d{5}/]
if people = Sunlight::Legislator.all_in_zipcode(zip)
people.each do |cp|
response << "#{cp.title}: #{cp.firstname} #{cp.lastname}\n"
end
end
elsif tweet.place and tweet.place.full_name
if people = Sunlight::Legislator.all_for(address: tweet.place.full_name)
people.values_at(:senior_senator, :junior_senator, :representative)
.compact
.each do |cp|
response << "#{cp.title}: #{cp.firstname} #{cp.lastname}\n"
end
end
else
response = "No geocoding information. " +
"Please retry and include your zip code."
end
if response.empty?
response = "Sorry, that information could not be retrieved."
end
begin
twitter.update( "@#{tweet.from_user}: #{response}",
in_reply_to_status_id: tweet.id )
puts "Responded to @#{tweet.from_user}."
rescue Twitter::Forbidden
puts "Error: #{tweet.inspect}"
end
DATA.truncate(data_pos)
DATA.puts tweet.id
last_id = tweet.id
end
sleep 10
end
__END__
66993867450363904
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment