-
Notifications
You must be signed in to change notification settings - Fork 0
/
library2JSON.pl
executable file
·238 lines (165 loc) · 5.21 KB
/
library2JSON.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
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
#!/opt/local/bin/perl -Cio
use strict;
use warnings;
use Path::Abstract;
use Local::Debug qw(debug_init debug debug_switch);
use Local::Routines qw(version parse get_json_date);
# Now uses Getopt::Long instead of Getopt:Std :
#use 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;
use POSIX qw(strftime);
use Mac::iTunes::Library;
use Mac::iTunes::Library::XML;
use Mac::iTunes::Library::Item;
# Not Used :
#use Text::Wrap;
my @progpath = split( /\//, $0 );
my $PROGNAME = $progpath[-1];
my $VER_NUM = "0.2";
# Options :
my $man = 0;
my $help = 0;
my $debug = 0;
my $curl = 0;
my $user = '';
my $pass = '';
my $version = '';
my $output = '';
my $lib_id = '';
my $author = '';
## 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,
'c|curl' => \$curl,
'u|user=s' => \$user,
'p|pass=s' => \$pass,
'v|version' => \$version,
'o|output-file=s' => \$output,
'l|library=s' => \$lib_id,
'a|author=s' => \$author
) or pod2usage(2);
pod2usage(1) if $help;
pod2usage( -verbose => 2 ) if $man;
## 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));
pod2usage("$0: No files given.") if ( @ARGV == 0 );
# Handle different options :
version( $version, $PROGNAME, $VER_NUM );
my $file = shift @ARGV;
debug_init($debug);
debug("Library is $file");
my $library = parse($file);
if ($output) {
open( MYFILE, '>', $output ) or die;
}
else {
open( MYFILE, '>&', \*STDOUT ) or die;
}
use JSON::XS;
my $json = JSON::XS->new();
# Get the hash of items
my %items = $library->items();
my $now = get_json_date();
print MYFILE "{\"code\" : \"200\", \"json\" : \n";
print MYFILE "{\n\t\"docs\" : [\n";
print MYFILE "\t{\n";
print MYFILE "\t\t\"library_id\" : \"$lib_id\",\n";
print MYFILE "\t\t\"library_file\" : \"$file\",\n";
print MYFILE "\t\t\"author\" : \"$author\",\n";
print MYFILE "\t\t\"type\" : \"library\",\n";
print MYFILE "\t\t\"created_at\" : \"$now\"\n\t},\n";
my $number_id = 0;
my $json_init = 0;
foreach my $artist ( sort keys %items ) {
debug( "foreach Artist : $artist", 2 );
# $artistSongs is a hash-ref
my $artistSongs = $items{$artist};
# Dereference $artistSongs so that you can pass it to keys()
# $songName is a key in the $artistSongs hash-ref
foreach my $songName ( sort keys %$artistSongs ) {
debug( "foreach Song Name $songName", 2 );
# The songs are stored as an array, because there can
# be multiple songs with identical names
my $artistSongItems = $artistSongs->{$songName};
# Go through all of the songs in the array-ref
foreach my $song (@$artistSongItems) {
#$number_id++;
debug( "foreach Song $song (counter is $number_id)", 2 );
$song->libraryID($lib_id);
if ( $json_init == 0 ) {
$json_init = 1;
#print MYFILE "\"$number_id\" : ";
print MYFILE "\t"
. $json->allow_nonref->allow_blessed->convert_blessed
->encode($song);
}
else {
print MYFILE ",\n";
#print MYFILE "\"$number_id\" : ";
print MYFILE "\t"
. $json->allow_nonref->allow_blessed->convert_blessed
->encode($song);
}
}
}
}
print MYFILE "\n\t]\n}\n";
print MYFILE ", \"headers\": {\"Content-Type\" : \"Application/json\"}}\n";
close(MYFILE);
debug("File $output created at $now.");
if ( $curl == 1 ) {
my $cmd =
'curl -X POST http://'
. $user . ':'
. $pass
. '@scriptunes.netgrowing.net:5984/blogdb -d @'
. $output
. ' -H "Content-Type:application/json"';
debug($cmd);
#system $cmd;
}
__END__
=head1 NAME
library2JSON - let you extract an iTunes Library file into JSON format.
=head1 SYNOPSIS
library2JSON library.xml [-o file.json] [-c -u user -p pass]
=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<-v, --version>
Print version number.
=item B<-c, --curl>
Create distant CouchDB document using curl.
=item B<-u, --user>
User name.
=item B<-p, --pass>
Password.
=item B<-o, --output-file file.json>
Write output to file.
=item B<-l, --library lib_id>
Specify the Library ID.
=item B<-a, --author user>
Specify the author.
=back
=head1 DESCRIPTION
This program generates a JSON output of an iTunes Library file.
=back
=head1 SEE ALSO
L<Mac::iTunes::Library::Item>.
=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