Created
August 25, 2012 01:58
-
-
Save 23inhouse/3458744 to your computer and use it in GitHub Desktop.
Find twitter and facebook accounts
This file contains 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
require 'mechanize' | |
require 'twitter' | |
require 'csv' | |
csv = CSV.open("./output.csv", "w") | |
# CSV Headers | |
csv << ["Winery Name", "Twitter Title", "Twitter Link", "Facebook Title", "Facebook Link"] | |
#wineries = ["novavitawines", "Wicks Estate Wines", "Izway Wines"] | |
CSV.foreach("./all_wineries.csv") do |row| | |
begin | |
winery = row.first | |
puts winery | |
agent = Mechanize.new | |
agent.get("http://www.google.com.au/search?q=site:twitter.com+#{winery}") | |
if link = agent.page.link_with(:text => /on Twitter$/) | |
twitter_title = link.text | |
twitter_link = (link.uri.to_s.match(/url\?q=([^\&]+)/) || [])[1] | |
end | |
agent = Mechanize.new | |
agent.get("http://www.google.com.au/search?q=site:facebook.com+#{winery}") | |
if link = agent.page.link_with(:text => /\| Facebook/) | |
fb_title = link.text | |
fb_link = (link.uri.to_s.match(/url\?q=([^\&]+)/) || [])[1] | |
end | |
new_row = [winery, twitter_title, twitter_link, fb_title, fb_link] | |
p new_row | |
csv << new_row | |
rescue | |
# don't die | |
end | |
sleep(rand(10)) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment