InstagramBridge.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. class InstagramBridge extends BridgeAbstract{
  3. private $request;
  4. public function loadMetadatas() {
  5. $this->maintainer = "pauder";
  6. $this->name = "Instagram Bridge";
  7. $this->uri = "http://instagram.com/";
  8. $this->description = "Returns the newest images";
  9. $this->parameters[] = array(
  10. 'u'=>array('name'=>'username')
  11. );
  12. }
  13. public function collectData(array $param){
  14. $html = '';
  15. if (isset($param['u'])) { /* user timeline mode */
  16. $this->request = $param['u'];
  17. $html = $this->getSimpleHTMLDOM('http://instagram.com/'.urlencode($this->request)) or $this->returnServerError('Could not request Instagram.');
  18. }
  19. else {
  20. $this->returnClientError('You must specify a Instagram username (?u=...).');
  21. }
  22. $innertext = null;
  23. foreach($html->find('script') as $script)
  24. {
  25. if ('' === $script->innertext) {
  26. continue;
  27. }
  28. $pos = strpos(trim($script->innertext), 'window._sharedData');
  29. if (0 !== $pos)
  30. {
  31. continue;
  32. }
  33. $innertext = $script->innertext;
  34. break;
  35. }
  36. $json = trim(substr($innertext, $pos+18), ' =;');
  37. $data = json_decode($json);
  38. $userMedia = $data->entry_data->ProfilePage[0]->user->media->nodes;
  39. foreach($userMedia as $media)
  40. {
  41. $item = array();
  42. $item['uri'] = "https://instagram.com/p/".$media->code."/";
  43. $item['content'] = '<img src="' . htmlentities($media->display_src) . '" />';
  44. if (isset($media->caption))
  45. {
  46. $item['title'] = $media->caption;
  47. } else {
  48. $item['title'] = basename($media->display_src);
  49. }
  50. $item['timestamp'] = $media->date;
  51. $this->items[] = $item;
  52. }
  53. }
  54. public function getName(){
  55. return (!empty($this->request) ? $this->request .' - ' : '') .'Instagram Bridge';
  56. }
  57. }