-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcleanup.cl
918 lines (826 loc) · 31 KB
/
tcleanup.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
;; tcleanup :: torrent maintenance does two things:
;; 1. removes from Transmission torrents which are done seeding, and
;; 2. removes videos from the filesystem which have been watched.
;;
;; TODO:
;; * need to find some way to rename badly named torrents, ones that Plex
;; won't see. Need to do it in a way that allows Transmission to
;; continue to seed. Possibilities:
;; 1. use the JSON-RPC interface to do it
;; 2. use ssh and create a symlink with the correct name
;; (1) is preferred and I'm assuming (2) will work with Plex
(in-package :user)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; PART I: remove torrents from Transmission
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; variables from the config file
(defvar *minimum-seed-seconds* nil)
(defvar *remove-seeded* nil)
(defvar *remove-watched* nil)
(defun tm (&rest args)
(multiple-value-bind (stdout stderr code)
(command-output
(format nil
"transmission-remote ~a:~a --auth=~a:~a~{ ~a~}"
(sys:getenv "TRANSMISSION_HOST")
(sys:getenv "TRANSMISSION_PORT")
(sys:getenv "TRANSMISSION_USER")
(sys:getenv "TRANSMISSION_PASS")
args))
(when (/= 0 code)
(error "transmission-remote failed: ~d:~{~% ~a~}~%" code stderr))
stdout))
(defun init-torrent-data (&aux (state :start)
words
id
torrent-data
(res '()))
;; Get a list of torrents from Transmission (via transmission-remote)
;; and return a list of (torrent-id . hash-table) where the hash table
;; is populated by ``key: value'' from the output of transmission-remote
;; so it can be easily queried later.
;;
(dolist (line (tm "-t" "all" "--info"))
(setq words (split-re " +" line))
;;(and *debug* (format t "~s line=~s~%" state words))
:top
(ecase state
(:start
(when (string/= "NAME" (car words)) (error "Expected NAME"))
(setq torrent-data (make-hash-table :size 777 :test #'equal))
(setq state :id))
(:id
(when (not (and (string= "" (first words))
(string= "Id:" (second words))))
(error "Expected Id:"))
(when (not (=~ "^(\\d+)$" (third words)))
(error "Expected number after Id: ~a" (third words)))
(setq id (parse-integer $1))
(setq state :gobble))
(:gobble
(when (null words) (go :next))
(when (string= "NAME" (car words))
(push (cons id torrent-data) res)
(setq state :start)
(go :top))
(when (string/= "" (car words)) (go :next))
(when (not (=~ "^ (.*): (.*)" line))
(error "Couldn't parse line: ~s" line))
(setf (gethash $1 torrent-data) $2)))
:next
)
(when (and id torrent-data)
(push (cons id torrent-data) res))
(nreverse res))
(defstruct torrent
;; set from info from transmission-remote
id
name
filename
percent-done
ratio
ratio-limit
date-finished
state
hash
tracker-name ; the name of the tracker
tracker-instance ; the actual tracker object
tracker-char
tracker-seed-time
;; calculated info
seed-min-time ; nil or a number of seconds to seed
seeded-for ; how much time seeded
seed-for ; how much time left to seed
series-name
season ; season #
episode ; episode # or nil
seasonp ; (and season (not episode))
removed ; non-nil if removed in pass I
never-delete
archive ; archive don't delete
error
)
(defvar *torrents*
;; A list of torrents which are seeding. It's created in pass I and
;; used in pass II. It maps the "Name" in the torrent file to a
;; torrent structure object.
nil)
(defvar *video-types* '("avi" "mp4" "mkv" "wmv" "ts" "flv" "m4v"))
;; Forward references to a few things in tget.cl, prevent compiler warning
(eval-when (compile)
(declaim (ftype (function)
query-series-name-to-series
series-never-delete
series-archive
series-name
canonicalize-series-name
query-episode
delete-episode-1)))
(defun tcleanup-transmission
(&optional remove-bad
&aux (default-seed-ratio
1.04
;; BOGUS: transmission-remote returns 1.04 as 1.0!! ARGH!!
#+ignore
(or
(dolist (line (tm "-t" "all" "-si"))
(when (=~ "^ Default seed ratio limit: ([0-9]+\\.[0-9]+)$"
line)
(return (read-from-string $1))))
(error "Couldn't find the default seed ration limit")))
print-done
print-other)
"When REMOVE-BAD is given, then only remove torrents which are not
registered with the tracker. These are torrents which have been removed
and we should abandon them and delete the episode."
(declare (special user::*all-trackers*))
(with-verbosity 2
(format t "Default seed ratio limit: ~,2f~%" default-seed-ratio))
(setq *now* (get-universal-time))
(setq *torrents* (make-hash-table :size 777 :test #'equal))
(dolist (info (init-torrent-data))
(let* ((data (cdr info))
(name (get-torrent-info "Name" data))
(location (get-torrent-info "Location" data))
(torrent
(make-torrent
:id (car info)
:name name
:filename name
:percent-done (get-torrent-info "Percent Done" data)
:ratio (get-torrent-info "Ratio" data)
:ratio-limit (get-torrent-info "Ratio Limit" data)
:date-finished (get-torrent-info "Date finished" data
:missing-ok t)
:state (get-torrent-info "State" data)
:hash (get-torrent-info "Hash" data)
:tracker-seed-time (get-torrent-info "Seeding Time" data
:missing-ok t)
:error (get-torrent-info "Tracker gave an error" data
:missing-ok t))))
;; used by PART II
(let (path)
(if* (and location name
(setq path (format nil "~a/~a" location name))
(file-directory-p path))
then (setq path (pathname-as-directory path))
(with-verbosity 2
(format t ";; Torrent directory:~%"))
(dolist (file (directory path))
(when (member (pathname-type file) *video-types*
:test #'equalp)
(with-verbosity 2
(format t ";; file: ~a~%" (file-namestring file)))
(setf (gethash (file-namestring file) *torrents*)
torrent)))
else (setf (gethash name *torrents*) torrent)))
(multiple-value-bind (series-name season episode)
(extract-episode-info-from-filename (torrent-filename torrent)
:episode-required nil)
(let ((s (query-series-name-to-series series-name)))
(when s
(setf (torrent-never-delete torrent) (series-never-delete s))
(setf (torrent-archive torrent) (series-archive s))))
(when series-name
(if* season
then (let (pretty)
(setq pretty
(season-and-episode-to-pretty-epnum season episode))
(setf (torrent-name torrent)
(format nil "~a ~a" series-name pretty))
(setf (torrent-season torrent) season)
(setf (torrent-episode torrent) episode)
(setf (torrent-seasonp torrent)
(and season (null episode))))
else (setf (torrent-name torrent) series-name))
(setf (torrent-series-name torrent) series-name)))
(when (and (torrent-error torrent)
(=~ "(not registered with this tracker|Unregistered torrent)"
(torrent-error torrent)))
;; Since the torrent is "unregistered", delete it and the episode
;; in the db.
(push (cons torrent :error) print-done)
(remove-torrent torrent :delete-episode t)
(go :next))
(when remove-bad
;; we're just removing torrents with errors and nothing more, so
;; skip the rest
(go :next))
(when (string/= "100%" (torrent-percent-done torrent))
;; skip it since it's not done
(with-verbosity 1
(push (cons torrent :incomplete) print-other))
(go :next))
;;;; Setup torrent data
(setf (torrent-ratio-limit torrent)
(if* (or (string= "Default" (torrent-ratio-limit torrent))
(string= "Unlimited" (torrent-ratio-limit torrent)))
then default-seed-ratio
else (read-from-string (torrent-ratio-limit torrent))))
(setf (torrent-ratio torrent)
(read-from-string (torrent-ratio torrent)))
(when (torrent-date-finished torrent)
(setf (torrent-date-finished torrent)
(string-trim '(#\space) (torrent-date-finished torrent)))
(setf (torrent-date-finished torrent)
(or (string-to-universal-time (torrent-date-finished torrent))
(error "Couldn't parse date: ~s"
(torrent-date-finished torrent))))
;; The number of seconds we have been seeding this torrent
(if* (and (string= "Finished" (torrent-state torrent))
;; Only private trackers provide this:
(torrent-tracker-seed-time torrent))
then ;; use the tracker's value of time seeded, if it's available
(when (not (=~ "\\((\\d+) seconds\\)"
(torrent-tracker-seed-time torrent)))
(error "Could not parse tracker 'Seeding Time': ~a."
(torrent-tracker-seed-time torrent)))
(setf (torrent-seeded-for torrent)
(parse-integer $1))
else ;; still seeding or done but tracker didn't tell us how
;; long it was seeded
(setf (torrent-seeded-for torrent)
(- *now* (torrent-date-finished torrent)))))
(let ((name (transmission-filename-to-tracker (torrent-filename torrent)
:hash (torrent-hash torrent)
:debug *debug*)))
(setf (torrent-tracker-name torrent) name)
(when name
(setf (torrent-tracker-instance torrent)
(tracker-name-to-instance name))))
;;;; Use torrent data to determine status
;; Determine if this torrent is "done" and can be removed.
;; Usually this is if either 1) seeding is complete, or 2) we have
;; seeded the torrent for *minimum-seed-seconds* seconds, but there
;; are exceptions and complications. See below for the exact rules.
;; Check for handlers for specific trackers. If they are, give
;; the handler the `torrent' object and let it possibly set
;; various times.
;;
(if* (null (torrent-tracker-name torrent))
thenret ;; (warn "null tracker: ~a" torrent)
elseif (dolist (tracker *all-trackers* t)
(when (match-re (tracker-re tracker)
(torrent-tracker-name torrent)
:return nil)
(setf (torrent-tracker-char torrent) (tracker-char tracker))
(funcall (tracker-setter tracker) torrent)
(return nil)))
then ;; Didn't match a tracker
(warn "Couldn't match tracker (~a) for ~a."
(torrent-tracker-name torrent)
(torrent-name torrent)))
(when (symbolp (torrent-ratio torrent))
;; Inf or None, either way, call ratio 0.0.
(setf (torrent-ratio torrent) 0.0))
;; First, determine if seeding is complete.
;; transmission-remote doesn't give us a "seeding complete"
;; indication, so we use "Ratio" >= "Ratio Limit".
(when (or (>= (torrent-ratio torrent)
(torrent-ratio-limit torrent))
(string= "Finished" (torrent-state torrent)))
(push (cons torrent :complete-ratio) print-done)
(remove-torrent torrent)
(go :next))
;; Seeding is not complete. See if we've seeded for the minimum
;; amount of time.
(if* (null (torrent-seeded-for torrent))
then (push (cons torrent :seeding) print-other)
elseif (> (torrent-seeded-for torrent)
(or
;; Prefer the torrent-specific value over the global one
(torrent-seed-min-time torrent)
*minimum-seed-seconds*))
then (push (cons torrent :complete-time) print-done)
(remove-torrent torrent)
else (with-verbosity 1
(setf (torrent-seed-for torrent)
(- (or (torrent-seed-min-time torrent)
*minimum-seed-seconds*)
(torrent-seeded-for torrent)))
(push (cons torrent :seeding) print-other)))
)
:next
)
(when print-done
(setq print-done (nreverse print-done))
(when (not remove-bad)
(if* *remove-seeded*
then (format t "These torrents were removed:~%~%")
else (format t "These torrents are complete:~%~%")))
(let ((header t))
(dolist (item print-done)
(destructuring-bind (torrent . status) item
(print-torrent torrent status :brief t :header header)
(setq header nil))) ))
(when (not remove-bad)
(when print-other
(setq print-other (nreverse print-other))
(format t "~%These torrents are incomplete:~%~%")
(let ((header t))
(dolist (item print-other)
(destructuring-bind (torrent . status) item
(print-torrent torrent status :brief t :header header)
(setq header nil)))))))
(defun print-torrent (torrent status
&key brief header
&aux (name (torrent-name torrent)))
(and *debug* (format t "~s~%" torrent))
(cond
(brief
(when header
(format t "~2a~41a~6a~6a~12a~12a~%"
"T" "name" "%done" "ratio" "seeded" "left"))
(format t "~2a~41a~6a~@[~6a~]~@[~12a~]~@[~12@a~]~%"
(or (torrent-tracker-char torrent) "")
;; truncate to 40
(if (> (length name) 40) (subseq name 0 40) name)
(torrent-percent-done torrent)
(if* (eq :error status)
then "Error"
elseif (not (eq :incomplete status))
then (format nil "~,2f" (torrent-ratio torrent)))
(if* (eq :error status)
then "Error"
elseif (not (eq :incomplete status))
then (relative-time-formatter (torrent-seeded-for torrent)
:brief t))
(when (eq :seeding status)
(relative-time-formatter (torrent-seed-for torrent)
:brief t))))
(t
(format t "~%~a~%" name)
(ecase status
(:error (format t " ERROR: ~a~%" (torrent-error torrent)))
(:incomplete
(format t " incomplete: ~a done~%" (torrent-percent-done torrent)))
(:complete-ratio
(format t " COMPLETE: seeded to ~,2f (seeded for ~a)~%"
(torrent-ratio torrent)
(relative-time-formatter (torrent-seeded-for torrent))))
(:complete-time
(format t " COMPLETE: seeded for ~a (ratio: ~,2f)~%"
(relative-time-formatter (torrent-seeded-for torrent))
(torrent-ratio torrent)))
(:seeding
(format t " incomplete: ratio: ~,2f (target ~,2f)~%"
(torrent-ratio torrent)
(torrent-ratio-limit torrent))
(format t " seeded for ~a~%"
(relative-time-formatter (torrent-seeded-for torrent)))
(format t " to go: ~a~%"
(relative-time-formatter (torrent-seed-for torrent))))))))
(defun relative-time-formatter (seconds &key brief)
(if* (null seconds)
then "---"
elseif brief
then (if* (> seconds #.(* 3600 24))
then (universal-time-to-string
(+ *now* seconds)
:relative *now*
:format (ut-to-string-formatter "%Dd %2H:%2M:%2S"))
else (universal-time-to-string
(+ *now* seconds)
:relative *now*
:format (ut-to-string-formatter "%2H:%2M:%2S")))
elseif (> seconds #.(* 3600 24))
then ;; more than a day, included days
(universal-time-to-string
(+ *now* seconds)
:relative *now*
:format (ut-to-string-formatter "%D day%p, %2H:%2M:%2S"))
else (universal-time-to-string
(+ *now* seconds)
:relative *now*
:format (ut-to-string-formatter "%2H:%2M:%2S"))))
(defun remove-torrent (torrent &key delete-episode &aux res)
(when (or delete-episode *remove-seeded*)
(if* (and (setq res (tm "-t" (torrent-id torrent) "-r"))
(=~ "success" (car res)))
then (setf (torrent-removed torrent) t)
t
else (error "Failed to remove ~a." (torrent-filename torrent)))
;; If the torrent became `unregistered' then delete it, since there was
;; something wrong with it.
(when delete-episode
(let* ((series-name (torrent-series-name torrent))
(season (torrent-season torrent))
(epnum (torrent-episode torrent))
series
ep)
(if* (and series-name season epnum)
then (setq series-name (canonicalize-series-name series-name))
(if* (setq series (query-series-name-to-series series-name))
then (setq ep
(query-episode :series-name (series-name series)
:season season
:ep-number epnum))
(if* (and ep (cdr ep))
then (warn "remove-torrent: got more than one ep: ~s"
ep)
elseif ep
then (setq ep (car ep))
(format t "~
NOTE: removing broken episode:
~a
Do \"tget --run\" to see if it is downloaded, and, if not,
download it manually.~%"
ep)
(delete-episode-1 ep)
else (warn "remove-torrent: unknown ep: ~s ~s ~s"
series season epnum))
else (warn "remove-torrent: could not find series from ~s"
series-name))
else (warn "remove-torrent: could not find episode from torrent: ~s"
torrent))))))
(defun get-torrent-info (key hash &key missing-ok)
(or (gethash key hash)
(if* missing-ok
then nil
else (error "Couldn't find ~a in torrent data" key))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; PART II: remove files which have been watched
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; variables from the config file
(defvar *ignore-watched-within*
"Ignore any torrents which have been watched within this number of hours.
The default is 72 hours, or 3 days."
#.(* 24 3))
(defvar *sqlite3-binary* "sqlite3")
(defvar *watch-directories* nil)
(defvar *plex-db*
"/me/tplex/plex-config/Library/Application Support/Plex Media Server/Plug-in Support/Databases/com.plexapp.plugins.library.db")
(defvar *watched-hash-table*
;; A hash table of the videos which have been watched. This is done by
;; looking at the Plex db.
;;
;; key :: path to video
;; value :: hours since video was watched
nil)
(defvar *plex-files-hash-table*
;; A hash table of all the files known by the Plex db.
;;
;; key :: path to video
;; value :: path to video
nil)
(defun tcleanup-files (&aux (initial-newline t) header
(check-for-empty-dirs '())
(symlink-pass t))
(flet ((announce (format-string &rest args)
(when initial-newline
(format t "~%")
(setq initial-newline nil))
(when header
(format t "~a~%" header)
(setq header nil))
(apply #'format t format-string args)))
;; Initialize a hash table of watched videos and files in plex
(initialize-watched)
(initialize-plex-files)
;; Now delete watched videos and they associated aux files (subtitles,
;; etc).
(dolist (thing *watch-directories*)
(destructuring-bind (directory container-directory . label) thing
(declare (ignore container-directory))
(setq header (format nil "~a:" label))
(map-over-directory
(lambda (p &aux series-name series archive p-aux-files)
(when (member (pathname-type p) *video-types* :test #'equalp)
(cond
((gethash (file-namestring p) *torrents*)
;; seeding torrent, ignore
)
((and (setq series-name
(extract-episode-info-from-filename
(file-namestring p)
:episode-required nil))
(setq series
(query-series-name-to-series series-name))
(series-never-delete series))
;; never delete, ignore
)
(t
(setq archive (and series (series-archive series)))
(when (and archive (not (probe-file archive)))
(.error "archive directory does not exist: ~a.~%" archive))
;;;;;;;;;; P is a candidate for cleanup
;; Find aux files so we can deal with them and P as a unit
(setq p-aux-files (find-aux-files p))
#+debugging
(when p-aux-files
(format t "p=~a~%" p)
(format t "aux=~s~%" p-aux-files))
(multiple-value-bind (ready-to-remove reason) (watchedp p)
(if* ready-to-remove
then
;;;;;;;;;; P is ready to cleanup
(if* *remove-watched*
then (cleanup-file p #'announce :move-to archive)
(when p-aux-files
(push p check-for-empty-dirs)
(dolist (aux-file p-aux-files)
(cleanup-file aux-file #'announce
:move-to archive)))
;;;;;;;;;; not in cleanup mode
elseif archive
then (announce "YES/ARCHIVE ~a:~a~%"
reason (file-namestring p))
(dolist (aux-file p-aux-files)
(announce " aux: ~a~%"
(file-namestring aux-file)))
else (announce "YES ~a:~a~%"
reason (file-namestring p))
(dolist (aux-file p-aux-files)
(announce " aux: ~a~%"
(file-namestring aux-file))))
elseif (null reason)
thenret ;; not watched
else (with-verbosity 1
;; Watched, but not ready to remove for some reason
(announce "NO ~a:~a~%" reason (file-namestring p))
(dolist (aux-file p-aux-files)
(announce " aux: ~a~%"
(file-namestring aux-file))))))
;; do we need this anymore????
(when (and (not (gethash (file-namestring p)
*plex-files-hash-table*))
(probe-file p))
(announce "HIDDEN: ~a~%" (file-namestring p)))))))
(pathname-as-directory directory)
:recurse t
:include-directories nil)
(when symlink-pass
;; Look for bad symbolic links, now that we've possible removed some
;; files.
(map-over-directory
(lambda (p)
(when (and (symbolic-link-p p) (not (probe-file p)))
(announce "SYMLINK: ~a~%" (file-namestring p))
(when (not *debug*) (delete-file p))))
(pathname-as-directory directory)
:recurse t
:include-directories nil)))
(when check-for-empty-dirs
;; In the process of removing videos and their aux files, some of
;; the directories will now be empty. When downloading from some
;; sites (e.g., RARBG), movies are often in a directory with
;; certain files (subs, others). The goal is to remove the entire
;; download directory.
;;
;; CHECK-FOR-EMPTY-DIRS is a list of videos removed. We check for
;; an empty "Subs/" directory and for the directory of the video
;; being empty.
(dolist (p check-for-empty-dirs)
(let ((pdir (path-pathname p)))
(let ((subs (merge-pathnames "Subs/" pdir)))
(and (probe-file subs)
(ignore-errors (excl.osi:rmdir subs))
(announce "rmdir: ~a~%" subs))
(and (ignore-errors (excl.osi:rmdir pdir))
(announce "rmdir: ~a~%" pdir))))))
) ;; dolist on *watch-directories*
))
(defun cleanup-file (p announce
&key move-to
&aux aux-p rar)
(flet ((df (p)
(when (probe-file p)
(if* (file-directory-p p)
then ;; an aux-file subdir (subs, likely), which
;; should be empty, so just try to rmdir it
(when (and (not *debug*)
(not (ignore-errors (delete-directory p))))
(funcall announce "NOTE: directory not empty: ~a~%"
p))
elseif move-to
then (funcall announce "~@[Would do:~* ~]mv ~a ~a~%"
*debug* p move-to)
(when (not *debug*)
(rename-file p
(merge-pathnames
(file-namestring p)
(pathname-as-directory move-to))))
else (funcall announce "~@[Would do:~* ~]rm ~a~%" *debug* p)
(when (not *debug*) (delete-file p))))))
(if* (setq aux-p (symbolic-link-p p))
then ;; delete the symlink and what it points to
(setq aux-p (merge-pathnames aux-p p))
(df aux-p)
(df p)
elseif (or (probe-file (setq rar (merge-pathnames #p(:type "rar") p)))
(probe-file (fiddle-case-filename rar :downcase))
(probe-file (fiddle-case-filename rar :upcase)))
then ;; the watched file came from a downloaded rar file, remove
;; everything else, too
(let* ((name (pathname-name p))
(wildcard (merge-pathnames (merge-pathnames "*.*" p)))
(files (directory wildcard)))
(dolist (file files)
(when (equalp name (pathname-name file))
(df file))))
(df p)
else (df p))))
(defun fiddle-case-filename (p direction)
(merge-pathnames
(make-pathname
:name (funcall (if* (eq :upcase direction)
then #'string-upcase
else #'string-downcase)
(pathname-name p))
:type (pathname-type p))
p))
(defun initialize-watched ()
(or (probe-file *plex-db*)
(error "Couldn't find PMS database."))
(or (excl.osi:find-in-path *sqlite3-binary*)
(error "Couldn't find command ~s." *sqlite3-binary*))
(setq *watched-hash-table*
(if* *watched-hash-table*
then (clrhash *watched-hash-table*)
else (make-hash-table :size 777 :test #'equal)))
(let* ((sqlite-cmd
;; Use a vector so shell escaping isn't an issue, with spaces
;; in those filenames, etc.
(vector *sqlite3-binary* "-csv" (namestring (truename *plex-db*))))
(nl #\newline)
(sql
(util.string:string+
"select p.file,s.last_viewed_at" nl
"from media_parts p, media_items mi, metadata_items md,metadata_item_settings s" nl
"where mi.id = p.media_item_id AND" nl
" md.id = mi.metadata_item_id AND" nl
" md.guid = s.guid AND" nl
" s.view_count > 0;" nl))
lines)
(multiple-value-bind (stdout stderr exit-code)
(command-output sqlite-cmd :input sql :whole t)
(if* (/= 0 exit-code)
then (error "exit code is ~s: stdout is ~a, stderr is: ~a."
exit-code stdout stderr)
elseif (or (null stdout) (string= "" stdout))
then ;; No watched shows?? I guess it's possible
(return-from initialize-watched nil))
(when (not (setq lines (split-re "$" stdout :multiple-lines t)))
(error "could not split sqlite3 output."))
(dolist (line lines)
(when (=~ "^\s*$" line) (return))
(when (not (=~ "\\s*(.*)\\|(.*)\\s*" line))
(error "Could not parse sqlite3 output: ~a." line))
;;(format t "file=~s~%" $1)
;;(format t " exists=~s~%" (probe-file $1))
(let* ((file $1)
(date (or (excl:string-to-universal-time $2)
(error "couldn't parse date: ~a." $2)))
(hours (truncate (/ (- *now* date) 3600))))
(with-verbosity 2
(format t "add watched: ~s, ~s~%" file hours))
(setf (gethash (file-namestring file) *watched-hash-table*)
hours))))))
(defun initialize-plex-files ()
(or (probe-file *plex-db*)
(error "Couldn't find PMS database."))
(or (excl.osi:find-in-path *sqlite3-binary*)
(error "Couldn't find command ~s." *sqlite3-binary*))
(setq *plex-files-hash-table*
(if* *plex-files-hash-table*
then (clrhash *plex-files-hash-table*)
else (make-hash-table :size 777 :test #'equal)))
(let* ((sqlite-cmd
;; Use a vector so shell escaping isn't an issue, with spaces
;; in those filenames, etc.
(vector *sqlite3-binary* "-csv" (namestring (truename *plex-db*))))
(nl #\newline)
(sql (util.string:string+ "select p.file from media_parts p;" nl))
files)
(multiple-value-bind (stdout stderr exit-code)
(command-output sqlite-cmd :input sql :whole t)
(if* (/= 0 exit-code)
then (error "exit code is ~s: stderr is: ~a." exit-code stderr)
elseif (or (null stdout) (string= "" stdout))
then ;; No files?? I guess it's possible
(return-from initialize-plex-files nil))
(when (not (setq files (split-re "$" stdout :multiple-lines t)))
(error "could not split sqlite3 output."))
(dolist (file files)
(when (=~ "^\s*$" file) (go skip))
(setq file (string-trim '(#\space #\newline) file))
(when (file-in-watched-directory-p file)
(setq file (file-namestring file))
(with-verbosity 2
(format t "add file: ~s~%" file))
(setf (gethash file *plex-files-hash-table*) file))
skip
))))
(eval-when (eval load compile) (require :strlib))
(defun file-in-watched-directory-p (file)
(dolist (thing *watch-directories*)
(destructuring-bind (directory container-directory . label) thing
(declare (ignore directory label))
(when (prefixp container-directory file)
(return t)))))
#+ignore ;; unused, keep tho
(defun escape-for-sqlite (filename)
;; single quotes are doubled
(replace-re (namestring filename) "'" "''"))
(defun watchedp (p &aux (file (namestring p)))
;; Return non-nil if the video given by P (a pathname) has been watched.
;; Return values are: ready-to-remove description
;;
(let ((hours (gethash (file-namestring file) *watched-hash-table*)))
;; Return if not watched
(when (not hours) (return-from watchedp nil))
;; Return if seeding
(when (seedingp file)
(return-from watchedp (values nil "seeding")))
(when (< hours *ignore-watched-within*)
(return-from watchedp
(values nil
(format nil "~dh<~dh" hours *ignore-watched-within*))))
;; Meets our time-based criteria for removal
(return-from watchedp
(values
t ;; yes, remove it
(if* (> hours 24)
then (format nil ">~dd" (truncate (/ hours 24)))
else (format nil ">~dh" hours))))))
;;(trace directory)
(defun find-aux-files (p)
;; P is a video. Our job is to find and return a list of all the aux
;; files related to this video.
;;
;; There are two cases:
;; 1. video in main directory with .srt that has same basename
;; 2. video in subdirectory with several aux files.
(labels
((simple-probe-match (p &aux tmp (res '()))
;; See if there's a .srt file with the same name as P.
;; Returns nil or a list of the matched file.
(dolist (type '("nfo" "sub" "srt" "nfo"))
(when (probe-file
(setq tmp (merge-pathnames (make-pathname :type type) p)))
(push tmp res)))
res)
(search-match (p &aux wildcard)
;; Look for .srt files with a similar name to P
;; Returns nil or a list of the matched files.
(setq wildcard (format nil "~a*.srt" (pathname-name p)))
(directory wildcard :directories-are-files nil))
(complex-match (p files &aux (aux-files-res '()))
(when files
(let* ((re (load-time-value
(compile-re "(.*)([Ss]\\d\\d[. ]?[Ee]\\d\\d).*")))
(p-before)
(p-ep)
(p-is-series-p
(multiple-value-bind (found whole before ep)
(match-re re (file-namestring p))
(declare (ignore whole))
(when found
(setq p-before before)
(setq p-ep ep)))))
(dolist (file files)
(when (or (not p-is-series-p)
(multiple-value-bind (matched whole before ep)
(match-re re (file-namestring file))
(declare (ignore whole))
(when (and matched
(string-equal before p-before)
(string-equal ep p-ep))
t)))
(pushnew file aux-files-res :test #'equalp)))))
aux-files-res)
)
(let ((aux-files '())
temp)
;; case (1)
(dolist (file (simple-probe-match p))
(pushnew file aux-files :test #'equalp))
(dolist (file (complex-match p (search-match p)))
(pushnew file aux-files :test #'equalp))
;; case (2)
(dolist (type '("srt" "idx" "sub"))
(when (probe-file (setq temp
(merge-pathnames (make-pathname :type type)
p)))
(pushnew temp aux-files :test #'equalp)))
(dolist (file '("RARBG.txt" "RARBG.com.txt" "RARBG_DO_NOT_MIRROR.exe"))
(when (probe-file (setq temp (merge-pathnames file p)))
(pushnew temp aux-files :test #'equalp)))
(when (probe-file (setq temp
(merge-pathnames "Subs/"
(path-pathname p))))
(dolist (file (complex-match p (directory temp)))
(when (file-directory-p file)
(dolist (f (directory (pathname-as-directory file)))
(pushnew f aux-files :test #'equalp)))
(pushnew file aux-files :test #'equalp)))
(nreverse aux-files))))
(defun seedingp (file &aux name)
;; Return non-nil if we are seeding FILE. Need to be careful, though,
;; since we might be looking at a symlink and not the original file being
;; seeded.
(if* (setq name (symbolic-link-p file))
then (setq name (file-namestring name))
else (setq name (file-namestring file)))
(let ((torrent (gethash name *torrents*)))
(and torrent (null (torrent-removed torrent)))))