• Das kreative Thema des Monats Oktober wird vor allem diejenigen unter Euch besonders ansprechen, denen Do-it-yourself quasi im Blut liegt. Zeigt uns Eure Improvisationskünste oder fotografiert, was andere Leute sich so im Alltag zusammengestrickt haben. --> Zum Mitmachen bitte hier klicken

iTunes Library mit Filesystem vergleichen

g000ze

Jonagold
Registriert
19.06.10
Beiträge
19
Hallo allerseits

Hat sich schon mal jemand von Euch gefragt, ob vielleicht der Inhalt der iTunes Library auch tatsächlich mit dem Inhalt des Dateisystemes übereinstimmt? Ich habe das untenstehende PHP Script gemacht, was erstaunliches zu Tage geführt hat.

Dieses Script findet Einträge, welche in der iTunes XML Library aber nicht im Dateisystem vorhanden sind. Umgekehrt findet es auch Dateien im iTunes Music Ordner, welche nicht in der XML Library sind.

Vielleicht kann das jemand von Euch gebrauchen.

Gruss g000ze

Code:
#!/usr/bin/php
<?php

# this script searches songs, that are declared in the 
# itunes xml library, but are not found in the 
# filesystem and vice versa. itunes lacks from
# the functionality that missings songs cannot
# be sorted and songs in a specific directory cannot
# be found automatically.

$xml_file = "/Users/username/Music/iTunes/iTunes Music Library.xml";
$root_dir = "/Users/username/Music/iTunes/iTunes Music";

$array_xml = fill_xml_array ($xml_file);
$array_fs = fill_fs_array ($root_dir);

function fill_xml_array($xml_file) {
    global $root_dir;

    $f = fopen($xml_file,'r');
    $array_xml = array ();
    
    $search_string = "<key>Location</key><string>file\://localhost";
    $search_line   = "$search_string" . str_replace(" " , "%20" , "$root_dir") . "/";

    while (!feof($f)) {
        $buffer = fgets($f, 4096);
        if (preg_match("#$search_line#", $buffer)) {
             # remove unneeded xml tags to get the filename.
             $string = preg_replace ("#$search_string#", '', $buffer);
             $string = preg_replace ("#</string>#", '', $string);
             # html decode special characters to match filename.
             $filename = rawurldecode (trim ($string));
             $filename = html_entity_decode($filename, ENT_QUOTES);
             array_push ($array_xml, $filename);
        }
    }
    fclose($f);
    return $array_xml;
}

function fill_fs_array ($directory) {
    $array_items = array();
    if ($handle = opendir($directory)) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") {
                if (is_dir($directory . "/" . $file)) {
                    $array_items = array_merge($array_items, fill_fs_array ($directory . "/" . $file));
                } 
                else {
                    if (
                        preg_match("#.*\.mp3$#i", $file) ||
                        preg_match("#.*\.m4a$#i", $file)
                       ) {
                        array_push ($array_items, $directory . "/" . $file);
                    }
                }
            }
        }
        closedir($handle);
    }
    return $array_items;
}

$result = array_diff ($array_xml, $array_fs);
if (count ($result) > 0) {
    echo "\nFiles in XML and not in Filesystem\n";
    print_r ($result);
}

$result = array_diff ($array_fs, $array_xml);
if (count ($result) > 0) {
    echo "\nFiles in Filesystem and not in XML\n";
    print_r ($result);
}

?>
 
Dafür gibt es schon lange Skripte bei Doug Adams.
Allerdings treten diese Probleme sowieso nur auf, wenn man Bedienungsfehler macht.