InstagramBridge.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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->update = "2014-05-25";
  10. $this->parameters[] =
  11. '[
  12. {
  13. "name" : "username",
  14. "identifier" : "u"
  15. }
  16. ]';
  17. }
  18. public function collectData(array $param){
  19. $html = '';
  20. if (isset($param['u'])) { /* user timeline mode */
  21. $this->request = $param['u'];
  22. $html = $this->file_get_html('http://instagram.com/'.urlencode($this->request)) or $this->returnError('Could not request Instagram.', 404);
  23. }
  24. else {
  25. $this->returnError('You must specify a Instagram username (?u=...).', 400);
  26. }
  27. $innertext = null;
  28. foreach($html->find('script') as $script)
  29. {
  30. if ('' === $script->innertext) {
  31. continue;
  32. }
  33. $pos = strpos(trim($script->innertext), 'window._sharedData');
  34. if (0 !== $pos)
  35. {
  36. continue;
  37. }
  38. $innertext = $script->innertext;
  39. break;
  40. }
  41. $json = trim(substr($innertext, $pos+18), ' =;');
  42. $data = json_decode($json);
  43. $userMedia = $data->entry_data->ProfilePage[0]->user->media->nodes;
  44. foreach($userMedia as $media)
  45. {
  46. $item = new \Item();
  47. $item->uri = "https://instagram.com/p/".$media->code."/";
  48. $item->content = '<img src="' . htmlentities($media->display_src) . '" />';
  49. if (isset($media->caption))
  50. {
  51. $item->title = $media->caption;
  52. } else {
  53. $item->title = basename($media->display_src);
  54. }
  55. $item->timestamp = $media->date;
  56. $this->items[] = $item;
  57. }
  58. }
  59. public function getName(){
  60. return (!empty($this->request) ? $this->request .' - ' : '') .'Instagram Bridge';
  61. }
  62. public function getURI(){
  63. return 'http://instagram.com/';
  64. }
  65. public function getCacheDuration(){
  66. return 3600;
  67. }
  68. }