mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-08-04 15:47:32 +02:00
[TwitterV2Bridge] Add parameter to include only media tweets (#2614)
This commit is contained in:
@@ -48,6 +48,11 @@ class TwitterV2Bridge extends BridgeAbstract {
|
|||||||
'exampleValue' => '20',
|
'exampleValue' => '20',
|
||||||
'title' => 'Maximum number of tweets to retrieve (limit is 100)'
|
'title' => 'Maximum number of tweets to retrieve (limit is 100)'
|
||||||
),
|
),
|
||||||
|
'imgonly' => array(
|
||||||
|
'name' => 'Only media tweets',
|
||||||
|
'type' => 'checkbox',
|
||||||
|
'title' => 'Activate to show only tweets with media (photo/video)'
|
||||||
|
),
|
||||||
'nopic' => array(
|
'nopic' => array(
|
||||||
'name' => 'Hide profile pictures',
|
'name' => 'Hide profile pictures',
|
||||||
'type' => 'checkbox',
|
'type' => 'checkbox',
|
||||||
@@ -159,6 +164,7 @@ EOD
|
|||||||
$maxResults = 100;
|
$maxResults = 100;
|
||||||
}
|
}
|
||||||
$idAsTitle = $this->getInput('idastitle');
|
$idAsTitle = $this->getInput('idastitle');
|
||||||
|
$onlyMediaTweets = $this->getInput('imgonly');
|
||||||
|
|
||||||
// Read API token from config.ini.php, put into Header
|
// Read API token from config.ini.php, put into Header
|
||||||
$this->apiToken = $this->getOption('twitterv2apitoken');
|
$this->apiToken = $this->getOption('twitterv2apitoken');
|
||||||
@@ -176,7 +182,6 @@ EOD
|
|||||||
$user = $this->makeApiCall('/users/by/username/'
|
$user = $this->makeApiCall('/users/by/username/'
|
||||||
. $this->getInput('u'), $params);
|
. $this->getInput('u'), $params);
|
||||||
|
|
||||||
//Debug::log('User JSON: ' . json_encode($user));
|
|
||||||
if(isset($user->errors)) {
|
if(isset($user->errors)) {
|
||||||
Debug::log('User JSON: ' . json_encode($user));
|
Debug::log('User JSON: ' . json_encode($user));
|
||||||
returnServerError('Requested username can\'t be found.');
|
returnServerError('Requested username can\'t be found.');
|
||||||
@@ -248,7 +253,6 @@ EOD
|
|||||||
returnServerError('Invalid query context !');
|
returnServerError('Invalid query context !');
|
||||||
}
|
}
|
||||||
|
|
||||||
//Debug::log('Data JSON: ' . json_encode($data));
|
|
||||||
if((isset($data->errors) && !isset($data->data)) ||
|
if((isset($data->errors) && !isset($data->data)) ||
|
||||||
(isset($data->meta) && $data->meta->result_count === 0)) {
|
(isset($data->meta) && $data->meta->result_count === 0)) {
|
||||||
Debug::log('Data JSON: ' . json_encode($data));
|
Debug::log('Data JSON: ' . json_encode($data));
|
||||||
@@ -275,15 +279,12 @@ EOD
|
|||||||
|
|
||||||
// Extract additional Users data into array
|
// Extract additional Users data into array
|
||||||
isset($data->includes->users) ? $includesUsers = $data->includes->users : $includesUsers = null;
|
isset($data->includes->users) ? $includesUsers = $data->includes->users : $includesUsers = null;
|
||||||
//Debug::log('Tweets Users JSON: ' . json_encode($includesUsers));
|
|
||||||
|
|
||||||
// Extract additional Tweets data into array
|
// Extract additional Tweets data into array
|
||||||
isset($data->includes->tweets) ? $includesTweets = $data->includes->tweets : $includesTweets = null;
|
isset($data->includes->tweets) ? $includesTweets = $data->includes->tweets : $includesTweets = null;
|
||||||
//Debug::log('Includes Tweets JSON: ' . json_encode($includesTweets));
|
|
||||||
|
|
||||||
// Extract main Tweets data into array
|
// Extract main Tweets data into array
|
||||||
$tweets = $data->data;
|
$tweets = $data->data;
|
||||||
//Debug::log('Tweets JSON: ' . json_encode($tweets));
|
|
||||||
|
|
||||||
// Make another API call to get user and media info for retweets
|
// Make another API call to get user and media info for retweets
|
||||||
// Is there some way to get this info included in original API call?
|
// Is there some way to get this info included in original API call?
|
||||||
@@ -309,7 +310,6 @@ EOD
|
|||||||
|
|
||||||
// Get the retweeted tweets
|
// Get the retweeted tweets
|
||||||
$retweetedData = $this->makeApiCall('/tweets', $params);
|
$retweetedData = $this->makeApiCall('/tweets', $params);
|
||||||
//Debug::log('retweetedData JSON: ' . json_encode($retweetedData));
|
|
||||||
|
|
||||||
// Extract retweets Media data into array
|
// Extract retweets Media data into array
|
||||||
isset($retweetedData->includes->media) ? $retweetedMedia
|
isset($retweetedData->includes->media) ? $retweetedMedia
|
||||||
@@ -324,9 +324,8 @@ EOD
|
|||||||
foreach($tweets as $tweet) {
|
foreach($tweets as $tweet) {
|
||||||
//Debug::log('Tweet JSON: ' . json_encode($tweet));
|
//Debug::log('Tweet JSON: ' . json_encode($tweet));
|
||||||
|
|
||||||
// Skip pinned tweet
|
// Skip pinned tweet (if selected)
|
||||||
if($hidePinned && $tweet->id === $pinnedTweetId) {
|
if($hidePinned && $tweet->id === $pinnedTweetId) {
|
||||||
//Debug::log('Skipping pinned tweet');
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -336,27 +335,23 @@ EOD
|
|||||||
$isReply = false;
|
$isReply = false;
|
||||||
if(isset($tweet->referenced_tweets)) {
|
if(isset($tweet->referenced_tweets)) {
|
||||||
if(in_array($tweet->referenced_tweets[0]->type, $retweetTypes)) {
|
if(in_array($tweet->referenced_tweets[0]->type, $retweetTypes)) {
|
||||||
//Debug::log('This is a retweet');
|
|
||||||
$isRetweet = true;
|
$isRetweet = true;
|
||||||
} elseif ($tweet->referenced_tweets[0]->type === 'replied_to') {
|
} elseif ($tweet->referenced_tweets[0]->type === 'replied_to') {
|
||||||
//Debug::log('This is a reply');
|
|
||||||
$isReply = true;
|
$isReply = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Skip replies and/or retweets. This check is primarily for lists
|
// Skip replies and/or retweets (if selected). This check is primarily for lists
|
||||||
// These should already be pre-filtered for username and keyword queries
|
// These should already be pre-filtered for username and keyword queries
|
||||||
if (($hideRetweets && $isRetweet) || ($hideReplies && $isReply)) {
|
if (($hideRetweets && $isRetweet) || ($hideReplies && $isReply)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$cleanedTweet = $tweet->text;
|
$cleanedTweet = $tweet->text;
|
||||||
//Debug::log('cleanedTweet: ' . $cleanedTweet);
|
|
||||||
|
|
||||||
// Perform optional filtering (skip tweets that don't contain desired word)
|
// Perform filtering (skip tweets that don't contain desired word, if provided)
|
||||||
if (! empty($tweetFilter)) {
|
if (! empty($tweetFilter)) {
|
||||||
if(stripos($cleanedTweet, $this->getInput('filter')) === false) {
|
if(stripos($cleanedTweet, $this->getInput('filter')) === false) {
|
||||||
//Debug::log('Skipping tweet that does not contain filter text');
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -369,7 +364,6 @@ EOD
|
|||||||
// Replace tweet object with original retweeted object
|
// Replace tweet object with original retweeted object
|
||||||
if($isRetweet) {
|
if($isRetweet) {
|
||||||
foreach($includesTweets as $includesTweet) {
|
foreach($includesTweets as $includesTweet) {
|
||||||
//Debug::log('Includes Tweet JSON: ' . json_encode($includesTweet));
|
|
||||||
if($includesTweet->id === $tweet->referenced_tweets[0]->id) {
|
if($includesTweet->id === $tweet->referenced_tweets[0]->id) {
|
||||||
$tweet = $includesTweet;
|
$tweet = $includesTweet;
|
||||||
break;
|
break;
|
||||||
@@ -422,6 +416,12 @@ EOD
|
|||||||
. ' (@'
|
. ' (@'
|
||||||
. $item['username'] . ')';
|
. $item['username'] . ')';
|
||||||
|
|
||||||
|
// Skip non-media tweet (if selected)
|
||||||
|
// This check must wait until after retweets are identified
|
||||||
|
if ($onlyMediaTweets && !isset($tweet->attachments->media_keys)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// Search for and replace URLs in Tweet text
|
// Search for and replace URLs in Tweet text
|
||||||
$foundUrls = false;
|
$foundUrls = false;
|
||||||
if(isset($tweet->entities->urls)) {
|
if(isset($tweet->entities->urls)) {
|
||||||
|
Reference in New Issue
Block a user