PinterestBridge.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. class PinterestBridge extends BridgeAbstract {
  3. const MAINTAINER = "pauder";
  4. const NAME = "Pinterest Bridge";
  5. const URI = "http://www.pinterest.com/";
  6. const DESCRIPTION = "Returns the newest images on a board";
  7. const PARAMETERS = array(
  8. 'By username and board' => array(
  9. 'u' => array(
  10. 'name' => 'username',
  11. 'required' => true
  12. ),
  13. 'b' => array(
  14. 'name' => 'board',
  15. 'required' => true
  16. )
  17. ),
  18. 'From search' => array(
  19. 'q' => array(
  20. 'name' => 'Keyword',
  21. 'required' => true
  22. )
  23. )
  24. );
  25. public function collectData(){
  26. $html = getSimpleHTMLDOM($this->getURI());
  27. if(!$html){
  28. switch($this->queriedContext){
  29. case 'By username and board':
  30. returnServerError('Username and/or board not found');
  31. case 'From search':
  32. returnServerError('Could not request Pinterest.');
  33. }
  34. }
  35. if($this->queriedContext === 'From search'){
  36. foreach($html->find('div.pinWrapper') as $div){
  37. $item = array();
  38. $a = $div->find('a.pinImageWrapper', 0);
  39. $img = $a->find('img', 0);
  40. $item['uri'] = $this->getURI() . $a->getAttribute('href');
  41. $item['content'] = '<img src="'
  42. . htmlentities(str_replace('/236x/', '/736x/', $img->getAttribute('src')))
  43. . '" alt="" />';
  44. $avatar = $div->find('div.creditImg', 0)->find('img', 0);
  45. $avatar = $avatar->getAttribute('data-src');
  46. $avatar = str_replace("\\", "", $avatar);
  47. $username = $div->find('div.creditName', 0);
  48. $board = $div->find('div.creditTitle', 0);
  49. $item['username'] = $username->innertext;
  50. $item['fullname'] = $board->innertext;
  51. $item['avatar'] = $avatar;
  52. $item['content'] .= '<br /><img align="left" style="margin: 2px 4px;" src="'
  53. . htmlentities($item['avatar'])
  54. . '" /> <strong>'
  55. . $item['username']
  56. . '</strong>'
  57. . '<br />'
  58. . $item['fullname'];
  59. $item['title'] = $img->getAttribute('alt');
  60. $this->items[] = $item;
  61. }
  62. } elseif($this->queriedContext === 'By username and board'){
  63. $container = $html->find('SCRIPT[type="application/ld+json"]', 0)
  64. or returnServerError('Unable to find data container!');
  65. $json = json_decode($container->innertext, true);
  66. foreach($json['itemListElement'] as $element){
  67. $item = array();
  68. $item['uri'] = $element['item']['sharedContent']['author']['url'];
  69. $item['title'] = $element['item']['name'];
  70. $item['author'] = $element['item']['user']['name'];
  71. $item['timestamp'] = strtotime($element['item']['datePublished']);
  72. $item['content'] = <<<EOD
  73. <a href="{$item['uri']}">
  74. <img src="{$element['item']['image']}">
  75. </a>
  76. <p>{$element['item']['text']}</p>
  77. EOD;
  78. $this->items[] = $item;
  79. }
  80. }
  81. }
  82. public function getURI(){
  83. switch($this->queriedContext){
  84. case 'By username and board':
  85. $uri = self::URI . urlencode($this->getInput('u')) . '/' . urlencode($this->getInput('b'));
  86. break;
  87. case 'From search':
  88. $uri = self::URI . 'search/?q=' . urlencode($this->getInput('q'));
  89. break;
  90. }
  91. return $uri;
  92. }
  93. public function getName(){
  94. switch($this->queriedContext){
  95. case 'By username and board':
  96. $specific = $this->getInput('u') . '-' . $this->getInput('b');
  97. break;
  98. case 'From search':
  99. $specific = $this->getInput('q');
  100. break;
  101. }
  102. return $specific . ' - ' . self::NAME;
  103. }
  104. }