Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

118 строки
4.5KB

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