Created
May 14, 2012 15:49
-
-
Save lindblom/2694664 to your computer and use it in GitHub Desktop.
Ett lite snabbhack för att dra in alla städer med en viss storlek samt tillhörande län och spara dem till en databas.
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 "nokogiri" | |
#require "open-uri" | |
class CityImporter | |
attr_reader :wikipedia_cities, :wikipedia_municipalities | |
def import_wikipedia_cities | |
@wikipedia_cities ||= [] | |
wikipedia_cities_page = Nokogiri::HTML(open('http://sv.wikipedia.org/wiki/Lista_%C3%B6ver_Sveriges_t%C3%A4torter')) | |
wikipedia_cities_page.css("table.sortable.wikitable tr").each_with_index do |row, i| | |
next if i == 0 | |
pop = row.css("td")[2].children.last.text.gsub(/[^0-9]+/, "").to_i | |
if pop > 1999 | |
@wikipedia_cities << [row.css("td")[0].text, row.css("td")[1].text] | |
puts "added: #{@wikipedia_cities.last.first}, #{@wikipedia_cities.last.last}\n" | |
end | |
end | |
puts "All done!" | |
end | |
def import_wikipedia_municipalities | |
@wikipedia_municipalities ||= [] | |
wikipedia_municipalities_page = Nokogiri::HTML(open('http://sv.wikipedia.org/wiki/Sveriges_kommuner_listade_per_l%C3%A4n')) | |
wikipedia_municipalities_page.css("table.sortable.wikitable tr").each_with_index do |row, i| | |
next if i == 0 | |
@wikipedia_municipalities << [row.css("td")[1].text, row.css("td")[2].text] | |
puts "added: #{@wikipedia_municipalities.last.first}, #{@wikipedia_municipalities.last.last}\n" | |
end | |
puts "All done!" | |
end | |
def add_to_db | |
states = wikipedia_municipalities.uniq { |m| m[1] } | |
states.each do |state| | |
puts "added: #{state.last} to database.\n" if State.create(name: state.last) | |
end | |
@wikipedia_cities.each do |city| | |
state = State.find_by_slug(get_state_slug(city)) | |
puts "Added: #{city[0]}" if City.create(name: city[0], state: state) | |
end | |
end | |
def initialize(autorun = true) | |
if autorun | |
import_wikipedia_cities | |
import_wikipedia_municipalities | |
end | |
puts "#{@wikipedia_municipalities.count} municipalities and #{@wikipedia_cities.count} cities added.\nRun 'add_to_db' to add to database.\n" | |
end | |
private | |
def get_state_slug(city) | |
@wikipedia_municipalities[@wikipedia_municipalities.index { |value| value[0]==city[1] }][1].parameterize | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment