diff options
-rw-r--r-- | public_transport/db-schedules.el | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/public_transport/db-schedules.el b/public_transport/db-schedules.el new file mode 100644 index 0000000..b4b7370 --- /dev/null +++ b/public_transport/db-schedules.el @@ -0,0 +1,91 @@ +;;; db-schedules.el --- Query DB community api for schedules -*- lexical-binding: t; -*- +;; +;; Copyright (C) 2024 Óscar Nájera +;; +;; Author: Óscar Nájera <hi@oscarnajera.com> +;; Maintainer: Óscar Nájera <hi@oscarnajera.com> +;; Created: June 26, 2024 +;; Modified: June 26, 2024 +;; Version: 0.0.1 +;; Keywords: abbrev bib c calendar comm convenience data docs emulations extensions faces files frames games hardware help hypermedia i18n internal languages lisp local maint mail matching mouse multimedia news outlines processes terminals tex tools unix vc wp +;; Homepage: https://github.com/titan/db-schedules +;; Package-Requires: ((emacs "29.1")) +;; +;; This file is not part of GNU Emacs. +;; +;;; Commentary: +;; +;; Query DB community api for schedules +;; +;;; Code: +;;; + + +(require 'request) +(require 'dash) + +(defun db-schedules-locations (query-string) + "Look for a station." + (interactive "sFuzzy search for a stop: ") + (request "https://v6.db.transport.rest/locations" + :params `((query . ,query-string) + (stops . true) + (poi . false)) + :parser 'json-parse-buffer + :success (cl-function + (lambda (&key data &allow-other-keys) + (let* ((output + (seq-keep (lambda (res) + (-let (((&hash "id" "name" "type") res)) + (when (string= type "stop") + (list name id)))) + data)) + (selected (completing-read "Which one first best? " (mapcar #'car output)))) + (assoc selected output #'string=)))))) + +(defun db-schedules--time (iso-string) + "From ISO-STRING time go only todays time." + (thread-last + (iso8601-parse iso-string) + (encode-time) + (format-time-string "%H:%M:%S"))) + +(defun db-schedules--departures (stop-id future-window) + (request (format "https://v6.db.transport.rest/stops/%s/departures" stop-id) + :params `((duration . ,future-window)) + :parser 'json-parse-buffer + :success (cl-function + (lambda (&key data &allow-other-keys) + (thread-last + (gethash "departures" data) + (seq-map-indexed + (lambda (row idx) + (-let* (((&hash "stop" "plannedWhen" "delay" "direction" "line") row) + ((&hash "name" "products") stop) + ((&hash "name" line-name) line) + type) + (maphash (lambda (k v) (when (eq v t) (push k type))) products) + (if (integerp delay) (push (format "Delayed %s" (seconds-to-string delay)) type)) + (list idx + (vector name + (db-schedules--time plannedWhen) + (string-join (list line-name direction) " ") + (string-join type " ")))))) + (setq tabulated-list-entries)) + (tabulated-list-print t))))) + +(with-current-buffer (get-buffer-create "*time schedule") + (tabulated-list-mode) + (setq tabulated-list-format + [("Stop" 25 nil) + ("time" 10 nil ) + ("direction" 25 nil) + ("extra" 25 nil)]) + + (tabulated-list-init-header) + + (db-schedules--departures "683258" 1000) + (display-buffer (current-buffer))) + +(provide 'db-schedules) +;;; db-schedules.el ends here |