forked from shuckster/iTunes-XML-parser-for-PHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iTunesXMLparser.class.php
251 lines (188 loc) · 5.92 KB
/
iTunesXMLparser.class.php
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
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
/*
iTunes XML parser for PHP
Copyright (C) 2013 Conan Theobald [http://github.com/shuckster]
version: 1.5
Changes:
* 1.5: Simplify parseDict, API changes
* 1.4: Parse info and playlists
* 1.3: New example, delete old/deprecated stuff
* 1.2: Now a class, improved sort-method
* 1.1: Type-cast integers and booleans
based on:
Copyright (C) 2005 Peter Minarik [http://www.wirsindecht.org]
version: 1.00
based on:
iTunes XML PhP Parser
Copyright (C) 2003 Robert A. Wallis [http://codetriangle.com/]
version: 1.00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE
as published by the Free Software Foundation; either version 2.1
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA
iTunes is a product by Apple Computer, Inc.
http://www.apple.com/
*/
class iTunesXMLParser {
public $file_name = '';
public $data = NULL;
public $sort_field = NULL;
public $sort_direction = 'ascending';
protected function openFileOrSource( $file = NULL, $source = NULL) {
// Open the XML document in the DOM
$dom = new DomDocument();
if ( NULL !== $file ) {
if ( !file_exists( $file ) ) {
die( 'iTunes XML file not found: ' . $file );
}
if ( !$dom->load( $file ) ) {
die( 'Could not parse iTunes XML file: ' . $file );
}
}
else if ( NULL !== $source ) {
if ( !$dom->loadXML( $source ) ) {
die( 'Could not parse XML source: ' . $source );
}
}
// Get the root element <plist>
$plist_node = $dom->documentElement;
$first_dict_node = NULL;
// First <dict> contains version-info + tracks-node
foreach ( $plist_node->childNodes as $child ) {
if ( 'dict' === $child->nodeName ) {
$first_dict_node = $child;
break;
}
}
// Fell-through: Parse
$this->file_name = $file;
$this->data = $this->parseDict( $first_dict_node, NULL );
}
public function parse( $source ) {
return $this->openFileOrSource( NULL, $source );
}
public function open( $file ) {
return $this->openFileOrSource( $file );
}
public function processPlaylists() {
if ( NULL === $this->data || !isset( $this->data[ 'Playlists' ] ) ) {
die( 'No data to work with' );
}
$tracks = (array) $this->data[ 'Tracks' ];
foreach( $this->data[ 'Playlists' ] as &$playlist ) {
$new_items = array();
foreach ( $playlist->{ 'Playlist Items' } as $item ) {
$track_id = $item->{ 'Track ID' };
$new_items[] = $tracks[ $track_id ];
}
$playlist->{ 'Playlist Items' } = $new_items;
}
}
// To be used with the uasort() array function
protected function sort( $left, $right ) {
$field = $this->sort_field;
$direction = $this->sort_direction;
if ( !isset( $left[ $field ] ) ) {
return 1;
}
elseif ( !isset( $right[ $field ] ) ) {
return -1;
}
// Return the strcmp() of the two fields
$left = $left[ $field ];
$right = $right[ $field ];
switch ( gettype( $left ) ) {
case 'boolean':
$left = (int) $left;
$right = (int) $right;
case 'integer':
case 'double':
if ( 'descending' === $direction ) {
return $left === $right ? 0 : ( $left > $right ? -1 : 1 );
}
else {
return $left === $right ? 0 : ( $right > $left ? -1 : 1 );
}
break;
default:
// Detect dates (ISO8601 based), convert to timestamps for comparison
$rx_date = '/^\d{4}\-\d{2}\-\d{2}T\d{2}\:\d{2}\:\d{2}(Z|\+\d{2}\:\d{2})$/';
if ( preg_match( $rx_date, $left ) && preg_match( $rx_date, $right ) ) {
$left = strtotime( $left );
$right = strtotime( $right );
if ( 'descending' === $direction ) {
return $left === $right ? 0 : ( $left > $right ? -1 : 1 );
}
else {
return $left === $right ? 0 : ( $right > $left ? -1 : 1 );
}
}
// Default to a string comparison
else {
if ( 'descending' === $direction ) {
return strcasecmp( $left, $right );
}
else {
return strcasecmp( $right, $left );
}
}
}
}
protected function parseDict( $baseNode ) {
$dicts = array();
$current_key = NULL;
$current_value = NULL;
foreach ( $baseNode->childNodes as $child ) {
$dict = NULL;
switch ( $child->nodeName ) {
case '#text':
break;
case 'key':
$current_key = $child->textContent;
$current_value = NULL;
break;
case 'array':
$current_value = $this->parseDict( $child );
break;
case 'dict':
$current_value = (object) $this->parseDict( $child );
break;
case 'true':
case 'false':
$current_value = 'true' === $child->nodeName;
break;
case 'integer':
$current_value = (int) $child->textContent;
break;
default:
$current_value = $child->textContent;
if ( preg_match( '/^(Music Folder|Location)$/', $current_key ) ) {
$current_value = urldecode( stripslashes( $current_value ) );
}
}
if ( NULL !== $current_value ) {
if ( 'array' === $baseNode->nodeName ) {
$dicts[] = $current_value;
}
else if ( NULL !== $current_key ) {
$dicts[ $current_key ] = $current_value;
$current_key = NULL;
}
$current_value = NULL;
}
}
// Sort the tracks
if ( $this->sort_field ) {
uasort( $dicts, array( $this, 'sort' ) );
}
return $dicts;
}
}