Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function findTracksInITunesFor($artistName, $albumTitle)
- {
- $iTunesTracks = null;
- try
- {
- $iTunesArtistInfo = $this->getArtistResultsFromITunes($artistName);
- if (($iTunesArtistInfo != null) && ( count($iTunesArtistInfo->results) > 0))
- {
- $iTunesAlbumInfo = $this->getAlbumAndTracksFromITunes($iTunesArtistInfo->results[0]->artistId, $albumTitle);
- if($iTunesAlbumInfo != null)
- {
- for( $i = 1 ; $i < count($iTunesAlbumInfo->results); $i++)
- {
- // The first result is the album/artist info with the tracks taking up the rest of the space in the results array
- $correctIndex = $i - 1;
- $iTunesTracks[$correctIndex]['title'] = $iTunesAlbumInfo->results[$i]->trackName;
- $iTunesTracks[$correctIndex]['URL'] = $iTunesAlbumInfo->results[$i]->previewUrl;
- $iTunesTracks[$correctIndex]['purchaseURL'] = $iTunesAlbumInfo->results[$i]->trackViewUrl;
- }
- }
- }
- }
- catch (Exception $e)
- {
- throw new Exception("Not going to get Tracks for: " . $albumTitle . " from iTunes.");
- }
- return $iTunesTracks;
- }
- protected function getArtistResultsFromITunes($artistName)
- {
- $iTunesInfo = null;
- $strippedArtistName = $artistName;
- $strippedArtistName = preg_replace("/[^a-zA-Z0-9]/", "", $strippedArtistName);
- if(is_string($artistName) && strlen($strippedArtistName) > 0)
- {
- $myCache = new Caching("./MashupCache/iTunes/", $strippedArtistName);
- if ($myCache->needToRenewData())
- {
- try
- {
- $formattedArtistString = str_replace(' ', '+', $artistName);
- $iTunesSearchString = 'http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsSearch?term=' . $formattedArtistString . '&entity=musicArtist';
- $searchResult = fetchThisURL($iTunesSearchString);
- $iTunesInfo = json_decode($searchResult);
- }
- catch(Exception $e)
- {
- echo $e->getMessage();
- }
- $serializedObject = serialize($iTunesInfo);
- $myCache->saveSerializedDataToFile($serializedObject);
- }
- else
- {
- // It doesn't need to be renewed so use local copy of array
- $iTunesInfo = $myCache->getUnserializedData();
- }
- }
- else
- {
- throw new Exception('Incorrect data type passed to getArtistResultsFromITunes()');
- }
- return $iTunesInfo;
- }
- protected function getAlbumAndTracksFromITunes($iTunesID, $albumName)
- {
- $iTunesInfo = null;
- $properFileName = $iTunesID . $albumName;
- $properFileName = preg_replace("/[^a-zA-Z0-9]/", "", $properFileName);
- if(strlen($properFileName) > 0)
- {
- $myCache = new Caching("./MashupCache/iTunes/", $properFileName);
- if ($myCache->needToRenewData())
- {
- try
- {
- // First get all the albums by an artist using a lookup request
- //http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsLookup?id=909253&entity=album
- $iTunesLookUpString = 'http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsLookup?id=' . $iTunesID . '&entity=album';
- $lookUpResult = fetchThisURL($iTunesLookUpString);
- // Now we have the look up results but we need to find the $iTunesID for the correct album then get the tracks that go with it
- // We need collectionName to equal $albumName
- $lookUp = json_decode($lookUpResult);
- if ( ! empty($lookUp))
- {
- $resultCount = $lookUp->resultCount; // This isn't always correct, maybe have to check more carefully what was returned...
- $i = 1; // First result is the artist not an album, I already had the artist info.
- $collectionID = null;
- while(($i < $resultCount) && ($collectionID == null))
- {
- // Look for album in iTunes...
- $collectionName = (string) $lookUp->results[$i]->collectionName;
- if( strcasecmp($collectionName, $albumName) == 0)
- {
- // We have our match
- $collectionID = $lookUp->results[$i]->collectionId;
- }
- // remember to increment!
- $i++;
- }
- if( $collectionID != null)
- {
- // If collectionID isn't null... lookup album again in iTunes with tracks and that is what we really really want!
- // cache and return...
- //http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsLookup?upc=075678317729&entity=song
- $newLookupString = 'http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsLookup?id=' . $collectionID . '&entity=song';
- $newLookUpResult = fetchThisURL($newLookupString);
- $iTunesInfo = json_decode($newLookUpResult);
- $serializedObject = serialize($iTunesInfo);
- $myCache->saveSerializedDataToFile($serializedObject);
- }
- }
- }
- catch(Exception $e)
- {
- echo $e->getMessage();
- }
- }
- else
- {
- // It doesn't need to be renewed so use local copy
- $iTunesInfo = $myCache->getUnserializedData();
- }
- }
- return $iTunesInfo;
- }
Advertisement
Add Comment
Please, Sign In to add comment