-
Notifications
You must be signed in to change notification settings - Fork 0
/
tget.cl
3898 lines (3490 loc) · 127 KB
/
tget.cl
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;; tget.cl -- download files from rss feeds for tv shows
;; This program was inspired by flexget.
;;
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Only a few sites and their RSS feeds are supported. TVT and BTN, for
;; now. Some others are in development. The problem is the RSS feeds from
;; many sites are completely worthly. kickass torrents, for example. You
;; can search and get an RSS feed on a search, but the individual results
;; for the search point not to .torrent files, but to other web pages.
;; This means tget would have to have a specialized parse for kickass
;; torrent torrent HTML pages. Ugh.
;;
;; Here's how tget works:
;;
;; An object database, AllegroCache, is used to store series, episode and
;; other information. Every episode downloaded by tget is stored in the
;; database.
;;
;; At program startup, the config file is loaded, and it can add or
;; substract series from the database. The config file also defines
;; `groups', for people or combinations of people, though this is just how
;; the author uses them. For each group defined in the config file, we:
;;
;; - Fetch the RSS feed, which exists in memory as a list of RSS objects.
;;
;; - RSS objects are turned into episode objects, by looking at the
;; source of the feed (TVT, BTN, etc) and calling a specialized parser on
;; each rss object. The result is an episode object in the database,
;; which is created with is marked as `transient'. Transient episodes
;; haven't been downloaded yet, but they are for series we have specified
;; in the config file. Episodes for series not in the config file are
;; never turned into RSS objects.
;;
;; - Now, we iterate over each series in this group and find `matches' for
;; transient episodes we just added to the database. (See
;; process-transient-objects for information on how the matching is done.)
;; Matching episodes are downloaded and marked as non-transient. All
;; remaining transient objects are deleted from the database.
;;
;; Problem:
;; The creation of so many transient episodes, many of which are removed
;; at the end of the group iteration, is very stressful on the database.
;; Over time, the number of deleted objects grows so large that queries
;; on the database take a very long time. A typical run of tget on a
;; compacted database takes a couple of seconds, at most. After a month,
;; it could take a minute or more.
;;
;; Possible solutions:
;; 1. Store the transient objects in a different database and only
;; instantiate episodes in the main database once they are downloaded.
;; 2. In function matching-episodes, there is a query-episode call to see
;; if a non-transient ep exists. This shouldn't be necessary, because
;; the series object has all the information about what we have
;; downloaded.
;; PROBLEM: shows like The Daily Show present a unique problem, since
;; there is no real way to store the downloaded state in the
;; series object. Gaps are normal and sometimes large.
;; 3. A hybrid of the above two. Normal series wouldn't need to store the
;; the episodes. How to identify the abnormal series, though?
;;
;; Solution #1 was implemented in version 2.0.
;; #3 might be OK, and would potentially shrink the database size a lot.
;; #2 is attractive, but I can't see how to make it work in all cases.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defpackage :user
(:import-from #:db.allegrocache
;; Specify each symbol instead of using the package,
;; because we need to review the :db argument usage for
;; each new function/macro.
#:*allegrocache*
#:close-database
#:commit
#:create-expression-cursor
#:db-object-oid
#:delete-instance
#:doclass
#:free-index-cursor
#:next-index-cursor
#:open-file-database
#:restore-database
#:retrieve-from-index
#:save-database)
(:import-from #:db.allegrocache.utils
#:defclass*
#:delete-persistent-class))
(in-package :user)
(eval-when (compile eval load)
(defvar *tget-version* "5.7.3")
)
(defvar *schema-version*
;; 1 == initial version
;; 2 == added `delay' slot
;; 3 == added schema versioning
;; 4 == added `container' slots to episode and quality
;; added `priority' to quality
;; added `repack' to episode
;; **** changed the name of an unused slot here, didn't need to change
;; **** schema based on my testing
;; 5 == changed series `last-episode' to `complete-to'
;; 6 == fix `container' and `repack' slots (in episode class)
;; 7 == add `pretty-epnum' to episode class
;; 8 == Fix `episode' & `pretty-epnum' for range episodes like for
;; "Mad.Men.S06E01-E02.PROPER.HDTV.x264-2HD.mp4"
;; 9 == added `subdir' slot to series
;; 10 == added `date-based' slot to series
;; 11 == `group-rss-url' is now a list
;; 12 == new series `private' slot; episode `tracker' slot; class
;; `group' is no longer persistent
;; 13 == never-delete and archive added to `series'
;;
;; see the db-upgrade methods for details.
13)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; acache hackery
#+(version>= 9 0)
(eval-when (compile eval load) (push :clos-describe-hack *features*))
;; this should be part of AC!
#+clos-describe-hack
(defun describe-persistent-clos-object (object stream)
(let* ((class (class-of object))
(slotds (mop:class-slots class))
(max-slot-name-length 0)
(instance-slotds ()))
(flet ((adjust-slot-name-length (name)
(setq max-slot-name-length
(max max-slot-name-length
(length (the string (prin1-to-string name))))))
(describe-slot (name &optional (allocation () alloc-p))
(let* ((boundp (slot-boundp object name))
(value (and boundp (slot-value object name))))
(if alloc-p
(format stream
" ~@<~3I~S ~S~VT~_~:[<unbound>~*~;~S~]~:>~%"
name allocation (+ max-slot-name-length 7)
boundp value)
(format stream
" ~@<~3I~S~VT~_~:[<unbound>~*~;~S~]~:>~%"
name max-slot-name-length
boundp value)))))
;; Figure out a good width for the slot-name column.
(dolist (slotd slotds)
(when (eq :persistent (excl::slotd-allocation slotd))
(adjust-slot-name-length (mop:slot-definition-name slotd))
(push slotd instance-slotds)))
(setq max-slot-name-length
(excl::first-column-width (+ max-slot-name-length 3)))
(when instance-slotds
(format stream " The '~a' object has the following persistent slots~%"
(class-name class))
(dolist (slotd (nreverse instance-slotds))
(describe-slot (mop:slot-definition-name slotd))))
(values))))
(defun print-object-persistent-clos-object (obj stream name-func)
(print-unreadable-object (obj stream :identity *print-escape* :type t)
(let ((oid (db-object-oid obj)))
(format stream "~s oid: ~s, ver ~s, trans: ~s, ~a"
;;;;addition:
(if* (funcall name-func obj)
thenret
else "-no name-")
;;;;...end addition.
oid
(db.ac::db-object-version obj)
(db.ac::db-object-trans obj)
(if* (and (slot-boundp obj 'db.ac::new-slots)
(eq :dead (db.ac::db-object-new-slots obj)))
then " deleted"
elseif (and (slot-boundp obj 'db.ac::modified)
(null (db.ac::db-object-modified obj))
(if (typep obj 'db.ac::ac-map)
(null (db.ac::ac-map-modified obj))
t))
then " not modified" else " modified")))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; classes
(defclass* quality (:conc-name t :print nil :init nil)
;; The name of the quality. Users define this to be something like
;; `:normal'.
(name :index :any-unique)
priority
container
source
codec
resolution)
(defmethod print-object ((obj quality) stream)
(cond
(*print-escape*
(print-object-persistent-clos-object
obj stream
(lambda (obj)
(format nil "~a, priority=~s, container=~s, source=~s, codec=~s, res=~s"
(quality-name obj)
(when (slot-boundp obj 'priority) (quality-priority obj))
(when (slot-boundp obj 'container) (quality-container obj))
(when (slot-boundp obj 'source) (quality-source obj))
(when (slot-boundp obj 'codec) (quality-codec obj))
(when (slot-boundp obj 'resolution) (quality-resolution obj))))))
(t ;; print it for humans
(format stream "#<quality ~s/~d [~@[~s,~]~s,~s,~s]>"
(quality-name obj)
(quality-priority obj)
(quality-container obj)
(quality-source obj)
(quality-codec obj)
(quality-resolution obj)))))
#+clos-describe-hack
(defmethod describe-object ((object quality) stream)
(describe-persistent-clos-object object stream))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;THIS IS A DUMMY CLASS DEFINITION--the real group objects is a defstruct
;;;;and is defined below.
(defclass* group (:print nil :init nil)
(name :index :any-unique)
;; rest of the slots removed
)
(defmethod print-object ((obj group) stream)
(cond
(*print-escape*
(print-object-persistent-clos-object
obj stream
(lambda (obj) (if (slot-boundp obj 'name) (group-name obj)))))
(t ;; print it for humans
(format stream "#<OLDgroup ~s>" (group-name obj)))))
#+clos-describe-hack
(defmethod describe-object ((object group) stream)
(describe-persistent-clos-object object stream))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defclass* series (:conc-name t :print nil :init nil)
;; The group name is used to find all the series objects which are in a
;; group. It is a keyword.
(group :index :any)
;; Only used for presentation to the user
pretty-name
;;canonicalized version of the series name
;; ***must match episode class naming***
(name :index :any-unique)
;; The series is considered complete to this season/episode or season.
;; Format is `(season . episode)' or `season'.
complete-to
;; episodes that came in after complete-to but there are gaps
discontinuous-episodes
delay
;;;; override for group:
quality
;;;; added in schema version 9:
;; Put episodes for this series in a subdirectory, because Plex Media
;; Service is really stupid about scanning and matching episodes.
subdir
;; This series is date based, and as such cannot use the complete-to
;; mechanism, since we can't really tell what the sequence of episodes
;; will be named. The Daily Show is like this.
date-based
;; Non-nil if this series should only be downloaded from a private
;; tracker.
private
;; If non-nil, then --cleanup never deletes episodes of this series.
never-delete
;; If non-nil, then --cleanup moves to this directory instead of
;; deleting.
archive
)
(defmethod print-object ((obj series) stream)
(cond
(*print-escape*
(print-object-persistent-clos-object
obj stream
(lambda (obj)
(if (slot-boundp obj 'pretty-name) (series-pretty-name obj)))))
(t ;; print it for humans
(format stream "#<series ~a>" (series-pretty-name obj)))))
#+clos-describe-hack
(defmethod describe-object ((object series) stream)
(describe-persistent-clos-object object stream))
(defclass* episode (:conc-name t :print nil :init nil)
series ; the series object for this ep
;;Downcased version of the series name. e.g. "vikings"
;; ***must match series class naming***
(series-name :index :any)
full-title ;e.g. "Vikings - 1x04 - Trial (.mp4)"
title ;e.g. "Trial (.mp4)"
torrent-url
pub-date ;universal time
;;number
(season :index :any)
;;number or :all
(episode :index :any)
pretty-epnum ; for human consumption
(repack :index :any)
;; quality from torrent
(container :index :any)
(source :index :any)
(codec :index :any)
(resolution :index :any)
;;;; optional:
type ;mime type
length ;length in bytes
filename ;e.g. "Vikings.S01E04.HDTV.x264-2HD.mp4"
;;;;
(transient :index :any)
;;;; non-persistent slots:
;; The tracker for this episode. It's not persistent because we only
;; need it while making the decision to download the object, and after
;; we exit it could come from a different tracker next time (if we don't
;; download it).
(tracker :allocation :instance))
(defmethod episode-p ((obj t)) nil)
(defmethod episode-p ((obj episode)) t)
(defmethod print-object ((obj episode) stream)
(cond
(*print-escape*
(print-object-persistent-clos-object
obj stream
(lambda (obj)
(format
nil
"~a~@[@~a~]~@[, REPACK~*~]~@[ ~a~]~@[; quality=~a~]; transient=~s"
(when (slot-boundp obj 'series-name) (episode-series-name obj))
(when (slot-boundp obj 'tracker)
(when (episode-tracker obj)
(tracker-name (episode-tracker obj))))
(when (slot-boundp obj 'repack) (episode-repack obj))
(when (slot-boundp obj 'pretty-epnum) (episode-pretty-epnum obj))
(pretty-episode-quality obj)
(if* (slot-boundp obj 'transient)
then (episode-transient obj)
else "-unbound-")))))
(t ;; print it for humans
(format stream "#<~s~@[@~a~]~@[, REPACK~*~], ~a [~a]>"
(episode-series-name obj)
(when (slot-boundp obj 'tracker)
(when (episode-tracker obj)
(tracker-name (episode-tracker obj))))
(episode-repack obj)
(episode-pretty-epnum obj)
(pretty-episode-quality obj)))))
(defun pretty-episode-quality (ep &key (separator #\,) &aux name all-bound)
;; Ignore priority of quality.
(if* (and (slot-boundp ep 'container)
(slot-boundp ep 'source)
(slot-boundp ep 'codec)
(slot-boundp ep 'resolution)
(setq all-bound t)
(setq name (episode-quality ep)))
then (princ-to-string name)
elseif (and all-bound
(null (episode-container ep))
(null (episode-source ep))
(null (episode-codec ep))
(null (episode-resolution ep)))
then "unknown"
elseif (or (slot-boundp ep 'container)
(slot-boundp ep 'source)
(slot-boundp ep 'codec)
(slot-boundp ep 'resolution))
then (list-to-delimited-string
(append
(when (slot-boundp ep 'container)
(list (princ-to-string (episode-container ep))))
(when (slot-boundp ep 'source)
(list (princ-to-string (episode-source ep))))
(when (slot-boundp ep 'codec)
(list (princ-to-string (episode-codec ep))))
(when (slot-boundp ep 'resolution)
(list (princ-to-string (episode-resolution ep)))))
separator)
else "undefined"))
#+clos-describe-hack
(defmethod describe-object ((object episode) stream)
(describe-persistent-clos-object object stream))
;;;; transient types not in the database:
(defstruct rss-item
source ;; host part of the url from which the feed came
title
link
guid
comments
pub-date
description
type
length
fileName)
(defstruct schema
version
tget-version)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Used to be a class called `group', can't use name
(defstruct (xgroup (:conc-name group-))
name ; a keyword
;;;; one of the next to
;; rss-url is legacy and kept for compatibility
rss-url ; a list of URLs
trackers
;;;;
delay
ratio
quality
download-path)
(defvar *group-name-to-group* (make-hash-table :size 777 :test #'eq))
(push '(clrhash *group-name-to-group*)
*init-forms*)
(defun group-name-to-group (group-name)
(gethash group-name *group-name-to-group*))
(defun (setf group-name-to-group) (group group-name)
(setf (gethash group-name *group-name-to-group*) group))
(defmethod print-object ((obj xgroup) stream)
(format stream "#<group ~s>" (group-name obj)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defstruct (transmission
(:constructor .make-transmission
(&key host port username password add-paused
trash-torrent-file ratio
docker-container-name docker-user
ssh-identity ssh-user)))
host
port
username
password
add-paused
trash-torrent-file
ratio
docker-container-name
docker-user
ssh-identity
ssh-user)
(defvar *torrent-handler* nil)
(push '(setq *torrent-handler* nil)
*init-forms*)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; UI Macros
(defmacro defquality (name &key priority container source codec resolution)
`(make-quality
:name ,name
:priority ,priority
:container ,container
:source ,source
:codec ,codec
:resolution ,resolution))
(defun check-atom-or-list (atom-or-list allowed-values what)
;; `nil' is allowed and ignored
(when atom-or-list
(when (not (consp atom-or-list))
(setq atom-or-list (list atom-or-list)))
(dolist (atom atom-or-list)
(or (member atom allowed-values :test #'eq)
(.error "Bad ~a value: ~s." what atom)))
atom-or-list))
(defun make-quality (&key name priority container source codec resolution)
(let ((old (retrieve-from-index 'quality 'name name :db *main*)))
(or (and (null priority)
(setq priority 1))
(numberp priority)
(.error "Priority must be a number: ~s." priority))
(setq container
(check-atom-or-list container *valid-containers* 'container))
(setq source (check-atom-or-list source *valid-sources* 'source))
(setq codec (check-atom-or-list codec *valid-codecs* 'codec))
(setq resolution
(check-atom-or-list resolution *valid-resolutions* 'resolution))
(if* old
then (setf (quality-priority old) priority)
(setf (quality-container old) container)
(setf (quality-source old) source)
(setf (quality-codec old) codec)
(setf (quality-resolution old) resolution)
old
else (let ((*allegrocache* *main*))
(make-instance 'quality
:name name
:priority priority
:container container
:source source
:codec codec
:resolution resolution)))))
(defmacro defgroup (name &key trackers rss-url delay ratio quality
download-path)
`(.make-group
:name ,name
:trackers ,trackers
:rss-url ,rss-url
:delay ,delay
:ratio ,ratio
:quality ,quality
:download-path ,download-path))
(defun .make-group (&key name trackers rss-url delay ratio quality
download-path)
(let ((old (group-name-to-group name)))
(setq trackers (check-trackers trackers))
(check-rss-url rss-url)
(check-delay delay)
(check-ratio ratio)
(check-quality quality)
(setq download-path (namestring download-path))
(if* old
then (setf (group-trackers old) trackers)
(setf (group-rss-url old) rss-url)
(setf (group-delay old) delay)
(setf (group-ratio old) ratio)
(setf (group-quality old) quality)
(setf (group-download-path old) download-path)
old
else (setf (group-name-to-group name)
(make-xgroup
:name name
:trackers trackers
:rss-url (if* (consp rss-url)
then rss-url
else (list rss-url))
:delay delay
:ratio ratio
:quality quality
:download-path download-path)))))
(defmacro defseries (name group &key delay quality remove catch-up subdir
date-based aliases private
never-delete archive)
(if* remove
then `(forget-series ,name :noisy nil)
else `(make-series
:name ,name
:group ,group
,@(when subdir `(:subdir ,subdir))
,@(when date-based `(:date-based ,date-based))
,@(when catch-up `(:catch-up ,catch-up))
,@(when delay `(:delay ,delay))
,@(when quality `(:quality ,quality))
,@(when aliases `(:aliases ',aliases))
,@(when private `(:private ',private))
,@(when never-delete `(:never-delete ',never-delete))
,@(when archive `(:archive ',archive)))))
(defun forget-series (name &key (noisy t))
(let* ((series-name (canonicalize-series-name name))
(s (query-series-name-to-series series-name)))
(if* s
then (format t "removing series ~a~%" s)
(delete-instance s)
(tget-commit *main*)
elseif noisy
then (warn "Could not find series: ~s." name))))
;; The following are not persistent and don't need to be.
;; They are initialized by loading the config file and are
;; used while processing episodes from the RSS feeds.
(defvar *all-series-names* (make-hash-table :size 777 :test #'equal))
(push '(clrhash *all-series-names*)
*init-forms*)
(defvar *series-name-aliases*
;; Key is a string naming a series, the value is the real name of the
;; series.
(make-hash-table :size 777 :test #'equal))
(push '(clrhash *series-name-aliases*)
*init-forms*)
(defun make-series (&key name group delay quality catch-up subdir
date-based aliases private never-delete archive
&aux series)
(let* ((pretty-name name)
(name (canonicalize-series-name name))
(old (query-series-name-to-series name)))
(check-delay delay)
(check-quality quality)
(check-group group)
(or (stringp name)
(.error "Series :name must be a string: ~s." name))
(when catch-up
(or (stringp catch-up)
(.error "Series :catch-up must be a string: ~s." catch-up)))
(when subdir
(or (stringp subdir)
(.error "Series :subdir must be a string: ~s." subdir)))
(check-boolean "Series :date-based option" date-based)
(check-boolean "Series :private option" private)
(check-boolean "Series :never-delete option" never-delete)
(or (null archive)
(stringp archive)
(.error "Series :archive must be nil or a string: ~s." archive))
(when aliases
(or (null aliases)
(dolist (alias aliases)
(or (stringp alias)
(.error "Non-string series alias: ~s." alias)))))
(setq series
(if* old
then (when (not (eq group (series-group old)))
(warn "Series ~s moved groups (~a to ~a)."
pretty-name (series-group old) group))
(when (string/= (series-name old) pretty-name)
(setf (series-pretty-name old) pretty-name))
(setf (series-group old) group)
(setf (series-delay old) delay)
(setf (series-quality old) quality)
(setf (series-subdir old) subdir)
(setf (series-date-based old) date-based)
(setf (series-private old) private)
(setf (series-never-delete old) never-delete)
(setf (series-archive old) archive)
old
else (let ((*allegrocache* *main*))
(make-instance 'series
:pretty-name pretty-name
:name name
:complete-to nil
:discontinuous-episodes nil
:group group
:delay delay
:quality quality
:subdir subdir
:date-based date-based
:private private
:never-delete never-delete
:archive archive))))
(setf (gethash pretty-name *all-series-names*) t)
(dolist (alias aliases)
;; Enter the aliases to be used during RSS to EPISODE conversion.
(setf (gethash (canonicalize-series-name alias) *series-name-aliases*)
name))
(when catch-up
(catch-up-series (concatenate 'simple-string name " " catch-up)
:series series))
series))
(defun dump-orphaned-series (delete)
(doclass (series (find-class 'series) :db *main*)
(let ((name (series-pretty-name series)))
(when (not (gethash name *all-series-names*))
(format t "Orphaned series: ~a" name)
(when delete
(format t "...removing...")
(forget-series name :noisy nil)
(format t "done."))
(format t "~%"))))
(doclass (ep (find-class 'episode) :db *main*)
(when (null (episode-series ep))
(let ((s (query-series-name-to-series (episode-series-name ep))))
(if* s
then ;; Got lost somehow... fix it
(format t ";; Fixing series slot of ~a~%" ep)
(setf (episode-series ep) s)
else (format t "Orphan episode: ~a" ep)
(when delete
(format t "...removing...")
(delete-instance ep)
(format t "done."))
(format t "~%")))))
(tget-commit *main*))
#+ignore ;; not used nor documented
(defmacro deftransmission (options &key host port username password
add-paused trash-torrent-file
ratio docker-container-name
docker-user ssh-identity ssh-user)
(declare (ignore options)) ;; just for indentation
(let ((g-host (gensym "host"))
(g-port (gensym "port"))
(g-username (gensym "username"))
(g-password (gensym "passwd"))
(g-add-paused (gensym "addpaused"))
(g-trash-torrent-file (gensym "trash"))
(g-ratio (gensym "ratio"))
(g-docker-container-name (gensym "dockername"))
(g-docker-user (gensym "dockeruser"))
(g-ssh-user (gensym "sshuser"))
(g-ssh-identity (gensym "sshid")))
`(let* ((,g-host ,host)
(,g-port ,port)
(,g-username ,username)
(,g-password ,password)
(,g-add-paused ,add-paused)
(,g-trash-torrent-file ,trash-torrent-file)
(,g-ratio ,ratio)
(,g-docker-container-name ,docker-container-name)
(,g-docker-user ,docker-user)
(,g-ssh-user ,ssh-user)
(,g-ssh-identity ,ssh-identity))
(set-torrent-handler
(make-transmission-remote-handler
:host ,g-host
:port ,g-port
:username ,g-username
:password ,g-password
:add-paused ,g-add-paused
:trash-torrent-file ,g-trash-torrent-file
:ratio ,g-ratio
:docker-container-name ,g-docker-container-name
:docker-user ,g-docker-user
:ssh-user ,g-ssh-user
:ssh-identity ,g-ssh-identity)))))
(defun make-transmission-remote-handler
(&key host port username password add-paused trash-torrent-file ratio
docker-container-name docker-user ssh-identity ssh-user)
(or (stringp host)
(.error "transmission host is not a string: ~s." host))
(or (numberp port)
(and (stringp port)
(numberp (setq port
(ignore-errors
(parse-integer port :junk-allowed nil)))))
(.error "transmission port is not a number or string: ~s."
port))
(or (stringp username)
(.error "transmission username is not a string: ~s."
username))
(or (stringp password)
(.error "transmission password is not a string: ~s."
password))
(check-ratio ratio)
(or (null docker-container-name)
(stringp docker-container-name)
(.error "docker-container-name is not a string: ~s." docker-container-name))
(or (null docker-user)
(stringp docker-user)
(.error "docker-user is not a string: ~s." docker-user))
(or (null ssh-identity)
(stringp ssh-identity)
(.error "ssh-identity is not a string: ~s." ssh-identity))
(or (null ssh-user)
(stringp ssh-user)
(.error "ssh-user is not a string: ~s." ssh-user))
(.make-transmission
:host host
:port port
:username username
:password password
:add-paused add-paused
:trash-torrent-file trash-torrent-file
:ratio ratio
:docker-container-name docker-container-name
:docker-user docker-user
:ssh-user ssh-user
:ssh-identity ssh-identity))
;; for compatibility
(setf (symbol-function 'make-transmission-remote)
(symbol-function 'make-transmission-remote-handler))
(defun set-torrent-handler (thing)
(when *torrent-handler*
(.error "There are multiple calls to set-torrent-handler in config file."))
(cond
((transmission-p thing)
;; checking is in make-transmission-remote-handler
)
((pathnamep thing)
(when (not (probe-file thing))
(.error "torrent handler points to non-existent directory: ~a."
thing)))
(t (.error "unknown torrent handler: ~s." thing)))
(setq *torrent-handler* thing))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun check-quality (quality)
(and quality
(or (and (symbolp quality)
(or (retrieve-from-index 'quality 'name quality :db *main*)
(keywordp quality)
(eq 't quality)
(symbol-function quality)
(.error "Quality ~s does not exist."
quality)))
(.error "Bad quality: ~s." quality))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; main
(eval-when (compile eval load)
(defvar *usage*
"
## Usage
There are two primary modes of operation: cleanup and download.
Cleanup mode is activated by using the --cleanup or -c arguments.
Download mode is activated by using the other primary behavior determining
arguments. See below.
Cleanup mode has two parts: removing torrents from Transmission after
they have seeded enough and removing video files watched by Plex.
[download] Primary behavior determining arguments (one of these must be given):
--add file-or-directory
--catch-up
--catch-up-series series-episode-name
--check-database
--clean-database
--compact-database
--delete-episodes series-name
--delete-episode episode-description
--delete-orphans
--delete-series series-name
--dump-all
--dump-complete-to
--dump-episodes series-name
--dump-orphans
--dump-series series-name
--dump-stats
--skip series-name
[download] Behavior modifying arguments:
--auto-backup condition
--config file
--cron
--db database-name
--debug
--force
--learn
--reset
--root data-directory
--verbose or -v
[cleanup] Behavior modifying arguments:
-h
--remove
--remove-bad
--remove-seeded
--remove-watched
--torrent-info
--torrents-only
")
(defvar *help*
"
### Usage details
The tget options are below. When there is an argument naming series,
these are canonicalized by removing single quotes and converting to lower
case. However, the series names presented to you will be stored in their
original form.
* `--help`
Print full help text and exit.
The following are arguments controlling primary behavior:
* `--cleanup` or `-c`
Active cleanup mode. See the cleanup options below.
* `--run`
The primary mode of operation, whereby RSS feeds are retrieved, searched
for new episodes and those episode torrents downloaded.
* `--add thing`
If `thing` is a file, it should be a `.torrent` file, which is
manually added. If `thing` is a directory, then all the `.torrent` files
in the directory are added. This circumvents any matching and assumes
the episodes should be downloaded. The series name,episode and
season numbers are taken directly from the file name. If the information
cannot be extracted, you can rename the files to suit `tget`.
If the episode has already been downloaded, then `--force` is required
to make `tget` download it again.
* `--catch-up`
Go through the database and make the newest episode of each series the
oldest episode that will ever be downloaded for that series; this
prevents old episodes, which are released from time to time, from being
downloaded.
* `--catch-up-series series-ep`
Catch series up to the episode given in the companion argument.
See examples below.
* `--check-database`
Report on items in the database which can be cleaned up.
* `--clean-database`
Remove items reported by `--check-database`.
* `--compact-database`
This saves and restores the database, compacting it at the same time.
* `--delete-episodes series-name`
Delete episodes with series name matching `series-name`. This is permanent!
Using this option with --auto-backup force is recommended.
`--force` can be used if there are multiple episodes that match.
* `--delete-episode episode-description`
Delete the episode matching `episode-description`. This is permanent!
Using this option with --auto-backup force is recommended. Example:
--delete-episode \"I Love Lucy S05E01\"
* `--delete-orphans`
Delete orphaned series and episodes from the database. See
`--dump-orphans` for more information.
* `--delete-series series-name`
Delete series with series name matching `series-name`. This is permanent!
Using this option with --auto-backup force is recommended.
* `--dump-all`
Dump all `episode` objects to stdout.
* `--dump-complete-to`
Dump a table of series and last downloaded information for all series in
the database to stdout. See --catch-up-series.
* `--dump-episodes series-name`
Dump all episode objects matching series name `series-name` to stdout.
* `--dump-orphans`
Dump orphaned series and episodes. Orphaned series are those which do
not appear in the config file but exist in the database. Orphaned
episodes are those are in the database but have no corresponding series
object.
* `--dump-series series-name`
Dump all series objects matching series name `series-name` to stdout.
* `--dump-stats`
Dump information about the database to stdout.
* `--skip series-name`
Skip the next episode of `series-name`. It does so by using the last
downloaded episode and incrementing it by 1.