(define-module (tw services media) #:use-module (gnu) #:use-module (gnu packages certs) #:use-module (gnu packages video) #:use-module (gnu services) #:use-module (gnu services configuration) #:use-module (gnu services mcron) #:use-module (gnu services shepherd) #:use-module (guix gexp) #:use-module (guix packages) #:export (yt-dlp-service-type yt-dlp-configuration get-iplayer-service-type get-iplayer-configuration)) (define (package-or-false? value) (or (package? value) (eq? #f value))) (define (string-or-gexp? value) (or (string? value) (gexp? value))) (define (random-minute hour) ;; Choose a random minute of the hour. The certbot service uses ;; `random' like this too, so it should be safe. #~'(next-minute-from (next-hour '(#$hour)) (list (random 60)))) (define-configuration/no-serialization yt-dlp-configuration (media-directory string "The directory in which to store downloaded media. The service expects a @code{.yt-dlp} config directory inside this one.") (yt-dlp (package-or-false yt-dlp) "The yt-dlp package to use; or @code{#false} to use the yt-dlp script inside the config directory.") (schedule (string-or-gexp (random-minute 4)) "The mcron schedule on which to run the download script. By default, picks a random time between 04:00 and 05:00 every night.") (user (string "root") "The Unix user name to run the script as.")) (define (yt-dlp-cronjob config) (list (let* ((yt-dlp-package (yt-dlp-configuration-yt-dlp config)) (yt-dlp-executable (if yt-dlp-package (file-append yt-dlp-package "/bin/yt-dlp") (string-append (yt-dlp-configuration-media-directory config) "/.yt-dlp/yt-dlp")))) #~(job #$(yt-dlp-configuration-schedule config) #$(program-file "yt-dlp-command" #~(begin (setenv "SSL_CERT_DIR" #$(file-append nss-certs "/etc/ssl/certs")) (chdir #$(yt-dlp-configuration-media-directory config)) ;; Auto-update if we're using yt-dlp from the media directory. #$@(if yt-dlp-package '() (list #~(system* #$yt-dlp-executable "--ignore-config" "--update"))) (execl #$yt-dlp-executable "yt-dlp" "--ignore-config" "--config-location" ".yt-dlp"))) #:user #$(yt-dlp-configuration-user config))))) (define yt-dlp-service-type (service-type (name 'yt-dlp) (extensions (list (service-extension mcron-service-type yt-dlp-cronjob))) (description "Trigger yt-dlp on a schedule to download videos from YouTube."))) (define-configuration/no-serialization get-iplayer-configuration (config-directory string "The directory in which to look for configuration files.") (get-iplayer (package get-iplayer) "The get-iplayer package to use.") (schedule (string-or-gexp (random-minute 1)) "The mcron schedule on which to run the download script. By default, picks a random time between 01:00 and 02:00 every night.") (user (string "root") "The Unix user name to run the script as.")) (define (get-iplayer-cronjob config) (list #~(job #$(get-iplayer-configuration-schedule config) #$(program-file "get-iplayer-command" #~(begin (setenv "SSL_CERT_DIR" #$(file-append nss-certs "/etc/ssl/certs")) (execl #$(file-append (get-iplayer-configuration-get-iplayer config) "/bin/get_iplayer") "get_iplayer" "--pvr" "--profile-dir" #$(get-iplayer-configuration-config-directory config)))) #:user #$(get-iplayer-configuration-user config)))) (define get-iplayer-service-type (service-type (name 'get-iplayer) (extensions (list (service-extension mcron-service-type get-iplayer-cronjob))) (description "Trigger get_iplayer on a schedule to download radio shows from the BBC.")))