~ihabunek/triglav

9944d3782cb2f665532c2af1561b5b4e536d1955 — Ivan Habunek 1 year, 10 months ago 76b0a9c
Add GPP Osijek data source
M config/dev.exs => config/dev.exs +4 -0
@@ 13,6 13,10 @@ config :triglav, Triglav.Repo,
  show_sensitive_data_on_connection_error: true,
  pool_size: 10

config :triglav, Triglav.PublicTransport.Npt,
  username: System.get_env("NTP_USERNAME"),
  password: System.get_env("NTP_PASSWORD")

# For development, we disable any cache and enable
# debugging and code reloading.
#

M config/runtime.exs => config/runtime.exs +4 -0
@@ 69,6 69,10 @@ if config_env() == :prod do
    api_key: System.get_env("MAILGUN_API_KEY"),
    domain: System.get_env("MAILGUN_DOMAIN")

  config :triglav, Triglav.PublicTransport.Npt,
    username: System.get_env("NTP_USERNAME"),
    password: System.get_env("NTP_PASSWORD")

  config :sentry,
    dsn: System.get_env("SENTRY_DSN"),
    environment_name: :prod,

M lib/triglav/public_transport.ex => lib/triglav/public_transport.ex +2 -0
@@ 14,6 14,7 @@ defmodule Triglav.PublicTransport do
  defdelegate import_autotrolej(opts \\ []), to: Triglav.Autotrolej, as: :import
  defdelegate import_hzpp(opts \\ []), to: Triglav.PublicTransport.ImportGtfs
  defdelegate import_zet(opts \\ []), to: Triglav.PublicTransport.ImportGtfs
  defdelegate import_gpp_osijek(opts \\ []), to: Triglav.PublicTransport.ImportGtfs

  @doc """
  For each operator, check if a new feed is available. If so, update the feed,


@@ 62,6 63,7 @@ defmodule Triglav.PublicTransport do
      "autotrolej" -> {:ok, &import_autotrolej/1}
      "hzpp" -> {:ok, &import_hzpp/1}
      "zet" -> {:ok, &import_zet/1}
      "gpp_osijek" -> {:ok, &import_gpp_osijek/1}
      _ -> {:error, :unknown_operator}
    end
  end

M lib/triglav/public_transport/import_gtfs.ex => lib/triglav/public_transport/import_gtfs.ex +9 -0
@@ 11,6 11,7 @@ defmodule Triglav.PublicTransport.ImportGtfs do
  alias Triglav.Helpers.{Counter, FileUtils}
  alias Triglav.Http
  alias Triglav.PublicTransport
  alias Triglav.PublicTransport.Npt
  alias Triglav.PublicTransport.Operators
  alias Triglav.PublicTransport.Schemas.{Feed, FileStats, Operator, Platform, Route, RouteVariant}
  alias Triglav.Repo


@@ 31,6 32,14 @@ defmodule Triglav.PublicTransport.ImportGtfs do
    import_from_url(operator, url, opts)
  end

  def import_gpp_osijek(opts \\ []) do
    operator = Operators.gpp_osijek()
    url = "https://b2b.promet-info.hr/dc/b2b.gtfs.osijekgpp"
    target = Triglav.tmp_dir!() <> "/#{operator.slug}.zip"
    Npt.download(url, target)
    import_from_archive(operator, target, opts)
  end

  def import_from_url(operator, url, opts \\ []) do
    target = Triglav.tmp_dir!()
    path = Http.download!(url, target, overwrite: true, filename: "#{operator.slug}.zip")

A lib/triglav/public_transport/npt.ex => lib/triglav/public_transport/npt.ex +19 -0
@@ 0,0 1,19 @@
defmodule Triglav.PublicTransport.Npt do
  @moduledoc """
  Download data from NPT (https://www.promet-info.hr/).
  """
  use Tesla

  plug Tesla.Middleware.BasicAuth,
    username: Application.get_env(:triglav, __MODULE__) |> Keyword.fetch!(:username),
    password: Application.get_env(:triglav, __MODULE__) |> Keyword.fetch!(:password)

  plug Tesla.Middleware.Timeout, timeout: 10_000
  plug Triglav.Tesla.Middleware.ErrrorOnStatus
  plug Triglav.Tesla.Middleware.Logger

  def download(url, target) do
    response = get!(url)
    File.write!(target, response.body)
  end
end

M lib/triglav/public_transport/operators.ex => lib/triglav/public_transport/operators.ex +8 -0
@@ 13,6 13,14 @@ defmodule Triglav.PublicTransport.Operators do
    })
  end

  def gpp_osijek() do
    get_or_insert!(%Operator{
      name: "Gradski Prijevoz Putnika Osijek",
      ref: "GPP Osijek",
      slug: "gpp_osijek"
    })
  end

  def hzpp() do
    get_or_insert!(%Operator{
      name: "HŽ Putnički prijevoz",