[gnome-maps/wip/mlundblad/transit-plugin-gometro: 4/4] Add transit plugin for the GoMetro API



commit 7ebea84b0b452e395684b0da60264e254e06d08d
Author: Marcus Lundblad <ml update uu se>
Date:   Wed Feb 5 23:00:05 2020 +0100

    Add transit plugin for the GoMetro API
    
    Adds a plugin for the South African GoMetro transit
    journey planning API.

 src/org.gnome.Maps.src.gresource.xml |  1 +
 src/transitplugins/goMetro.js        | 99 ++++++++++++++++++++++++++++++++++++
 2 files changed, 100 insertions(+)
---
diff --git a/src/org.gnome.Maps.src.gresource.xml b/src/org.gnome.Maps.src.gresource.xml
index 961ac33f..d6866b84 100644
--- a/src/org.gnome.Maps.src.gresource.xml
+++ b/src/org.gnome.Maps.src.gresource.xml
@@ -113,6 +113,7 @@
     <file alias="geojsonvt/tile.js">tile.js</file>
     <file alias="geojsonvt/transform.js">transform.js</file>
     <file alias="geojsonvt/wrap.js">wrap.js</file>
+    <file>transitplugins/goMetro.js</file>
     <file>transitplugins/opendataCH.js</file>
     <file>transitplugins/openTripPlanner.js</file>
     <file>transitplugins/resrobot.js</file>
diff --git a/src/transitplugins/goMetro.js b/src/transitplugins/goMetro.js
new file mode 100644
index 00000000..cf3b1743
--- /dev/null
+++ b/src/transitplugins/goMetro.js
@@ -0,0 +1,99 @@
+/* -*- Mode: JS2; indent-tabs-mode: nil; js2-basic-offset: 4 -*- */
+/* vim: set et ts=4 sw=4: */
+/*
+ * Copyright (c) 2019 Marcus Lundblad
+ *
+ * GNOME Maps is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * GNOME Maps is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with GNOME Maps; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Marcus Lundblad <ml update uu se>
+ */
+
+/**
+ * This module implements a transit routing plugin for the South African
+ * GoMetro API.
+ * https://proserver.gometro.co.za/api/v1/docs/#multimodal-routing
+ */
+
+const BASE_URL = 'https://proserver.gometro.co.za/api';
+const API_VERSION = 'v1';
+
+const NATIVE_TIMEZONE = 'Africa/Cape_Town';
+
+const GLib = imports.gi.GLib;
+
+const Application = imports.application;
+const OpenTripPlanner = imports.transitplugins.openTripPlanner;
+const Utils = imports.utils;
+
+var GoMetro = class GoMetro extends OpenTripPlanner.OpenTripPlanner {
+    constructor() {
+        super({ baseUrl: BASE_URL });
+
+        this._tz = GLib.TimeZone.new(NATIVE_TIMEZONE);
+    }
+
+    _getPlanUrlWithLocations() {
+        let fromLocation = this._query.filledPoints[0].place.location;
+        let toLocation = this._query.filledPoints.last().place.location;
+
+        let result = '%s/%s/trips/%s,%s/%s,%s/%s/%s'.format(BASE_URL,
+                                                       API_VERSION,
+                                                       fromLocation.latitude,
+                                                       fromLocation.longitude,
+                                                       toLocation.latitude,
+                                                       toLocation.longitude,
+                                                       this._getDateAndTimeParams(),
+                                                       this._getModesParam());
+
+        Utils.debug('plan url: ' + result);
+
+        return result;
+    }
+
+    _getDateAndTimeParams() {
+        let time;
+        let date;
+        let result;
+
+        if (this._extendPrevious) {
+            let params = {};
+
+            this._addCommonParams(params);
+            time = params.time;
+            date = params.date;
+        } else {
+            time = this._query.time;
+            date = this._query.date;
+        }
+
+        Utils.debug('time: ' + time + ', date: ' + date);
+
+        if (time) {
+            return '%s/%s%s'.format(date, this._query.arriveBy ? '-' : '', time);
+        } else  {
+            let dateTime = GLib.DateTime.new_now(this._tz);
+
+            return '%s-%s-%s/%s:%s'.format(dateTime.get_year(),
+                                            dateTime.get_month(),
+                                            dateTime.get_day_of_month(),
+                                            dateTime.get_hour(),
+                                            dateTime.get_minute())
+        }
+    }
+
+    _getModesParam() {
+        // TODO: check allowed modes…
+        return 'RAIL,BUS,FERRY,TRAM,GONDOLA,WALK';
+    }
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]