Beispiel #1
0
 /**
  * gather_arts
  * Returns arts for what we're passed in.
  */
 public function gather_arts($type, $options = array(), $limit = 5)
 {
     $images = array();
     $final_results = array();
     $possible_keys = array('LargeImage', 'MediumImage', 'SmallImage');
     $mediaType = $type == 'album' || $type == 'artist' ? 'Music' : 'Video';
     // Prevent the script from timing out
     set_time_limit(0);
     // Create the Search Object
     $amazon = new AmazonSearch($this->amazon_developer_public_key, $this->amazon_developer_private_api_key, $this->amazon_developer_associate_tag, $this->amazon_base_url);
     if (AmpConfig::get('proxy_host') && AmpConfig::get('proxy_port')) {
         $proxyhost = AmpConfig::get('proxy_host');
         $proxyport = AmpConfig::get('proxy_port');
         $proxyuser = AmpConfig::get('proxy_user');
         $proxypass = AmpConfig::get('proxy_pass');
         debug_event('amazon', 'setProxy', 5);
         $amazon->setProxy($proxyhost, $proxyport, $proxyuser, $proxypass);
     }
     $search_results = array();
     /* Set up the needed variables */
     $max_pages_to_search = max($this->amazon_max_results_pages, $amazon->_default_results_pages);
     // while we have pages to search
     do {
         $raw_results = $amazon->search(array('artist' => '', 'album' => '', 'keywords' => $options['keyword']), $mediaType);
         $total = count($raw_results) + count($search_results);
         // If we've gotten more then we wanted
         if ($limit && $total > $limit) {
             $raw_results = array_slice($raw_results, 0, -($total - $limit), true);
             debug_event('amazon-xml', "Found {$total}, limit {$limit}; reducing and breaking from loop", 5);
             // Merge the results and BREAK!
             $search_results = array_merge($search_results, $raw_results);
             break;
         }
         // if limit defined
         $search_results = array_merge($search_results, $raw_results);
         $pages_to_search = min($max_pages_to_search, $amazon->_maxPage);
         debug_event('amazon-xml', "Searched results page " . ($amazon->_currentPage + 1) . "/" . $pages_to_search, '5');
         $amazon->_currentPage++;
     } while ($amazon->_currentPage < $pages_to_search);
     // Only do the second search if the first actually returns something
     if (count($search_results)) {
         $final_results = $amazon->lookup($search_results);
     }
     /* Log this if we're doin debug */
     debug_event('amazon-xml', "Searched using " . $options['keyword'] . ", results: " . count($final_results), 5);
     /* Foreach through what we've found */
     foreach ($final_results as $result) {
         $key = '';
         /* Recurse through the images found */
         foreach ($possible_keys as $k) {
             if (strlen($result[$k])) {
                 $key = $k;
                 break;
             }
         }
         // foreach
         if (!empty($key)) {
             // Rudimentary image type detection, only JPG and GIF allowed.
             if (substr($result[$key], -4) == '.jpg') {
                 $mime = "image/jpeg";
             } elseif (substr($result[$key], -4) == '.gif') {
                 $mime = "image/gif";
             } elseif (substr($result[$key], -4) == '.png') {
                 $mime = "image/png";
             } else {
                 /* Just go to the next result */
                 continue;
             }
             $data = array();
             $data['url'] = $result[$key];
             $data['mime'] = $mime;
             $data['title'] = $this->name;
             $images[] = $data;
             if (!empty($limit)) {
                 if (count($images) >= $limit) {
                     return $images;
                 }
             }
         }
     }
     // if we've got something
     return $images;
 }
Beispiel #2
0
 /**
  * gather_amazon
  * This takes keywords and performs a search of the Amazon website
  * for the art. It returns an array of found objects with mime/url keys
  */
 public function gather_amazon($limit = 5, $keywords = '')
 {
     $images = array();
     $final_results = array();
     $possible_keys = array('LargeImage', 'MediumImage', 'SmallImage');
     if ($this->type == 'album') {
         $album = new Album($this->uid);
     } else {
         return $images;
     }
     // Prevent the script from timing out
     set_time_limit(0);
     if (empty($keywords)) {
         $keywords = $album->full_name;
         /* If this isn't a various album combine with artist name */
         if ($album->artist_count == '1') {
             $keywords .= ' ' . $album->artist_name;
         }
     }
     /* Attempt to retrieve the album art order */
     $amazon_base_urls = AmpConfig::get('amazon_base_urls');
     /* If it's not set */
     if (!count($amazon_base_urls)) {
         $amazon_base_urls = array('http://webservices.amazon.com');
     }
     /* Foreach through the base urls that we should check */
     foreach ($amazon_base_urls as $amazon_base) {
         // Create the Search Object
         $amazon = new AmazonSearch(AmpConfig::get('amazon_developer_public_key'), AmpConfig::get('amazon_developer_private_key'), AmpConfig::get('amazon_developer_associate_tag'), $amazon_base);
         if (AmpConfig::get('proxy_host') and AmpConfig::get('proxy_port')) {
             $proxyhost = AmpConfig::get('proxy_host');
             $proxyport = AmpConfig::get('proxy_port');
             $proxyuser = AmpConfig::get('proxy_user');
             $proxypass = AmpConfig::get('proxy_pass');
             debug_event('amazon', 'setProxy', 5);
             $amazon->setProxy($proxyhost, $proxyport, $proxyuser, $proxypass);
         }
         $search_results = array();
         /* Set up the needed variables */
         $max_pages_to_search = max(AmpConfig::get('max_amazon_results_pages'), $amazon->_default_results_pages);
         // while we have pages to search
         do {
             $raw_results = $amazon->search(array('artist' => '', 'album' => '', 'keywords' => $keywords));
             $total = count($raw_results) + count($search_results);
             // If we've gotten more then we wanted
             if ($limit && $total > $limit) {
                 $raw_results = array_slice($raw_results, 0, -($total - $limit), true);
                 debug_event('amazon-xml', "Found {$total}, limit {$limit}; reducing and breaking from loop", 5);
                 // Merge the results and BREAK!
                 $search_results = array_merge($search_results, $raw_results);
                 break;
             }
             // if limit defined
             $search_results = array_merge($search_results, $raw_results);
             $pages_to_search = min($max_pages_to_search, $amazon->_maxPage);
             debug_event('amazon-xml', "Searched results page " . ($amazon->_currentPage + 1) . "/" . $pages_to_search, '5');
             $amazon->_currentPage++;
         } while ($amazon->_currentPage < $pages_to_search);
         // Only do the second search if the first actually returns something
         if (count($search_results)) {
             $final_results = $amazon->lookup($search_results);
         }
         /* Log this if we're doin debug */
         debug_event('amazon-xml', "Searched using {$keywords} with " . AmpConfig::get('amazon_developer_key') . " as key, results: " . count($final_results), 5);
         // If we've hit our limit
         if (!empty($limit) && count($final_results) >= $limit) {
             break;
         }
     }
     // end foreach
     /* Foreach through what we've found */
     foreach ($final_results as $result) {
         $key = '';
         /* Recurse through the images found */
         foreach ($possible_keys as $k) {
             if (strlen($result[$k])) {
                 $key = $k;
                 break;
             }
         }
         // foreach
         // Rudimentary image type detection, only JPG and GIF allowed.
         if (substr($result[$key], -4) == '.jpg') {
             $mime = "image/jpeg";
         } elseif (substr($result[$key], -4) == '.gif') {
             $mime = "image/gif";
         } elseif (substr($result[$key], -4) == '.png') {
             $mime = "image/png";
         } else {
             /* Just go to the next result */
             continue;
         }
         $data = array();
         $data['url'] = $result[$key];
         $data['mime'] = $mime;
         $images[] = $data;
         if (!empty($limit)) {
             if (count($images) >= $limit) {
                 return $images;
             }
         }
     }
     // if we've got something
     return $images;
 }