set_preferences(); $data = $user->prefs; if (strlen(trim($data['amazon_base_url']))) { $this->amazon_base_url = trim($data['amazon_base_url']); } else { debug_event($this->name,'No amazon base url, plugin skipped','3'); return false; } if (strlen(trim($data['amazon_developer_public_key']))) { $this->amazon_developer_public_key = trim($data['amazon_developer_public_key']); } else { debug_event($this->name,'No amazon developer public key, plugin skipped','3'); return false; } if (strlen(trim($data['amazon_developer_private_api_key']))) { $this->amazon_developer_private_api_key = trim($data['amazon_developer_private_api_key']); } else { debug_event($this->name,'No amazon developer private key, plugin skipped','3'); return false; } if (strlen(trim($data['amazon_max_results_pages']))) { $this->amazon_max_results_pages = trim($data['amazon_max_results_pages']); } else { $this->amazon_max_results_pages = 1; } if (strlen(trim($data['amazon_developer_associate_tag']))) { $this->amazon_developer_associate_tag = trim($data['amazon_developer_associate_tag']); } else { $this->amazon_developer_associate_tag = ''; } return true; } // load /** * get_arts * Returns arts for what we're passed in. */ public static 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') 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($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); // If we've hit our limit if (!empty($limit) && count($final_results) >= $limit) { break; } /* 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; } // get_metadata } // end AmpacheAmazon ?>