Skip to content

Commit

Permalink
update iso data
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslandoga committed Jul 2, 2024
1 parent 12c2005 commit 296ab6c
Show file tree
Hide file tree
Showing 7 changed files with 1,268 additions and 759 deletions.
11 changes: 11 additions & 0 deletions lib/location/subdivision.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ defmodule Location.Subdivision do
:ets.insert(ets, {entry["code"], to_struct(entry)})
end)

File.read!(restore_source_file())
|> Jason.decode!()
|> Enum.each(fn entry ->
entry = translate_entry(translations, entry)
:ets.insert(ets, {entry["code"], to_struct(entry)})
end)

File.read!(override_source_file())
|> Jason.decode!()
|> Enum.each(fn entry ->
Expand Down Expand Up @@ -72,6 +79,10 @@ defmodule Location.Subdivision do
Application.app_dir(:location, "priv/iso_3166-2.json")
end

defp restore_source_file() do
Application.app_dir(:location, "priv/restore/iso_3166-2.json")
end

defp translations_file() do
Application.app_dir(:location, "priv/iso_3166-2.en-translations.json")
end
Expand Down
47 changes: 42 additions & 5 deletions mix_tasks/update_iso_data.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,47 @@ defmodule Mix.Tasks.UpdateIsoData do
%HTTPoison.Response{status_code: 200, body: countries} = HTTPoison.get!(@countries_src)
File.write!(@countries_dest, countries)

%HTTPoison.Response{status_code: 200, body: subdivisions} = HTTPoison.get!(@subdivisions_src)
%{"3166-2" => subdivisions} = Jason.decode!(subdivisions)
subdivisions = Enum.map(fn subdivision -> Map.delete(subdivision, "parent") end)
subdivisions = Jason.encode_to_iodata!(%{"3166-2" => subdivisions})
File.write!(@subdivisions_dest, subdivisions)
%HTTPoison.Response{status_code: 200, body: new_subdivisions} =
HTTPoison.get!(@subdivisions_src)

%{"3166-2" => new_subdivisions} = Jason.decode!(new_subdivisions)

new_subdivisions =
Enum.map(new_subdivisions, fn subdivision -> Map.delete(subdivision, "parent") end)

new_subdivisions_codes = MapSet.new(new_subdivisions, fn %{"code" => code} -> code end)

# ensures no codes are deleted
# if a code no longer exists in new subdivisions, we put it in restore folder
restored_subdivisions_path = Application.app_dir(:location, "/priv/restore/iso_3166-2.json")

new_restored_subdivisions =
if File.exists?(@subdivisions_dest) do
File.read!(@subdivisions_dest)
|> Jason.decode!()
|> Map.fetch!("3166-2")
|> Enum.reject(fn %{"code" => code} -> MapSet.member?(new_subdivisions_codes, code) end)
end

prev_restored_subdivisions =
if File.exists?(restored_subdivisions_path) do
Jason.decode!(File.read!(restored_subdivisions_path))
end

restored_subdivisions =
(new_restored_subdivisions || []) ++ (prev_restored_subdivisions || [])

restored_subdivisions =
restored_subdivisions
|> Enum.uniq_by(fn %{"code" => code} -> code end)
|> Enum.sort_by(fn %{"code" => code} -> code end)

File.write!(
restored_subdivisions_path,
Jason.encode_to_iodata!(restored_subdivisions, pretty: true)
)

new_subdivisions = Jason.encode_to_iodata!(%{"3166-2" => new_subdivisions}, pretty: true)
File.write!(@subdivisions_dest, new_subdivisions)
end
end
7 changes: 5 additions & 2 deletions priv/iso_3166-1.json
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,7 @@
{
"alpha_2": "IR",
"alpha_3": "IRN",
"common_name": "Iran",
"flag": "🇮🇷",
"name": "Iran, Islamic Republic of",
"numeric": "364",
Expand Down Expand Up @@ -953,6 +954,7 @@
{
"alpha_2": "LA",
"alpha_3": "LAO",
"common_name": "Laos",
"flag": "🇱🇦",
"name": "Lao People's Democratic Republic",
"numeric": "418"
Expand Down Expand Up @@ -1653,6 +1655,7 @@
{
"alpha_2": "SY",
"alpha_3": "SYR",
"common_name": "Syria",
"flag": "🇸🇾",
"name": "Syrian Arab Republic",
"numeric": "760"
Expand Down Expand Up @@ -1746,9 +1749,9 @@
"alpha_2": "TR",
"alpha_3": "TUR",
"flag": "🇹🇷",
"name": "Turkey",
"name": "Türkiye",
"numeric": "792",
"official_name": "Republic of Turkey"
"official_name": "Republic of Türkiye"
},
{
"alpha_2": "TV",
Expand Down
Loading

0 comments on commit 296ab6c

Please sign in to comment.