InstagramBridge.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. class InstagramBridge extends BridgeAbstract {
  3. const MAINTAINER = 'pauder';
  4. const NAME = 'Instagram Bridge';
  5. const URI = 'https://instagram.com/';
  6. const DESCRIPTION = 'Returns the newest images';
  7. const PARAMETERS = array(
  8. array(
  9. 'u' => array(
  10. 'name' => 'username',
  11. 'required' => true
  12. ),
  13. 'media_type' => array(
  14. 'name' => 'Media type',
  15. 'type' => 'list',
  16. 'required' => false,
  17. 'values' => array(
  18. 'Both' => 'all',
  19. 'Video' => 'video',
  20. 'Picture' => 'picture'
  21. ),
  22. 'defaultValue' => 'all'
  23. )
  24. ),
  25. array(
  26. 'h' => array(
  27. 'name' => 'hashtag',
  28. 'required' => true
  29. ),
  30. 'media_type' => array(
  31. 'name' => 'Media type',
  32. 'type' => 'list',
  33. 'required' => false,
  34. 'values' => array(
  35. 'Both' => 'all',
  36. 'Video' => 'video',
  37. 'Picture' => 'picture'
  38. ),
  39. 'defaultValue' => 'all'
  40. )
  41. )
  42. );
  43. public function collectData(){
  44. $html = getSimpleHTMLDOM($this->getURI())
  45. or returnServerError('Could not request Instagram.');
  46. $innertext = null;
  47. foreach($html->find('script') as $script) {
  48. if('' === $script->innertext) {
  49. continue;
  50. }
  51. $pos = strpos(trim($script->innertext), 'window._sharedData');
  52. if(0 !== $pos) {
  53. continue;
  54. }
  55. $innertext = $script->innertext;
  56. break;
  57. }
  58. $json = trim(substr($innertext, $pos + 18), ' =;');
  59. $data = json_decode($json);
  60. if(!is_null($this->getInput('u'))) {
  61. $userMedia = $data->entry_data->ProfilePage[0]->graphql->user->edge_owner_to_timeline_media->edges;
  62. } else {
  63. $userMedia = $data->entry_data->TagPage[0]->graphql->hashtag->edge_hashtag_to_media->edges;
  64. }
  65. foreach($userMedia as $media) {
  66. $media = $media->node;
  67. // Check media type
  68. switch($this->getInput('media_type')) {
  69. case 'all': break;
  70. case 'video':
  71. if($media->is_video === false) continue 2;
  72. break;
  73. case 'picture':
  74. if($media->is_video === true) continue 2;
  75. break;
  76. default: break;
  77. }
  78. $item = array();
  79. $item['uri'] = self::URI . 'p/' . $media->shortcode . '/';
  80. $item['content'] = '<img src="' . htmlentities($media->display_url) . '" />';
  81. if (isset($media->edge_media_to_caption->edges[0]->node->text)) {
  82. $item['title'] = $media->edge_media_to_caption->edges[0]->node->text;
  83. } else {
  84. $item['title'] = basename($media->display_url);
  85. }
  86. $item['timestamp'] = $media->taken_at_timestamp;
  87. $this->items[] = $item;
  88. }
  89. }
  90. public function getName(){
  91. if(!is_null($this->getInput('u'))) {
  92. return $this->getInput('u') . ' - Instagram Bridge';
  93. }
  94. return parent::getName();
  95. }
  96. public function getURI(){
  97. if(!is_null($this->getInput('u'))) {
  98. return self::URI . urlencode($this->getInput('u'));
  99. } elseif(!is_null($this->getInput('h'))) {
  100. return self::URI . 'explore/tags/' . urlencode($this->getInput('h'));
  101. }
  102. return parent::getURI();
  103. }
  104. }