InstagramBridge.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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( array(
  8. 'u' => array(
  9. 'name' => 'username',
  10. 'required' => true
  11. ),
  12. 'media_type' => array(
  13. 'name' => 'Media type',
  14. 'type' => 'list',
  15. 'required' => false,
  16. 'values' => array(
  17. 'Both' => 'all',
  18. 'Video' => 'video',
  19. 'Picture' => 'picture'
  20. ),
  21. 'defaultValue' => 'all'
  22. )
  23. ));
  24. public function collectData(){
  25. $html = getSimpleHTMLDOM($this->getURI())
  26. or returnServerError('Could not request Instagram.');
  27. $innertext = null;
  28. foreach($html->find('script') as $script) {
  29. if('' === $script->innertext) {
  30. continue;
  31. }
  32. $pos = strpos(trim($script->innertext), 'window._sharedData');
  33. if(0 !== $pos) {
  34. continue;
  35. }
  36. $innertext = $script->innertext;
  37. break;
  38. }
  39. $json = trim(substr($innertext, $pos + 18), ' =;');
  40. $data = json_decode($json);
  41. $userMedia = $data->entry_data->ProfilePage[0]->user->media->nodes;
  42. foreach($userMedia as $media) {
  43. // Check media type
  44. switch($this->getInput('media_type')) {
  45. case 'all': break;
  46. case 'video':
  47. if($media->is_video === false) continue 2;
  48. break;
  49. case 'picture':
  50. if($media->is_video === true) continue 2;
  51. break;
  52. default: break;
  53. }
  54. $item = array();
  55. $item['uri'] = self::URI . 'p/' . $media->code . '/';
  56. $item['content'] = '<img src="' . htmlentities($media->display_src) . '" />';
  57. if (isset($media->caption)) {
  58. $item['title'] = $media->caption;
  59. } else {
  60. $item['title'] = basename($media->display_src);
  61. }
  62. $item['timestamp'] = $media->date;
  63. $this->items[] = $item;
  64. }
  65. }
  66. public function getName(){
  67. if(!is_null($this->getInput('u'))) {
  68. return $this->getInput('u') . ' - Instagram Bridge';
  69. }
  70. return parent::getName();
  71. }
  72. public function getURI(){
  73. if(!is_null($this->getInput('u'))) {
  74. return self::URI . urlencode($this->getInput('u'));
  75. }
  76. return parent::getURI();
  77. }
  78. }