25
26
use GNUpod::XMLhelper;
26
27
use GNUpod::FooBar;
28
use vars qw(%opts @keeplist);
30
print "gnupod_search.pl Version 0.94rc1 (C) 2002-2004 Adrian Ulrich\n";
29
use vars qw(%opts @keeplist %rename_tags);
31
use constant DEFAULT_SPACE => 32;
33
print "gnupod_search.pl Version ###__VERSION__### (C) Adrian Ulrich\n";
32
35
$opts{mount} = $ENV{IPOD_MOUNTPOINT};
33
36
#Don't add xml and itunes opts.. we *NEED* the mount opt to be set..
34
GetOptions(\%opts, "help|h", "mount|m=s", "artist|a=s",
35
"album|l=s", "title|t=s", "id|i=s",
36
"genre|g=s", "once|o", "delete", "RMME|d");
38
# WARNING: If you add new options wich don't do matching, change newfile()
40
GetOptions(\%opts, "version", "help|h", "mount|m=s", "artist|a=s",
41
"album|l=s", "title|t=s", "id|i=s", "rename=s@",
42
"view=s","genre|g=s", "match-once|o", "delete", "RMME|d");
43
GNUpod::FooBar::GetConfig(\%opts, {view=>'s', mount=>'s', 'match-once'=>'b'}, "gnupod_search");
38
45
usage() if $opts{help};
46
version() if $opts{version};
40
47
usage("\n-d was removed, use '--delete'\n") if $opts{RMME};
48
$opts{view} ||= 'ialt'; #Default view
50
#Check if input makes sense:
51
die "You can't use --delete and --rename together\n" if($opts{delete} && $opts{rename});
54
foreach(@{$opts{rename}}) {
55
my($key,$val) = split(/=/,$_,2);
56
next unless $key && $val;
57
next if $key eq "id";#Dont allow something like THIS
58
$rename_tags{lc($key)} = $val;
47
67
my $con = GNUpod::FooBar::connect(\%opts);
48
68
usage($con->{status}."\n") if $con->{status};
51
print "ID : ARTIST / ALBUM / TITLE\n";
52
print "==================================\n";
53
GNUpod::XMLhelper::doxml($con->{xml}) or usage("Failed to parse $con->{xml}\n");
54
#XML::Parser finished, write new file
55
GNUpod::XMLhelper::writexml($con->{xml}) if $opts{delete};
56
print "==================================\n";
72
GNUpod::XMLhelper::doxml($con->{xml}) or usage("Failed to parse $con->{xml}, did you run gnupod_INIT.pl?\n");
73
#XML::Parser finished, write new file if we deleted or renamed
74
GNUpod::XMLhelper::writexml($con) if $opts{delete} or defined($opts{rename});
66
my $ntm = keys(%opts)-1-$opts{once}-$opts{delete};
84
# 2 = mount + view (both are ALWAYS set)
85
my $ntm = keys(%opts)-2-$opts{'match-once'}-$opts{delete}-(defined $opts{rename});
68
88
foreach my $opx (keys(%opts)) {
69
next if $opx =~ /mount|once|delete/; #Skip this
89
next if $opx =~ /mount|match-once|delete|view|rename/; #Skip this
70
90
if($el->{file}->{$opx} =~ /$opts{$opx}/i) {
75
if(($opts{once} && $matched) || $ntm == $matched) {
76
print "[RM] " if $opts{delete};
77
print "$el->{file}->{id}";
78
print " " x (10-length($el->{file}->{id})-($opts{delete}*5));
79
print ": $el->{file}->{artist} / ";
80
print "$el->{file}->{album} / ";
81
print "$el->{file}->{title}\n";
96
if(($opts{'match-once'} && $matched) || $ntm == $matched) {
97
##Rename HashRef items
98
foreach(keys(%rename_tags)) {
99
$el->{file}->{$_} = $rename_tags{$_};
102
pview($el->{file},undef,$opts{delete});
82
104
unlink(GNUpod::XMLhelper::realpath($opts{mount},$el->{file}->{path}))
83
105
or warn "[!!] Remove failed: $!\n" if $opts{delete};
86
108
GNUpod::XMLhelper::mkfile($el);
87
109
$keeplist[$el->{file}->{id}] = 1;
112
##We'll rewrite the xml file: add it
113
if(!$opts{delete} && defined($opts{rename})) {
114
GNUpod::XMLhelper::mkfile($el);
115
$keeplist[$el->{file}->{id}] = 1;
91
120
############################################
92
121
# Eventhandler for PLAYLIST items
94
return unless $opts{delete}; #Just searching
123
return unless $opts{delete} or defined($opts{rename}); #Just searching
125
# Delete or rename needs to rebuild the XML file
95
127
my ($el, $name, $plt) = @_;
96
128
if($plt eq "pl" && ref($el->{add}) eq "HASH") { #Add action
97
129
if(defined($el->{add}->{id}) && int(keys(%{$el->{add}})) == 1) { #Only id
98
return unless($keeplist[$el->{add}->{id}]); #ID not on keeplist. dropt it
130
return unless($keeplist[$el->{add}->{id}]); #ID not on keeplist. drop it
101
133
elsif($plt eq "spl" && ref($el->{splcont}) eq "HASH") { #spl content
106
138
GNUpod::XMLhelper::mkfile($el,{$plt."name"=>$name});
142
##############################################################
143
# Printout Search output
145
my($orf,$xhead, $xdelete) = @_;
149
$qh{n}{k} = $orf->{songnum}; $qh{n}{w} = 4; $qh{t}{n} = "SNUM";
150
$qh{t}{k} = $orf->{title}; $qh{t}{s} = "TITLE";
151
$qh{a}{k} = $orf->{artist}; $qh{a}{s} = "ARTIST";
152
$qh{r}{k} = $orf->{rating}; $qh{r}{w} = 4; $qh{r}{s} = "RTNG";
153
$qh{p}{k} = $orf->{path}; $qh{p}{w} = 96; $qh{p}{s} = "PATH";
154
$qh{l}{k} = $orf->{album}; $qh{l}{s} = "ALBUM";
155
$qh{g}{k} = $orf->{genre}; $qh{g}{s} = "GENRE";
156
$qh{c}{k} = $orf->{playcount}; $qh{c}{w} = 4; $qh{c}{s} = "CNT";
157
$qh{i}{k} = $orf->{id}; $qh{i}{w} = 4; $qh{i}{s} = "ID";
158
$qh{u}{k} = GNUpod::XMLhelper::realpath($opts{mount},$orf->{path}); $qh{u}{w} = 96; $qh{u}{s} = "UNIXPATH";
162
my $ll = 0; #LineLength
163
foreach(split(//,$opts{view})) {
165
my $cs = $qh{$_}{k}; #CurrentString
166
$cs = $qh{$_}{s} if $xhead; #Replace it if HEAD is needed
168
my $cl = $qh{$_}{w}||DEFAULT_SPACE; #Current length
169
$ll += $cl+1; #Incrase LineLength
170
printf("%-*s",$cl,$cs);
173
if($xdelete && !$xhead) {
109
188
###############################################################
115
194
Usage: gnupod_search.pl [-h] [-m directory] File1 File2 ...
118
-m, --mount=directory : iPod mountpoint, default is \$IPOD_MOUNTPOINT
119
-t, --title=TITLE : print songs by Title
120
-a, --artist=ARTIST : print songs by Artist
121
-l, --album=ALBUM : print songs by Album
122
-i, --id=ID : print songs by ID
123
-g, --genre=GENRE : print songs by Genre
124
-o, --once : Search doesn't need to match multiple times (eg. -a & -l)
125
--delete : REMOVE (!) matched songs
127
Note: Argument for title/artist/album.. has to be UTF8 encoded, *not* latin1!
196
-h, --help display this help and exit
197
--version output version information and exit
198
-m, --mount=directory iPod mountpoint, default is \$IPOD_MOUNTPOINT
199
-t, --title=TITLE search songs by Title
200
-a, --artist=ARTIST search songs by Artist
201
-l, --album=ALBUM search songs by Album
202
-i, --id=ID search songs by ID
203
-g, --genre=GENRE search songs by Genre
204
-o, --match-once Search doesn't need to match multiple times (eg. -a & -l)
205
--delete REMOVE (!) matched songs
206
--view=ialt Modify output, default=ialt
207
t = title a = artist r = rating p = iPod Path
208
l = album g = genre c = playcount i = id
209
u = UnixPath n = Songnum
210
--rename=KEY=VAL Change tags on found songs. Example: --rename="ARTIST=Foo Bar"
212
Note: * Argument for title/artist/album/etc has to be UTF8 encoded, *not* latin1!
213
* All arguments are handled as regular expressions! If you want to search for
214
eg. ID '3' (excluding 13,63,32..), you would have to write: --id="^3\$"
216
Report bugs to <bug-gnupod\@nongnu.org>
223
gnupod_search.pl (gnupod) ###__VERSION__###
224
Copyright (C) Adrian Ulrich 2002-2004
226
This is free software; see the source for copying conditions. There is NO
227
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.