#!/usr/bin/perl
#iTunes Music Ingester by Joab Jackson is licensed under a Creative Commons #Attribution-NonCommercial 3.0 Unported #License.

#iTunes is an Apple trademark.

#Collects Name, Track ID, Artist, Album, Genre, Year, Date Added, Play Count, Play Date UTC, Rating, all embedded within #<key> </key>


#Open file...

$File = "iTunes Music Library.xml";
open (FILE, "$File");

#Open database connection....
use DBI;
$dbh = DBI->connect('DBI:mysql:music', '[USERNAME]', '[PASSWORD]')
			|| die "ERROR: $DBI::errstr";


while (<FILE>) {

	chomp;

				
	#Note that all the categories must be in the exact order as they are in the iTunes XML file


		if (/>Name</){

			$prep = s/\r/\n/g;

			$prep = s/\r$//g;

			$prep = s/\t//g;

			$prep = s/<key>Name<\/key><string>//;

			$prep = s/<\/string>//;

			$Name = "$_";

			chomp $Name;

					}					

						

						

		elsif (/>Track ID</){

			$prep = s/\r/\n/g;

			$prep = s/\r$//g;

			$prep = s/\t//g;

			$prep = s/<key>Track ID<\/key><integer>//;

			$prep = s/<\/integer>//;

			$TrackID = "$_";

			chomp $TrackID;

					}				

						

		elsif (/>Artist</){

			$prep = s/\r/\n/g;

			$prep = s/\r$//g;

			$prep = s/\t//g;

			$prep = s/<key>Artist<\/key><string>//;

			$prep = s/<\/string>//;

			$Artist = "$_";

			chomp $Artist;

					}



					

		elsif (/>Album</){

			$prep = s/\r/\n/g;

			$prep = s/\r$//g;

			$prep = s/\t//g;

			$prep = s/<key>Album<\/key><string>//;

			$prep = s/<\/string>//;

			$Album = "$_";

			chomp $Album;

			}					





		elsif (/>Genre</){

			$prep = s/\r/\n/g;

			$prep = s/\r$//g;

			$prep = s/\t//g;

			$prep = s/<key>Genre<\/key><string>//;

			$prep = s/<\/string>//;

			$Genre = "$_";

			chomp $Genre;		

								}

			

		elsif (/>Year</){

			$prep = s/\r/\n/g;

			$prep = s/\r$//g;

			$prep = s/\t//g;

			$prep = s/<key>Year<\/key><integer>//;

			$prep = s/<\/integer>//;

			$Year = "$_";

			chomp $Year;

					}	

			

		elsif (/>Date Added</){

			$prep = s/\r/\n/g;

			$prep = s/\r$//g;

			$prep = s/\t//g;

			$prep = s/<key>Date Added<\/key><date>//;

			$prep = s/<\/date>//;

			$DateAdded = "$_";

#Needs to be in the format of yyyymmdd

			if ($DateAdded =~ m/(^\d{4})/ ){
				$YearAdded = $1;
				}

       
	
			if ($DateAdded =~ m/(-\d{2}-)/ ){
				$Month = $1;
				$Month =~ m/(\d{2})/;
				$Month = $1;
				}

			if ($DateAdded =~ m/(-\d{2}T)/ ){
				$Day = $1;
				$Day =~ m/(\d{2})/;
				$Day = $1;
				}


			$DateAdded = $YearAdded . $Month . $Day;
	
			chomp $DateAdded;

					}	

			

		elsif (/>Play Count</){

			$prep = s/\r/\n/g;

			$prep = s/\r$//g;

			$prep = s/\t//g;

			$prep = s/<key>Play Count<\/key><integer>//;

			$prep = s/<\/integer>//;

			chomp $_;

			$Plays = "$_";

					}

					

					

		elsif (/>Play Date UTC</){

			$prep = s/\r/\n/g;

			$prep = s/\r$//g;

			$prep = s/\t//g;

			$prep = s/<key>Play Date UTC<\/key><date>//;

			$prep = s/<\/date>//;

			$LastPlayed = "$_";

#Needs to be in the format of 2011-01-15 15:15:15

			if ($LastPlayed =~ m/(^\d{4}-\d{2}-\d{2})/ ){
				$Date = $1;
							}

			if ($LastPlayed =~ m/(\d{2}:\d{2}:\d{2})/ ){
					$Time = $1;
						}

			$LastPlayed = $Date . " " . $Time;	
	
			chomp $LastPlayed;


					}

					

		elsif (/>Rating</){

			$prep = s/\r/\n/g;

			$prep = s/\r$//g;

			$prep = s/\t//g;

			$prep = s/<key>Rating<\/key><integer>//;

			$prep = s/<\/integer>//;

			chomp $_;

			$Rating = "$_";

					}

			

			

#Wrap up and commit to db, using the last tag in each entry

		elsif (/<\/dict>/){
			if (($Genre eq 'Audiobook') || ($Genre eq 'Podcast') || ($Genre eq 'Radio')){
#Do nothing with the info, clear values...

					
				$Name = " ";
				$TrackID = " ";
				$Artist = " ";
				$Album = " ";
				$Genre = " ";
				$Year = " ";
				$DateAdded = " ";
				$Plays = "0";
				$LastPlayed = " ";
				$Rating = "0";

				}

			else {
		
$sth = $dbh->prepare("insert into iTunes values('$TrackID', '$Name', '$Artist', '$Album', '$Genre', '$Year', '$DateAdded', '$Plays', '$LastPlayed', '$Rating')");

			$sth->execute();

			print("Added $Name by $Artist from $Album\n");

#Clear values...

				$Name = " ";
				$TrackID = " ";
				$Artist = " ";
				$Album = " ";
				$Genre = " ";
				$Year = " ";
				$DateAdded = " ";
				$Plays = "0";
				$LastPlayed = " ";
				$Rating = "0";
							}


						}


	}


close FILE;
$dbh->disconnect();
