From 393e59ffece0d799c40b27d10c459a194cc69f14 Mon Sep 17 00:00:00 2001 From: Timo Wilken Date: Sat, 23 Mar 2024 17:32:59 -0500 Subject: Fix timezone-setting script It seems ipapi.co now requires a User-Agent header, else requests immediately get rate limited. Also improve error handling, and use streaming mode to fix mystery errors, as `http-get' choked on a bytevector response where it expected a string. --- tw/system.scm | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/tw/system.scm b/tw/system.scm index 34f398a8..e62ad095 100644 --- a/tw/system.scm +++ b/tw/system.scm @@ -125,33 +125,39 @@ (with-extensions (list guile-json-4 guile-gnutls) ; guile-gnutls needed by (web client) #~(begin (use-modules ((srfi srfi-11) #:select (let-values)) + ((ice-9 textual-ports) #:select (get-string-all)) + (ice-9 format) (json) (web client) (web response)) (define api-url "https://ipapi.co/json") + (define headers '((user-agent . "tw-dotfiles/0.0 (abuse@twilken.net)"))) ;; According to the Arch Wiki, when checking for "up" as the second ;; argument, VPN connections could cause undesired timezone changes. (when (and (string=? "connectivity-change" (caddr (command-line))) (string=? "FULL" (getenv "CONNECTIVITY_STATE"))) - (let-values (((response text) (http-get api-url))) + (let-values (((response body-port) (http-get api-url #:headers headers #:streaming? #t))) (unless (= 200 (response-code response)) - (error "Got error response to request:" response)) - (let* ((json (json-string->scm text)) - (timezone (assoc-ref json "timezone")) - (zonefile (string-append #$tzdata "/share/zoneinfo/" timezone)) - (regdb-country (assoc-ref json "country_code")) - (error-code - (status:exit-val - ;; Set WiFi regulatory domain to use the correct channels. - (system* #$(file-append iw "/bin/iw") "reg" "set" regdb-country)))) - (format (current-error-port) "Updated WiFi regdomain to ~a: ~a~%" - regdb-country (if (zero? error-code) "success" - (format #f "error ~d" error-code))) + (error "Got error response to request:" + (response-code response) (get-string-all body-port))) + (let ((json (json->scm body-port))) + (when (assoc-ref json "error") + (error "Got error response to request:" + (assoc-ref json "reason") (assoc-ref json "message"))) + ;; Set WiFi regulatory domain to use the correct channels. + (let* ((regdb-country (assoc-ref json "country_code")) + (error-code + (system* #$(file-append iw "/sbin/iw") "reg" "set" regdb-country))) + (format (current-error-port) "Updated WiFi regdomain to ~a: ~a~%" + regdb-country (if (status:exit-val error-code) "success" + (format #f "error ~d" error-code)))) ;; Set local timezone. - (delete-file "/etc/localtime") - (symlink zonefile "/etc/localtime") - (format (current-error-port) - "Updated timezone to ~a: success~%" timezone))))))) + (let* ((timezone (assoc-ref json "timezone")) + (zonefile (string-append #$tzdata "/share/zoneinfo/" timezone))) + (delete-file "/etc/localtime") + (symlink zonefile "/etc/localtime") + (format (current-error-port) + "Updated timezone to ~a: success~%" timezone)))))))) ;; This text is added verbatim to the Xorg config file. (define touchpad-xorg-config "\ -- cgit v1.2.3