-
Notifications
You must be signed in to change notification settings - Fork 0
/
importItunesXml.php
165 lines (132 loc) · 4.38 KB
/
importItunesXml.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
<?php
// Use Zend_Loader from here on in
require_once ('application/global.php');
Zend_Loader::loadClass('ItunesTrack');
$libraryFile = 'iTunes Music Library.xml';
$keyMap = array(
'Track ID' => 'trackId',
'Name' => 'name',
'Artist' => 'artist',
'Album Artist' => 'albumArtist',
'Album' => 'album',
'Genre' => 'genre',
'Kind' => 'kind',
'Size' => 'size',
'Total Time' => 'totalTime',
'Track Number' => 'trackNumber',
'Track Count' => 'trackCount',
'Disc Number' => 'discNumber',
'Disc Count' => 'discCount',
'Year' => 'year',
'Date Modified' => 'dateModified',
'Date Added' => 'dateAdded',
'Bit Rate' => 'bitRate',
'Sample Rate' => 'sampleRate',
'Comments' => 'comments',
'Play Count' => 'playCount',
'Play Date' => 'playDate',
'Play Date UTC' => 'playDateUtc',
'Persistent ID' => 'persistentId',
'Track Type' => 'trackType',
'Location' => 'location',
);
$table = new ItunesTrack();
// delete all current rows
echo "deleting existing data... ";
$table->delete('1 = 1');
echo "done\n";
echo "processing xml...\n";
$track = $table->createRow();
$depth = 0;
$elements = array();
$inTracks = FALSE;
$inTrack = FALSE;
$inElement = FALSE;
$tracksDepth = -1;
$leftTracks = 0;
$prevData = '';
function startElement($parser, $name, $attrs)
{
global $depth, $elements, $tracksDepth;
//echo "startElement $depth $name\n";
$elements[$depth++] = $name;
}
function endElement($parser, $name)
{
global $depth, $elements, $tracksDepth, $leftTracks, $inTracks;
if ($depth == $tracksDepth && $leftTracks++ == 1) {
echo "\nLeaving Tracks\n";
$inTracks = !$inTracks;
}
$depth--;
}
function tagContent($parser, $data) {
global $depth, $elements, $tracksDepth, $inTracks, $inTrack, $prevData, $keyMap, $table, $track;
$data = trim($data);
$curDepth = $depth - 1;
$curElement = $elements[$curDepth];
if (strlen($data)) {
//echo "tagContent $curDepth $curElement:$data:\n";
if ($data == 'Tracks') {
$inTracks = !$inTracks;
$tracksDepth = $depth;
echo "Entering Tracks\n";
}
if ($inTracks) {
if ($data == 'Track ID') {
//echo "New Track ($prevData)\n";
if (!$inTrack) {
$inTrack = !$inTrack;
}
elseif (isset($track->trackId)) {
//print_r($track);
$track->save();
echo "+";
flush();
$track = $table->createRow();
}
$track = $table->createRow();
}
if ($inTrack) {
if (isset($keyMap[$prevData])) {
$prop = $keyMap[$prevData];
$type = strToLower($curElement);
//echo ">> trackData $prop:$type:$data:\n";
switch ($type) {
case 'integer':
$value = (int) $data;
break;
case 'string':
$value = (string) $data;
break;
case 'date':
$value = (string) $data;
break;
default:
break;
}
if ($prop == 'location')
$value = str_replace('file://localhost/Volumes/music/', '', $value);
if (isset($track->$prop))
$track->$prop = $value;
}
}
}
$prevData = $data;
}
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, 'startElement', 'endElement');
xml_set_character_data_handler($xml_parser, 'tagContent');
if (!($fp = fopen($libraryFile, "r"))) {
die("could not open XML input");
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);
echo " done\n";