PinterestBridge.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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->parameters["By username and board"] = array(
  12. 'u'=>array('name'=>'username'),
  13. 'b'=>array('name'=>'board')
  14. );
  15. $this->parameters["From search"] = array(
  16. 'q'=>array('name'=>'Keyword')
  17. );
  18. }
  19. public function collectData(array $param){
  20. $html = '';
  21. if (isset($param['u']) || isset($param['b'])) {
  22. if (empty($param['u']))
  23. {
  24. $this->returnClientError('You must specify a Pinterest username (?u=...).');
  25. }
  26. if (empty($param['b']))
  27. {
  28. $this->returnClientError('You must specify a Pinterest board for this username (?b=...).');
  29. }
  30. $this->username = $param['u'];
  31. $this->board = $param['b'];
  32. $html = $this->getSimpleHTMLDOM($this->getURI().'/'.urlencode($this->username).'/'.urlencode($this->board)) or $this->returnServerError('Username and/or board not found');
  33. } else if (isset($param['q']))
  34. {
  35. $this->query = $param['q'];
  36. $html = $this->getSimpleHTMLDOM($this->getURI().'/search/?q='.urlencode($this->query)) or $this->returnServerError('Could not request Pinterest.');
  37. }
  38. else {
  39. $this->returnClientError('You must specify a Pinterest username and a board name (?u=...&b=...).');
  40. }
  41. foreach($html->find('div.pinWrapper') as $div)
  42. {
  43. $a = $div->find('a.pinImageWrapper',0);
  44. $img = $a->find('img', 0);
  45. $item = array();
  46. $item['uri'] = $this->getURI().$a->getAttribute('href');
  47. $item['content'] = '<img src="' . htmlentities(str_replace('/236x/', '/736x/', $img->getAttribute('src'))) . '" alt="" />';
  48. if (isset($this->query))
  49. {
  50. $avatar = $div->find('div.creditImg', 0)->find('img', 0);
  51. $avatar = $avatar->getAttribute('data-src');
  52. $avatar = str_replace("\\", "", $avatar);
  53. $username = $div->find('div.creditName', 0);
  54. $board = $div->find('div.creditTitle', 0);
  55. $item['username'] =$username->innertext;
  56. $item['fullname'] = $board->innertext;
  57. $item['avatar'] = $avatar;
  58. $item['content'] .= '<br /><img align="left" style="margin: 2px 4px;" src="'.htmlentities($item['avatar']).'" /> <strong>'.$item['username'].'</strong>';
  59. $item['content'] .= '<br />'.$item['fullname'];
  60. }
  61. $item['title'] = $img->getAttribute('alt');
  62. //$item['timestamp'] = $media->created_time;
  63. $this->items[] = $item;
  64. }
  65. }
  66. public function getName(){
  67. if (isset($this->query))
  68. {
  69. return $this->query .' - Pinterest';
  70. } else {
  71. return $this->username .' - '. $this->board.' - Pinterest';
  72. }
  73. }
  74. }