aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuchen Pei <id@ypei.org>2023-10-02 18:20:09 +1100
committerYuchen Pei <id@ypei.org>2023-10-02 18:20:09 +1100
commitb43240e5936d327c35229c874abdb5b326ab437d (patch)
treec2f6a4ad0dfbc97c8779cdf4be2e25f7fb1fd0e4
parent02388f26c9ce8e28845166ef29307227fc35f7ce (diff)
Add path to state, and landing page is for state selection.
-rw-r--r--bom.el78
1 files changed, 56 insertions, 22 deletions
diff --git a/bom.el b/bom.el
index 1c2b79a..8831ad3 100644
--- a/bom.el
+++ b/bom.el
@@ -41,12 +41,14 @@
(wa . "IDW14199"))
"Alist of states and territories and their corresponding filenames on the BOM FTP server, see <http://www.bom.gov.au/catalogue/data-feeds.shtml>.")
-(defun bom-api-vic ()
- "Get Victoria weather from the BOM FTP."
- (with-current-buffer
- (find-file-noselect
- "/ftp:anonymous@ftp.bom.gov.au:/anon/gen/fwo/IDV10753.xml")
- (libxml-parse-xml-region (point-min) (point-max))))
+(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)))
+ (with-current-buffer
+ (find-file-noselect
+ (format "/ftp:anonymous@ftp.bom.gov.au:/anon/gen/fwo/%s.xml" filename))
+ (libxml-parse-xml-region (point-min) (point-max)))
+ (user-error "State %s not found" state)))
(defun bom-get-areas (resp)
"Given an API response RESP, get all areas."
@@ -105,31 +107,63 @@ Used as parentfn for hierarchy."
(if (or min-temp max-temp)
(format "%s - %s"
(if min-temp (concat min-temp "C") "")
- (if max-temp (concat max-temp "C") ""))))
+ (if max-temp (concat max-temp "C") ""))
+ ""))
(defun bom-format-areas-org (areas)
- "Format hierarchy of AREAS with forecasts."
- (concat "#+title: Victoria weather forecast\n\n"
- (string-join
- (hierarchy-map 'bom-format-area bom-area-hierarchy 1)
- "\n")))
+ "Format hierarchy of AREAS with forecasts.
+
+We use the description of the first root as the state name."
+ (let ((state-name (dom-attr (car (hierarchy-roots areas)) 'description)))
+ (format "#+title: %s weather forecast\n\n%s"
+ state-name
+ (string-join
+ (hierarchy-map 'bom-format-area areas 1)
+ "\n"))))
+
+(defun bom-org-to-html (s)
+ "Export org string S to html and return the html string."
+ (with-temp-buffer
+ (insert s)
+ (org-export-as 'html)))
(defun bom-format-areas-html (areas)
"Format hierarchy of AREAS with forecasts to html."
- (with-temp-buffer
- (insert (bom-format-areas-org areas))
- (org-export-as 'html)))
+ (bom-org-to-html (bom-format-areas-org areas)))
-(defun bom-get-vic-forecasts ()
- "Call BOM API and return hierarchy of VIC areas with forecasts."
+(defun bom-get-forecasts (state)
+ "Call BOM API and return hierarchy of STATE areas with forecasts."
(let ((areas (hierarchy-new)))
(hierarchy-add-trees
- bom-area-hierarchy
- (setq bom-areas (bom-get-areas (bom-api-vic)))
+ areas
+ (setq bom-areas (bom-get-areas (bom-api state)))
'bom-area-parent)
- (hierarchy-sort bom-area-hierarchy 'bom-area-lessp)
+ (hierarchy-sort areas 'bom-area-lessp)
areas))
+(defun bom-format-state-lists-org ()
+ (format
+ "#+title: Australia weather forecast\n%s"
+ (mapconcat
+ (lambda (pair) (format "[[file:%s][%s]]"
+ (car pair)
+ (upcase (format "%s" (car pair)))))
+ bom-state-files
+ " ")))
+
+(defun bom-format-state-lists-html ()
+ (bom-org-to-html (bom-format-state-lists-org)))
+
+(defun bom-format-state-lists-html ())
+
+(defun bom-serve (path)
+ "Serve based on PATH."
+ (setq path (substring path 1))
+ (if (string-empty-p path)
+ (bom-format-state-lists-html)
+ (bom-format-areas-html
+ (bom-get-forecasts path))))
+
;;;###autoload
(defun bom-start ()
"Start serving weather forecasts."
@@ -141,8 +175,8 @@ Used as parentfn for hierarchy."
(ws-response-header process 200 '("Content-type" . "text/html"))
(process-send-string
process
- (bom-format-areas-html
- (bom-get-vic-forecasts)))))
+ (bom-serve (alist-get :GET headers))
+ )))
9000)))
(defun bom-stop ()