require 'json'
content = File.open('country_codes_table.html').read #_lines[0..20].join
# content = %q{
#
#
# Code |
# Country name |
# Year |
# ccTLD |
# ISO 3166-2 |
# Notes |
#
#
# AD |
# Andorra |
# 1974 |
# .ad |
# ISO 3166-2:AD |
# |
#
#
# AE |
# United Arab Emirates |
# 1974 |
# .ae |
# ISO 3166-2:AE |
# |
#
#
# AF |
# Afghanistan |
# 1974 |
# .af |
# ISO 3166-2:AF |
# |
#
#
# }
codes = content.scan(/tt>(\w+)<\/tt/m)
names = content.scan(/title="([\w|\s|\'|\-|\,]+)"/m)
zipped = codes.zip(names)
hash = {}
zipped.each do |pair|
pair = pair.flatten
hash[pair.first.to_s.downcase] = pair.last
end
json = JSON.pretty_generate(hash)
File.open('ISO-3166-2_codes.en.json', 'w+') do |f|
f.puts json
end