diff options
author | Arun Isaac | 2023-10-14 21:46:45 +0100 |
---|---|---|
committer | Arun Isaac | 2023-10-14 21:46:45 +0100 |
commit | 82333d70f3a5dc64af99463516c5490bfd4e3894 (patch) | |
tree | 8d469a437442fb394004d27e4f2471470738fcd5 | |
parent | c26df4b22651fa3077ef2c2a1643e810b2864ece (diff) | |
download | rent-in-london-82333d70f3a5dc64af99463516c5490bfd4e3894.tar.gz rent-in-london-82333d70f3a5dc64af99463516c5490bfd4e3894.tar.lz rent-in-london-82333d70f3a5dc64af99463516c5490bfd4e3894.zip |
Abstract JSON requests.
* rent.scm (json-get): New function.
(cycling-distances, lines-at-station): Use json-get.
-rw-r--r-- | rent.scm | 96 |
1 files changed, 49 insertions, 47 deletions
@@ -266,6 +266,13 @@ times and comparing them using @var{time-predicate}." (call-with-input-file cache-file get-bytevector-all))) +(define* (json-get uri #:optional cache-live-time) + "Invoke @code{http-get*} on @var{uri} and process the response as +JSON." + (call-with-port (open-bytevector-input-port + (http-get* uri cache-live-time)) + json->scm)) + (define (house-page-sxml house) "Return the HTML of the page for @var{house} in SXML form." (call-with-port (open-bytevector-input-port @@ -351,58 +358,53 @@ characters. Else, return @code{#f}." "Compute the cycling distance between @var{source}, a point, and @var{destinations}, a list of points. The returned distance is a list of actual distances by cycle, and not distance as the crow flies." - (call-with-port (open-bytevector-input-port - (http-get* - ;; We use ~f for latitude/longitude since ~a - ;; would use the exponential float notation for - ;; small numbers and the API does not like - ;; that. - (format #f "~a/table/v1/bike/~a?annotations=distance&sources=0&destinations=~a" - %osrm-base-url - (string-join - (map (match-lambda - (#(latitude longitude) - (format #f "~f,~f" longitude latitude))) - (cons source destinations)) - ";") - (string-join - (map number->string - (iota (length destinations) 1)) - ";")))) - (lambda (port) - (map (cut / <> 1000) - (vector->list (json-ref (json->scm port) - "distances" 0)))))) + (map (cut / <> 1000) + (vector->list + (json-ref + (json-get + ;; We use ~f for latitude/longitude since ~a would use the + ;; exponential float notation for small numbers and the API does + ;; not like that. + (format #f "~a/table/v1/bike/~a?annotations=distance&sources=0&destinations=~a" + %osrm-base-url + (string-join + (map (match-lambda + (#(latitude longitude) + (format #f "~f,~f" longitude latitude))) + (cons source destinations)) + ";") + (string-join + (map number->string + (iota (length destinations) 1)) + ";"))) + "distances" 0)))) (define (lines-at-station station-name) "Return a list of tube lines serving @var{station-name}." (let* ((modes (list "dlr" "elizabeth-line" "tube")) (station-id - (call-with-port (open-bytevector-input-port - (http-get* (string-append %tfl-base-url - "/StopPoint/Search/" - (uri-encode station-name) - "?modes=" - (string-join modes ",")))) - (lambda (port) - ;; Assume the first search result is the station we are - ;; looking for. - (json-ref (json->scm port) - "matches" 0 "id"))))) - (call-with-port (open-bytevector-input-port - (http-get* (string-append %tfl-base-url - "/StopPoint/" station-id))) - (lambda (port) - (vector->list - ;; Assume that we actually find tube lines at this - ;; station. This may not be the case if the supplied - ;; station-name is not actually a tube station. - (json-ref (find (lambda (mode-group) - (member (json-ref mode-group "modeName") - modes)) - (vector->list (json-ref (json->scm port) - "lineModeGroups"))) - "lineIdentifier")))))) + ;; Assume the first search result is the station we are + ;; looking for. + (json-ref + (json-get (string-append %tfl-base-url + "/StopPoint/Search/" + (uri-encode station-name) + "?modes=" + (string-join modes ","))) + "matches" 0 "id"))) + ;; Assume that we actually find tube lines at this station. This + ;; may not be the case if the supplied station-name is not + ;; actually a tube station. + (vector->list + (json-ref (find (lambda (mode-group) + (member (json-ref mode-group "modeName") + modes)) + (vector->list + (json-ref + (json-get (string-append %tfl-base-url + "/StopPoint/" station-id)) + "lineModeGroups"))) + "lineIdentifier")))) (define (list-house house) "Display details of @var{house} on the current output port." |