Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save flov/292275 to your computer and use it in GitHub Desktop.
Save flov/292275 to your computer and use it in GitHub Desktop.

Revisions

  1. flov revised this gist Feb 2, 2010. 2 changed files with 24 additions and 8 deletions.
    8 changes: 0 additions & 8 deletions get all states for countries
    Original file line number Diff line number Diff line change
    @@ -1,8 +0,0 @@
    require 'open-uri'
    require 'hpricot'

    (1...267).each do |x|
    open("http://www.couchsurfing.org/get_option_values.html?cpaint_function=get_states_auto&cpaint_argument[]=#{x}") do |f|
    f.first.split('#').collect{|i| i.split('|')}.each{|i| puts "country_id: #{x}, state_id: #{i[0]}, state: #{i[1]}"}
    end
    end
    24 changes: 24 additions & 0 deletions get all states for countries and write them to yaml file
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    require 'open-uri'
    require 'yaml'

    h={}
    (1..267).each do |x|
    open("http://www.couchsurfing.org/get_option_values.html?cpaint_function=get_states_auto&cpaint_argument[]=#{x}") do |f|
    if f.first
    # anybody knows why it doesnt check if the string is empty before it executes the split ???
    # I went through all x which dont work and substitude f.first with
    # x!=112 and x!=122 and x!=124 and x!= 150 and x!=219 and x!= 240
    # which works. Pretty weird!
    f.first.split('#').collect{|i| i.split('|')}.each do |i|
    h["state_#{i[0]}"]={}
    h["state_#{i[0]}"]["id"] = i[0]
    h["state_#{i[0]}"]["state_id"] = i[1]
    h["state_#{i[0]}"]["country_id"] = x
    puts "country_id: #{x}, state_id: #{i[0]}, state: #{i[1]}"
    end
    end
    end
    end
    File.open('states.yml', 'w') do |f|
    f.write(h.to_yaml)
    end
  2. flov created this gist Feb 2, 2010.
    8 changes: 8 additions & 0 deletions get all states for countries
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    require 'open-uri'
    require 'hpricot'

    (1...267).each do |x|
    open("http://www.couchsurfing.org/get_option_values.html?cpaint_function=get_states_auto&cpaint_argument[]=#{x}") do |f|
    f.first.split('#').collect{|i| i.split('|')}.each{|i| puts "country_id: #{x}, state_id: #{i[0]}, state: #{i[1]}"}
    end
    end