~ihabunek/triglav

219eff60b24b3856a707e8a2102b0f5ebd73ed62 — Ivan Habunek a month ago 113d8b3 autotrolej-gtfs
wip
M lib/triglav/public_transport.ex => lib/triglav/public_transport.ex +1 -1
@@ 12,7 12,7 @@ defmodule Triglav.PublicTransport do
  require Logger

  defdelegate generate_mappings(feed_id), to: Mappings, as: :generate
  defdelegate import_autotrolej(opts \\ []), to: Triglav.Autotrolej, as: :import
  defdelegate import_autotrolej(opts \\ []), to: Triglav.PublicTransport.ImportGtfs
  defdelegate import_gp_sibenik(opts \\ []), to: Triglav.PublicTransport.ImportGtfs
  defdelegate import_gpp_osijek(opts \\ []), to: Triglav.PublicTransport.ImportGtfs
  defdelegate import_hzpp(opts \\ []), to: Triglav.PublicTransport.ImportGtfs

M lib/triglav/public_transport/import_gtfs.ex => lib/triglav/public_transport/import_gtfs.ex +23 -2
@@ 3,8 3,9 @@ defmodule Triglav.PublicTransport.ImportGtfs do
  Importdata from GTFS files into `Triglav.PublicTransport` schemas.

  Sources:
    - ZET - https://zet.hr/gtfs2
    - HŽ - http://www.hzpp.hr/Media/Default/GTFS/GTFS_files.zip
    - Autotrolej  https://gt.autotrolej.hr/google-transit.zip
    - HŽ          http://www.hzpp.hr/Media/Default/GTFS/GTFS_files.zip
    - ZET         https://zet.hr/gtfs2
  """
  alias NimbleCSV.RFC4180, as: CsvParser
  alias Triglav.Gtfs


@@ 20,6 21,12 @@ defmodule Triglav.PublicTransport.ImportGtfs do

  require Logger

  def import_autotrolej(opts \\ []) do
    operator = Operators.autotrolej()
    url = "https://gt.autotrolej.hr/google-transit.zip"
    import_from_url(operator, url, opts)
  end

  def import_zet(opts \\ []) do
    operator = Operators.zet()
    url = "https://zet.hr/gtfs-scheduled/latest"


@@ 71,6 78,20 @@ defmodule Triglav.PublicTransport.ImportGtfs do
  defp import_from_archive(operator, path, opts) do
    files = Gtfs.extract_archive(path)

    # Hack to fix broken autotrolej CSV - properly escape double quotes
    if operator.slug == "autotrolej" do
      stops_txt = File.read!(files.stops)

      stops_txt =
        String.replace(
          stops_txt,
          ~s(1863,Paveki 3  \"Luje\",),
          ~s(1863,"Paveki 3  ""Luje""",)
        )

      File.write!(files.stops, stops_txt)
    end

    # Unique feed ref is calculated as a hash of loaded data.
    feed_ref = FileUtils.md5sum!(path)


M lib/triglav/public_transport/mappings.ex => lib/triglav/public_transport/mappings.ex +4 -11
@@ 39,17 39,10 @@ defmodule Triglav.PublicTransport.Mappings do
  end

  def delete(feed_id) do
    {count, nil} = Repo.delete_all(where(PlatformMapping, feed_id: ^feed_id))
    Logger.debug("Deleted #{count} pt_platform_mappings")

    {count, nil} = Repo.delete_all(where("pt_route_mapped_route_relations", feed_id: ^feed_id))
    Logger.debug("Deleted #{count} pt_route_mapped_route_relations")

    {count, nil} = Repo.delete_all(where("pt_route_mapped_route_master_relations", feed_id: ^feed_id))
    Logger.debug("Deleted #{count} pt_route_mapped_route_master_relations")

    {count, nil} = Repo.update_all(where(RouteVariant, feed_id: ^feed_id), set: [mapped_relation_id: nil])
    Logger.debug("Deleted #{count} pt_route_variants")
    Repo.delete_all_log(where(PlatformMapping, feed_id: ^feed_id))
    Repo.delete_all_log(where("pt_route_mapped_route_relations", feed_id: ^feed_id))
    Repo.delete_all_log(where("pt_route_mapped_route_master_relations", feed_id: ^feed_id))
    Repo.update_all(where(RouteVariant, feed_id: ^feed_id), set: [mapped_relation_id: nil])
  end

  # Match imported PublicTransport platforms to platforms in OSM.

M lib/triglav/repo.ex => lib/triglav/repo.ex +14 -1
@@ 3,6 3,8 @@ defmodule Triglav.Repo do
    otp_app: :triglav,
    adapter: Ecto.Adapters.Postgres

  require Logger

  def select!(sql, params \\ []) do
    {:ok, result} = query(sql, params)



@@ 50,6 52,17 @@ defmodule Triglav.Repo do
  end

  @doc """
  Calls Ecto.Repo.delete_all and logs how many rows were deleted.
  """
  @spec delete_all_log(Ecto.Query.t(), keyword()) :: {non_neg_integer, nil | [term]}
  def delete_all_log(query, opts \\ []) do
    {table_name, _} = query.from.source
    {count, result} = delete_all(query, opts)
    Logger.debug("Deleted #{count} rows from #{table_name}")
    {count, result}
  end

  @doc """
  Runs the given function inside a transaction.

  This function is a wrapper around `Ecto.Repo.transaction`, with the following differences:


@@ 62,7 75,7 @@ defmodule Triglav.Repo do

  This function accepts the same options as `Ecto.Repo.transaction/2`.
  """
  @spec transact((() -> result) | (module -> result), Keyword.t()) :: result
  @spec transact((-> result) | (module -> result), Keyword.t()) :: result
        when result: :ok | {:ok, any} | :error | {:error, any}
  def transact(fun, opts \\ []) do
    transaction_result =