PinterestBridge.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. ),
  18. 'From search' => array(
  19. 'q' => array(
  20. 'name' => 'Keyword',
  21. 'required' => true
  22. )
  23. )
  24. );
  25. public function collectData(){
  26. switch($this->queriedContext) {
  27. case 'By username and board':
  28. $this->collectExpandableDatas($this->getURI() . '.rss');
  29. $this->fixLowRes();
  30. break;
  31. case 'From search':
  32. default:
  33. $html = getSimpleHTMLDOMCached($this->getURI());
  34. $this->getSearchResults($html);
  35. }
  36. }
  37. private function fixLowRes() {
  38. $newitems = [];
  39. $pattern = '/https\:\/\/i\.pinimg\.com\/[a-zA-Z0-9]*x\//';
  40. foreach($this->items as $item) {
  41. $item['content'] = preg_replace($pattern, 'https://i.pinimg.com/originals/', $item['content']);
  42. $newitems[] = $item;
  43. }
  44. $this->items = $newitems;
  45. }
  46. private function getSearchResults($html){
  47. $json = json_decode($html->find('#jsInit1', 0)->innertext, true);
  48. $results = $json['resourceDataCache'][0]['data']['results'];
  49. foreach($results as $result) {
  50. $item = array();
  51. $item['uri'] = self::URI . $result['board']['url'];
  52. // Some use regular titles, others provide 'advanced' infos, a few
  53. // provide even less info. Thus we attempt multiple options.
  54. $item['title'] = trim($result['title']);
  55. if($item['title'] === '')
  56. $item['title'] = trim($result['rich_summary']['display_name']);
  57. if($item['title'] === '')
  58. $item['title'] = trim($result['grid_description']);
  59. $item['timestamp'] = strtotime($result['created_at']);
  60. $item['username'] = $result['pinner']['username'];
  61. $item['fullname'] = $result['pinner']['full_name'];
  62. $item['avatar'] = $result['pinner']['image_small_url'];
  63. $item['author'] = $item['username'] . ' (' . $item['fullname'] . ')';
  64. $item['content'] = '<img align="left" style="margin: 2px 4px;" src="'
  65. . htmlentities($item['avatar'])
  66. . '" /><p><strong>'
  67. . $item['username']
  68. . '</strong><br>'
  69. . $item['fullname']
  70. . '</p><br><img src="'
  71. . $result['images']['736x']['url']
  72. . '" alt="" /><br><p>'
  73. . $result['description']
  74. . '</p>';
  75. $item['enclosures'] = array($result['images']['orig']['url']);
  76. $this->items[] = $item;
  77. }
  78. }
  79. public function getURI(){
  80. switch($this->queriedContext) {
  81. case 'By username and board':
  82. $uri = self::URI . '/' . urlencode($this->getInput('u')) . '/' . urlencode($this->getInput('b'));// . '.rss';
  83. break;
  84. case 'From search':
  85. $uri = self::URI . '/search/?q=' . urlencode($this->getInput('q'));
  86. break;
  87. default: return parent::getURI();
  88. }
  89. return $uri;
  90. }
  91. public function getName(){
  92. switch($this->queriedContext) {
  93. case 'By username and board':
  94. $specific = $this->getInput('u') . ' - ' . $this->getInput('b');
  95. break;
  96. case 'From search':
  97. $specific = $this->getInput('q');
  98. break;
  99. default: return parent::getName();
  100. }
  101. return $specific . ' - ' . self::NAME;
  102. }
  103. }