diff options
author | 2023-09-06 21:14:11 +0700 | |
---|---|---|
committer | 2023-09-06 16:14:11 +0200 | |
commit | dbe37cc302bec84f622fbd65aa88690b0d946521 (patch) | |
tree | 553d03c5cc39e40c75341256b5a18887f7583139 | |
parent | 52b90e0873e56e55b39c4379748f54d55e46248e (diff) | |
download | rss-bridge-dbe37cc302bec84f622fbd65aa88690b0d946521.tar.gz rss-bridge-dbe37cc302bec84f622fbd65aa88690b0d946521.tar.zst rss-bridge-dbe37cc302bec84f622fbd65aa88690b0d946521.zip |
[TwitterBridge] Filter out any promoted tweet (#3652)
* Filter out any advertise tweet
* Make some filter work, fix bug that may happen with tweet id list.
* clear phpcs warning, ignore line length warning
-rw-r--r-- | bridges/TwitterBridge.php | 62 | ||||
-rw-r--r-- | lib/TwitterClient.php | 13 |
2 files changed, 49 insertions, 26 deletions
diff --git a/bridges/TwitterBridge.php b/bridges/TwitterBridge.php index a5d09f8a..8470dcf7 100644 --- a/bridges/TwitterBridge.php +++ b/bridges/TwitterBridge.php @@ -217,7 +217,7 @@ EOD private function getFullText($id) { $url = sprintf( - 'https://cdn.syndication.twimg.com/tweet-result?id=%s&lang=en', + 'https://cdn.syndication.twimg.com/tweet-result?id=%s&lang=en&token=449yf2pc4g', $id ); @@ -306,37 +306,59 @@ EOD } } + $hidePictures = $this->getInput('nopic'); + + $hidePinned = $this->getInput('nopinned'); + if ($hidePinned) { + $pinnedTweetId = null; + if ($data->user_info && $data->user_info->legacy->pinned_tweet_ids_str) { + $pinnedTweetId = $data->user_info->legacy->pinned_tweet_ids_str[0]; + } + } + // Array of Tweet IDs $tweetIds = []; // Filter out unwanted tweets foreach ($data->tweets as $tweet) { - if (isset($tweet->rest_id)) { - $tweetIds[] = $tweet->rest_id; - $tweet = $tweet->legacy; - } - if (!$tweet) { continue; } + + if (isset($tweet->legacy)) { + $legacy_info = $tweet->legacy; + } else { + $legacy_info = $tweet; + } + // Filter out retweets to remove possible duplicates of original tweet switch ($this->queriedContext) { case 'By keyword or hashtag': - if ((isset($tweet->retweeted_status) || isset($tweet->retweeted_status_result)) && substr($tweet->full_text, 0, 4) === 'RT @') { + // phpcs:ignore + if ((isset($legacy_info->retweeted_status) || isset($legacy_info->retweeted_status_result)) && substr($legacy_info->full_text, 0, 4) === 'RT @') { continue 2; } break; } - $tweets[] = $tweet; - } - $hidePictures = $this->getInput('nopic'); + // Skip own Retweets... + if (isset($legacy_info->retweeted_status) && $legacy_info->retweeted_status->user->id_str === $tweet->user->id_str) { + continue; + // phpcs:ignore + } elseif (isset($legacy_info->retweeted_status_result) && $tweet->retweeted_status_result->result->legacy->user_id_str === $legacy_info->user_id_str) { + continue; + } - $hidePinned = $this->getInput('nopinned'); - if ($hidePinned) { - $pinnedTweetId = null; - if ($user && $user->pinned_tweet_ids_str) { - $pinnedTweetId = $user->pinned_tweet_ids_str; + $tweetId = (isset($legacy_info->id_str) ? $legacy_info->id_str : $tweet->rest_id); + // Skip pinned tweet + if ($hidePinned && ($tweetId === $pinnedTweetId)) { + continue; + } + + if (isset($tweet->rest_id)) { + $tweetIds[] = $tweetId; } + $rtweet = $legacy_info; + $tweets[] = $rtweet; } if ($this->queriedContext === 'By username') { @@ -345,16 +367,6 @@ EOD $i = 0; foreach ($tweets as $tweet) { - // Skip own Retweets... - if (isset($tweet->retweeted_status) && $tweet->retweeted_status->user->id_str === $tweet->user->id_str) { - continue; - } - - // Skip pinned tweet - if ($hidePinned && $tweet->id_str === $pinnedTweetId) { - continue; - } - $item = []; $realtweet = $tweet; diff --git a/lib/TwitterClient.php b/lib/TwitterClient.php index d2a09fdd..20f21482 100644 --- a/lib/TwitterClient.php +++ b/lib/TwitterClient.php @@ -146,9 +146,14 @@ class TwitterClient } if (isset($timeline->data->user)) { - if (!isset($entry->content->itemContent->tweet_results->result->legacy)) { + if (!isset($entry->content->itemContent->tweet_results->result)) { continue; } + + if (isset($entry->content->itemContent->promotedMetadata)) { + continue; + } + $tweets[] = $entry->content->itemContent->tweet_results->result; $userIds[] = $entry->content->itemContent->tweet_results->result->core->user_results->result; @@ -156,6 +161,12 @@ class TwitterClient if (!isset($entry->content->content->tweetResult->result->legacy)) { continue; } + + // Filter out any advertise tweet + if (isset($entry->content->content->tweetPromotedMetadata)) { + continue; + } + $tweets[] = $entry->content->content->tweetResult->result; $userIds[] = $entry->content->content->tweetResult->result->core->user_result->result; |