| 1 |
#!/usr/bin/perl |
| 2 |
# |
| 3 |
# This file is part of Audio-MPD |
| 4 |
# |
| 5 |
# This software is copyright (c) 2007 by Jerome Quelin. |
| 6 |
# |
| 7 |
# This is free software; you can redistribute it and/or modify it under |
| 8 |
# the same terms as the Perl 5 programming language system itself. |
| 9 |
# |
| 10 |
|
| 11 |
use strict; |
| 12 |
use warnings; |
| 13 |
use 5.010; |
| 14 |
|
| 15 |
# PODNAME: mpd-dump-ratings |
| 16 |
# ABSTRACT: dump mpd ratings & filter them |
| 17 |
|
| 18 |
use DB_File; |
| 19 |
use Encode; |
| 20 |
use Getopt::Euclid; |
| 21 |
use List::MoreUtils qw[ all ]; |
| 22 |
|
| 23 |
my %db; |
| 24 |
tie %db, 'DB_File', "$ENV{HOME}/.mpd/ratings.db"; |
| 25 |
my $music = '/home/jquelin/Music'; |
| 26 |
|
| 27 |
FILENAME: |
| 28 |
foreach my $file (sort keys %db) { |
| 29 |
# minimum rating |
| 30 |
next unless $db{$file} >= $ARGV{-min}; |
| 31 |
|
| 32 |
# match some pattern(s) |
| 33 |
next if exists $ARGV{'<pattern>'} |
| 34 |
&& not all { $file =~ /$_/i } @{ $ARGV{'<pattern>'} }; |
| 35 |
|
| 36 |
# orphan files |
| 37 |
if ( $ARGV{-orphan} ) { |
| 38 |
next if -f "$music/$file"; |
| 39 |
|
| 40 |
# trying to repair |
| 41 |
if ( $ARGV{-repair} ) { |
| 42 |
my $utf = encode('utf-8', $file); |
| 43 |
if ( -f "$music/$utf" ) { |
| 44 |
print "$db{$file}: $utf [utf8]"; |
| 45 |
if ( exists($db{$utf}) && defined($db{$utf}) ) { |
| 46 |
# duplicate rating |
| 47 |
print "[duplicate]"; |
| 48 |
if ( $db{$utf} == $db{$file} ) { |
| 49 |
delete $db{$file}; |
| 50 |
say "[fixed]"; |
| 51 |
} else { |
| 52 |
say "[mismatch $db{$file}!=$db{$utf}]"; |
| 53 |
print "enter new rating: "; |
| 54 |
my $rate = <STDIN>; |
| 55 |
chomp $rate; |
| 56 |
$db{$utf} = $rate; |
| 57 |
delete $db{$file}; |
| 58 |
} |
| 59 |
} else { |
| 60 |
# ok, single rating, convert to utf8 |
| 61 |
$db{$utf} = $db{$file}; |
| 62 |
delete $db{$file}; |
| 63 |
say "[fixed]"; |
| 64 |
} |
| 65 |
} else { |
| 66 |
if ( $ARGV{-delete} ) { |
| 67 |
say "$db{$file}: $file [delete]"; |
| 68 |
delete $db{$file}; |
| 69 |
} else { |
| 70 |
say "$db{$file}: $file [orphan]"; |
| 71 |
} |
| 72 |
} |
| 73 |
next; # |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
print "$db{$file}: $file\n"; |
| 78 |
} |
| 79 |
|
| 80 |
__END__ |
| 81 |
|
| 82 |
=pod |
| 83 |
|
| 84 |
=head1 NAME |
| 85 |
|
| 86 |
mpd-dump-ratings - dump mpd ratings & filter them |
| 87 |
|
| 88 |
=head1 VERSION |
| 89 |
|
| 90 |
version 2.004 |
| 91 |
|
| 92 |
=head1 DESCRIPTION |
| 93 |
|
| 94 |
Dumps the mpd ratings associated to songs. Each filename will be dumped |
| 95 |
on a line with a prefix "rating: ". One can filter out the filenames |
| 96 |
dumped with some options. |
| 97 |
|
| 98 |
=head1 NAME |
| 99 |
|
| 100 |
mpd-dump-ratings - dump mpd ratings |
| 101 |
|
| 102 |
=head1 USAGE |
| 103 |
|
| 104 |
mpd-dump-ratings [options] |
| 105 |
|
| 106 |
=head1 OPTIONS |
| 107 |
|
| 108 |
=over 4 |
| 109 |
|
| 110 |
=item -m[in[imum]] [=] <min> |
| 111 |
|
| 112 |
Minimum rating for a song to be displayed. Default to 0. |
| 113 |
|
| 114 |
=for Euclid: min.type: int > 0 |
| 115 |
min.default: 0 |
| 116 |
|
| 117 |
=item -o[rphan[ed]] |
| 118 |
|
| 119 |
Print only orphan pathes (ie, with no matching filename). |
| 120 |
|
| 121 |
=item -r[epair] |
| 122 |
|
| 123 |
Try to fix the database for orphan pathes (ie, doesn't work if |
| 124 |
C<-orphan> is not supplied). Existing fixes performed: |
| 125 |
|
| 126 |
=over 4 |
| 127 |
|
| 128 |
=item try to convert orphan path to utf8. |
| 129 |
|
| 130 |
=back |
| 131 |
|
| 132 |
If one of those fix leads to a valid path, it will try to fix database. |
| 133 |
Of course, it checks if this leads to duplicate values. |
| 134 |
|
| 135 |
=item -d[elete] |
| 136 |
|
| 137 |
If C<-repair> did not manage to find a plausible replacement for the |
| 138 |
orphan rating, then it will remove this rating from the database. This |
| 139 |
can be the case when you removed a song... |
| 140 |
|
| 141 |
B<Warning>: you will potentially loose some information! |
| 142 |
|
| 143 |
=item <pattern> |
| 144 |
|
| 145 |
Filter out songs that don't match given C<pattern>. |
| 146 |
|
| 147 |
=for Euclid: repeatable |
| 148 |
pattern.type: string |
| 149 |
pattern.default: '' |
| 150 |
|
| 151 |
=item --version |
| 152 |
|
| 153 |
=item --usage |
| 154 |
|
| 155 |
=item --help |
| 156 |
|
| 157 |
=item --man |
| 158 |
|
| 159 |
Print the usual program information |
| 160 |
|
| 161 |
=back |
| 162 |
|
| 163 |
=head1 AUTHOR |
| 164 |
|
| 165 |
Jerome Quelin, "<jquelin at cpan.org>" |
| 166 |
|
| 167 |
=head1 COPYRIGHT & LICENSE |
| 168 |
|
| 169 |
Copyright (c) 2008 Jerome Quelin, all rights reserved. |
| 170 |
|
| 171 |
This program is free software; you can redistribute it and/or modify it |
| 172 |
under the same terms as Perl itself. |
| 173 |
|
| 174 |
=head1 AUTHOR |
| 175 |
|
| 176 |
Jerome Quelin |
| 177 |
|
| 178 |
=head1 COPYRIGHT AND LICENSE |
| 179 |
|
| 180 |
This software is copyright (c) 2007 by Jerome Quelin. |
| 181 |
|
| 182 |
This is free software; you can redistribute it and/or modify it under |
| 183 |
the same terms as the Perl 5 programming language system itself. |
| 184 |
|
| 185 |
=cut |