blob: ec24417b10bbb4a416f53fe60e54a412851ab4f1 (
plain)
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
|
;;; delivery-track.el --- Track packages -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2024 Óscar Nájera
;;
;; Author: Óscar Nájera <hi@oscarnajera.com>
;; Maintainer: Óscar Nájera <hi@oscarnajera.com>
;; Created: August 28, 2024
;; Modified: August 28, 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/delivery-track
;; Package-Requires: ((emacs "27.1"))
;;
;; This file is not part of GNU Emacs.
;;
;;; Commentary:
;;
;; Track packages
;;
;;; Code:
(require 'org)
(require 'org-id)
(require 'url)
(defvar url-http-end-of-headers)
(defun delivery-track--dhl-de (track-id buffer callback)
"Async request to API using TRACK-ID and process with CALLBACK.
It writes into the org-node in BUFFER."
(let* ((url-request-method "GET")
(params `((piececode ,track-id)
("noRedirect" true)
(language "en"))))
(thread-first
"https://www.dhl.de/int-verfolgen/data/search?"
(concat (url-build-query-string params))
(url-retrieve callback (list buffer)))))
(defun delivery-track-entry--dhl (_request-status buffer)
"Parse response from DHL and write in in BUFFER."
(goto-char url-http-end-of-headers)
(let* ((shipment-info (thread-last
(json-parse-buffer)
(gethash "sendungen")
(seq-find
(lambda (item)
(eq t (gethash "hasCompleteDetails" item))))))
(history (thread-last
shipment-info
(gethash "sendungsdetails")
(gethash "sendungsverlauf")))
(id (gethash "id" shipment-info))
(status (gethash "kurzStatus" history))
(events (reverse (gethash "events" history))))
(with-current-buffer buffer
(goto-char (org-find-entry-with-id id))
(org-entry-put nil "status" status)
(org-next-visible-heading 1)
(seq-let (level _rlevel _todo _prio headline)
(org-heading-components)
(when (and (= level 2)
(string= "Shipment reverse history" headline))
(org-cut-subtree))
(insert "** Shipment reverse history\n")
(seq-doseq (event events)
(thread-first
(gethash "datum" event)
(parse-time-string)
(encode-time)
(org-insert-timestamp t t))
(insert " " (gethash "status" event) "\n"))))))
(defun delivery-track-update-dhl (track-id)
"Update tracking information for DHL TRACK-ID."
(interactive (list (read-string "What is the tracking id? " (org-id-get))))
(delivery-track--dhl-de track-id (current-buffer) #'delivery-track-entry--dhl))
(provide 'delivery-track)
;;; delivery-track.el ends here
|