forked from smallwat3r/tubestatus.el
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tubestatus.el
132 lines (111 loc) · 4.94 KB
/
tubestatus.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
;;; tubestatus.el --- Get the London Tube service status -*- lexical-binding: t; -*-
;; Copyright (C) 2021 Matthieu Petiteau
;; Author: Matthieu Petiteau <[email protected]>
;; URL: https://github.com/smallwat3r/tubestatus.el
;; Package-Requires: ((emacs "26.1") (request "0.3.2"))
;; Version: 0.0.2
;; This file is NOT part of GNU Emacs.
;; GNU Emacs 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 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs 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 GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This package allows you to get the live service status of the London
;; Tube using the TfL API (https://api.tfl.gov.uk).
;;; Code:
(require 'cl-lib)
(require 'request)
(defvar tubestatus-tfl-api-url "https://api.tfl.gov.uk/line/%s/status"
"TfL line status API endpoint.")
(defvar tubestatus-tfl-lines
'(("Bakerloo" . "bakerloo")
("Central" . "central")
("Circle" . "circle")
("District" . "district")
("DLR" . "dlr")
("Elizabeth Line" . "mode/elizabeth-line")
("Liberty" . "liberty")
("Lioness" . "lioness")
("Hammersmith and City" . "hammersmith-city")
("Jubilee" . "jubilee")
("Metropolitan" . "metropolitan")
("Mildmay" . "mildmay")
("Nothern" . "northern")
("Picadilly" . "piccadilly")
("Suffragette" . "suffragette")
("Victoria" . "victoria")
("Waterloo and City" . "waterloo-city")
("Weaver" . "weaver")
("Windrush" . "windrush"))
"Association list of TfL Tube lines.")
(defface tubestatus-good-service-face
'((t :foreground "LimeGreen"))
"The tubestatus face used when there is a good service on a line."
:group 'tubestatus-faces)
(defface tubestatus-minor-delay-face
'((t :foreground "gold"))
"The tubestatus face used when there is minor delays on a line."
:group 'tubestatus-faces)
(defface tubestatus-major-delay-face
'((t :foreground "OrangeRed"))
"The tubestatus face used when there is major delays on a line."
:group 'tubestatus-faces)
(defface tubestatus-line-closed-face
'((t :foreground "SlateGrey"))
"The tubestatus face used when a line is closed."
:group 'tubestatus-faces)
(defface tubestatus-special-service-face
'((t :foreground "DodgerBlue"))
"The tubestatus face used when a line runs with a special service."
:group 'tubestatus-faces)
(defun tubestatus--render (data buffer)
"Render DATA in the tubestatus BUFFER."
(switch-to-buffer-other-window buffer)
(setq buffer-read-only nil)
(erase-buffer)
(let* ((content (elt data 0))
(status-content (elt (assoc-default 'lineStatuses content) 0))
(reason (assoc-default 'reason status-content))
(sev (assoc-default 'statusSeverity status-content))
(dot "\u2022"))
(insert (concat
(format "*%s*\n\nStatus:\n " (assoc-default 'name content))
(cond ((eql sev 10) (propertize dot 'face 'tubestatus-good-service-face))
((eql sev 20) (propertize dot 'face 'tubestatus-line-closed-face))
((eql sev 0) (propertize dot 'face 'tubestatus-special-service-face))
((>= sev 8) (propertize dot 'face 'tubestatus-minor-delay-face))
(t (propertize dot 'face 'tubestatus-major-delay-face)))
(format " %s" (assoc-default 'statusSeverityDescription status-content))
(if reason
(format "\n\nDetails:\n %s" reason)))))
(goto-char (point-min))
(setq buffer-read-only t))
(defun tubestatus--query (line)
"Get data from the TfL API for a specific LINE."
(request (format tubestatus-tfl-api-url line)
:parser 'json-read
:success
(cl-function
(lambda (&key data &allow-other-keys)
(let ((buffer (get-buffer-create "*tubestatus*")))
(tubestatus--render data buffer))))
:error
(cl-function
(lambda (&rest args &key error-thrown &allow-other-keys)
(message "An error has occurred while reaching the TfL API: %s"
error-thrown)))))
;;;###autoload
(defun tubestatus ()
"Get the current service status of a London Tube line."
(interactive)
(tubestatus--query
(cdr (assoc (completing-read "Select a line: " tubestatus-tfl-lines)
tubestatus-tfl-lines))))
(provide 'tubestatus)
;;; tubestatus.el ends here