aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimo Wilken2024-03-23 17:32:59 -0500
committerTimo Wilken2024-03-23 17:32:59 -0500
commit393e59ffece0d799c40b27d10c459a194cc69f14 (patch)
treef6930ce902d06fe49b82528ed8d7a47ef83d75d8
parent8dee5d5372920516181da14983f9c5118bdd0ad6 (diff)
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.
-rw-r--r--tw/system.scm40
1 files 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 "\