-
Notifications
You must be signed in to change notification settings - Fork 0
/
libraryPath.pl
executable file
·169 lines (117 loc) · 3.75 KB
/
libraryPath.pl
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
#!/opt/local/bin/perl
use strict;
use warnings;
use Path::Abstract;
use Local::Debug qw(debug debug_init);
use Local::Routines qw(version);
# On demand export :
#use Routines qw(dummy);
#dummy ();
# Now uses Getopt::Long instead of Getopt:Std :
use Getopt::Long qw{:config no_ignore_case no_auto_abbrev};
# Getopt::Long encourages the use of Pod::Usage to produce help messages :
use Pod::Usage;
my @progpath = split( /\//, $0 );
my $PROGNAME = $progpath[-1];
my $VER_NUM = "0.2.1";
# Options :
my $man = 0;
my $help = 0;
my $debug = 0;
my $version = '';
my $count = '';
my $prefix = '/';
## Parse options and print usage if there is a syntax error,
## or if usage was explicitly requested.
GetOptions(
'h|help|?' => \$help,
'm|man' => \$man,
'D|Debug+' => \$debug,
'v|version' => \$version,
'c|count!' => \$count,
'p|prefix=s' => \$prefix
) or pod2usage(2);
pod2usage(1) if $help;
pod2usage( -verbose => 2 ) if $man;
# Handle different options :
version( $version, $PROGNAME, $VER_NUM );
## If no arguments were given, then allow STDIN to be used only
## if it's not connected to a terminal (otherwise print usage)
pod2usage("$0: No files given.") if ( ( @ARGV == 0 ) && ( -t STDIN ) );
debug_init($debug);
# iTunes XML Library :
my $library = '<stdin>';
if (@ARGV) {
# don't use shift here becose '<>' seems to rely on @ARGV :
$library = $ARGV[0];
}
# Hashmap containing new directories at given depth (see prefix) :
my %newloc;
debug("Debug output set to $debug");
debug("Prefix set to : $prefix");
debug("MP3 count set to : $count");
debug("In Library file : $library");
my $nb_mp3 = 0;
# No more need to open the file since '<>':
#open(FILEHANDLER, $library) or die $!;
#while (my $line = <FILEHANDLER>)
while ( my $line = <> ) {
if ( $line =~ /.*<key>Location.*/ ) {
$line =~ s/%20/\ /g;
if ( $line =~ /.*localhost$prefix.*/ ) {
$nb_mp3++;
my @path = split( /\/\/localhost/, $line );
my @path2 = split( /\//, $prefix );
my $count = scalar @path2;
if ( $count == 0 ) {
$count = 1;
}
my @path3 = split( /\//, $path[1] );
if ( $newloc{ $path3[$count] } ) {
$newloc{ $path3[$count] }++;
}
else {
$newloc{ $path3[$count] } = 1;
}
}
}
}
foreach my $path_final ( sort keys %newloc ) {
my $finalpath = Path::Abstract->new( $prefix . "/" . $path_final );
print "$finalpath\t: " . $newloc{$path_final} . " tracks\n";
}
#close (FILEHANDLER);
__END__
=head1 NAME
libraryPath - let you find every path in a iTunes Library
=head1 SYNOPSIS
libraryPath [-Dvhmc] iTuneLibrary.xml [-p /prefix]
=head1 OPTIONS
=over 8
=item B<-h, --help>
Print a brief help message and exits.
=item B<-m, --man>
Prints the manual page and exits.
=item B<-D, --Debug>
Print more informations during execution.
=item B<-v, --version>
Print version number.
=item B<-c, --count>
Print the number of tracks in parent folder.
=item B<-p, --prefix path>
Use prefix as path to digg into XML file.
=back
=head1 DESCRIPTION
This program allow an iTunes user to digg into an iTunes XML file. The
output will list every folder containing music tracks (together with
the track count form a given parent folder (prefix).
=head1 SEE ALSO
L<perlpod>, L<perldoc>, L<Getopt::Long>, L<Pod::Usage>.
=head1 COPYRIGHT
Copyright 2012 Christophe Laferriere.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.2 or
any later version published by the Free Software Foundation; with no
Invariant Sections, with no Front-Cover Texts, and with no Back-Cover
Texts.
=cut