summaryrefslogtreecommitdiff
path: root/flora/services/trainspotter.scm
blob: 860f0ab7b005c24eae7b725ee3d579bfa2b5320d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env gosh

(use gauche.process)
(use srfi-13)
(use srfi-19)
(use file.util)

(define url "https://ilztalbahn.eu/wp-content/uploads/2020/07/gtfs.zip")
(define cachefile "/tmp/urlwatch-cache")
(define storagedir "/var/lib/urlwatch-ilztal")

(do-process `(mkdir "-p" ,storagedir))

(define (get-hash url)
  (string-trim-both
   (process-output->string `((curl ,url)
                             (sha256sum)))))

(define hash
  (get-hash url))

(define pretty-date
  (date->string (time-utc->date (current-time)) "~5"))

(define (hash-did-change oldhash newhash)
  (print (format "the url's hash has changed to ~s!" newhash))
  (let ([newfile (string-append storagedir "/" newhash)])
    (do-process `(curl ,url "-o" ,newfile))
    (let ([diff (if oldhash
                    (let ([oldfile (string-append storagedir "/" oldhash)])
                      (process-output->string `(zipcmp ,oldfile ,newfile)))
                    "")])
      (do-pipeline `((echo ,(format "

Neuer hash ist ~a
Link ist ~a

~a

(~a)

" newhash url diff pretty-date))
                     (mail "-s" "Ilztalbahn GTFS Update"
                           "stuebinm@disroot.org"
                           "-a" "From: trainspotter@flora.stuebinm.eu")))
      (sexp-list->file
       cachefile
       (list (list pretty-date newhash))
       :if-exists :append))))

(if (file-is-writable? cachefile)
    (let ([oldhash (cadr (last (file->sexp-list cachefile)))])
      (if (equal? (cadr (last (file->sexp-list cachefile))) hash)
          (print (format "url did not change, hash remains ~a" hash))
          (hash-did-change oldhash hash)))
    (hash-did-change #f hash))