aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2024-06-27 01:55:52 +0200
committerOscar Najera <hi@oscarnajera.com>2024-06-27 01:55:52 +0200
commitb96db170cbaabd085c295794f96a0ff7a79bde31 (patch)
treeae805dec305875ca552bfd8b9e796fc14a04ab93
parent90602579fb543a15b689de5b6bcc2ad41ad47c04 (diff)
downloadscratch-b96db170cbaabd085c295794f96a0ff7a79bde31.tar.gz
scratch-b96db170cbaabd085c295794f96a0ff7a79bde31.tar.bz2
scratch-b96db170cbaabd085c295794f96a0ff7a79bde31.zip
Query German public transport departures
-rw-r--r--public_transport/db-schedules.el91
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