Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

120 lignes
4.6KB

  1. ;;;; A program to print out the current real-time status of the computer parking terminal equipped
  2. ;;;; parking garages in the city of Linköping using the municipal open data API.
  3. (import medea)
  4. (import http-client)
  5. (import srfi-1)
  6. (import (chicken io))
  7. (import srfi-13)
  8. (import srfi-14)
  9. (define (curry fn a)
  10. (lambda (b)
  11. (fn a b)))
  12. (define (curry2 fn a b)
  13. (lambda (c)
  14. (fn a b c)))
  15. (define (perhaps fn a)
  16. (if a (fn a) a))
  17. (define (upon pred? fn a)
  18. (if (pred? a)
  19. (fn a)
  20. a))
  21. (define (member? a ln)
  22. (if (member a ln) #t #f))
  23. (define (default b a)
  24. (if a a b))
  25. ;;; Return the value for a given key in an association list, or #f if it doesn't exist.
  26. (define value
  27. (compose (curry perhaps cdr) assoc))
  28. ;;; Return the value for a given key in an association list, or a default value if it doesn't exist.
  29. (define (value-with-default k v ln)
  30. (default v (value k ln)))
  31. (define (has-key? k ln)
  32. (member? k (map car ln)))
  33. (define (sort less-than? ln)
  34. (if (<= (length ln) 1)
  35. ln
  36. (append (sort less-than? (filter (complement (curry less-than? (car ln))) (cdr ln)))
  37. (list (car ln))
  38. (sort less-than? (filter (curry less-than? (car ln)) (cdr ln))))))
  39. ;;; Sort using fn to extract a value for less-than? to compare for each element in ln.
  40. (define (sort-via less-than? fn ln)
  41. (sort (lambda (a b)
  42. (less-than? (fn a) (fn b))) ln))
  43. (define (display-newline str)
  44. (display str)
  45. (newline))
  46. (define (->string a)
  47. (cond ((string? a) a)
  48. ((symbol? a) (symbol->string a))
  49. ((number? a) (number->string a))
  50. ((char? a) (string a))))
  51. ;;; Given a list of either symbols or strings and an association list:
  52. ;;; look up every symbol from left to right in the association list and replace it with the value found
  53. ;;; then concatenate the resulting list of strings.
  54. ;;; Inspired by Python 3's dictionary formatting string syntax.
  55. (define (format-assoc statement ln)
  56. (apply string-append
  57. (map (curry2 upon symbol? (compose ->string (curry (flip value) ln))) statement)))
  58. ;;; Get data from a uri and parse it as json.
  59. (define (simple-json-request uri)
  60. (with-input-from-request uri #f read-json))
  61. ;;; The API key is an alphanumeric string of length 32
  62. ;;; The Linköping municipal open data API key generator portal is located at http://opendata.linkoping.se
  63. (define parking-key
  64. (string-delete char-set:whitespace (with-input-from-file "key-parking" read-line)))
  65. ;;; Get a list of parking lots with various metadata from the municipal servers.
  66. ;;; It should be noted that this API is both glitchy and bugged. Two bugs have been noted:
  67. ;;; the timestamp parameter, which is supposed to give you a list of lots changed since a given UNIX timestamp
  68. ;;; instead results in giving you every lot up until a timestamp of under one hour into the future, after which
  69. ;;; it returns nothing. This bug may be platform related; as of this writing the server system providing the API
  70. ;;; runs on Windows Server 2016 via IIS 10.0.
  71. ;;; The second bug involves returning only partial data upon request - no pattern to this bug has been observed.
  72. (define (get-parking-lots)
  73. (value 'parkingareanewlist
  74. (simple-json-request (string-append "http://parkering.linkoping.se/Parkeringsdata/ParkeringsdataV1.svc/GetParkeringsYtaList/" parking-key "/0"))))
  75. ;;; Only lots tracked by parking lot computer terminals have the parkingspacesavailable key.
  76. (define (computerized? lot)
  77. (has-key? 'parkingspacesavailable lot))
  78. ;;; The handicap lots are tracked as separate lots with duplicate names in the data set.
  79. (define (only-has-handicap-spots? lot)
  80. (equal? '(4) (value 'parkingtypes lot)))
  81. ;;; Electric car lots have a separate lot type, and the downtown parking garages have spaces with charging stations
  82. ;;; but they are not implemented in the data set.
  83. (define (electric-car-lot? lot)
  84. (member? 3 (value 'parkingtypes lot)))
  85. (define (main)
  86. ;; We want members to be parsed as lowercase symbols, and arrays to be parsed as lists.
  87. ;; This is not a legal way to parse json, which requires case-sensitive members
  88. ;; but makes for neater scheme code.
  89. (json-parsers (append `((member . ,(lambda (k v)
  90. (cons (string->symbol (string-downcase k)) v)))
  91. (array . ,identity)) (json-parsers)))
  92. (display-newline "Parking spaces available:")
  93. (map (compose display-newline (curry format-assoc '(name ": " parkingspacesavailable "/" parkingspaces)))
  94. (sort-via string-ci<? (curry value 'name)
  95. (filter (conjoin computerized? (complement only-has-handicap-spots?)) (get-parking-lots)))))
  96. (main)