Skip to content

Commit

Permalink
Extract importer to separate file.
Browse files Browse the repository at this point in the history
  • Loading branch information
lenart committed Oct 15, 2010
1 parent 765145b commit ccc4ecc
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 53 deletions.
55 changes: 2 additions & 53 deletions app/models/spot.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class Spot < ActiveRecord::Base

# RESERVED_PERMALINKS = %w(spot spots city cities admin login logout signup)

include SpotImporter

acts_as_mappable :default_units => :kms,
:default_formula => :sphere
Expand Down Expand Up @@ -48,59 +50,6 @@ def latlng

def full_location
[location, city].join(" ").strip
end

def self.import_from_rss(overwrite = false)
require 'nokogiri'
require 'open-uri'

doc = Nokogiri::XML(open('http://maps.google.com/maps/ms?ie=UTF8&hl=en&oe=UTF8&msa=0&output=georss&msid=115530814119313728840.00047d9535ae3d1256bef'))
# doc = Nokogiri::XML(open('lib/data/tocke.xml'))

spots = doc.search('item')

logger.info "Starting import of #{spots.size} items! #{Time.now}"
puts "Starting import of #{spots.size} items! #{Time.now}"

spots.each do |s|
spot = Spot.find_or_create_by_permalink(s.at('guid').content)

next if !overwrite && !spot.new_record?

spot.title = s.at('title').content
spot.permalink = s.at('guid').content
spot.category_id = 1

latlng = s.xpath('georss:point').first.content.gsub!(/\n/,'').strip!.split(' ')
spot.lat = latlng[0]
spot.lng = latlng[1]
spot.zoom = 17

spot.notes = s.at('description').inner_html
spot.author = s.at('author').content

spot.deleted = false
spot.open = !spot.notes.include?('zaklenjen')

# Clean up google html
spot.notes.gsub!(/<br>/i,"\n")
spot.notes.gsub!(/<\/?div(.|\n)*?>/i,"")
spot.notes.gsub!(/<\/?span(.|\n)*?>/i,"")
spot.notes.gsub!(/<\/?font(.|\n)*?>/i,"")
spot.notes.gsub!(/<\/?b(.|\n)*?>/i,"*")
spot.notes.gsub!('&nbsp;'," ")

if spot.save
logger.info "Added new spot #{spot.title}"
puts "Added new spot #{spot.title}"
else
logger.error "Spot not saved #{spot.permalink}, #{spot.title}"
# puts "Spot not saved #{spot.permalink}, #{spot.title}"
print "."
end
end

puts "Import completed. Done!"
end

def restore
Expand Down
72 changes: 72 additions & 0 deletions app/models/spot_importer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
module SpotImporter

require 'nokogiri'
require 'open-uri'

def self.included(klass)
klass.extend ClassMethods
end

module ClassMethods

# Import spots from Google Maps
def import_from_gmaps_rss(overwrite = false)

doc = Nokogiri::XML(open('http://maps.google.com/maps/ms?ie=UTF8&hl=en&oe=UTF8&msa=0&output=georss&msid=115530814119313728840.00047d9535ae3d1256bef'))
# doc = Nokogiri::XML(open('lib/data/tocke.xml'))

spots = doc.search('item')

logger.info "Starting import of #{spots.size} items! #{Time.now}"

spots.each do |s|
spot = Spot.find_or_create_by_permalink(s.at('guid').content)

next if !overwrite && !spot.new_record?

begin
spot.title = s.at('title').content
spot.permalink = s.at('guid').content
spot.category_id = 1

latlng = s.xpath('georss:point').first.content.gsub!(/\n/,'').strip!.split(' ')
spot.lat = latlng[0]
spot.lng = latlng[1]
spot.zoom = 17

spot.notes = s.at('description').inner_html
spot.author = s.at('author').content

spot.deleted = false
spot.open = !spot.notes.include?('zaklenjen')

# Clean up google html
spot.notes.gsub!(/<br>/i,"\n")
spot.notes.gsub!(/<\/?div(.|\n)*?>/i,"")
spot.notes.gsub!(/<\/?span(.|\n)*?>/i,"")
spot.notes.gsub!(/<\/?font(.|\n)*?>/i,"")
spot.notes.gsub!(/<\/?b(.|\n)*?>/i,"*")
spot.notes.gsub!('&nbsp;'," ")

if spot.save
logger.info "Added new spot #{spot.title}"
puts "Added new spot #{spot.title}"
else
logger.error "Spot not saved #{spot.permalink}, #{spot.title}"
# puts "Spot not saved #{spot.permalink}, #{spot.title}"
print "."
end
rescue => e
logger.error "Problem parsing the node #{spot.permalink}"
end

end

puts "Import completed. Done!"
end

# END ClassMethods
end


end
1 change: 1 addition & 0 deletions config/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

# Add additional load paths for your own custom dirs
# config.load_paths += %W( #{RAILS_ROOT}/extras )
# config.load_paths += Dir["#{RAILS_ROOT}/app/models/*"].find_all { |f| File.stat(f).directory? }

# Specify gems that this application depends on and have them installed with rake gems:install
config.gem "haml", :version => '>= 2.0.6'
Expand Down

0 comments on commit ccc4ecc

Please sign in to comment.