2013-10-29 09:26:48 +01:00
|
|
|
<?php
|
2017-02-11 16:16:56 +01:00
|
|
|
class InstagramBridge extends BridgeAbstract {
|
|
|
|
|
|
|
|
const MAINTAINER = 'pauder';
|
|
|
|
const NAME = 'Instagram Bridge';
|
2018-10-24 16:33:49 +02:00
|
|
|
const URI = 'https://www.instagram.com/';
|
2017-02-11 16:16:56 +01:00
|
|
|
const DESCRIPTION = 'Returns the newest images';
|
|
|
|
|
2018-03-19 13:29:24 +01:00
|
|
|
const PARAMETERS = array(
|
2019-01-05 12:29:26 +01:00
|
|
|
'Username' => array(
|
2018-03-19 13:29:24 +01:00
|
|
|
'u' => array(
|
|
|
|
'name' => 'username',
|
|
|
|
'required' => true
|
|
|
|
)
|
2017-07-25 15:10:06 +02:00
|
|
|
),
|
2019-01-05 12:29:26 +01:00
|
|
|
'Hashtag' => array(
|
2018-03-19 13:29:24 +01:00
|
|
|
'h' => array(
|
|
|
|
'name' => 'hashtag',
|
|
|
|
'required' => true
|
2018-05-29 12:32:18 +02:00
|
|
|
)
|
|
|
|
),
|
2019-01-05 12:29:26 +01:00
|
|
|
'Location' => array(
|
2018-11-07 22:17:53 +01:00
|
|
|
'l' => array(
|
|
|
|
'name' => 'location',
|
|
|
|
'required' => true
|
|
|
|
)
|
|
|
|
),
|
2018-05-29 12:32:18 +02:00
|
|
|
'global' => array(
|
|
|
|
'media_type' => array(
|
2018-03-19 13:29:24 +01:00
|
|
|
'name' => 'Media type',
|
|
|
|
'type' => 'list',
|
|
|
|
'required' => false,
|
|
|
|
'values' => array(
|
2018-05-29 12:32:18 +02:00
|
|
|
'All' => 'all',
|
|
|
|
'Story' => 'story',
|
2018-03-19 13:29:24 +01:00
|
|
|
'Video' => 'video',
|
2018-05-29 12:32:18 +02:00
|
|
|
'Picture' => 'picture',
|
2018-03-19 13:29:24 +01:00
|
|
|
),
|
|
|
|
'defaultValue' => 'all'
|
|
|
|
)
|
2017-02-11 16:16:56 +01:00
|
|
|
)
|
2018-05-29 12:32:18 +02:00
|
|
|
|
2018-03-19 13:29:24 +01:00
|
|
|
);
|
2017-02-11 16:16:56 +01:00
|
|
|
|
2019-09-10 14:37:50 +02:00
|
|
|
const USER_QUERY_HASH = '58b6785bea111c67129decbe6a448951';
|
|
|
|
const TAG_QUERY_HASH = '174a5243287c5f3a7de741089750ab3b';
|
2019-09-11 19:08:12 +02:00
|
|
|
const STORY_QUERY_HASH = '865589822932d1b43dfe312121dd353a';
|
2019-09-10 14:37:50 +02:00
|
|
|
|
|
|
|
protected function getInstagramUserId($username) {
|
|
|
|
|
|
|
|
if(is_numeric($username)) return $username;
|
|
|
|
|
|
|
|
$cacheFac = new CacheFactory();
|
|
|
|
$cacheFac->setWorkingDir(PATH_LIB_CACHES);
|
|
|
|
$cache = $cacheFac->create(Configuration::getConfig('cache', 'type'));
|
|
|
|
$cache->setScope(get_called_class());
|
|
|
|
$cache->setKey([$username]);
|
|
|
|
$key = $cache->loadData();
|
|
|
|
|
2019-09-11 19:08:12 +02:00
|
|
|
if($key == null || true) {
|
2019-09-10 14:37:50 +02:00
|
|
|
$data = getContents(self::URI . 'web/search/topsearch/?query=' . $username);
|
2019-09-11 19:08:12 +02:00
|
|
|
|
|
|
|
foreach(json_decode($data)->users as $user) {
|
|
|
|
if($user->user->username === $username) {
|
|
|
|
$key = $user->user->pk;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if($key == null) {
|
|
|
|
returnServerError('Unable to find username in search result.');
|
|
|
|
}
|
2019-09-10 14:37:50 +02:00
|
|
|
$cache->saveData($key);
|
|
|
|
}
|
|
|
|
return $key;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
public function collectData(){
|
|
|
|
|
2018-11-07 22:17:53 +01:00
|
|
|
if(is_null($this->getInput('u')) && $this->getInput('media_type') == 'story') {
|
|
|
|
returnClientError('Stories are not supported for hashtags nor locations!');
|
2018-05-29 12:32:18 +02:00
|
|
|
}
|
|
|
|
|
2018-05-05 14:00:59 +02:00
|
|
|
$data = $this->getInstagramJSON($this->getURI());
|
2017-02-11 16:16:56 +01:00
|
|
|
|
2018-03-19 13:29:24 +01:00
|
|
|
if(!is_null($this->getInput('u'))) {
|
2019-09-10 14:37:50 +02:00
|
|
|
$userMedia = $data->data->user->edge_owner_to_timeline_media->edges;
|
2018-11-07 22:17:53 +01:00
|
|
|
} elseif(!is_null($this->getInput('h'))) {
|
2019-09-10 14:37:50 +02:00
|
|
|
$userMedia = $data->data->hashtag->edge_hashtag_to_media->edges;
|
2018-11-07 22:17:53 +01:00
|
|
|
} elseif(!is_null($this->getInput('l'))) {
|
|
|
|
$userMedia = $data->entry_data->LocationsPage[0]->graphql->location->edge_location_to_media->edges;
|
2018-03-19 13:29:24 +01:00
|
|
|
}
|
2017-02-11 16:16:56 +01:00
|
|
|
|
2017-07-29 19:28:00 +02:00
|
|
|
foreach($userMedia as $media) {
|
2018-03-19 13:17:42 +01:00
|
|
|
$media = $media->node;
|
2018-05-05 14:00:59 +02:00
|
|
|
|
|
|
|
if(!is_null($this->getInput('u'))) {
|
2018-05-29 12:32:18 +02:00
|
|
|
switch($this->getInput('media_type')) {
|
2018-05-05 14:00:59 +02: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 12:32:18 +02:00
|
|
|
if($this->getInput('media_type') == 'video' && !$media->is_video) continue;
|
2017-07-25 15:10:06 +02:00
|
|
|
}
|
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
$item = array();
|
2018-03-19 13:17:42 +01:00
|
|
|
$item['uri'] = self::URI . 'p/' . $media->shortcode . '/';
|
2018-05-05 14:00:59 +02:00
|
|
|
|
2019-01-06 18:14:23 +01:00
|
|
|
if (isset($media->owner->username)) {
|
|
|
|
$item['author'] = $media->owner->username;
|
|
|
|
}
|
|
|
|
|
2018-03-19 13:17:42 +01:00
|
|
|
if (isset($media->edge_media_to_caption->edges[0]->node->text)) {
|
2018-12-12 16:44:37 +01:00
|
|
|
$textContent = $media->edge_media_to_caption->edges[0]->node->text;
|
2017-02-11 16:16:56 +01:00
|
|
|
} else {
|
2019-04-20 22:15:30 +02:00
|
|
|
$textContent = '(no text)';
|
2018-12-12 16:44:37 +01:00
|
|
|
}
|
|
|
|
|
2018-12-26 20:32:44 +01:00
|
|
|
$item['title'] = ($media->is_video ? '▶ ' : '') . trim($textContent);
|
2018-12-12 16:44:37 +01:00
|
|
|
$titleLinePos = strpos(wordwrap($item['title'], 120), "\n");
|
|
|
|
if ($titleLinePos != false) {
|
|
|
|
$item['title'] = substr($item['title'], 0, $titleLinePos) . '...';
|
2017-02-11 16:16:56 +01:00
|
|
|
}
|
2018-05-05 14:00:59 +02:00
|
|
|
|
|
|
|
if(!is_null($this->getInput('u')) && $media->__typename == 'GraphSidecar') {
|
2019-09-11 19:08:12 +02:00
|
|
|
|
2018-05-05 14:00:59 +02:00
|
|
|
$data = $this->getInstagramStory($item['uri']);
|
|
|
|
$item['content'] = $data[0];
|
|
|
|
$item['enclosures'] = $data[1];
|
|
|
|
} else {
|
2019-04-20 22:15:30 +02:00
|
|
|
$mediaURI = self::URI . 'p/' . $media->shortcode . '/media?size=l';
|
2018-12-26 20:32:44 +01:00
|
|
|
$item['content'] = '<a href="' . htmlentities($item['uri']) . '" target="_blank">';
|
2019-04-20 22:15:30 +02:00
|
|
|
$item['content'] .= '<img src="' . htmlentities($mediaURI) . '" alt="' . $item['title'] . '" />';
|
2018-12-26 20:32:44 +01:00
|
|
|
$item['content'] .= '</a><br><br>' . nl2br(htmlentities($textContent));
|
2019-04-20 22:15:30 +02:00
|
|
|
$item['enclosures'] = array($mediaURI);
|
2018-05-05 14:00:59 +02:00
|
|
|
}
|
|
|
|
|
2018-03-19 13:17:42 +01:00
|
|
|
$item['timestamp'] = $media->taken_at_timestamp;
|
2018-05-05 14:00:59 +02:00
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
$this->items[] = $item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 14:00:59 +02:00
|
|
|
protected function getInstagramStory($uri) {
|
|
|
|
|
2019-09-11 19:08:12 +02:00
|
|
|
$shortcode = explode('/', $uri)[4];
|
|
|
|
$data = getContents(self::URI .
|
|
|
|
'graphql/query/?query_hash=' .
|
|
|
|
self::STORY_QUERY_HASH .
|
|
|
|
'&variables={"shortcode"%3A"' .
|
|
|
|
$shortcode .
|
|
|
|
'"}');
|
|
|
|
|
|
|
|
$mediaInfo = json_decode($data)->data->shortcode_media;
|
2018-05-05 14:00:59 +02:00
|
|
|
|
|
|
|
//Process the first element, that isn't in the node graph
|
2018-10-18 16:45:03 +02:00
|
|
|
if (count($mediaInfo->edge_media_to_caption->edges) > 0) {
|
|
|
|
$caption = $mediaInfo->edge_media_to_caption->edges[0]->node->text;
|
|
|
|
} else {
|
|
|
|
$caption = '';
|
|
|
|
}
|
2018-05-05 14:00:59 +02:00
|
|
|
|
|
|
|
$enclosures = [$mediaInfo->display_url];
|
2018-11-05 12:55:58 +01:00
|
|
|
$content = '<img src="' . htmlentities($mediaInfo->display_url) . '" alt="' . $caption . '" />';
|
2018-05-05 14:00:59 +02:00
|
|
|
|
|
|
|
foreach($mediaInfo->edge_sidecar_to_children->edges as $media) {
|
2018-11-03 12:12:37 +01:00
|
|
|
$display_url = $media->node->display_url;
|
|
|
|
if(!in_array($display_url, $enclosures)) { // add only if not added yet
|
2018-11-05 12:55:58 +01:00
|
|
|
$content .= '<img src="' . htmlentities($display_url) . '" alt="' . $caption . '" />';
|
2018-11-03 12:12:37 +01:00
|
|
|
$enclosures[] = $display_url;
|
|
|
|
}
|
2018-05-05 14:00:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return [$content, $enclosures];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getInstagramJSON($uri) {
|
|
|
|
|
2019-09-10 14:37:50 +02:00
|
|
|
if(!is_null($this->getInput('u'))) {
|
|
|
|
|
|
|
|
$userId = $this->getInstagramUserId($this->getInput('u'));
|
|
|
|
|
|
|
|
$data = getContents(self::URI .
|
|
|
|
'graphql/query/?query_hash=' .
|
|
|
|
self::USER_QUERY_HASH .
|
|
|
|
'&variables={"id"%3A"' .
|
|
|
|
$userId .
|
|
|
|
'"%2C"first"%3A10}');
|
|
|
|
return json_decode($data);
|
2018-05-05 14:00:59 +02:00
|
|
|
|
2019-09-10 14:37:50 +02:00
|
|
|
} elseif(!is_null($this->getInput('h'))) {
|
|
|
|
$data = getContents(self::URI .
|
|
|
|
'graphql/query/?query_hash=' .
|
|
|
|
self::TAG_QUERY_HASH .
|
|
|
|
'&variables={"tag_name"%3A"' .
|
|
|
|
$this->getInput('h') .
|
|
|
|
'"%2C"first"%3A10}');
|
|
|
|
return json_decode($data);
|
|
|
|
|
|
|
|
} else {
|
2018-05-05 14:00:59 +02:00
|
|
|
|
2019-09-10 14:37:50 +02:00
|
|
|
$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]);
|
|
|
|
|
|
|
|
}
|
2018-05-05 14:00:59 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
public function getName(){
|
2017-07-29 19:28:00 +02:00
|
|
|
if(!is_null($this->getInput('u'))) {
|
2017-02-14 22:20:55 +01:00
|
|
|
return $this->getInput('u') . ' - Instagram Bridge';
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::getName();
|
2017-02-11 16:16:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getURI(){
|
2017-07-29 19:28:00 +02:00
|
|
|
if(!is_null($this->getInput('u'))) {
|
2018-10-24 16:33:49 +02:00
|
|
|
return self::URI . urlencode($this->getInput('u')) . '/';
|
2018-03-19 13:29:24 +01:00
|
|
|
} elseif(!is_null($this->getInput('h'))) {
|
|
|
|
return self::URI . 'explore/tags/' . urlencode($this->getInput('h'));
|
2018-11-07 22:17:53 +01:00
|
|
|
} elseif(!is_null($this->getInput('l'))) {
|
|
|
|
return self::URI . 'explore/locations/' . urlencode($this->getInput('l'));
|
2017-02-14 22:36:33 +01:00
|
|
|
}
|
|
|
|
return parent::getURI();
|
2017-02-11 16:16:56 +01:00
|
|
|
}
|
2013-10-29 09:26:48 +01:00
|
|
|
}
|