Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

123 linhas
4.7KB

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