PinterestBridge.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. class PinterestBridge extends FeedExpander {
  3. const MAINTAINER = 'pauder';
  4. const NAME = 'Pinterest Bridge';
  5. const URI = 'https://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. 'r' => array(
  18. 'name' => 'Use custom RSS',
  19. 'type' => 'checkbox',
  20. 'required' => false,
  21. 'title' => 'Uncheck to return data via custom filters (more data)'
  22. )
  23. ),
  24. 'From search' => array(
  25. 'q' => array(
  26. 'name' => 'Keyword',
  27. 'required' => true
  28. )
  29. )
  30. );
  31. public function collectData(){
  32. switch($this->queriedContext) {
  33. case 'By username and board':
  34. if($this->getInput('r')) {
  35. $html = getSimpleHTMLDOMCached($this->getURI());
  36. $this->getUserResults($html);
  37. } else {
  38. $this->collectExpandableDatas($this->getURI() . '.rss');
  39. }
  40. break;
  41. case 'From search':
  42. default:
  43. $html = getSimpleHTMLDOMCached($this->getURI());
  44. $this->getSearchResults($html);
  45. }
  46. }
  47. private function getUserResults($html){
  48. $json = json_decode($html->find('#jsInit1', 0)->innertext, true);
  49. $results = $json['tree']['children'][0]['children'][0]['children'][0]['options']['props']['data']['board_feed'];
  50. $username = $json['resourceDataCache'][0]['data']['owner']['username'];
  51. $fullname = $json['resourceDataCache'][0]['data']['owner']['full_name'];
  52. $avatar = $json['resourceDataCache'][0]['data']['owner']['image_small_url'];
  53. foreach($results as $result) {
  54. $item = array();
  55. $item['uri'] = $result['link'];
  56. // Some use regular titles, others provide 'advanced' infos, a few
  57. // provide even less info. Thus we attempt multiple options.
  58. $item['title'] = trim($result['title']);
  59. if($item['title'] === "")
  60. $item['title'] = trim($result['rich_summary']['display_name']);
  61. if($item['title'] === "")
  62. $item['title'] = trim($result['description']);
  63. $item['timestamp'] = strtotime($result['created_at']);
  64. $item['username'] = $username;
  65. $item['fullname'] = $fullname;
  66. $item['avatar'] = $avatar;
  67. $item['author'] = $item['username'] . ' (' . $item['fullname'] . ')';
  68. $item['content'] = '<img align="left" style="margin: 2px 4px;" src="'
  69. . htmlentities($item['avatar'])
  70. . '" /><p><strong>'
  71. . $item['username']
  72. . '</strong><br>'
  73. . $item['fullname']
  74. . '</p><br><img src="'
  75. . $result['images']['736x']['url']
  76. . '" alt="" /><br><p>'
  77. . $result['description']
  78. . '</p>';
  79. $item['enclosures'] = array($result['images']['orig']['url']);
  80. $this->items[] = $item;
  81. }
  82. }
  83. private function getSearchResults($html){
  84. $json = json_decode($html->find('#jsInit1', 0)->innertext, true);
  85. $results = $json['resourceDataCache'][0]['data']['results'];
  86. foreach($results as $result) {
  87. $item = array();
  88. $item['uri'] = self::URI . $result['board']['url'];
  89. // Some use regular titles, others provide 'advanced' infos, a few
  90. // provide even less info. Thus we attempt multiple options.
  91. $item['title'] = trim($result['title']);
  92. if($item['title'] === "")
  93. $item['title'] = trim($result['rich_summary']['display_name']);
  94. if($item['title'] === "")
  95. $item['title'] = trim($result['grid_description']);
  96. $item['timestamp'] = strtotime($result['created_at']);
  97. $item['username'] = $result['pinner']['username'];
  98. $item['fullname'] = $result['pinner']['full_name'];
  99. $item['avatar'] = $result['pinner']['image_small_url'];
  100. $item['author'] = $item['username'] . ' (' . $item['fullname'] . ')';
  101. $item['content'] = '<img align="left" style="margin: 2px 4px;" src="'
  102. . htmlentities($item['avatar'])
  103. . '" /><p><strong>'
  104. . $item['username']
  105. . '</strong><br>'
  106. . $item['fullname']
  107. . '</p><br><img src="'
  108. . $result['images']['736x']['url']
  109. . '" alt="" /><br><p>'
  110. . $result['description']
  111. . '</p>';
  112. $item['enclosures'] = array($result['images']['orig']['url']);
  113. $this->items[] = $item;
  114. }
  115. }
  116. public function getURI(){
  117. switch($this->queriedContext) {
  118. case 'By username and board':
  119. $uri = self::URI . '/' . urlencode($this->getInput('u')) . '/' . urlencode($this->getInput('b'));// . '.rss';
  120. break;
  121. case 'From search':
  122. $uri = self::URI . '/search/?q=' . urlencode($this->getInput('q'));
  123. break;
  124. default: return parent::getURI();
  125. }
  126. return $uri;
  127. }
  128. public function getName(){
  129. switch($this->queriedContext) {
  130. case 'By username and board':
  131. $specific = $this->getInput('u') . ' - ' . $this->getInput('b');
  132. break;
  133. case 'From search':
  134. $specific = $this->getInput('q');
  135. break;
  136. default: return parent::getName();
  137. }
  138. return $specific . ' - ' . self::NAME;
  139. }
  140. }