2021-04-12 12:21:07 +03:00
|
|
|
defmodule Mix.Tasks.DownloadCountryDatabase do
|
2022-07-28 15:59:39 +03:00
|
|
|
@moduledoc """
|
|
|
|
This task downloads the Country Lite database from DB-IP for self-hosted or development purposes.
|
|
|
|
Plausible Cloud runs a paid version of DB-IP with more detailed geolocation data.
|
|
|
|
"""
|
|
|
|
|
2021-04-12 12:21:07 +03:00
|
|
|
use Mix.Task
|
|
|
|
use Plausible.Repo
|
|
|
|
require Logger
|
|
|
|
|
|
|
|
# coveralls-ignore-start
|
|
|
|
|
|
|
|
def run(_) do
|
|
|
|
Application.ensure_all_started(:httpoison)
|
|
|
|
Application.ensure_all_started(:timex)
|
2024-07-23 10:02:14 +03:00
|
|
|
this_month = Date.utc_today()
|
2024-07-15 14:31:58 +03:00
|
|
|
last_month = Date.shift(this_month, month: -1)
|
2021-04-12 12:21:07 +03:00
|
|
|
this_month = this_month |> Date.to_iso8601() |> binary_part(0, 7)
|
|
|
|
last_month = last_month |> Date.to_iso8601() |> binary_part(0, 7)
|
|
|
|
this_month_url = "https://download.db-ip.com/free/dbip-country-lite-#{this_month}.mmdb.gz"
|
|
|
|
last_month_url = "https://download.db-ip.com/free/dbip-country-lite-#{last_month}.mmdb.gz"
|
|
|
|
Logger.info("Downloading #{this_month_url}")
|
|
|
|
res = HTTPoison.get!(this_month_url)
|
|
|
|
|
|
|
|
res =
|
|
|
|
case res.status_code do
|
|
|
|
404 ->
|
|
|
|
Logger.info("Got 404 for #{this_month_url}, trying #{last_month_url}")
|
|
|
|
HTTPoison.get!(last_month_url)
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
res
|
|
|
|
end
|
|
|
|
|
|
|
|
if res.status_code == 200 do
|
|
|
|
File.mkdir("priv/geodb")
|
2023-01-17 18:05:09 +03:00
|
|
|
File.write!("priv/geodb/dbip-country.mmdb.gz", res.body)
|
2021-04-12 12:21:07 +03:00
|
|
|
Logger.info("Downloaded and saved the database successfully")
|
|
|
|
else
|
|
|
|
Logger.error("Unable to download and save the database. Response: #{inspect(res)}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|