2013-10-29 12:26:48 +04:00
|
|
|
<?php
|
2017-02-11 18:16:56 +03:00
|
|
|
class InstagramBridge extends BridgeAbstract {
|
|
|
|
|
|
|
|
const MAINTAINER = 'pauder';
|
|
|
|
const NAME = 'Instagram Bridge';
|
2017-03-21 23:27:12 +03:00
|
|
|
const URI = 'https://instagram.com/';
|
2017-02-11 18:16:56 +03:00
|
|
|
const DESCRIPTION = 'Returns the newest images';
|
|
|
|
|
2018-03-19 15:29:24 +03:00
|
|
|
const PARAMETERS = array(
|
|
|
|
array(
|
|
|
|
'u' => array(
|
|
|
|
'name' => 'username',
|
|
|
|
'required' => true
|
|
|
|
)
|
2017-07-25 16:10:06 +03:00
|
|
|
),
|
2018-03-19 15:29:24 +03:00
|
|
|
array(
|
|
|
|
'h' => array(
|
|
|
|
'name' => 'hashtag',
|
|
|
|
'required' => true
|
2018-05-29 13:32:18 +03:00
|
|
|
)
|
|
|
|
),
|
|
|
|
'global' => array(
|
|
|
|
'media_type' => array(
|
2018-03-19 15:29:24 +03:00
|
|
|
'name' => 'Media type',
|
|
|
|
'type' => 'list',
|
|
|
|
'required' => false,
|
|
|
|
'values' => array(
|
2018-05-29 13:32:18 +03:00
|
|
|
'All' => 'all',
|
|
|
|
'Story' => 'story',
|
2018-03-19 15:29:24 +03:00
|
|
|
'Video' => 'video',
|
2018-05-29 13:32:18 +03:00
|
|
|
'Picture' => 'picture',
|
2018-03-19 15:29:24 +03:00
|
|
|
),
|
|
|
|
'defaultValue' => 'all'
|
|
|
|
)
|
2017-02-11 18:16:56 +03:00
|
|
|
)
|
2018-05-29 13:32:18 +03:00
|
|
|
|
2018-03-19 15:29:24 +03:00
|
|
|
);
|
2017-02-11 18:16:56 +03:00
|
|
|
|
|
|
|
public function collectData(){
|
|
|
|
|
2018-05-29 13:32:18 +03:00
|
|
|
if(!is_null($this->getInput('h')) && $this->getInput('media_type') == 'story') {
|
|
|
|
returnClientError('Stories are not supported for hashtags!');
|
|
|
|
}
|
|
|
|
|
2018-05-05 15:00:59 +03:00
|
|
|
$data = $this->getInstagramJSON($this->getURI());
|
2017-02-11 18:16:56 +03:00
|
|
|
|
2018-03-19 15:29:24 +03:00
|
|
|
if(!is_null($this->getInput('u'))) {
|
|
|
|
$userMedia = $data->entry_data->ProfilePage[0]->graphql->user->edge_owner_to_timeline_media->edges;
|
|
|
|
} else {
|
|
|
|
$userMedia = $data->entry_data->TagPage[0]->graphql->hashtag->edge_hashtag_to_media->edges;
|
|
|
|
}
|
2017-02-11 18:16:56 +03:00
|
|
|
|
2017-07-29 20:28:00 +03:00
|
|
|
foreach($userMedia as $media) {
|
2018-03-19 15:17:42 +03:00
|
|
|
$media = $media->node;
|
2018-05-05 15:00:59 +03:00
|
|
|
|
|
|
|
if(!is_null($this->getInput('u'))) {
|
2018-05-29 13:32:18 +03:00
|
|
|
switch($this->getInput('media_type')) {
|
2018-05-05 15:00:59 +03:00
|
|
|
case 'all': break;
|
|
|
|
case 'video':
|
|
|
|
if($media->__typename != 'GraphVideo') continue 2;
|
|
|
|
break;
|
|
|
|
case 'picture':
|
|
|
|
if($media->__typename != 'GraphImage') continue 2;
|
|
|
|
break;
|
|
|
|
case 'story':
|
|
|
|
if($media->__typename != 'GraphSidecar') continue 2;
|
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
} else {
|
2018-05-29 13:32:18 +03:00
|
|
|
if($this->getInput('media_type') == 'video' && !$media->is_video) continue;
|
2017-07-25 16:10:06 +03:00
|
|
|
}
|
|
|
|
|
2017-02-11 18:16:56 +03:00
|
|
|
$item = array();
|
2018-03-19 15:17:42 +03:00
|
|
|
$item['uri'] = self::URI . 'p/' . $media->shortcode . '/';
|
2018-05-05 15:00:59 +03:00
|
|
|
|
2018-03-19 15:17:42 +03:00
|
|
|
if (isset($media->edge_media_to_caption->edges[0]->node->text)) {
|
|
|
|
$item['title'] = $media->edge_media_to_caption->edges[0]->node->text;
|
2017-02-11 18:16:56 +03:00
|
|
|
} else {
|
2018-03-19 15:17:42 +03:00
|
|
|
$item['title'] = basename($media->display_url);
|
2017-02-11 18:16:56 +03:00
|
|
|
}
|
2018-05-05 15:00:59 +03:00
|
|
|
|
|
|
|
if(!is_null($this->getInput('u')) && $media->__typename == 'GraphSidecar') {
|
|
|
|
$data = $this->getInstagramStory($item['uri']);
|
|
|
|
$item['content'] = $data[0];
|
|
|
|
$item['enclosures'] = $data[1];
|
|
|
|
} else {
|
2018-06-30 00:55:33 +03:00
|
|
|
$item['content'] = '<img src="' . htmlentities($media->display_url) . '" alt="'. $item['title'] . '" />';
|
2018-05-05 15:00:59 +03:00
|
|
|
$item['enclosures'] = array($media->display_url);
|
|
|
|
}
|
|
|
|
|
2018-03-19 15:17:42 +03:00
|
|
|
$item['timestamp'] = $media->taken_at_timestamp;
|
2018-05-05 15:00:59 +03:00
|
|
|
|
2017-02-11 18:16:56 +03:00
|
|
|
$this->items[] = $item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 15:00:59 +03:00
|
|
|
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]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-11 18:16:56 +03:00
|
|
|
public function getName(){
|
2017-07-29 20:28:00 +03:00
|
|
|
if(!is_null($this->getInput('u'))) {
|
2017-02-15 00:20:55 +03:00
|
|
|
return $this->getInput('u') . ' - Instagram Bridge';
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::getName();
|
2017-02-11 18:16:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getURI(){
|
2017-07-29 20:28:00 +03:00
|
|
|
if(!is_null($this->getInput('u'))) {
|
2017-02-15 00:36:33 +03:00
|
|
|
return self::URI . urlencode($this->getInput('u'));
|
2018-03-19 15:29:24 +03:00
|
|
|
} elseif(!is_null($this->getInput('h'))) {
|
|
|
|
return self::URI . 'explore/tags/' . urlencode($this->getInput('h'));
|
2017-02-15 00:36:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return parent::getURI();
|
2017-02-11 18:16:56 +03:00
|
|
|
}
|
2013-10-29 12:26:48 +04:00
|
|
|
}
|