AmazonBridge.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. class AmazonBridge extends BridgeAbstract {
  3. const MAINTAINER = 'Alexis CHEMEL';
  4. const NAME = 'Amazon';
  5. const URI = 'https://www.amazon.com/';
  6. const CACHE_TIMEOUT = 3600; // 1h
  7. const DESCRIPTION = 'Returns products from Amazon search';
  8. const PARAMETERS = array(array(
  9. 'q' => array(
  10. 'name' => 'Keyword',
  11. 'required' => true,
  12. ),
  13. 'sort' => array(
  14. 'name' => 'Sort by',
  15. 'type' => 'list',
  16. 'required' => false,
  17. 'values' => array(
  18. 'Relevance' => 'relevanceblender',
  19. 'Price: Low to High' => 'price-asc-rank',
  20. 'Price: High to Low' => 'price-desc-rank',
  21. 'Average Customer Review' => 'review-rank',
  22. 'Newest Arrivals' => 'date-desc-rank',
  23. ),
  24. 'defaultValue' => 'relevanceblender',
  25. ),
  26. 'tld' => array(
  27. 'name' => 'Country',
  28. 'type' => 'list',
  29. 'required' => true,
  30. 'values' => array(
  31. 'Australia' => 'com.au',
  32. 'Brazil' => 'com.br',
  33. 'Canada' => 'ca',
  34. 'China' => 'cn',
  35. 'France' => 'fr',
  36. 'Germany' => 'de',
  37. 'India' => 'in',
  38. 'Italy' => 'it',
  39. 'Japan' => 'co.jp',
  40. 'Mexico' => 'com.mx',
  41. 'Netherlands' => 'nl',
  42. 'Spain' => 'es',
  43. 'United Kingdom' => 'co.uk',
  44. 'United States' => 'com',
  45. ),
  46. 'defaultValue' => 'com',
  47. ),
  48. ));
  49. public function getName(){
  50. if(!is_null($this->getInput('tld')) && !is_null($this->getInput('q'))) {
  51. return 'Amazon.'.$this->getInput('tld').': '.$this->getInput('q');
  52. }
  53. return parent::getName();
  54. }
  55. public function collectData() {
  56. $uri = 'https://www.amazon.'.$this->getInput('tld').'/';
  57. $uri .= 's/?field-keywords='.urlencode($this->getInput('q')).'&sort='.$this->getInput('sort');
  58. $html = getSimpleHTMLDOM($uri)
  59. or returnServerError('Could not request Amazon.');
  60. foreach($html->find('li.s-result-item') as $element) {
  61. $item = array();
  62. // Title
  63. $title = $element->find('h2', 0);
  64. $item['title'] = html_entity_decode($title->innertext, ENT_QUOTES);
  65. // Url
  66. $uri = $title->parent()->getAttribute('href');
  67. $uri = substr($uri, 0, strrpos($uri, '/'));
  68. $item['uri'] = substr($uri, 0, strrpos($uri, '/'));
  69. // Content
  70. $image = $element->find('img', 0);
  71. $price = $element->find('span.s-price', 0);
  72. $price = ($price) ? $price->innertext : '';
  73. $item['content'] = '<img src="'.$image->getAttribute('src').'" /><br />'.$price;
  74. $this->items[] = $item;
  75. }
  76. }
  77. }