#!C:/Perl/bin/perl.exe -w #The purpose of this script is to search for artists/ablums/songs #which dont yet exist in the dat.xml file and add them and also #to remove artists/albums/songs from the dat.xml file which no #longer exist in the file structure. use XML::XPath; use XML::XPath::XMLParser; use XML::DOM; use Cwd; use promptUser; use categoryNames; (@categories) or die; $start_time = time; #Variables for reporting changes $artistsRemoved = 0; $albumsRemoved = 0; $songsRemoved = 0; $artistsAdded = 0; $albumsAdded = 0; $songsAdded = 0; #Read in the dat.xml file $parser = XML::DOM::Parser->new(); $document = $parser->parsefile("dat.xml"); $root = $document->getDocumentElement; $xp = XML::XPath->new(filename => 'dat.xml'); $nodeset = $xp->find("/mp3s"); #Keep the CGI-BIN dir so we can get back there $cgibin = fastgetcwd; #Change dir to the MP3 root chdir "$MP3ROOT" or die "Cannot chdir to $MP3ROOT : $!"; #&checkRemoved; &checkAdded; $end_time = time; print "Total time = " . ($end_time - $start_time) . " seconds\n\n"; print "\nResults\n--------------\n"; print "Num artists removed = $artistsRemoved\n"; print "Num albums removed = $albumsRemoved\n"; print "Num songs removed = $songsRemoved\n"; print "Num artists added = $artistsAdded\n"; print "Num albums added = $albumsAdded\n"; print "Num songs added = $songsAdded\n"; #Ask user wheter to save or not $answer = &promptUser("Would you like to save the new file?", 'n'); if ($answer eq 'y') { chdir "$cgibin" or die "Cannot chdir to $cgibin : $!"; #Move current dat.xml to dat.xml_bck #unlink "dat.xml_bck"; #rename "dat.xml", "dat.xml_bck"; #$document->printToFile("dat.xml"); } exit; sub checkRemoved() { ##################################################### #First, go through and remove anything that needs it# ##################################################### @artists = $root->getElementsByTagName("Artist", 0); foreach $artist (@artists) { #Check if the artist still exists $artistName = $artist->getAttribute("name"); $artistName =~ s/&/&/g; if (! -d "$artistName") { print "Artist REMOVED = $albumName\n"; ++$artistsRemoved; $root->removeChild($artist); next; } @albums = $artist->getElementsByTagName("Album"); foreach $album (@albums) { #Check if the album still exists $albumName = $album->getAttribute("name"); $albumName =~ s/&/&/g; if (! -d "$artistName/$albumName") { print "Album REMOVED = $albumName\n"; ++$albumsRemoved; $artist->removeChild($album); next; } @songs = $album->getElementsByTagName("Song", 0); foreach $song (@songs) { #Check if the song still exists $songName = $song->getAttribute("name"); $songName =~ s/&/&/g; if (! -f "$artistName/$albumName/$songName") { print "Song REMOVED = $songName\n"; ++$songsRemoved; $album->removeChild($song); next; } } } @songs = $artist->getElementsByTagName("Song", 0); foreach $song (@songs) { #Check if the song still exists $songName = $song->getAttribute("name"); $songName =~ s/&/&/g; if (! -f "$artistName/$songName") { print "Song REMOVED = $songName\n"; ++$songsRemoved; $artist->removeChild($song); next; } } } #Finished removing anything that needs it } sub checkAdded() { ################################################ #Now, go through and add anything that needs it# ################################################ foreach $artist (<*>) { #print "Checking if $artist XML Node exists...\n"; if (-d "$artist") { #Make sure the artist exists in the dat file $artistName = $artist; $artistName =~ s/&/&/g; $nodeset = $xp->find("/mp3s/Artist[\@name=\"$artistName\"]"); if ($nodeset->size == 0) { $artistElem = $document->createElement('Artist'); $artistElem->setAttribute("name", "$artistName"); $root->appendChild($artistElem); print "Artist ADDED = $artistName\n"; ++$artistsAdded; } else { #Just locate the artist Element $artistElem = $nodeset->pop; } &Artist; } } } sub Artist() { #Change to artist directory chdir "$artist" or die "Cannot chdir to $artist : $!"; foreach $album (<*>) { if (-d "$album") { #Make sure the album exists in the dat file $albumName = $album; $albumName =~ s/&/&/g; $nodeset = $xp->find("mp3s/Artist[\@name=\"$artistName\"]/Album[\@name=\"$albumName\"]"); if ($nodeset->size == 0) { $albumElem = $document->createElement('Album'); $albumElem->setAttribute("name", "$albumName"); $artistElem->appendChild($albumElem); ++$albumsAdded; } else { #Just locate the album Element $albumElem = $nodeset->pop; } &Album; } } foreach $song (<*.mp3>) { $songName = $song; $songName =~ s/&/&/g; $nodeset = $xp->find("mp3s/Artist[\@name=\"$artistName\"]/Album[\@name=\"$albumName\"]/Song[\@name=\"$songName\"]"); if ($nodeset->size == 0) { $songElem = $document->createElement('Song'); $songElem->setAttribute("name", "$songName"); $artistElem->appendChild($songElem); ++$songAdded; } } #Go back up one directory chdir ".."; } sub Album() { #Change to ablum directory chdir "$album" or die "Cannot chdir to $album : $!"; foreach $song (<*.mp3>) { $songName = $song; $songName =~ s/&/&/g; $nodeset = $xp->find("mp3s/Artist[\@name=\"$artistName\"]/Album[\@name=\"$albumName\"]/Song[\@name=\"$songName\"]"); if ($nodeset->size == 0) { $songElem = $document->createElement('Song'); $songElem->setAttribute("name", "$songName"); $albumElem->appendChild($songElem); ++$songAdded; } } #Go back up one directory chdir ".."; }