From a22a0346524910c596f97aba955b7346b7479c2d Mon Sep 17 00:00:00 2001 From: stuebinm Date: Thu, 7 Apr 2022 21:44:23 +0200 Subject: flora: fix the trainspotter script --- flora/configuration.nix | 2 +- flora/services/trainspotter.nix | 17 +++++++++++++ flora/services/trainspotter.scm | 56 +++++++++++++++++++++++++++++++++++++++++ flora/services/urlwatcher.nix | 17 ------------- flora/services/urlwatcher.scm | 56 ----------------------------------------- 5 files changed, 74 insertions(+), 74 deletions(-) create mode 100644 flora/services/trainspotter.nix create mode 100644 flora/services/trainspotter.scm delete mode 100644 flora/services/urlwatcher.nix delete mode 100644 flora/services/urlwatcher.scm (limited to 'flora') diff --git a/flora/configuration.nix b/flora/configuration.nix index aa0602b..51fee59 100644 --- a/flora/configuration.nix +++ b/flora/configuration.nix @@ -14,7 +14,7 @@ ./services/blog.nix ./services/znc.nix ./services/mail.nix - ./services/urlwatcher.nix + ./services/trainspotter.nix ]; # Use the GRUB 2 boot loader. diff --git a/flora/services/trainspotter.nix b/flora/services/trainspotter.nix new file mode 100644 index 0000000..5b81f7e --- /dev/null +++ b/flora/services/trainspotter.nix @@ -0,0 +1,17 @@ +{ config, lib, pkgs, ... }: + +{ + systemd.services.trainspotter = { + enable = true; + description = "script watching the ilztalbahn gtfs for changes"; + wantedBy = [ "multi-user.target" ]; + after = [ "network.target" ]; + serviceConfig.Type = "simple"; + path = with pkgs; [ curl libzip mailutils ]; + script = '' + ${pkgs.gauche}/bin/gosh ${pkgs.copyPathToStore ./trainspotter.scm} + ''; + startAt = "daily"; + }; + +} diff --git a/flora/services/trainspotter.scm b/flora/services/trainspotter.scm new file mode 100644 index 0000000..860f0ab --- /dev/null +++ b/flora/services/trainspotter.scm @@ -0,0 +1,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)) diff --git a/flora/services/urlwatcher.nix b/flora/services/urlwatcher.nix deleted file mode 100644 index 5b6d7bf..0000000 --- a/flora/services/urlwatcher.nix +++ /dev/null @@ -1,17 +0,0 @@ -{ config, lib, pkgs, ... }: - -{ - systemd.services.urlwatcher-ilztal = { - enable = true; - description = "script watching the ilztalbahn gtfs for changes"; - wantedBy = [ "multi-user.target" ]; - after = [ "network.target" ]; - serviceConfig.Type = "simple"; - path = with pkgs; [ curl libzip system-sendmail ]; - script = '' - ${pkgs.gauche}/bin/gosh ${pkgs.copyPathToStore ./urlwatcher.scm} - ''; - startAt = "daily"; - }; - -} diff --git a/flora/services/urlwatcher.scm b/flora/services/urlwatcher.scm deleted file mode 100644 index 0519903..0000000 --- a/flora/services/urlwatcher.scm +++ /dev/null @@ -1,56 +0,0 @@ -#!/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 " -From: urlwatcher@flora.stuebinm.eu -Subject: Ilztalbahn GTFS Update - -Neuer hash: ~a -Link: ~a - -~a - -(~a) - -" newhash url diff pretty-date)) - (sendmail "stuebinm@disroot.org" "-t"))) - (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)) -- cgit v1.2.3