PinterestBridge.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * RssBridgePinterest
  4. * Returns the newest photos on a board
  5. * 2014-05-25
  6. *
  7. * @name Pinterest Bridge
  8. * @homepage http://www.pinterest.com/
  9. * @description Returns the newest images on a board
  10. * @maintainer pauder
  11. * @use1(u="username",b="board")
  12. * @use2(q="keyword")
  13. */
  14. class PinterestBridge extends BridgeAbstract{
  15. private $username;
  16. private $board;
  17. private $query;
  18. public function collectData(array $param){
  19. $html = '';
  20. if (isset($param['u']) || isset($param['b'])) {
  21. if (empty($param['u']))
  22. {
  23. $this->returnError('You must specify a Pinterest username (?u=...).', 400);
  24. }
  25. if (empty($param['b']))
  26. {
  27. $this->returnError('You must specify a Pinterest board for this username (?b=...).', 400);
  28. }
  29. $this->username = $param['u'];
  30. $this->board = $param['b'];
  31. $html = file_get_html($this->getURI().'/'.urlencode($this->username).'/'.urlencode($this->board)) or $this->returnError('Could not request Pinterest.', 404);
  32. } else if (isset($param['q']))
  33. {
  34. $this->query = $param['q'];
  35. $html = file_get_html($this->getURI().'/search/?q='.urlencode($this->query)) or $this->returnError('Could not request Pinterest.', 404);
  36. }
  37. else {
  38. $this->returnError('You must specify a Pinterest username and a board name (?u=...&b=...).', 400);
  39. }
  40. foreach($html->find('div.pinWrapper') as $div)
  41. {
  42. $a = $div->find('a.pinImageWrapper',0);
  43. $img = $a->find('img', 0);
  44. $item = new \Item();
  45. $item->uri = $this->getURI().$a->getAttribute('href');
  46. $item->content = '<img src="' . htmlentities(str_replace('/236x/', '/736x/', $img->getAttribute('src'))) . '" alt="" />';
  47. if (isset($this->query))
  48. {
  49. $avatar = $div->find('img.creditImg', 0);
  50. $username = $div->find('span.creditName', 0);
  51. $board = $div->find('span.creditTitle', 0);
  52. $item->username =$username->innertext;
  53. $item->fullname = $board->innertext;
  54. $item->avatar = $avatar->getAttribute('src');
  55. $item->content .= '<br /><img align="left" style="margin: 2px 4px;" src="'.htmlentities($item->avatar).'" /> <strong>'.$item->username.'</strong>';
  56. $item->content .= '<br />'.$item->fullname;
  57. }
  58. $item->title = $img->getAttribute('alt');
  59. //$item->timestamp = $media->created_time;
  60. $this->items[] = $item;
  61. }
  62. }
  63. public function getName(){
  64. if (isset($this->query))
  65. {
  66. return $this->query .' - Pinterest';
  67. } else {
  68. return $this->username .' - '. $this->board.' - Pinterest';
  69. }
  70. }
  71. public function getURI(){
  72. return 'http://www.pinterest.com';
  73. }
  74. public function getCacheDuration(){
  75. return 3600;
  76. }
  77. }