-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
Renamed parameters as stories are only available in user mode. Use a regex instead of HTML parsing to extract the JSON, as it is way faster.
- Loading branch information
There are no files selected for viewing
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
|
@@ -12,14 +12,15 @@ class InstagramBridge extends BridgeAbstract { | ||
'name' => 'username', | 'name' => 'username', | ||
'required' => true | 'required' => true | ||
), | ), | ||
'media_type' => array( | 'media_type_u' => array( | ||
'name' => 'Media type', | 'name' => 'Media type', | ||
'type' => 'list', | 'type' => 'list', | ||
'required' => false, | 'required' => false, | ||
'values' => array( | 'values' => array( | ||
'Both' => 'all', | 'All' => 'all', | ||
'Story' => 'story', | |||
'Video' => 'video', | 'Video' => 'video', | ||
'Picture' => 'picture' | 'Picture' => 'picture', | ||
), | ), | ||
'defaultValue' => 'all' | 'defaultValue' => 'all' | ||
) | ) | ||
|
@@ -29,7 +30,7 @@ class InstagramBridge extends BridgeAbstract { | ||
'name' => 'hashtag', | 'name' => 'hashtag', | ||
'required' => true | 'required' => true | ||
), | ), | ||
'media_type' => array( | 'media_type_h' => array( | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
teromene
Author
Member
|
|||
'name' => 'Media type', | 'name' => 'Media type', | ||
'type' => 'list', | 'type' => 'list', | ||
'required' => false, | 'required' => false, | ||
|
@@ -44,27 +45,8 @@ class InstagramBridge extends BridgeAbstract { | ||
); | ); | ||
|
|
||
public function collectData(){ | public function collectData(){ | ||
$html = getSimpleHTMLDOM($this->getURI()) | |||
or returnServerError('Could not request Instagram.'); | |||
|
|||
$innertext = null; | |||
|
|||
foreach($html->find('script') as $script) { | |||
if('' === $script->innertext) { | |||
continue; | |||
} | |||
|
|||
$pos = strpos(trim($script->innertext), 'window._sharedData'); | |||
if(0 !== $pos) { | |||
continue; | |||
} | |||
|
|||
$innertext = $script->innertext; | |||
break; | |||
} | |||
|
|
||
$json = trim(substr($innertext, $pos + 18), ' =;'); | $data = $this->getInstagramJSON($this->getURI()); | ||
$data = json_decode($json); | |||
|
|
||
if(!is_null($this->getInput('u'))) { | if(!is_null($this->getInput('u'))) { | ||
$userMedia = $data->entry_data->ProfilePage[0]->graphql->user->edge_owner_to_timeline_media->edges; | $userMedia = $data->entry_data->ProfilePage[0]->graphql->user->edge_owner_to_timeline_media->edges; | ||
|
@@ -74,32 +56,83 @@ public function collectData(){ | ||
|
|
||
foreach($userMedia as $media) { | foreach($userMedia as $media) { | ||
$media = $media->node; | $media = $media->node; | ||
// Check media type |
|
||
switch($this->getInput('media_type')) { | if(!is_null($this->getInput('u'))) { | ||
case 'all': break; | switch($this->getInput('media_type_u')) { | ||
case 'video': | case 'all': break; | ||
if($media->is_video === false) continue 2; | case 'video': | ||
break; | if($media->__typename != 'GraphVideo') continue 2; | ||
case 'picture': | break; | ||
if($media->is_video === true) continue 2; | case 'picture': | ||
break; | if($media->__typename != 'GraphImage') continue 2; | ||
default: break; | break; | ||
case 'story': | |||
if($media->__typename != 'GraphSidecar') continue 2; | |||
break; | |||
default: break; | |||
} | |||
} else { | |||
if($this->getInput('media_type_h') == 'video' && !$media->is_video) continue; | |||
} | } | ||
|
|
||
$item = array(); | $item = array(); | ||
$item['uri'] = self::URI . 'p/' . $media->shortcode . '/'; | $item['uri'] = self::URI . 'p/' . $media->shortcode . '/'; | ||
$item['content'] = '<img src="' . htmlentities($media->display_url) . '" />'; |
|
||
if (isset($media->edge_media_to_caption->edges[0]->node->text)) { | if (isset($media->edge_media_to_caption->edges[0]->node->text)) { | ||
$item['title'] = $media->edge_media_to_caption->edges[0]->node->text; | $item['title'] = $media->edge_media_to_caption->edges[0]->node->text; | ||
} else { | } else { | ||
$item['title'] = basename($media->display_url); | $item['title'] = basename($media->display_url); | ||
} | } | ||
|
|||
if(!is_null($this->getInput('u')) && $media->__typename == 'GraphSidecar') { | |||
$data = $this->getInstagramStory($item['uri']); | |||
$item['content'] = $data[0]; | |||
$item['enclosures'] = $data[1]; | |||
} else { | |||
$item['content'] = '<img src="' . htmlentities($media->display_url) . '" alt="'. $item["title"] . '" />'; | |||
$item['enclosures'] = array($media->display_url); | |||
} | |||
|
|||
$item['timestamp'] = $media->taken_at_timestamp; | $item['timestamp'] = $media->taken_at_timestamp; | ||
$item['enclosures'] = array($media->display_url); |
|
||
$this->items[] = $item; | $this->items[] = $item; | ||
} | } | ||
} | } | ||
|
|
||
protected function getInstagramStory($uri) { | |||
|
|||
$data = $this->getInstagramJSON($uri); | |||
$mediaInfo = $data->entry_data->PostPage[0]->graphql->shortcode_media; | |||
|
|||
//Process the first element, that isn't in the node graph | |||
$caption = $mediaInfo->edge_media_to_caption->edges[0]->node->text; | |||
|
|||
$enclosures = [$mediaInfo->display_url]; | |||
$content = '<img src="' . htmlentities($mediaInfo->display_url) . '" alt="'. $caption . '" />'; | |||
|
|||
foreach($mediaInfo->edge_sidecar_to_children->edges as $media) { | |||
|
|||
$content .= '<img src="' . htmlentities($media->node->display_url) . '" alt="'. $caption . '" />'; | |||
$enclosures[] = $media->node->display_url; | |||
|
|||
} | |||
|
|||
return [$content, $enclosures]; | |||
|
|||
} | |||
|
|||
protected function getInstagramJSON($uri) { | |||
|
|||
$html = getContents($uri) | |||
or returnServerError('Could not request Instagram.'); | |||
$scriptRegex = '/window\._sharedData = (.*);<\/script>/'; | |||
|
|||
preg_match($scriptRegex, $html, $matches, PREG_OFFSET_CAPTURE, 0); | |||
|
|||
return json_decode($matches[1][0]); | |||
|
|||
} | |||
|
|||
public function getName(){ | public function getName(){ | ||
if(!is_null($this->getInput('u'))) { | if(!is_null($this->getInput('u'))) { | ||
return $this->getInput('u') . ' - Instagram Bridge'; | return $this->getInput('u') . ' - Instagram Bridge'; | ||
|
Why change the parameter name(s)? This breaks existing users.