PinterestBridge.php 3.4 KB

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