From 73c731c8026dd31830fc8aeab4f65efe120c224e Mon Sep 17 00:00:00 2001 From: Oscar Najera Date: Thu, 29 Aug 2024 19:10:00 +0200 Subject: track deliveries --- elisp/delivery-track.el | 81 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 elisp/delivery-track.el (limited to 'elisp') diff --git a/elisp/delivery-track.el b/elisp/delivery-track.el new file mode 100644 index 0000000..ec24417 --- /dev/null +++ b/elisp/delivery-track.el @@ -0,0 +1,81 @@ +;;; delivery-track.el --- Track packages -*- lexical-binding: t; -*- +;; +;; Copyright (C) 2024 Óscar Nájera +;; +;; Author: Óscar Nájera +;; Maintainer: Óscar Nájera +;; 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 -- cgit v1.2.3