~ubuntu-branches/ubuntu/feisty/gnupod-tools/feisty

« back to all changes in this revision

Viewing changes to src/gnupod_search.pl

  • Committer: Bazaar Package Importer
  • Author(s): Brian Nelson
  • Date: 2005-04-05 09:10:01 UTC
  • mto: (2.1.1 sarge)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20050405091001-vjtr9oktjemr6mn6
Tags: upstream-0.98
ImportĀ upstreamĀ versionĀ 0.98

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
###__PERLBIN__###
1
2
#  Copyright (C) 2002-2004 Adrian Ulrich <pab at blinkenlights.ch>
2
3
#  Part of the gnupod-tools collection
3
4
#
25
26
use GNUpod::XMLhelper;
26
27
use GNUpod::FooBar;
27
28
use Getopt::Long;
28
 
use vars qw(%opts @keeplist);
29
 
 
30
 
print "gnupod_search.pl Version 0.94rc1 (C) 2002-2004 Adrian Ulrich\n";
 
29
use vars qw(%opts @keeplist %rename_tags);
 
30
 
 
31
use constant DEFAULT_SPACE => 32;
 
32
 
 
33
print "gnupod_search.pl Version ###__VERSION__### (C) Adrian Ulrich\n";
31
34
 
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");
 
37
#
 
38
# WARNING: If you add new options wich don't do matching, change newfile()
 
39
#
 
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");
37
44
 
38
45
usage() if $opts{help};
39
 
 
 
46
version() if $opts{version};
40
47
usage("\n-d was removed, use '--delete'\n") if $opts{RMME};
 
48
$opts{view} ||= 'ialt'; #Default view
 
49
 
 
50
#Check if input makes sense:
 
51
die "You can't use --delete and --rename together\n" if($opts{delete} && $opts{rename});
 
52
 
 
53
#Build %rename_tags
 
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;
 
59
}
 
60
 
41
61
 
42
62
go();
43
63
 
47
67
 my $con = GNUpod::FooBar::connect(\%opts);
48
68
 usage($con->{status}."\n") if $con->{status};
49
69
 
50
 
 
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"; 
 
70
 pview(undef,1);
 
71
 
 
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});
57
75
 
58
76
 
59
77
}
63
81
sub newfile {
64
82
 my($el) =  @_;
65
83
my $matched;
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});
 
86
 
67
87
 
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) {
71
91
    $matched++;
72
92
   }
73
93
  }
74
94
 
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";
 
95
 
 
96
  if(($opts{'match-once'} && $matched) || $ntm == $matched) {
 
97
    ##Rename HashRef items
 
98
    foreach(keys(%rename_tags)) {
 
99
      $el->{file}->{$_} = $rename_tags{$_};
 
100
    }
 
101
    ##Print it
 
102
    pview($el->{file},undef,$opts{delete});
 
103
    ##maybe unlinkit..
82
104
    unlink(GNUpod::XMLhelper::realpath($opts{mount},$el->{file}->{path}))
83
105
    or warn "[!!] Remove failed: $!\n" if $opts{delete};
84
106
  }
86
108
   GNUpod::XMLhelper::mkfile($el);
87
109
   $keeplist[$el->{file}->{id}] = 1;
88
110
  }
 
111
  
 
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;
 
116
  }
 
117
  
89
118
}
90
119
 
91
120
############################################
92
121
# Eventhandler for PLAYLIST items
93
122
sub newpl {
94
 
 return unless $opts{delete}; #Just searching
 
123
 return unless $opts{delete} or defined($opts{rename}); #Just searching
 
124
 
 
125
 # Delete or rename needs to rebuild the XML file
 
126
 
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
99
131
  }
100
132
 }
101
133
 elsif($plt eq "spl" && ref($el->{splcont}) eq "HASH") { #spl content
106
138
  GNUpod::XMLhelper::mkfile($el,{$plt."name"=>$name});
107
139
}
108
140
 
 
141
 
 
142
##############################################################
 
143
# Printout Search output
 
144
sub pview {
 
145
 my($orf,$xhead, $xdelete) = @_;
 
146
 
 
147
 #Build refs
 
148
 my %qh = ();
 
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";
 
159
 
 
160
 #Prepare view
 
161
 
 
162
 my $ll = 0; #LineLength
 
163
  foreach(split(//,$opts{view})) {
 
164
      print "|" if $ll;
 
165
      my $cs = $qh{$_}{k};           #CurrentString
 
166
         $cs = $qh{$_}{s} if $xhead; #Replace it if HEAD is needed
 
167
 
 
168
      my $cl = $qh{$_}{w}||DEFAULT_SPACE;       #Current length
 
169
         $ll += $cl+1;               #Incrase LineLength
 
170
     printf("%-*s",$cl,$cs);
 
171
  }
 
172
  
 
173
  if($xdelete && !$xhead) {
 
174
   print " [RM]\n";
 
175
  }
 
176
  elsif($xhead) {
 
177
   print "\n";
 
178
   print "=" x $ll;
 
179
   print "\n";
 
180
  }
 
181
  else {
 
182
   print "\n";
 
183
  }
 
184
 
 
185
}
 
186
 
 
187
 
109
188
###############################################################
110
189
# Basic help
111
190
sub usage {
114
193
$rtxt
115
194
Usage: gnupod_search.pl [-h] [-m directory] File1 File2 ...
116
195
 
117
 
   -h, --help             : This ;)
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
126
 
 
127
 
Note: Argument for title/artist/album.. has to be UTF8 encoded, *not* latin1!
128
 
 
129
 
EOF
130
 
}
131
 
 
132
 
 
 
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"
 
211
 
 
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\$"
 
215
 
 
216
Report bugs to <bug-gnupod\@nongnu.org>
 
217
EOF
 
218
}
 
219
 
 
220
 
 
221
sub version {
 
222
die << "EOF";
 
223
gnupod_search.pl (gnupod) ###__VERSION__###
 
224
Copyright (C) Adrian Ulrich 2002-2004
 
225
 
 
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.
 
228
 
 
229
EOF
 
230
}
133
231