;;; bom.el -- Australian weather forecast from the Bureau -*- lexical-binding: t -*- ;; Copyright (C) 2023 Free Software Foundation, Inc. ;; Author: Yuchen Pei ;; Package-Requires: ((emacs "28.2") (web-server "0.0.2")) ;; This file is part of bom.el. ;; bom.el is free software: you can redistribute it and/or modify it ;; under the terms of the GNU Affero General Public License as ;; published by the Free Software Foundation, either version 3 of the ;; License, or (at your option) any later version. ;; bom.el 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 Affero General ;; Public License for more details. ;; You should have received a copy of the GNU Affero General Public ;; License along with bom.el. If not, see . ;;; Commentary: ;; Australian weather forecast from the Bureau. A client that ;; downloads weather forecast and serves them using emacs-web-server. ;;; Code: (require 'hierarchy) (require 'web-server) (require 'ox-publish) (defvar bom-state-files '((act . "IDN11060") (nsw . "IDN11060") (nt . "IDD10207") (qld . "IDQ11295") (sa . "IDS10044") (tas . "IDT16710") (vic . "IDV10753") (wa . "IDW14199")) "Alist of states and territories and their corresponding filenames on the BOM FTP server, see .") (defun bom-api (state) "Get weather forecast data of STATE from the BOM FTP." (if-let ((filename (alist-get (intern (downcase state)) bom-state-files))) (let* ((buffer (find-file-noselect (format "/ftp:anonymous@ftp.bom.gov.au:/anon/gen/fwo/%s.xml" filename))) (result (with-current-buffer buffer (libxml-parse-xml-region (point-min) (point-max))))) (kill-buffer buffer) result) (user-error "State %s not found" state))) (defun bom-get-areas (resp) "Given an API response RESP, get all areas." (dom-children (dom-by-tag resp 'forecast))) (defvar bom-areas nil "List of areas with forecasts.") (defvar bom-server nil "The server object.") (defvar bom-port 9000 "The port number of the server.") (defun bom-area-parent (area) "Search `bom-areas' for the parent of AREA. Used as parentfn for hierarchy." (let ((parent-aac (dom-attr area 'parent-aac))) (cl-find-if (lambda (area) (equal (dom-attr area 'aac) parent-aac)) bom-areas))) (defun bom-hyphenate-downcase (name) "Downcase NAME and replace all space with hyphens." (replace-regexp-in-string " " "-" (downcase name))) (defun bom-format-area (area level) "Format an AREA of LEVEL." (format "%s %s :PROPERTIES: :CUSTOM_ID: %s :END: %s " (make-string level ?*) (dom-attr area 'description) (bom-hyphenate-downcase (dom-attr area 'description)) (if (dom-children area) (concat "\n" (bom-format-forecasts (dom-children area))) ""))) (defun bom-area-lessp (a1 a2) "Compare two areas A1 and A2 based on their AAC identifier." (string-lessp (dom-attr a1 'aac) (dom-attr a2 'aac))) (defun bom-format-forecasts (forecasts) "Format a list of FORECASTS." (mapconcat 'bom-format-forecast forecasts "\n")) (defun bom-format-forecast (forecast) "Format a FORECAST period." (let ((date (substring (dom-attr forecast 'start-time-local) 5 10)) (data (dom-children forecast)) (min-temp) (max-temp) (precis) (prec-prob)) (dolist (item data) (pcase (dom-attr item 'type) ("air_temperature_minimum" (setq min-temp (dom-text item))) ("air_temperature_maximum" (setq max-temp (dom-text item))) ("probability_of_precipitation" (setq prec-prob (dom-text item))) ("precis" (setq precis (dom-text item))))) (format "- %s :: %s %s %s" date prec-prob precis (bom-format-temp-range min-temp max-temp)))) (defun bom-format-temp-range (min-temp max-temp) "Format a temperature range from MIN-TEMP to MAX-TEMP." (if (or min-temp max-temp) (format "%s - %s" (if min-temp (concat min-temp "C") "") (if max-temp (concat max-temp "C") "")) "")) (defun bom-format-areas-org (areas) "Format hierarchy of AREAS with forecasts into an org string. We use the description of the first root as the state name. The org string is ready to be inserted into an empty buffer for display or export." (let ((state-name (dom-attr (car (hierarchy-roots areas)) 'description))) (format "#+title: %s weather forecast [[elisp:(bom)][Back]] %s" state-name (string-join (hierarchy-map 'bom-format-area areas 1) "\n")))) (defun bom-to-path (sexp) "Given an SEXP, return the corresponding path. For example, (foo \"bar\" \"baz\") is converted to /foo/bar/baz." (pcase-let ((`(,func . ,args) (read sexp))) (unless (and (symbolp func) (memq func '(bom bom-state)) (every 'stringp args)) (user-error "Invalid sexp: %s" sexp)) (format "/%s" (string-join (cons (format "%s" func) args) "/")))) (defun bom-elisp-link-export (link desc format) "An :export function for elisp: links. Takes a LINK with description DESC, assuming FORMAT is html, output the http path." (cl-assert (eq format 'html)) (format "%s" (bom-to-path link) (or desc link))) (defun bom-org-to-html (s) "Export org string S to html and return the html string." (with-temp-buffer (insert s) (let ((org-html-postamble) (org-link-parameters (cons '("elisp" :export bom-elisp-link-export) org-link-parameters))) (let ((result (org-export-as 'html))) (pop org-link-parameters) result)))) (defun bom-format-areas-html (areas) "Format hierarchy of AREAS with forecasts to an html string. The html string can be sent as a web server response." (bom-org-to-html (bom-format-areas-org areas))) (defun bom-get-forecasts (state) "Call BOM API and return hierarchy of STATE areas with forecasts." (let ((areas (hierarchy-new))) (hierarchy-add-trees areas (setq bom-areas (bom-get-areas (bom-api state))) 'bom-area-parent) (hierarchy-sort areas 'bom-area-lessp) areas)) (defun bom-state-org (state) (bom-format-areas-org (bom-get-forecasts state))) (defun bom-org () "Format the \"landing page\" org string. The string is ready for display or export." (format "#+title: Australia weather forecast %s" (mapconcat (lambda (pair) (format "[[elisp:(bom-state \"%s\")][%s]]" (car pair) (upcase (format "%s" (car pair))))) bom-state-files " "))) ;;;###autoload (defun bom-state (state) "Open bom forecast for STATE." (interactive "sState: ") (with-current-buffer (get-buffer-create "*bom*") (erase-buffer) (insert (bom-states-org state)) (goto-char (point-min)) (org-mode)) (display-buffer "*bom*")) ;;;###autoload (defun bom () "Entrypoint - open bom landing page." (interactive) (with-current-buffer (get-buffer-create "*bom*") (erase-buffer) (insert (bom-org)) (goto-char (point-min)) (org-mode)) (display-buffer "*bom*")) (defun bom-from-path (path) "Translate a GET PATH to a pair of function and string args. For example, if path is /foo/bar/baz, then we return (foo . (\"bar\" \"baz\"))" (unless (string-prefix-p "/" path) (user-error "Invalid path: %s" path)) (pcase-let ((`(,func-name . ,args) (cdr (split-string path "/")))) (unless (memq (intern func-name) '(bom bom-state)) (user-error "Invalid path: %s" path)) (cons (intern (format "%s-org" func-name)) args))) (defun bom-serve (path) "Serve based on PATH." (bom-org-to-html (apply (bom-from-path path)))) ;;;###autoload (defun bom-start () "Start serving weather forecasts." (when bom-server (bom-stop)) (setq bom-server (ws-start (lambda (request) (with-slots (process headers) request (condition-case res (bom-serve (alist-get :GET headers)) (user-error (ws-send-404 process (format "%s" (cadr res)))) (error (ws-send-500 process)) (:success (ws-response-header process 200 '("Content-type" . "text/html")) (process-send-string process res)) ))) bom-port))) (defun bom-stop () "Stop serving weather forecasts." (ws-stop bom-server)) (provide 'bom) ;;; bom.el ends here