/** * This method I created as part of my favourite or loved tracks mashup. The code is for creating an instance of this * class populated by the albums that correspond to a user's loved tracks in last.fm. By putting the code here as a static method * future users of this class can populate their music collection with this info too. * * In the array I also put the track name and the url on Last.fm, I used non-numeric indices for this extra data. * * @return musicCollection */ public static function collectionFromLovedTracks() { $lovedTracksInfo = array(); $myLovedTracks = musicCollection::getLovedTracksFor(myInfo::MY_LAST_FM_USER_NAME); // New Caching Code $strippedUserName = preg_replace("/[^a-zA-Z0-9]/", "", myInfo::MY_LAST_FM_USER_NAME); if (strlen($strippedUserName) > 0) { $strippedUserName = $strippedUserName . 'LovedTracks'; $myCache = new Caching("./MashupCache/LovedTracks/", $strippedUserName); if ($myCache->needToRenewData()) { $lovedTracksCount = count($myLovedTracks); $artistName = ''; $albumTitle = ''; $songTitle = ''; $songURL = ''; $i = 0; while ($i < $lovedTracksCount) { // Need album titles $formattedSongTitle = str_replace(' ', '+', $myLovedTracks[$i]['name']); // http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsSearch?term=song+title&entity=musicTrack $iTunesSearch = 'http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsSearch?term=' . $formattedSongTitle . '&media=music&entity=musicTrack&attribute=musicTrackTerm'; $iTunesSearchResults = fetchThisURL($iTunesSearch); $iTunesResults = json_decode($iTunesSearchResults); $artistName = $myLovedTracks[$i]['artist']['name']; $songTitle = $myLovedTracks[$i]['name']; $songURL = 'http://' . $myLovedTracks[$i]['url']; // This should solve some linking issues... if ($iTunesResults->resultCount > 0) { $j = 0; $foundAlbum = false; while ($j < $iTunesResults->resultCount && !$foundAlbum) { if (strcmp($iTunesResults->results[$j]->artistName, $myLovedTracks[$i]['artist']['name']) == 0) { $albumTitle = $iTunesResults->results[$j]->collectionName; $foundAlbum = true; } $j++; } // if we don't find one, such as in the case of Tom Waits "Looking for the heart of Saturday Night" , we are end up with the wrong data... if (!$foundAlbum) { // This is necessary $albumTitle = ''; } } else { $albumTitle = ''; } $lovedTracksInfo[$i] = array(0 => $artistName, 1 => $albumTitle, 'songTitle' => $songTitle, 'songURL' => $songURL); $i++; } $reformatedTracks = serialize($lovedTracksInfo); $myCache->saveSerializedDataToFile($reformatedTracks); } else { // It doesn't need to be renewed so use local copy of array $lovedTracksInfo = $myCache->getUnserializedData(); } } return new musicCollection($lovedTracksInfo); }