From 6ff038b4d29ec761336c807f4a79831c99f29d67 Mon Sep 17 00:00:00 2001 From: stuebinm Date: Thu, 7 Apr 2022 00:30:14 +0200 Subject: flora is a trainspotter now (well, a trainschedulespotter to be precise) --- flora/configuration.nix | 1 + flora/services/urlwatcher.nix | 24 +++++++++++++++++++ flora/services/urlwatcher.scm | 56 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 flora/services/urlwatcher.nix create mode 100644 flora/services/urlwatcher.scm (limited to 'flora') diff --git a/flora/configuration.nix b/flora/configuration.nix index fde327b..aa0602b 100644 --- a/flora/configuration.nix +++ b/flora/configuration.nix @@ -14,6 +14,7 @@ ./services/blog.nix ./services/znc.nix ./services/mail.nix + ./services/urlwatcher.nix ]; # Use the GRUB 2 boot loader. diff --git a/flora/services/urlwatcher.nix b/flora/services/urlwatcher.nix new file mode 100644 index 0000000..56e043a --- /dev/null +++ b/flora/services/urlwatcher.nix @@ -0,0 +1,24 @@ +{ 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 = [ pkgs.git ]; + script = '' + ${pkgs.gauche}/bin/gosh ${pkgs.copyPathToStore ./urlwatcher.scm} + ''; + }; + + systemd.timers.wikibot = { + wantedBy = [ "timers.target" ]; + timerConfig = { + Unit = "urlwatcher-ilztal.service"; + OnBootSec = "5h"; + OnActiveSec = "1d"; + }; + }; +} diff --git a/flora/services/urlwatcher.scm b/flora/services/urlwatcher.scm new file mode 100644 index 0000000..2884c05 --- /dev/null +++ b/flora/services/urlwatcher.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 " +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"))) + (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