public
Created

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
namespace :hs do
desc "This rake task imports 1 geographic area only"
task :import_one_area => :environment do
require 'nokogiri'
require 'open-uri'
require 'mechanize'
 
agent = Mechanize.new
puts "Enter the page url from which you wish to import data: "
page = STDIN.gets
page2 = agent.get(page)
# page = agent.get("http://www.salatomatic.com/c/Southwest-Houston+82")
#loops through all places in each region
page2.search('.subtitleLink a').map{|a| page2.uri.merge a[:href]}.each do |uri|
begin
page4 = agent.get uri
# puts page4.at('.titleBM').text
# puts page4.at('.titleBM').next.text
name = page4.at('.titleBM').text
addy = page4.at('.titleBM').next.text
description = page4.at('hr').next.text.strip
more_info = page4.at('.normalLink').text
page5 = page4.link_with(:text => "Click here").click
website = page5.uri.to_s
street, city, state, zipcode = addy.match(/(.*), (.*), ([A-Z]{2}) (\d{5})/).captures
phone = more_info.match(/(\D\d{3}\D\s\d{3}\D\d{4})/).captures
puts name
puts addy
puts phone
puts website
puts description
puts "**********"
Place.create!(:name => name.strip,
:address => street.strip,
:city => city.strip,
:state => state.strip,
:zipcode => zipcode.strip,
:phone => phone[0].strip,
:website => website,
:category => "Masjid",
:description => description)
rescue
next
end
end
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.