summaryrefslogtreecommitdiffstats
path: root/bin/fileupdate.pl
blob: 4353d0575df952152ba81be6db5d39ed732ce4bb (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
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
#!/usr/bin/perl -w

# Find and file away MP3's.  Run multiple times and will
#  ignore addition of duplicates in db (based on MD5 hash
#  of full file path.

use FindBin qw($Bin);
require "$Bin/init";

use Data::Dumper;
use Getopt::Long;

use vars qw($help $pretend $id3 $rename $sort $all $verbose);

Getopt::Long::Configure('bundling','no_ignore_case');
GetOptions
    ("h|help"        => \&usage,
     "p|pretend"        => \$pretend,
     "i|id3"         => \$id3,
     "r|rename"      => \$rename,
     "s|sort"        => \$sort,
     "a|all"         => \$all,
     "rename_all"    => \$rename_all,
     "sort_all"	     => \$sort_all,
     "v|verbose"     => \$verbose);
 
if ( !$help && !$all && !$id3 && !$rename && !$sort && !$rename_all && !$sort_all ) {
	usage();
}

if ($help) { 
	usage();
}

if($id3 or $all)
{
    my @flagged = $ampache->get_table_where("flagged","WHERE type = 'setid3'");

    foreach my $update(@flagged)
    {
        my @info = $ampache->get_song_info($update->{'song'});
        my $cmd = update_id3_tag($ampache,@info);
        if($rename or $all)
        { 
            if($verbose){ print "Marking for rename after id3\n"; }
            if(!$pretend){ $ampache->change_flags(@info,'setid3','ren'); }
        }
        else
        {
            if($sort or $all)
            {
                if($verbose){ print "Marking for sort after id3\n"; }
                if(!$pretend){ $ampache->change_flags(@info,'setid3','sort'); }
            }
            else
            {
                if($verbose){ print "Stopping after id3 update\n"; }
                if(!$pretend){ $ampache->change_flags(@info,'setid3','notify'); }
            }
        }
    }
}

if($rename or $all)
{
    my $filename = '';
    my @flagged = $ampache->get_table_where("flagged","WHERE type = 'ren'");
    foreach my $update (@flagged)
    {
        my @info = $ampache->get_song_info($update->{'song'});
        my $cmd = rename_file($ampache,\$filename,@info);
        if(!$pretend){ $ampache->update_song($cmd,@info); }
        if($sort or $all)
        {
            if($verbose){ print "Marking for sort after rename\n"; }
            if(!$pretend){ $ampache->change_flags(@info,'ren','sort'); }
        }
        else
        {
            if($verbose){ print "Updating filename in DB after rename\n"; }
            if(!$pretend){ $ampache->change_flags(@info,'ren','notify'); }
        }
    }
}

if ($rename_all) { 
	my $filename = '';
	my @flagged = $ampache->get_table_where("catalog,song","WHERE catalog.catalog_type='local' AND catalog.id=song.catalog","song.id AS song");
	foreach my $update (@flagged) {
		my @info = $ampache->get_song_info($update->{'song'});
		my $cmd = rename_file($ampache,\$filename,@info);
		if(!$pretend){ $ampache->update_song($cmd,@info); }
	} # End Foreach
} # End Rename All

if ($sort_all) { 
    my $filename = '';
    my @flagged = $ampache->get_table_where("catalog,song","WHERE catalog.catalog_type='local' AND catalog.id=song.catalog","song.id AS song");
    foreach my $update(@flagged)
    {
        my @info = $ampache->get_song_info($update->{'song'});
        my $cmd = sort_file($ampache,\$filename,@info);
        if(!$pretend){ $ampache->update_song($cmd,@info); }
    if($verbose){ print "Updating filename in DB after sort\n"; }
    if(!$pretend){ $ampache->change_flags(@info,'sort','notify'); }
    }
} # End Sort ALL


if($sort or $all)
{
    my $filename = '';
    my @flagged = $ampache->get_table_where("flagged","WHERE type = 'sort'");
    foreach my $update(@flagged)
    {
        my @info = $ampache->get_song_info($update->{'song'});
        my $cmd = sort_file($ampache,\$filename,@info);
        if(!$pretend){ $ampache->update_song($cmd,@info); }
    if($verbose){ print "Updating filename in DB after sort\n"; }
    if(!$pretend){ $ampache->change_flags(@info,'sort','notify'); }
    }
}

# # # # #
# subs
# # # # # # #

# %A = album name
# %a = artist name
# %C = catalog path (for the specified song)
# %c = comment
# %g = genre
# %y = year
# %T = track number
# %t = song title
#
# %filename I use for filename

sub get_catalog_setting
{ 
    my ($self,$catalog,$setting) = @_;
    #bless $self;
    my $cmd = $self->get_catalog_option($catalog,$setting);
    return $cmd;
}

sub update_id3_tag 
{
    my ($self,$song) = @_;
    my $id3set = get_catalog_setting($self,$song->{'catalog'},'id3_set_command');
    $id3set =~ s/\Q%A\E/$song->{'album'}/g;
    $id3set =~ s/\Q%a\E/$song->{'artist'}/g;
    $id3set =~ s/\Q%C\E/$song->{'catalog'}/g;
    $id3set =~ s/\Q%c\E/$song->{'comment'}/g;
    if(($song->{'genre'} * 1) < 255){$id3set =~ s/\Q%g\E/$song->{'genre'}/g;}
    else{$id3set =~ s/ -g %g//g;}
    $id3set =~ s/\Q%T\E/$song->{'track'}/g;
    $id3set =~ s/\Q%t\E/$song->{'title'}/g;
    $id3set =~ s/\Q%y\E/$song->{'year'}/g;
    $id3set =~ s/\Q%filename\E//g;
    # $id3set =~ s/([\'\"])/\\$1/g;
    my $filename = $song->{'file'};
    my $id3tag_command = "$id3set \"$filename\"";
    return do_call($id3tag_command);
}

sub rename_file 
{
    my ($self,$filename,$song) = @_;
    my $ren_pattern = get_catalog_setting($self,$song->{'catalog'},'rename_pattern');
    #my $sort_pattern = get_catalog_setting($self,$song->{'catalog'},'sort_pattern'); 
    my $basedir;
    if( $song->{'file'} =~ m/^(.*)\/.*?$/ )
    {
        $basedir = $1;
    }
    else{ die "Could not determine base directory for $song->{'file'}\n"; }

    # We want to pad track numbers with leading zeros:
    if($song->{'track'} < 10)
    {
        $song->{'track'} = "0".$song->{'track'};
    }

    # we need to clean title,album,artist,comment,genre,track, and year
    $song->{'title'} =~ s/[\/]/-/g;
    $song->{'album'} =~ s/[\/]/-/g;
    $song->{'artist'} =~ s/[\/]/-/g;
    $song->{'comment'} =~ s/[\/]/-/g;
    $song->{'genre'} =~ s/[\/]/-/g;
    $song->{'track'} =~ s/[\/]/-/g;
    $song->{'year'} =~ s/[\/]/-/g;

    $ren_pattern =~ s/\Q%A\E/$song->{'album'}/g;
    $ren_pattern =~ s/\Q%a\E/$song->{'artist'}/g;
    $ren_pattern =~ s/\Q%C\E/$song->{'catalog'}/g;
    $ren_pattern =~ s/\Q%c\E/$song->{'comment'}/g;
    $ren_pattern =~ s/\Q%g\E/$song->{'genre'}/g;
    $ren_pattern =~ s/\Q%T\E/$song->{'track'}/g;
    $ren_pattern =~ s/\Q%t\E/$song->{'title'}/g;
    $ren_pattern =~ s/\Q%y\E/$song->{'year'}/g;
    $ren_pattern =~ s/\Q%filename\E/$song->{'file'}/g;
    my $oldfilename = $song->{'file'};
    my $newfilename = $basedir . "/" . $ren_pattern;
    # result is backslashes in filename
    # $newfilename =~ s/([\'\"])/\\$1/g;

	print "\tNew: $newfilename -- OLD: $oldfilename\n";

    if(! -e "$newfilename")
    {
        my $ren_command = "mv \"$oldfilename\" \"$newfilename\"";
        $filename = $newfilename;
        do_call($ren_command);
        return $filename;
    }
    else
    {
        print STDERR "File exists: $newfilename\n";
        $filename = $oldfilename;
        return $filename;
    }
}

sub sort_file 
{
    my ($self, $filename, $song) = @_;
    my $basename;
    my $basedir;
    if( $song->{'file'} =~ m/^(.*)\/(.*?)$/ )
    {
        $basename = $2;
        $basedir = $1
    }
    else{ die "Could not determine base name for $song->{'file'}\n"; }

    # we need to clean title,album,artist,comment,genre,track, and year
    $song->{'title'} =~ s/[\/]/-/g;
    $song->{'album'} =~ s/[\/]/-/g;
    $song->{'artist'} =~ s/[\/]/-/g;
    $song->{'comment'} =~ s/[\/]/-/g;
    $song->{'genre'} =~ s/[\/]/-/g;
    $song->{'track'} =~ s/[\/]/-/g;
    $song->{'year'} =~ s/[\/]/-/g;

    my $location = get_catalog_setting($self,$song->{'catalog'},'sort_pattern');
    $location =~ s/\Q%A\E/$song->{'album'}/g;
    $location =~ s/\Q%a\E/$song->{'artist'}/g;
    $location =~ s/\Q%C\E/$song->{'catalog'}/g;
    $location =~ s/\Q%c\E/$song->{'comment'}/g;
    $location =~ s/\Q%g\E/$song->{'genre'}/g;
    $location =~ s/\Q%T\E/$song->{'track'}/g;
    $location =~ s/\Q%t\E/$song->{'title'}/g;
    $location =~ s/\Q%y\E/$song->{'year'}/g;
    # result is wrong paths
    # $location =~ s/([\'\"])/\\$1/g;

    create($location);

    # The basename is calculated so we can see if the file already exists
    if(! -e "$location/$basename")
    {
        my $cmd = "/bin/mv \"".$song->{'file'}."\" \"$location\"";
        my $ret = do_call($cmd);
        if(empty_dir($basedir))
        {
            print "Removing empty directory $basedir\n";
            $cmd = "/bin/rmdir \"$basedir\"";
            do_call($cmd);
        }
        $filename = $location."/".$basename;
        return $filename;
    }
    else 
    {
        print STDERR "File exists: $location/$basename\n";
        $filename = $song->{'file'};
        return $filename;
    }
}

sub usage 
{
    my $usage = qq{
    fileupdate [--id3|--rename|--rename_all|--sort|--sort_all|--all] [--help] [--pretend] [--verbose]
        --pretend   	Display command taken, without actually doing anything.
	
        --id3       	Update id3 tags for all files flagged with 'id3'
	
        --rename    	Rename files flagged with 'rename'
	
	--rename_all	Renames all files based on id3 info
	
        --sort      	Sort files flagged with 'sort'
	
	--sort_all	Sort all files based on id3 info
	
        --all		Performs id3 update, rename, and sort
                    		for all files flagged with 'id3'
        --verbose	Shows detailed information about what's happening.

        --help		This message
    };
    die $usage;
}

sub do_call 
{
    my @cmd = @_;
    my $return = 0;

    if($verbose && !$pretend){ print "@cmd\n";}
    if($pretend){ print "@cmd\n"; }
    else
    { 
        $return = system @cmd; 
    }
    return $return;
}

sub create 
{
    my ($path) = @_;
    if(! -e $path)
    {
        return do_call("mkdir","-p",$path);
    }
    return 1;
}

# empty_dir borrowed from Tom Phoenix (rootbeer@teleport.com)
# posted in comp.lang.perl.misc on 3/21/97

sub empty_dir ($) 
{
    local(*DIR, $_);
    return unless opendir DIR, $_[0];
    while (defined($_ = readdir DIR)) {
        next if /^\.\.?$/;
        closedir DIR;
        return 0;
    }
    closedir DIR;
    1;
}
1;