-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
beet-cleanup-cover-only-directories
executable file
·88 lines (71 loc) · 2.55 KB
/
beet-cleanup-cover-only-directories
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
#!/usr/bin/perl
# abstract: clean up after beet imports that leave only a coverfile after a 'Remove old' action
# Also report directories where the content adds up to no more than 2M
# Also report directories with only nfo, sfv, m3u files
# Write to the console and a logfile than can be examined before being executed
# as a shell script
# Paths and special shell characters are quoted properly
# The shell script will delete itself once executed
use strict;
use warnings;
use Cwd qw(abs_path);
use File::Find;
use File::LsColor qw(ls_color);
my $start_path = shift // '.';
my $max_size = 2 * 1024 * 1024; # 2 MB
my $logfile = 'dirs_with_only_cover_in_them_or_less_than_2M.log';
open(my $fh, '>', $logfile) or die "Can't open $logfile: $!";
$|++;
my $to_delete = 0;
sub check_dir {
my $dir = $_;
return unless -d $dir;
# Check for only one file, cover.jpg
opendir(my $dh, $dir) or die "Can't open directory $dir: $!";
my @files = grep { !/^\.\.?$/ } readdir($dh);
closedir($dh);
if (@files == 1 && $files[0] =~ m/(cover[.](?:jpe?g|png))/i) {
my $cover_file = $1;
my $abspath = abs_path($dir);
print sprintf("only %s: %s\n", ls_color($cover_file), ls_color($abspath));
print $fh sprintf("rm -rv @{[ quotemeta($abspath) ]}\n");
$to_delete++;
return;
}
# handle leftover m3u+nfo+sfv files
elsif (scalar @files == scalar grep { /[.](?:m3u|nfo|sfv)$/ } @files) {
my $abspath = abs_path($dir);
print sprintf("only {nfo,sfv,m3u}: %s\n", ls_color($abspath));
print $fh sprintf("rm -rv @{[ quotemeta($abspath) ]}\n");
$to_delete++;
return;
}
# handle cases with leftover m3u+nfo+sfv + a random cover file
elsif (scalar @files == scalar grep { /[.](?:m3u|nfo|sfv|jpg)$/ } @files) {
my $abspath = abs_path($dir);
print sprintf("only {nfo,sfv,m3u,jpg}: %s\n", ls_color($abspath));
print $fh sprintf("rm -rv @{[ quotemeta($abspath) ]}\n");
$to_delete++;
return;
}
# Check for size less than 2M
my $size = 0;
find(sub { $size += -s if -f }, $dir);
# skip check of +tracks/ directory, AND make sure not to output the artist
# directory itself if it consists only of this +tracks/ directory
if ($size < $max_size and $dir ne '+tracks' and $dir =~ m/^[+]/) {
my $abspath = abs_path($dir);
print sprintf("< 2M %s\n", ls_color($abspath));
print $fh sprintf("rm -rv @{[ quotemeta($abspath) ]}\n");
$to_delete++;
}
}
finddepth(\&check_dir, $start_path);
# remove the generated bash script when it's executed
if($to_delete) {
print $fh 'rm -v $0';
close $fh;
}
else {
unlink $logfile;
}