blob: 34d25cf220b6c1d722515d0028aa8d15256c9c1a (
about) (
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
(define-module (tw services media)
#:use-module (gnu)
#: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))
(define (package-or-false? value)
(or (package? value) (eq? #f value)))
(define (string-or-gexp? value)
(or (string? value) (gexp? value)))
(define (random-time-every-second-night day-predicate)
#~(lambda (now)
(next-minute-from
(next-hour-from
;; If today's day number satisfies `day-predicate' (e.g. `even?'),
;; then run today, else tomorrow.
(if (#$day-predicate (tm:yday (localtime now)))
now
(next-day-from now))
;; Between 1 and 6 a.m., randomly. The certbot service uses `random'
;; like this too, so it should be safe.
(list (1+ (random 6))))
(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-time-every-second-night 'even?)) "The mcron
schedule on which to run the download script. By default, picks a random time
between 01:00 and 06:00 every second 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
(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.")))
|