[InstagramBridge] Fix instagram GraphSidecar output and Video embedding (#1361)

* [InstagramBridge] Fix GraphSidecar output

Fix following issues which related to output of the GraphSidecar type posts.
- The GraphSidecar post's media wasn't outputted except for first picture when searching by hashtag or location
- Video didn't embedded
NOTE:
The function getInstagramStory() which was called when the post type is GraphSidecar didn't seem to work just as one intended.
Because the web request called in that function is just to get the media of single post, NOT to get the media of Story.
But I don't have any idea to solve #694, so it seems be better to rename these function and member variable properly.
This commit is contained in:
Shuto Yano 2019-12-01 19:25:20 +09:00 committed by LogMANOriginal
parent 4b3c3c58d2
commit 48b0164676

View file

@ -32,14 +32,14 @@ class InstagramBridge extends BridgeAbstract {
'required' => false, 'required' => false,
'values' => array( 'values' => array(
'All' => 'all', 'All' => 'all',
'Story' => 'story',
'Video' => 'video', 'Video' => 'video',
'Picture' => 'picture', 'Picture' => 'picture',
'Multiple' => 'multiple',
), ),
'defaultValue' => 'all' 'defaultValue' => 'all'
), ),
'direct_links' => array( 'direct_links' => array(
'name' => 'Use direct image links', 'name' => 'Use direct media links',
'type' => 'checkbox', 'type' => 'checkbox',
) )
) )
@ -48,7 +48,7 @@ class InstagramBridge extends BridgeAbstract {
const USER_QUERY_HASH = '58b6785bea111c67129decbe6a448951'; const USER_QUERY_HASH = '58b6785bea111c67129decbe6a448951';
const TAG_QUERY_HASH = '174a5243287c5f3a7de741089750ab3b'; const TAG_QUERY_HASH = '174a5243287c5f3a7de741089750ab3b';
const STORY_QUERY_HASH = '865589822932d1b43dfe312121dd353a'; const SHORTCODE_QUERY_HASH = '865589822932d1b43dfe312121dd353a';
protected function getInstagramUserId($username) { protected function getInstagramUserId($username) {
@ -79,11 +79,6 @@ class InstagramBridge extends BridgeAbstract {
} }
public function collectData(){ public function collectData(){
if(is_null($this->getInput('u')) && $this->getInput('media_type') == 'story') {
returnClientError('Stories are not supported for hashtags nor locations!');
}
$directLink = !is_null($this->getInput('direct_links')) && $this->getInput('direct_links'); $directLink = !is_null($this->getInput('direct_links')) && $this->getInput('direct_links');
$data = $this->getInstagramJSON($this->getURI()); $data = $this->getInstagramJSON($this->getURI());
@ -99,23 +94,19 @@ class InstagramBridge extends BridgeAbstract {
foreach($userMedia as $media) { foreach($userMedia as $media) {
$media = $media->node; $media = $media->node;
if(!is_null($this->getInput('u'))) {
switch($this->getInput('media_type')) { switch($this->getInput('media_type')) {
case 'all': break; case 'all': break;
case 'video': case 'video':
if($media->__typename != 'GraphVideo') continue 2; if($media->__typename != 'GraphVideo' || !$media->is_video) continue 2;
break; break;
case 'picture': case 'picture':
if($media->__typename != 'GraphImage') continue 2; if($media->__typename != 'GraphImage') continue 2;
break; break;
case 'story': case 'multiple':
if($media->__typename != 'GraphSidecar') continue 2; if($media->__typename != 'GraphSidecar') continue 2;
break; break;
default: break; default: break;
} }
} else {
if($this->getInput('media_type') == 'video' && !$media->is_video) continue;
}
$item = array(); $item = array();
$item['uri'] = self::URI . 'p/' . $media->shortcode . '/'; $item['uri'] = self::URI . 'p/' . $media->shortcode . '/';
@ -124,24 +115,21 @@ class InstagramBridge extends BridgeAbstract {
$item['author'] = $media->owner->username; $item['author'] = $media->owner->username;
} }
if (isset($media->edge_media_to_caption->edges[0]->node->text)) { $textContent = $this->getTextContent($media);
$textContent = $media->edge_media_to_caption->edges[0]->node->text;
} else {
$textContent = '(no text)';
}
$item['title'] = ($media->is_video ? '▶ ' : '') . trim($textContent); $item['title'] = ($media->is_video ? '▶ ' : '') . $textContent;
$titleLinePos = strpos(wordwrap($item['title'], 120), "\n"); $titleLinePos = strpos(wordwrap($item['title'], 120), "\n");
if ($titleLinePos != false) { if ($titleLinePos != false) {
$item['title'] = substr($item['title'], 0, $titleLinePos) . '...'; $item['title'] = substr($item['title'], 0, $titleLinePos) . '...';
} }
if(!is_null($this->getInput('u')) && $media->__typename == 'GraphSidecar') { switch($media->__typename) {
case 'GraphSidecar':
$data = $this->getInstagramStory($item['uri']); $data = $this->getInstagramSidecarData($item['uri'], $item['title']);
$item['content'] = $data[0]; $item['content'] = $data[0];
$item['enclosures'] = $data[1]; $item['enclosures'] = $data[1];
} else { break;
case 'GraphImage':
if($directLink) { if($directLink) {
$mediaURI = $media->display_url; $mediaURI = $media->display_url;
} else { } else {
@ -151,46 +139,81 @@ class InstagramBridge extends BridgeAbstract {
$item['content'] .= '<img src="' . htmlentities($mediaURI) . '" alt="' . $item['title'] . '" />'; $item['content'] .= '<img src="' . htmlentities($mediaURI) . '" alt="' . $item['title'] . '" />';
$item['content'] .= '</a><br><br>' . nl2br(htmlentities($textContent)); $item['content'] .= '</a><br><br>' . nl2br(htmlentities($textContent));
$item['enclosures'] = array($mediaURI); $item['enclosures'] = array($mediaURI);
break;
case 'GraphVideo':
$data = $this->getInstagramVideoData($item['uri']);
$item['content'] = $data[0];
if($directLink) {
$item['enclosures'] = $data[1];
} else {
$item['enclosures'] = array(self::URI . 'p/' . $media->shortcode . '/media?size=l');
}
break;
default: break;
} }
$item['timestamp'] = $media->taken_at_timestamp; $item['timestamp'] = $media->taken_at_timestamp;
$this->items[] = $item; $this->items[] = $item;
} }
} }
protected function getInstagramStory($uri) { // returns Sidecar(a post which has multiple media)'s contents and enclosures
protected function getInstagramSidecarData($uri, $postTitle) {
$mediaInfo = $this->getSinglePostData($uri);
$textContent = $this->getTextContent($mediaInfo);
$enclosures = array();
$content = '';
foreach($mediaInfo->edge_sidecar_to_children->edges as $singleMedia) {
$singleMedia = $singleMedia->node;
if($singleMedia->is_video) {
if(in_array($singleMedia->video_url, $enclosures)) continue; // check if not added yet
$content .= '<video controls><source src="' . $singleMedia->video_url . '" type="video/mp4"></video><br>';
array_push($enclosures, $singleMedia->video_url);
} else {
if(in_array($singleMedia->display_url, $enclosures)) continue; // check if not added yet
$content .= '<a href="' . $singleMedia->display_url . '" target="_blank">';
$content .= '<img src="' . $singleMedia->display_url . '" alt="' . $postTitle . '" />';
$content .= '</a><br>';
array_push($enclosures, $singleMedia->display_url);
}
}
$content .= '<br>' . nl2br(htmlentities($textContent));
return array($content, $enclosures);
}
// returns Video post's contents and enclosures
protected function getInstagramVideoData($uri) {
$mediaInfo = $this->getSinglePostData($uri);
$textContent = $this->getTextContent($mediaInfo);
$content = '<video controls><source src="' . $mediaInfo->video_url . '" type="video/mp4"></video><br>';
$content .= '<br>' . nl2br(htmlentities($textContent));
return array($content, array($mediaInfo->video_url));
}
protected function getTextContent($media) {
$textContent = '(no text)';
//Process the first element, that isn't in the node graph
if (count($media->edge_media_to_caption->edges) > 0) {
$textContent = trim($media->edge_media_to_caption->edges[0]->node->text);
}
return $textContent;
}
protected function getSinglePostData($uri) {
$shortcode = explode('/', $uri)[4]; $shortcode = explode('/', $uri)[4];
$data = getContents(self::URI . $data = getContents(self::URI .
'graphql/query/?query_hash=' . 'graphql/query/?query_hash=' .
self::STORY_QUERY_HASH . self::SHORTCODE_QUERY_HASH .
'&variables={"shortcode"%3A"' . '&variables={"shortcode"%3A"' .
$shortcode . $shortcode .
'"}'); '"}');
$mediaInfo = json_decode($data)->data->shortcode_media; return json_decode($data)->data->shortcode_media;
//Process the first element, that isn't in the node graph
if (count($mediaInfo->edge_media_to_caption->edges) > 0) {
$caption = $mediaInfo->edge_media_to_caption->edges[0]->node->text;
} else {
$caption = '';
}
$enclosures = array($mediaInfo->display_url);
$content = '<img src="' . htmlentities($mediaInfo->display_url) . '" alt="' . $caption . '" />';
foreach($mediaInfo->edge_sidecar_to_children->edges as $media) {
$display_url = $media->node->display_url;
if(!in_array($display_url, $enclosures)) { // add only if not added yet
$content .= '<img src="' . htmlentities($display_url) . '" alt="' . $caption . '" />';
$enclosures[] = $display_url;
}
}
return array($content, $enclosures);
} }
protected function getInstagramJSON($uri) { protected function getInstagramJSON($uri) {