1
0

PinterestBridge.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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><br />'
  57. . $item['fullname'];
  58. $item['title'] = $img->getAttribute('alt');
  59. $this->items[] = $item;
  60. }
  61. } elseif($this->queriedContext === 'By username and board'){
  62. $container = $html->find('SCRIPT[type="application/ld+json"]', 0)
  63. or returnServerError('Unable to find data container!');
  64. $json = json_decode($container->innertext, true);
  65. foreach($json['itemListElement'] as $element){
  66. $item = array();
  67. $item['uri'] = $element['item']['sharedContent']['author']['url'];
  68. $item['title'] = $element['item']['name'];
  69. $item['author'] = $element['item']['user']['name'];
  70. $item['timestamp'] = strtotime($element['item']['datePublished']);
  71. $item['content'] = <<<EOD
  72. <a href="{$item['uri']}">
  73. <img src="{$element['item']['image']}">
  74. </a>
  75. <p>{$element['item']['text']}</p>
  76. EOD;
  77. $this->items[] = $item;
  78. }
  79. }
  80. }
  81. public function getURI(){
  82. switch($this->queriedContext){
  83. case 'By username and board':
  84. $uri = self::URI . urlencode($this->getInput('u')) . '/' . urlencode($this->getInput('b'));
  85. break;
  86. case 'From search':
  87. $uri = self::URI . 'search/?q=' . urlencode($this->getInput('q'));
  88. break;
  89. default: return parent::getURI();
  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. default: return parent::getName();
  102. }
  103. return $specific . ' - ' . self::NAME;
  104. }
  105. }