Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1.  public function findTracksInITunesFor($artistName, $albumTitle)
  2. {
  3.     $iTunesTracks = null;
  4.            
  5.     try
  6.     {
  7.         $iTunesArtistInfo = $this->getArtistResultsFromITunes($artistName);
  8.                
  9.         if (($iTunesArtistInfo != null) && ( count($iTunesArtistInfo->results) > 0))  
  10.         {
  11.             $iTunesAlbumInfo = $this->getAlbumAndTracksFromITunes($iTunesArtistInfo->results[0]->artistId, $albumTitle);
  12.                    
  13.             if($iTunesAlbumInfo != null)
  14.             {                      
  15.                 for( $i = 1 ; $i < count($iTunesAlbumInfo->results); $i++)
  16.                 {
  17.                     // The first result is the album/artist info with the tracks taking up the rest of the space in the results array
  18.                     $correctIndex = $i - 1;
  19.                     $iTunesTracks[$correctIndex]['title'] = $iTunesAlbumInfo->results[$i]->trackName;
  20.                     $iTunesTracks[$correctIndex]['URL'] = $iTunesAlbumInfo->results[$i]->previewUrl;
  21.                     $iTunesTracks[$correctIndex]['purchaseURL'] = $iTunesAlbumInfo->results[$i]->trackViewUrl;
  22.                 }
  23.             }
  24.         }
  25.     }
  26.     catch (Exception $e)
  27.     {
  28.         throw new Exception("Not going to get Tracks for: " . $albumTitle . " from iTunes.");
  29.     }
  30.        
  31.            
  32.     return $iTunesTracks;
  33. }
  34.  
  35. protected function getArtistResultsFromITunes($artistName)
  36. {
  37.     $iTunesInfo = null;
  38.          
  39.     $strippedArtistName = $artistName;
  40.     $strippedArtistName = preg_replace("/[^a-zA-Z0-9]/", "", $strippedArtistName);
  41.            
  42.     if(is_string($artistName) && strlen($strippedArtistName) > 0)
  43.     {
  44.         $myCache = new Caching("./MashupCache/iTunes/", $strippedArtistName);
  45.                
  46.         if ($myCache->needToRenewData())
  47.         {
  48.             try
  49.             {
  50.                 $formattedArtistString = str_replace(' ', '+', $artistName);
  51.                 $iTunesSearchString = 'http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsSearch?term=' . $formattedArtistString . '&entity=musicArtist';
  52.                 $searchResult = fetchThisURL($iTunesSearchString);
  53.                 $iTunesInfo = json_decode($searchResult);
  54.             }
  55.             catch(Exception $e)
  56.             {  
  57.                 echo $e->getMessage();
  58.             }              
  59.                 $serializedObject = serialize($iTunesInfo);
  60.                    
  61.                 $myCache->saveSerializedDataToFile($serializedObject);
  62.             }
  63.             else
  64.             {
  65.                 // It doesn't need to be renewed so use local copy of array
  66.                 $iTunesInfo =  $myCache->getUnserializedData();
  67.             }
  68.         }
  69.         else
  70.         {
  71.             throw new Exception('Incorrect data type passed to getArtistResultsFromITunes()');
  72.         }
  73.            
  74.     return $iTunesInfo;
  75. }
  76.  
  77. protected function getAlbumAndTracksFromITunes($iTunesID, $albumName)
  78. {
  79.     $iTunesInfo = null;
  80.        
  81.     $properFileName = $iTunesID . $albumName;
  82.     $properFileName = preg_replace("/[^a-zA-Z0-9]/", "", $properFileName);
  83.            
  84.     if(strlen($properFileName) > 0)
  85.     {
  86.         $myCache = new Caching("./MashupCache/iTunes/", $properFileName);
  87.                
  88.         if ($myCache->needToRenewData())
  89.         {
  90.             try
  91.             {
  92.                 // First get all the albums by an artist using a lookup request
  93.                 //http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsLookup?id=909253&entity=album
  94.                 $iTunesLookUpString = 'http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsLookup?id=' . $iTunesID . '&entity=album';
  95.                 $lookUpResult = fetchThisURL($iTunesLookUpString);
  96.                 // 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
  97.                 // We need collectionName to equal $albumName
  98.                 $lookUp = json_decode($lookUpResult);
  99.                 if ( ! empty($lookUp))
  100.                 {
  101.                     $resultCount = $lookUp->resultCount;  // This isn't always correct, maybe have to check more carefully what was returned...
  102.                        
  103.                     $i = 1;  // First result is the artist not an album, I already had the artist info.
  104.                     $collectionID = null;
  105.                            
  106.                     while(($i < $resultCount) && ($collectionID == null))
  107.                     {
  108.                         // Look for album in iTunes...
  109.                         $collectionName = (string) $lookUp->results[$i]->collectionName;
  110.                         if( strcasecmp($collectionName, $albumName) == 0)  
  111.                         {
  112.                             // We have our match
  113.                             $collectionID = $lookUp->results[$i]->collectionId;
  114.                         }
  115.                                
  116.                         // remember to increment!
  117.                         $i++;
  118.                     }
  119.                            
  120.                     if( $collectionID != null)
  121.                     {
  122.                         // If collectionID isn't null... lookup album again in iTunes with tracks and that is what we really really want!
  123.                         // cache and return...
  124.                                 //http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsLookup?upc=075678317729&entity=song
  125.                         $newLookupString = 'http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsLookup?id=' . $collectionID . '&entity=song';
  126.                         $newLookUpResult = fetchThisURL($newLookupString);
  127.                         $iTunesInfo = json_decode($newLookUpResult);
  128.                                
  129.                         $serializedObject = serialize($iTunesInfo);
  130.                         $myCache->saveSerializedDataToFile($serializedObject);
  131.                     }
  132.                 }      
  133.             }
  134.             catch(Exception $e)
  135.             {  
  136.                 echo $e->getMessage();  
  137.             }              
  138.         }
  139.         else
  140.         {
  141.             // It doesn't need to be renewed so use local copy
  142.             $iTunesInfo =  $myCache->getUnserializedData();
  143.         }
  144.     }
  145.            
  146.     return $iTunesInfo;
  147. }