|
|
|
<?php
|
|
|
|
|
|
|
|
require 'iTunesLibrary.php';
|
|
|
|
|
|
|
|
$real_time_start = microtime(true);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* I moved my iTunes Music folder to a new disc, I don't think I did it properly
|
|
|
|
* so all my playlists were empty, the tracks in them had new IDs and locations
|
|
|
|
* There might be a way to recover using iTunes, but with a 250GB library, it's a faff
|
|
|
|
* moving it around.
|
|
|
|
*
|
|
|
|
* So I found myself with an new 'iTunes Library.xml' with empty playlist, but with the
|
|
|
|
* new track IDs, and an old 'iTunes Music Library.xml' with the old track IDs and names
|
|
|
|
*
|
|
|
|
* This script loads up both the old and the new libraries, then,
|
|
|
|
* for each track in each playlist in the old library, it looks up the
|
|
|
|
* ID in the new library, using the track name
|
|
|
|
* Then, if you want, it writes each playlist to an XML file that can be imported into iTunes.
|
|
|
|
*/
|
|
|
|
$newiTunesFile = 'iTunes-new.xml';
|
|
|
|
$oldiTunesFile = 'iTunes-old.xml';
|
|
|
|
$newiTunesFileGZ = 'iTunes-new.xml.gz';
|
|
|
|
$oldiTunesFileGZ = 'iTunes-old.xml.gz';
|
|
|
|
|
|
|
|
$trackLimit = 5000;
|
|
|
|
|
|
|
|
if (!ini_set('default_charset', 'UTF-8')) {
|
|
|
|
echo "could not set default_charset to UTF-8<br>";
|
|
|
|
}
|
|
|
|
|
|
|
|
echo "ini_get('default_charset') ". ini_get('default_charset')."\n";
|
|
|
|
|
|
|
|
$writeNewPlaylistFile = true;
|
|
|
|
$outputDir = "newPlaylists";
|
|
|
|
|
|
|
|
if ($writeNewPlaylistFile == true) {
|
|
|
|
if (!file_exists($outputDir)) {
|
|
|
|
mkdir($outputDir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$time_start = microtime(true);
|
|
|
|
|
|
|
|
// load up old library, and playlists
|
|
|
|
$oldLibrary = new iTunesLibrary($oldiTunesFile, true, $trackLimit);
|
|
|
|
|
|
|
|
$time = (microtime(true) - $time_start);
|
|
|
|
printf("Time to load old lib and playlists: %01.3f\n", $time);
|
|
|
|
|
|
|
|
|
|
|
|
$time_start = microtime(true);
|
|
|
|
// load up new library, just the tracks
|
|
|
|
$newLibrary = new iTunesLibrary($newiTunesFile, false);
|
|
|
|
|
|
|
|
$time = (microtime(true) - $time_start);
|
|
|
|
printf("Time to load new lib: %01.3f\n", $time);
|
|
|
|
|
|
|
|
|
|
|
|
echo "old lib playlist count: " . $oldLibrary->getPlaylistCount() . "\n";
|
|
|
|
echo "new lib track count: " . $newLibrary->getCount() . "\n";
|
|
|
|
echo "old lib track count: " . $oldLibrary->getCount() . "\n";
|
|
|
|
|
|
|
|
foreach ($oldLibrary->getPlaylists() as $playlist) {
|
|
|
|
|
|
|
|
$time_start = microtime(true);
|
|
|
|
|
|
|
|
if (count($playlist->tracks)>0 && count($playlist->tracks) < $trackLimit) {
|
|
|
|
|
|
|
|
echo "Playlist Name: $playlist->Name\n";
|
|
|
|
|
|
|
|
$newTrackIDs = array();
|
|
|
|
foreach ($playlist->tracks as $trackID => $trackValues) {
|
|
|
|
// echo "trackID: $trackID, Name = " . $trackValues["Name"] . "\n";
|
|
|
|
list($id, $location) = $newLibrary->getIDAndLocation($trackID, $trackValues["Name"], $trackValues["Total_Time"], $trackValues["Artist"], $trackValues["Album"]);
|
|
|
|
|
|
|
|
// echo "newID: $id, new location: $location\n\n";
|
|
|
|
if (is_null($id) == false) {
|
|
|
|
$newTrackIDs[]=$id;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
// echo "OHNO: Could not find new ID for: " . $trackValues["Name"] . "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$time = (microtime(true) - $time_start);
|
|
|
|
printf("Time to lookup new data for playlist with %u tracks: %01.3f\n",count($playlist->tracks), $time);
|
|
|
|
// write new xml, need name and trackIds
|
|
|
|
if ($writeNewPlaylistFile == true && count($newTrackIDs)>0) {
|
|
|
|
outputPlaylistFile($playlist->Name, $newTrackIDs);
|
|
|
|
$time = (microtime(true) - $time_start);
|
|
|
|
printf("Time to write new playlist with %u tracks: %01.3f\n",count($playlist->tracks), $time);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$time = (microtime(true) - $real_time_start);
|
|
|
|
printf("Total time: %01.3f\n", $time);
|
|
|
|
|
|
|
|
exit;
|
|
|
|
/**
|
|
|
|
* Get the track name and location for a specific track ID
|
|
|
|
*
|
|
|
|
* @param string $playlistName
|
|
|
|
* @param array $trackIDs
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function outputPlaylistFile($playlistName, $trackIDs){
|
|
|
|
|
|
|
|
global $outputDir;
|
|
|
|
$playlistName = $playlistName . "_NEW";
|
|
|
|
$playlistNameFilename = $playlistName . ".xml";
|
|
|
|
|
|
|
|
$header = <<< HEAD
|
|
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
|
|
<plist version="1.0">
|
|
|
|
<dict>
|
|
|
|
<key>Tracks</key>
|
|
|
|
<dict>
|
|
|
|
HEAD;
|
|
|
|
|
|
|
|
$mid1 = <<< MID1
|
|
|
|
</dict><key>Playlists</key>
|
|
|
|
<array>
|
|
|
|
<dict>
|
|
|
|
<key>Name</key><string>
|
|
|
|
MID1;
|
|
|
|
|
|
|
|
$footer = <<< FOOT
|
|
|
|
</array>
|
|
|
|
</dict>
|
|
|
|
</array>
|
|
|
|
</dict>
|
|
|
|
</plist>
|
|
|
|
FOOT;
|
|
|
|
|
|
|
|
|
|
|
|
$mid = <<< MID
|
|
|
|
</string>
|
|
|
|
<key>Description</key><string></string>
|
|
|
|
<key>All Items</key><true/>
|
|
|
|
<key>Playlist Items</key>
|
|
|
|
<array>
|
|
|
|
MID;
|
|
|
|
|
|
|
|
|
|
|
|
$str = $header;
|
|
|
|
|
|
|
|
foreach ($trackIDs as $trackID) {
|
|
|
|
$str = $str ."<key>" . $trackID ."</key>";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$str = $str . $mid1 . $playlistName . $mid;
|
|
|
|
|
|
|
|
// loop through tracks
|
|
|
|
foreach ($trackIDs as $trackID) {
|
|
|
|
$str = $str . "<dict><key>Track ID</key><integer>" . $trackID . "</integer></dict>";
|
|
|
|
}
|
|
|
|
|
|
|
|
$str = $str . $footer;
|
|
|
|
|
|
|
|
$filename = "$outputDir/$playlistNameFilename";
|
|
|
|
|
|
|
|
if (!$handle = fopen($filename, 'w')) {
|
|
|
|
echo "Cannot open file ($filename)\n";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write $somecontent to our opened file.
|
|
|
|
if (fwrite($handle, $str) === FALSE) {
|
|
|
|
echo "Cannot write to file ($filename)\n";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// echo "Success, wrote to file ($filename)\n";
|
|
|
|
|
|
|
|
fclose($handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|