~ihabunek/triglav

ad298a565e14b26751f73ed1a17dbf3bee91fe47 — Ivan Habunek 1 year, 5 months ago d968c82
Add jadrolinija
M lib/triglav/gtfs.ex => lib/triglav/gtfs.ex +1 -0
@@ 70,4 70,5 @@ defmodule Triglav.Gtfs do
  def parse_route_type("1"), do: :subway
  def parse_route_type("2"), do: :train
  def parse_route_type("3"), do: :bus
  def parse_route_type("4"), do: :ferry
end

M lib/triglav/public_transport.ex => lib/triglav/public_transport.ex +1 -0
@@ 73,6 73,7 @@ defmodule Triglav.PublicTransport do
      "hzpp" -> {:ok, &import_hzpp/1}
      "zet" -> {:ok, &import_zet/1}
      "gpp_osijek" -> {:ok, &import_gpp_osijek/1}
      "jadrolinija" -> {:ok, &import_jadrolinija/1}
      _ -> {:error, :unknown_operator}
    end
  end

M lib/triglav/public_transport/import_gtfs.ex => lib/triglav/public_transport/import_gtfs.ex +32 -31
@@ 40,6 40,14 @@ defmodule Triglav.PublicTransport.ImportGtfs do
    import_from_archive(operator, target, opts)
  end

  def import_jadrolinija(opts \\ []) do
    operator = Operators.jadrolinija()
    url = "https://b2b.promet-info.hr/dc/b2b.gtfs.jl"
    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")


@@ 49,15 57,8 @@ defmodule Triglav.PublicTransport.ImportGtfs do
  def import_from_archive(operator, path, opts \\ []) do
    files = Gtfs.extract_archive(path)

    feed_ref =
      if Map.has_key?(files, :feed_info) do
        feed_info = parse_feed_info(files.feed_info)
        feed_info.feed_version
      else
        # If the archive does not contain `feed_info.txt` use the hash of it's
        # contents as the unique feed ref.
        FileUtils.md5sum!(path)
      end
    # Unique feed ref is calculated as a hash of loaded data.
    feed_ref = FileUtils.md5sum!(path)

    import_gtfs(files, operator, feed_ref, opts)
  end


@@ 106,28 107,17 @@ defmodule Triglav.PublicTransport.ImportGtfs do

  defp import_feed(operator, files, feed_ref) do
    now = DateTime.utc_now() |> DateTime.truncate(:second)

    if Map.has_key?(files, :feed_info) do
      feed_info = parse_feed_info(files.feed_info)

      Repo.insert!(%Feed{
        start_date: parse_gtfs_date(feed_info.feed_start_date),
        end_date: parse_gtfs_date(feed_info.feed_end_date),
        imported_at: now,
        operator_id: operator.id,
        ref: feed_info.feed_version,
        file_stats: file_stats(files),
        active: true
      })
    else
      Repo.insert!(%Feed{
        imported_at: now,
        operator_id: operator.id,
        ref: feed_ref,
        file_stats: file_stats(files),
        active: true
      })
    end
    {start_date, end_date} = get_feed_dates(files)

    Repo.insert!(%Feed{
      start_date: start_date,
      end_date: end_date,
      imported_at: now,
      operator_id: operator.id,
      ref: feed_ref,
      file_stats: file_stats(files),
      active: true
    })
  end

  defp file_stats(files) do


@@ 337,6 327,17 @@ defmodule Triglav.PublicTransport.ImportGtfs do
    end
  end

  defp get_feed_dates(files) do
    if Map.has_key?(files, :feed_info) do
      feed_info = parse_feed_info(files.feed_info)
      start_date = parse_gtfs_date(feed_info.feed_start_date)
      end_date = parse_gtfs_date(feed_info.feed_end_date)
      {start_date, end_date}
    else
      {nil, nil}
    end
  end

  defp parse_feed_info(path) do
    stream_csv(path) |> Enum.at(0)
  end

M lib/triglav/public_transport/operators.ex => lib/triglav/public_transport/operators.ex +11 -1
@@ 17,7 17,8 @@ defmodule Triglav.PublicTransport.Operators do
    get_or_insert!(%Operator{
      name: "Gradski Prijevoz Putnika Osijek",
      ref: "GPP Osijek",
      slug: "gpp_osijek"
      slug: "gpp_osijek",
      wikidata_id: "Q13056097"
    })
  end



@@ 30,6 31,15 @@ defmodule Triglav.PublicTransport.Operators do
    })
  end

  def jadrolinija() do
    get_or_insert!(%Operator{
      name: "Jadrolinija",
      ref: "Jadrolinija",
      slug: "jadrolinija",
      wikidata_id: "Q652564"
    })
  end

  def zet() do
    get_or_insert!(%Operator{
      name: "Zagrebački električni tramvaj",

M lib/triglav/public_transport/schemas/route.ex => lib/triglav/public_transport/schemas/route.ex +1 -1
@@ 14,7 14,7 @@ defmodule Triglav.PublicTransport.Schemas.Route do
    field :network, :string
    field :order_no, :integer
    field :ref, :string
    field :route_type, Ecto.Enum, values: [:bus, :train, :tram, :subway]
    field :route_type, Ecto.Enum, values: [:bus, :train, :tram, :subway, :ferry]

    has_many :variants, RouteVariant