Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 244 lines (214 sloc) 7.983 kb
d4631b9 nouvelle version 204
Klink authored
1 #!/usr/bin/perl
2 #
f98e365 Cleanups
Klink authored
3 # Copyright (c) 2012 Mathieu Roy <yeupou--gnu.org>
4 # http://yeupou.wordpress.com
d4631b9 nouvelle version 204
Klink authored
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 # USA
20
21 use strict "vars";
22 use Fcntl ':flock';
23 use POSIX qw(strftime);
24 use File::Basename;
06fedc4 nouvelle version 207
Klink authored
25 use File::Copy;
c0f919b New release 311
Klink authored
26 use Getopt::Long;
e875b71 New prerelease 3 (on top of 3.28)
Klink authored
27 use Term::ANSIColor qw(:constants);
d4631b9 nouvelle version 204
Klink authored
28
ab14394 New release 334
Klink authored
29 # config:
d4631b9 nouvelle version 204
Klink authored
30 my $user = "klink";
d26a676 New release 275
Klink authored
31 my $maindir = "/storage/abstract/musique";
32 my $importdir = "/storage/abstract/musique/.A TRIER";
d43c12a New release 339
Klink authored
33 my $debug = 0;
c0f919b New release 311
Klink authored
34 my $getopt;
e875b71 New prerelease 3 (on top of 3.28)
Klink authored
35 my $wentwell = 1;
c0f919b New release 311
Klink authored
36
37
38 # get standard opts with getopt
39 eval {
40 $getopt = GetOptions("debug" => \$debug);
41 };
42
13feb92 New release 320
Klink authored
43 if ($debug) {
f98e365 Cleanups
Klink authored
44 print "DEBUG MODE: (type enter)\n";
13feb92 New release 320
Klink authored
45 <STDIN>;
46 }
d4631b9 nouvelle version 204
Klink authored
47
48 # enter working directories
49 chdir($maindir) or die "Unable to enter $maindir. Exit";
50 chdir($importdir) or die "Unable to enter $importdir. Exit";
51
52 # in $importdir, there should be dirs containing, each one, an album,
53 # with a file named import with the following content
54 # style|band|album|year
55 opendir(IMPORT, $importdir);
56 while (defined(my $dir = readdir(IMPORT))) {
57 # silently ignores anything but standard directories
58 next unless -d $dir;
59 next if $dir eq "." or $dir eq "..";
60
61 # ignores directories with no import file within
e875b71 New prerelease 3 (on top of 3.28)
Klink authored
62 print ON_RED, WHITE, "No $dir/import (style|band|year|album), skip directory.\n", RESET unless -e "$dir/import";
d4631b9 nouvelle version 204
Klink authored
63 next unless -e "$dir/import";
64
62de8d8 New release 317
Klink authored
65 # ignores flagged directories
e875b71 New prerelease 3 (on top of 3.28)
Klink authored
66 print ON_RED, WHITE, "$dir/ignore exists, skip directory.\n", RESET if -e "$dir/ignore";
62de8d8 New release 317
Klink authored
67 next if -e "$dir/ignore";
68
69
d4631b9 nouvelle version 204
Klink authored
70 # otherwise, find out band name and all
71 open(ALBUMINFO, "< $dir/import");
72 my $style;
ab14394 New release 334
Klink authored
73 my $is_va = 0;
d4631b9 nouvelle version 204
Klink authored
74 my $band;
75 my $album;
76 my $year = "0000";
77 while (<ALBUMINFO>) {
78 chomp($_);
f2b3bd5 New release 219
Klink authored
79 ($style,$band,$year,$album) = split(/\|/, $_);
7a81c38 New release 315
Klink authored
80 last;
d4631b9 nouvelle version 204
Klink authored
81 }
d26a676 New release 275
Klink authored
82 close(ALBUMINFO);
83
a4205ed New release 314
Klink authored
84 # check we have something valid
e875b71 New prerelease 3 (on top of 3.28)
Klink authored
85 unless ($style and $band and $album) {
86 print ON_RED, WHITE, "Unable to get any info from the existing $dir/import file. style = $style; band = $band ; album = $album. Skip directory.\n", RESET;
87 next;
88 }
a4205ed New release 314
Klink authored
89
ab14394 New release 334
Klink authored
90 # various artists case
349f94a New release 340
Klink authored
91 $is_va = 1 if ($band eq "-----VARIOUS ARTISTS-----");
ab14394 New release 334
Klink authored
92
81214f8 New release 313
Klink authored
93 # create the destination directory,
ab14394 New release 334
Klink authored
94 # FIXME: seems to be some issues with accentued characters, not sure
95 # why.
81214f8 New release 313
Klink authored
96 my $destdir = "$maindir/$style/$band/$album";
97 $destdir = "$maindir/$style/$band/$year-$album" if $year;
ab14394 New release 334
Klink authored
98 $destdir = "$maindir/$style/$album" if $is_va;
81214f8 New release 313
Klink authored
99
100 if (-d "$destdir") {
e875b71 New prerelease 3 (on top of 3.28)
Klink authored
101 print ON_RED, WHITE, "$destdir already exists (check and type enter if ready)!\n", RESET;
81214f8 New release 313
Klink authored
102 <STDIN>;
103 } else {
104 system("/bin/mkdir", "-p", $destdir) unless $debug;
105 print "/bin/mkdir -p $destdir\n";
106 }
d4631b9 nouvelle version 204
Klink authored
107
108 # now deals with each file within:
109 # - move images to new dir
110 # - ogg or mp3, update tags, rename
111 opendir(ALBUMDIR, $dir);
112 while (defined(my $file = readdir(ALBUMDIR))) {
113 # ignore dirs
114 next if -d $file;
115
116 # find out suffix, ignore file if none found
117 my $suffix = 0;
118 my $realfile;
fcc0c21 New release 316
Klink authored
119 if ($file =~ /^(.*)(\.[^.]*)$/) { $suffix = lc($2); $realfile = $1; }
d4631b9 nouvelle version 204
Klink authored
120 next unless $suffix && $realfile;
121
122 # if image, simply move it
1a6fc42 nouvelle version 209
Klink authored
123 if ($suffix eq ".png" or $suffix eq ".jpg") {
51368c6 New release 277
Klink authored
124 print "mv $file $destdir/\n";
ab14394 New release 334
Klink authored
125 move("$importdir/$dir/$file", "$destdir") unless $debug;
1a6fc42 nouvelle version 209
Klink authored
126 }
127
d4631b9 nouvelle version 204
Klink authored
128 # if mp3 or ogg, use lltag to update tag and rename
51368c6 New release 277
Klink authored
129 if ($suffix eq ".ogg" or $suffix eq ".mp3" or $suffix eq ".flac") {
c72fbbc New release 321
Klink authored
130
51a5944 New release 342
Klink authored
131 # default name scheme
a12ce3f New release 345
Klink authored
132 my $naming = "%a-%d-%A-%n-%t";
51a5944 New release 342
Klink authored
133
e875b71 New prerelease 3 (on top of 3.28)
Klink authored
134 # lltag fails sometimes to find out the NUMBER and TITLE tags
135 # values on the fly. Extract them
136 # beforehand to avoid any trouble
c72fbbc New release 321
Klink authored
137 my @lltag_opts = ();
b58554a New prerelease 5 (on top of 3.28)
Klink authored
138 print ON_BLUE, WHITE, "Extract TITLE and NUMBER tags from $file... ", RESET;
bc0fd3f New release 3.32
Klink authored
139 open(ALBUMINFO, "lltag --id3v2 -S \"$importdir/$dir/$file\" |");
e875b71 New prerelease 3 (on top of 3.28)
Klink authored
140 my ($title, $number);
141 while(<ALBUMINFO>) {
142 $title = $1 if /\sTITLE=(.*)$/i;
143 $number = $1 if /\sTRACKNUMBER=(.*)$/i;
3b2fdb6 New prerelease 1 (on top of 3.29)
Klink authored
144 $number = $1 if /\sNUMBER=(.*)$/i;
e875b71 New prerelease 3 (on top of 3.28)
Klink authored
145 last if ($title and $number);
c72fbbc New release 321
Klink authored
146 }
e875b71 New prerelease 3 (on top of 3.28)
Klink authored
147 close(ALBUMINFO);
b58554a New prerelease 5 (on top of 3.28)
Klink authored
148 print ON_BLUE, WHITE, "$number, $title\n", RESET;
e875b71 New prerelease 3 (on top of 3.28)
Klink authored
149 if ($title eq "") {
150 # missing title is always a no-go
151 print ON_RED, WHITE, "Something is very wrong with $dir/$file, we failed to extract the title of the current song. Skip file.\n", RESET;
3b92311 New release 3.29
Klink authored
152 $wentwell = 0;
e875b71 New prerelease 3 (on top of 3.28)
Klink authored
153 next;
154 }
3fbb628 New prerelease 7 (on top of 3.28)
Klink authored
155 if ($number eq "") {
156 # track number is weird but acceptable, ask the user
157 # after trying some lucky guess
6d2f6f5 New prerelease 13 (on top of 3.28)
Klink authored
158 $number = $2 if $file =~ /(^|\D)(\d\d?)\D/;
6092f01 New prerelease 8 (on top of 3.28)
Klink authored
159 print BOLD, "Weird, we have no track number for this one.\n", RESET, "Care to provide some?\n(type ENTER to use the lucky guess ", BOLD, "$number", RESET, ")\n";
a42cd4d New prerelease 12 (on top of 3.28)
Klink authored
160 my $stdin;
161 chomp($stdin = <STDIN>);
162 $number = $stdin unless $stdin eq "";
3fbb628 New prerelease 7 (on top of 3.28)
Klink authored
163 }
f7a64c3 New prerelease 11 (on top of 3.28)
Klink authored
164 @lltag_opts = ("--TITLE", $title,
165 "--NUMBER", $number);
e875b71 New prerelease 3 (on top of 3.28)
Klink authored
166
ab14394 New release 334
Klink authored
167 # Various artists
168 if ($is_va) {
169 # always extract the correct band name
170 # (yes, not uberclean to call so many times lltag, but let's
171 # keep it stupid/simple)
1110cc4 New release 341
Klink authored
172 $band = "";
b58554a New prerelease 5 (on top of 3.28)
Klink authored
173 print ON_BLUE, WHITE, "Extract BAND from $file (various artists)... ", RESET;
bc0fd3f New release 3.32
Klink authored
174 open(ALBUMINFO, "lltag --id3v2 -S \"$importdir/$dir/$file\" |");
ab14394 New release 334
Klink authored
175 while(<ALBUMINFO>) {
176 $band = $1 if /\sARTIST=(.*)$/i;
177 last if $band;
178 }
179 close(ALBUMINFO);
b58554a New prerelease 5 (on top of 3.28)
Klink authored
180 print ON_BLUE, WHITE, "$band\n", RESET;
ab14394 New release 334
Klink authored
181
182 # add specific tags (try to set the usual ones)
183 push(@lltag_opts, ("--tag", "ALBUMARTIST=$album"), ("--tag", "TPE2=$album"));
51a5944 New release 342
Klink authored
184
185 # specific naming scheme
4006a2f New release 347
Klink authored
186 $naming = "%A-%d-%n-%a-%t";
51a5944 New release 342
Klink authored
187
e875b71 New prerelease 3 (on top of 3.28)
Klink authored
188 if ($band eq "") {
189 # missing band is a no-go here
190 print ON_RED, WHITE, "Something is very wrong with $dir/$file, we failed to extract the band of the current song while it is VA. Skip file.\n", RESET;
3b92311 New release 3.29
Klink authored
191 $wentwell = 0;
e875b71 New prerelease 3 (on top of 3.28)
Klink authored
192 next;
193 }
ab14394 New release 334
Klink authored
194 }
2f16249 New release 319
Klink authored
195
c72fbbc New release 321
Klink authored
196 if ($debug) {
2f16249 New release 319
Klink authored
197 system("lltag", "--dry-run", "--preserve-time", "--yes",
198 "--id3v2",
199 "--ARTIST", $band,
200 "--ALBUM", $album,
201 "--DATE", $year,
202 "--maj",
203 "--GENRE", $style,
c72fbbc New release 321
Klink authored
204 @lltag_opts,
2f16249 New release 319
Klink authored
205 "--rename-min",
206 "--rename-slash", "_",
b6ec1aa New release 346
Klink authored
207 "--rename", "$destdir/$naming",
2f16249 New release 319
Klink authored
208 "$importdir/$dir/$file");
209 <STDIN>;
210 } else {
211 system("lltag", "--preserve-time", "--yes", "--quiet",
212 "--id3v2",
213 "--ARTIST", $band,
214 "--ALBUM", $album,
215 "--DATE", $year,
216 "--maj",
217 "--GENRE", $style,
c72fbbc New release 321
Klink authored
218 @lltag_opts,
2f16249 New release 319
Klink authored
219 "--rename-min",
220 "--rename-slash", "_",
b6ec1aa New release 346
Klink authored
221 "--rename", "$destdir/$naming",
2f16249 New release 319
Klink authored
222 "$importdir/$dir/$file");
223 }
1a6fc42 nouvelle version 209
Klink authored
224 }
d4631b9 nouvelle version 204
Klink authored
225 }
d26a676 New release 275
Klink authored
226 closedir(ALBUMDIR);
d4631b9 nouvelle version 204
Klink authored
227
751a6f6 nouvelle version 210
Klink authored
228 # more cleanups
6965f5f Obsoletes darius-tools, filenames cleanup.
Klink authored
229 print "/usr/local/bin/urlize -D $destdir\n";
230 system("/usr/local/bin/urlize", "-D", $destdir) unless $debug;
0e86d3b New release 306
Klink authored
231 print "/bin/chown -R $user:$user $maindir/$style/band/\n";
232 system("/bin/chown", "-R", "$user:$user", "$maindir/$style/$band/") unless $debug;
233 print "/bin/chmod -R a+r $maindir/$style/$band/\n";
234 system("/bin/chmod", "-R", "a+r", "$maindir/$style/$band/") unless $debug;
d4631b9 nouvelle version 204
Klink authored
235
fef2ec4 New release 303
Klink authored
236 # if we get here, everything was moved, we can safely eraze initial dir
e875b71 New prerelease 3 (on top of 3.28)
Klink authored
237 print "rm -rvf $importdir/$dir\n" if $wentwell;
238 print ON_RED, WHITE, "$importdir/$dir kept as it is, some files we're not renamed proper!", RESET unless $wentwell;
239 system("/bin/rm", "-rf", "$importdir/$dir") unless ($debug or !$wentwell);
d4631b9 nouvelle version 204
Klink authored
240 }
d26a676 New release 275
Klink authored
241 closedir(IMPORT);
d4631b9 nouvelle version 204
Klink authored
242
243 # EOF
Something went wrong with that request. Please try again.