Skip to content

Instantly share code, notes, and snippets.

@23inhouse
Created August 25, 2012 01:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save 23inhouse/3458744 to your computer and use it in GitHub Desktop.
Save 23inhouse/3458744 to your computer and use it in GitHub Desktop.
Find twitter and facebook accounts
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
# Twitter
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
# Facebook
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