PinterestBridge.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * RssBridgePinterest
  4. * Returns the newest photos on a board
  5. *
  6. * @name Pinterest Bridge
  7. * @description Returns the newest images on a board
  8. * @use1(u="username",b="board")
  9. * @use2(q="keyword")
  10. */
  11. class PinterestBridge extends BridgeAbstract{
  12. private $username;
  13. private $board;
  14. private $query;
  15. public function collectData(array $param){
  16. $html = '';
  17. if (isset($param['u']) || isset($param['b'])) {
  18. if (empty($param['u']))
  19. {
  20. $this->returnError('You must specify a Pinterest username (?u=...).', 400);
  21. }
  22. if (empty($param['b']))
  23. {
  24. $this->returnError('You must specify a Pinterest board for this username (?b=...).', 400);
  25. }
  26. $this->username = $param['u'];
  27. $this->board = $param['b'];
  28. $html = file_get_html($this->getURI().'/'.urlencode($this->username).'/'.urlencode($this->board)) or $this->returnError('Could not request Pinterest.', 404);
  29. } else if (isset($param['q']))
  30. {
  31. $this->query = $param['q'];
  32. $html = file_get_html($this->getURI().'/search/?q='.urlencode($this->query)) or $this->returnError('Could not request Pinterest.', 404);
  33. }
  34. else {
  35. $this->returnError('You must specify a Pinterest username and a board name (?u=...&b=...).', 400);
  36. }
  37. foreach($html->find('div.pinWrapper') as $div)
  38. {
  39. $a = $div->find('a.pinImageWrapper',0);
  40. $img = $a->find('img', 0);
  41. $item = new \Item();
  42. $item->uri = $this->getURI().$a->getAttribute('href');
  43. $item->content = '<img src="' . htmlentities($img->getAttribute('src')) . '" alt="" />';
  44. if (isset($this->query))
  45. {
  46. $avatar = $div->find('img.creditImg', 0);
  47. $username = $div->find('span.creditName', 0);
  48. $board = $div->find('span.creditTitle', 0);
  49. $item->username =$username->innertext;
  50. $item->fullname = $board->innertext;
  51. $item->avatar = $avatar->getAttribute('src');
  52. $item->content .= '<br /><img align="left" style="margin: 2px 4px;" src="'.htmlentities($item->avatar).'" /> <strong>'.$item->username.'</strong>';
  53. $item->content .= '<br />'.$item->fullname;
  54. } else {
  55. $credit = $div->find('a.creditItem',0);
  56. $item->content .= '<br />'.$credit->innertext;
  57. }
  58. $item->title = basename($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 0;
  76. }
  77. }