Skip to content

Instantly share code, notes, and snippets.

@k1ife
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k1ife/b149935e37b759fdd665 to your computer and use it in GitHub Desktop.
Save k1ife/b149935e37b759fdd665 to your computer and use it in GitHub Desktop.
getting from Geonames.org only needed regions with theirs own names and saveing them into my table
require 'mysql2'
@title = []
@title_en = []
@title_alt = []
@title_str = ''
@title_en_str = ''
@title_alt_str = ''
geonames = Mysql2::Client.new(:host => "localhost", :username => "root", :password => 'pass', :database => 'geonames')
geonames.query("SELECT geonameId, iso_alpha2, name, currency, languages FROM countryinfo").each do |country|
@country_name = country['name']
rsite = Mysql2::Client.new(:host => "localhost", :username => "root", :password => 'pass', :database => 'rsite_dev')
rsite.query(" SELECT id FROM countries WHERE title_en = '#{@country_name}' ").each do |couid|
@country_id = couid['id']
rsite.close
@alpha2 = country['iso_alpha2']
@lang = country['languages'].split(",")[0]
geonames.query(" SELECT code, name, geonameid FROM admin1CodesAscii WHERE code LIKE '#{@alpha2}%' ").each do |region|
@regionid = region['geonameid']
@title_en_str = region['name'].gsub(/'/, "`")
@title_str = region['name'].gsub(/'/, "`")
geonames.query("SELECT isoLanguage, alternateName, isPreferredName FROM alternatename WHERE geonameid = '#{@regionid}' AND isoLanguage != 'link' AND isoLanguage != 'abbr' ").each do |alt|
@title_alt_str = @title_alt_str + ", " + alt['alternateName'].gsub(/'/, "`")
end
@title_alt_str = @title_str + ", " + @title_alt_str
rsite = Mysql2::Client.new(:host => "localhost", :username => "root", :password => 'pass', :database => 'rsite_dev')
rsite.query(" INSERT INTO regions SET country_id = '#{@country_id}', title = '#{@title_str}', title_en = '#{@title_en_str}', title_alt = '#{@title_alt_str}' ")
rsite.close
@title = []
@title_en = []
@title_alt = []
@title_str = ''
@title_en_str = ''
@title_alt_str = ''
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment