2013-10-29 12:26:48 +04:00
|
|
|
<?php
|
2017-02-11 18:16:56 +03:00
|
|
|
class InstagramBridge extends BridgeAbstract {
|
|
|
|
|
2020-12-09 08:47:48 +03:00
|
|
|
// const MAINTAINER = 'pauder';
|
2017-02-11 18:16:56 +03:00
|
|
|
const NAME = 'Instagram Bridge';
|
2018-10-24 17:33:49 +03:00
|
|
|
const URI = 'https://www.instagram.com/';
|
2017-02-11 18:16:56 +03:00
|
|
|
const DESCRIPTION = 'Returns the newest images';
|
|
|
|
|
2022-01-25 22:31:10 +03:00
|
|
|
const CONFIGURATION = array(
|
|
|
|
'session_id' => array(
|
|
|
|
'required' => false,
|
|
|
|
),
|
|
|
|
'cache_timeout' => array(
|
|
|
|
'required' => false,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2018-03-19 15:29:24 +03:00
|
|
|
const PARAMETERS = array(
|
2019-01-05 14:29:26 +03:00
|
|
|
'Username' => array(
|
2018-03-19 15:29:24 +03:00
|
|
|
'u' => array(
|
|
|
|
'name' => 'username',
|
2022-03-24 13:59:34 +03:00
|
|
|
'exampleValue' => 'aesoprockwins',
|
2018-03-19 15:29:24 +03:00
|
|
|
'required' => true
|
|
|
|
)
|
2017-07-25 16:10:06 +03:00
|
|
|
),
|
2019-01-05 14:29:26 +03:00
|
|
|
'Hashtag' => array(
|
2018-03-19 15:29:24 +03:00
|
|
|
'h' => array(
|
|
|
|
'name' => 'hashtag',
|
2022-03-24 13:59:34 +03:00
|
|
|
'exampleValue' => 'beautifulday',
|
2018-03-19 15:29:24 +03:00
|
|
|
'required' => true
|
2018-05-29 13:32:18 +03:00
|
|
|
)
|
|
|
|
),
|
2019-01-05 14:29:26 +03:00
|
|
|
'Location' => array(
|
2018-11-08 00:17:53 +03:00
|
|
|
'l' => array(
|
|
|
|
'name' => 'location',
|
2022-03-24 13:59:34 +03:00
|
|
|
'exampleValue' => 'london',
|
2018-11-08 00:17:53 +03:00
|
|
|
'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',
|
2018-03-19 15:29:24 +03:00
|
|
|
'Video' => 'video',
|
2018-05-29 13:32:18 +03:00
|
|
|
'Picture' => 'picture',
|
2019-12-01 13:25:20 +03:00
|
|
|
'Multiple' => 'multiple',
|
2018-03-19 15:29:24 +03:00
|
|
|
),
|
|
|
|
'defaultValue' => 'all'
|
2019-10-03 23:14:21 +03:00
|
|
|
),
|
|
|
|
'direct_links' => array(
|
2019-12-01 13:25:20 +03:00
|
|
|
'name' => 'Use direct media links',
|
2019-10-03 23:14:21 +03:00
|
|
|
'type' => 'checkbox',
|
2018-03-19 15:29:24 +03:00
|
|
|
)
|
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
|
|
|
|
2022-03-23 06:09:59 +03:00
|
|
|
const TEST_DETECT_PARAMETERS = array(
|
|
|
|
'https://www.instagram.com/metaverse' => array('u' => 'metaverse'),
|
|
|
|
'https://instagram.com/metaverse' => array('u' => 'metaverse'),
|
|
|
|
'http://www.instagram.com/metaverse' => array('u' => 'metaverse'),
|
|
|
|
);
|
|
|
|
|
2019-09-10 15:37:50 +03:00
|
|
|
const USER_QUERY_HASH = '58b6785bea111c67129decbe6a448951';
|
2020-09-02 09:02:34 +03:00
|
|
|
const TAG_QUERY_HASH = '9b498c08113f1e09617a1703c22b2f32';
|
2019-12-01 13:25:20 +03:00
|
|
|
const SHORTCODE_QUERY_HASH = '865589822932d1b43dfe312121dd353a';
|
2019-09-10 15:37:50 +03:00
|
|
|
|
2022-01-25 22:31:10 +03:00
|
|
|
public function getCacheTimeout() {
|
|
|
|
$customTimeout = $this->getOption('cache_timeout');
|
|
|
|
return $customTimeout || parent::getCacheTimeout();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getContents($uri) {
|
|
|
|
$headers = array();
|
|
|
|
$sessionId = $this->getOption('session_id');
|
|
|
|
if ($sessionId) {
|
|
|
|
$headers[] = 'cookie: sessionid=' . $sessionId;
|
|
|
|
}
|
|
|
|
return getContents($uri, $headers);
|
|
|
|
}
|
|
|
|
|
2019-09-10 15:37:50 +03: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());
|
2019-11-01 20:06:38 +03:00
|
|
|
$cache->setKey(array($username));
|
2019-09-10 15:37:50 +03:00
|
|
|
$key = $cache->loadData();
|
|
|
|
|
2019-09-11 20:28:46 +03:00
|
|
|
if($key == null) {
|
2022-01-25 22:31:10 +03:00
|
|
|
$data = $this->getContents(self::URI . 'web/search/topsearch/?query=' . $username);
|
2019-09-11 20:08:12 +03:00
|
|
|
foreach(json_decode($data)->users as $user) {
|
2020-02-27 00:35:44 +03:00
|
|
|
if(strtolower($user->user->username) === strtolower($username)) {
|
2019-09-11 20:08:12 +03:00
|
|
|
$key = $user->user->pk;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if($key == null) {
|
|
|
|
returnServerError('Unable to find username in search result.');
|
|
|
|
}
|
2019-09-10 15:37:50 +03:00
|
|
|
$cache->saveData($key);
|
|
|
|
}
|
|
|
|
return $key;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-11 18:16:56 +03:00
|
|
|
public function collectData(){
|
2019-10-03 23:14:21 +03:00
|
|
|
$directLink = !is_null($this->getInput('direct_links')) && $this->getInput('direct_links');
|
|
|
|
|
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'))) {
|
2019-09-10 15:37:50 +03:00
|
|
|
$userMedia = $data->data->user->edge_owner_to_timeline_media->edges;
|
2018-11-08 00:17:53 +03:00
|
|
|
} elseif(!is_null($this->getInput('h'))) {
|
2019-09-10 15:37:50 +03:00
|
|
|
$userMedia = $data->data->hashtag->edge_hashtag_to_media->edges;
|
2018-11-08 00:17:53 +03:00
|
|
|
} elseif(!is_null($this->getInput('l'))) {
|
|
|
|
$userMedia = $data->entry_data->LocationsPage[0]->graphql->location->edge_location_to_media->edges;
|
2018-03-19 15:29:24 +03:00
|
|
|
}
|
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
|
|
|
|
2019-12-01 13:25:20 +03:00
|
|
|
switch($this->getInput('media_type')) {
|
|
|
|
case 'all': break;
|
|
|
|
case 'video':
|
|
|
|
if($media->__typename != 'GraphVideo' || !$media->is_video) continue 2;
|
|
|
|
break;
|
|
|
|
case 'picture':
|
|
|
|
if($media->__typename != 'GraphImage') continue 2;
|
|
|
|
break;
|
|
|
|
case 'multiple':
|
|
|
|
if($media->__typename != 'GraphSidecar') continue 2;
|
|
|
|
break;
|
|
|
|
default: break;
|
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
|
|
|
|
2019-01-06 20:14:23 +03:00
|
|
|
if (isset($media->owner->username)) {
|
|
|
|
$item['author'] = $media->owner->username;
|
|
|
|
}
|
|
|
|
|
2019-12-01 13:25:20 +03:00
|
|
|
$textContent = $this->getTextContent($media);
|
2018-12-12 18:44:37 +03:00
|
|
|
|
2019-12-01 13:25:20 +03:00
|
|
|
$item['title'] = ($media->is_video ? '▶ ' : '') . $textContent;
|
2018-12-12 18:44:37 +03:00
|
|
|
$titleLinePos = strpos(wordwrap($item['title'], 120), "\n");
|
|
|
|
if ($titleLinePos != false) {
|
|
|
|
$item['title'] = substr($item['title'], 0, $titleLinePos) . '...';
|
2017-02-11 18:16:56 +03:00
|
|
|
}
|
2018-05-05 15:00:59 +03:00
|
|
|
|
2020-02-27 00:13:40 +03:00
|
|
|
if($directLink) {
|
|
|
|
$mediaURI = $media->display_url;
|
|
|
|
} else {
|
|
|
|
$mediaURI = self::URI . 'p/' . $media->shortcode . '/media?size=l';
|
|
|
|
}
|
|
|
|
|
2022-04-09 00:47:10 +03:00
|
|
|
$pattern = array('/\@([\w\.]+)/', '/#([\w\.]+)/');
|
|
|
|
$replace = array(
|
|
|
|
'<a href="https://www.instagram.com/$1">@$1</a>',
|
|
|
|
'<a href="https://www.instagram.com/explore/tags/$1">#$1</a>');
|
|
|
|
|
2019-12-01 13:25:20 +03:00
|
|
|
switch($media->__typename) {
|
|
|
|
case 'GraphSidecar':
|
2021-01-10 11:14:58 +03:00
|
|
|
$data = $this->getInstagramSidecarData($item['uri'], $item['title'], $media, $textContent);
|
2019-12-01 13:25:20 +03:00
|
|
|
$item['content'] = $data[0];
|
|
|
|
$item['enclosures'] = $data[1];
|
|
|
|
break;
|
|
|
|
case 'GraphImage':
|
|
|
|
$item['content'] = '<a href="' . htmlentities($item['uri']) . '" target="_blank">';
|
|
|
|
$item['content'] .= '<img src="' . htmlentities($mediaURI) . '" alt="' . $item['title'] . '" />';
|
2022-04-09 00:47:10 +03:00
|
|
|
$item['content'] .= '</a><br><br>' . nl2br(preg_replace($pattern, $replace, htmlentities($textContent)));
|
2019-12-01 13:25:20 +03:00
|
|
|
$item['enclosures'] = array($mediaURI);
|
|
|
|
break;
|
|
|
|
case 'GraphVideo':
|
2021-01-10 11:14:58 +03:00
|
|
|
$data = $this->getInstagramVideoData($item['uri'], $mediaURI, $media, $textContent);
|
2019-12-01 13:25:20 +03:00
|
|
|
$item['content'] = $data[0];
|
|
|
|
if($directLink) {
|
|
|
|
$item['enclosures'] = $data[1];
|
|
|
|
} else {
|
2020-02-27 00:13:40 +03:00
|
|
|
$item['enclosures'] = array($mediaURI);
|
2019-12-01 13:25:20 +03:00
|
|
|
}
|
2020-02-27 00:13:40 +03:00
|
|
|
$item['thumbnail'] = $mediaURI;
|
2019-12-01 13:25:20 +03:00
|
|
|
break;
|
|
|
|
default: break;
|
2018-05-05 15:00:59 +03:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-01 13:25:20 +03:00
|
|
|
// returns Sidecar(a post which has multiple media)'s contents and enclosures
|
2021-01-10 11:14:58 +03:00
|
|
|
protected function getInstagramSidecarData($uri, $postTitle, $mediaInfo, $textContent) {
|
2019-12-01 13:25:20 +03:00
|
|
|
$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);
|
|
|
|
}
|
2018-10-18 17:45:03 +03:00
|
|
|
}
|
2019-12-01 13:25:20 +03:00
|
|
|
$content .= '<br>' . nl2br(htmlentities($textContent));
|
2018-05-05 15:00:59 +03:00
|
|
|
|
2019-12-01 13:25:20 +03:00
|
|
|
return array($content, $enclosures);
|
|
|
|
}
|
2018-05-05 15:00:59 +03:00
|
|
|
|
2019-12-01 13:25:20 +03:00
|
|
|
// returns Video post's contents and enclosures
|
2021-01-10 11:14:58 +03:00
|
|
|
protected function getInstagramVideoData($uri, $mediaURI, $mediaInfo, $textContent) {
|
2020-02-27 00:13:40 +03:00
|
|
|
$content = '<video controls>';
|
|
|
|
$content .= '<source src="' . $mediaInfo->video_url . '" poster="' . $mediaURI . '" type="video/mp4">';
|
|
|
|
$content .= '<img src="' . $mediaURI . '" alt="">';
|
|
|
|
$content .= '</video><br>';
|
2019-12-01 13:25:20 +03:00
|
|
|
$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);
|
2018-05-05 15:00:59 +03:00
|
|
|
}
|
2019-12-01 13:25:20 +03:00
|
|
|
return $textContent;
|
|
|
|
}
|
2018-05-05 15:00:59 +03:00
|
|
|
|
|
|
|
protected function getInstagramJSON($uri) {
|
|
|
|
|
2019-09-10 15:37:50 +03:00
|
|
|
if(!is_null($this->getInput('u'))) {
|
|
|
|
|
|
|
|
$userId = $this->getInstagramUserId($this->getInput('u'));
|
2022-01-25 22:31:10 +03:00
|
|
|
$data = $this->getContents(self::URI .
|
2019-09-10 15:37:50 +03:00
|
|
|
'graphql/query/?query_hash=' .
|
|
|
|
self::USER_QUERY_HASH .
|
|
|
|
'&variables={"id"%3A"' .
|
|
|
|
$userId .
|
|
|
|
'"%2C"first"%3A10}');
|
|
|
|
return json_decode($data);
|
2018-05-05 15:00:59 +03:00
|
|
|
|
2019-09-10 15:37:50 +03:00
|
|
|
} elseif(!is_null($this->getInput('h'))) {
|
2022-01-25 22:31:10 +03:00
|
|
|
$data = $this->getContents(self::URI .
|
2019-09-10 15:37:50 +03:00
|
|
|
'graphql/query/?query_hash=' .
|
|
|
|
self::TAG_QUERY_HASH .
|
|
|
|
'&variables={"tag_name"%3A"' .
|
|
|
|
$this->getInput('h') .
|
|
|
|
'"%2C"first"%3A10}');
|
2022-01-25 22:31:10 +03:00
|
|
|
|
2019-09-10 15:37:50 +03:00
|
|
|
return json_decode($data);
|
|
|
|
|
|
|
|
} else {
|
2018-05-05 15:00:59 +03:00
|
|
|
|
2022-01-02 12:36:09 +03:00
|
|
|
$html = getContents($uri);
|
2019-09-10 15:37:50 +03:00
|
|
|
$scriptRegex = '/window\._sharedData = (.*);<\/script>/';
|
|
|
|
|
|
|
|
preg_match($scriptRegex, $html, $matches, PREG_OFFSET_CAPTURE, 0);
|
|
|
|
|
|
|
|
return json_decode($matches[1][0]);
|
|
|
|
|
|
|
|
}
|
2018-05-05 15:00:59 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
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'))) {
|
2018-10-24 17:33:49 +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'));
|
2018-11-08 00:17:53 +03:00
|
|
|
} elseif(!is_null($this->getInput('l'))) {
|
|
|
|
return self::URI . 'explore/locations/' . urlencode($this->getInput('l'));
|
2017-02-15 00:36:33 +03:00
|
|
|
}
|
|
|
|
return parent::getURI();
|
2017-02-11 18:16:56 +03:00
|
|
|
}
|
2022-03-23 06:09:59 +03:00
|
|
|
|
|
|
|
public function detectParameters($url){
|
|
|
|
$params = array();
|
|
|
|
|
|
|
|
// By username
|
|
|
|
$regex = '/^(https?:\/\/)?(www\.)?instagram\.com\/([^\/?\n]+)/';
|
|
|
|
|
|
|
|
if(preg_match($regex, $url, $matches) > 0) {
|
|
|
|
$params['u'] = urldecode($matches[3]);
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2013-10-29 12:26:48 +04:00
|
|
|
}
|