[TwitterBridge] Fix search, user, list ID (#3566)

* Add ability to fetch user, list tweet

* Fix user, search, list ID although list still broke

* clear whitespace

* Revert CACHE_TIMEOUT

* clear whitespace, change single quote

* Clear PHP warning, add ability to get full-text if truncated

* Clear PHP warning

* clear warning

* clear whitespace

* Add check condition for mediaDetails.

* Add whitespace

* Add try catch exception for get full-text tweet

* clear warning

* clear warning
This commit is contained in:
csisoap 2023-07-26 03:36:41 +07:00 committed by GitHub
parent 2cc89b767c
commit 556bca58cf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 364 additions and 70 deletions

View file

@ -210,6 +210,16 @@ EOD
}
}
private function getFullText($id)
{
$url = sprintf(
'https://cdn.syndication.twimg.com/tweet-result?id=%s&lang=en',
$id
);
return json_decode(getContents($url), false);
}
public function collectData()
{
// $data will contain an array of all found tweets (unfiltered)
@ -220,13 +230,11 @@ EOD
$tweets = [];
// Get authentication information
$cache = RssBridge::getCache();
$api = new TwitterClient($cache);
// Try to get all tweets
switch ($this->queriedContext) {
case 'By username':
$cache = RssBridge::getCache();
$api = new TwitterClient($cache);
$screenName = $this->getInput('u');
$screenName = trim($screenName);
$screenName = ltrim($screenName, '@');
@ -238,35 +246,45 @@ EOD
case 'By keyword or hashtag':
// Does not work with the recent twitter changes
$params = [
'q' => urlencode($this->getInput('q')),
'tweet_mode' => 'extended',
'tweet_search_mode' => 'live',
'q' => urlencode($this->getInput('q')),
'tweet_mode' => 'extended',
'tweet_search_mode' => 'live',
];
$data = $this->makeApiCall('/1.1/search/tweets.json', $params)->statuses;
$tweets = $api->search($params)->statuses;
$data = (object) [
'tweets' => $tweets
];
break;
case 'By list':
// Does not work with the recent twitter changes
$params = [
'slug' => strtolower($this->getInput('list')),
'owner_screen_name' => strtolower($this->getInput('user')),
'tweet_mode' => 'extended',
// $params = [
// 'slug' => strtolower($this->getInput('list')),
// 'owner_screen_name' => strtolower($this->getInput('user')),
// 'tweet_mode' => 'extended',
// ];
$query = [
'screenName' => strtolower($this->getInput('user')),
'listSlug' => strtolower($this->getInput('list'))
];
$data = $this->makeApiCall('/1.1/lists/statuses.json', $params);
$data = $api->fetchListTweets($query, $this->queriedContext);
break;
case 'By list ID':
// Does not work with the recent twitter changes
$params = [
'list_id' => $this->getInput('listid'),
'tweet_mode' => 'extended',
// $params = [
// 'list_id' => $this->getInput('listid'),
// 'tweet_mode' => 'extended',
// ];
$query = [
'listId' => $this->getInput('listid')
];
$data = $this->makeApiCall('/1.1/lists/statuses.json', $params);
$data = $api->fetchListTweets($query, $this->queriedContext);
break;
default:
returnServerError('Invalid query context !');
}
@ -314,6 +332,7 @@ EOD
$this->feedIconUrl = $data->user_info->legacy->profile_image_url_https ?? null;
}
$i = 0;
foreach ($tweets as $tweet) {
// Skip own Retweets...
if (isset($tweet->retweeted_status) && $tweet->retweeted_status->user->id_str === $tweet->user->id_str) {
@ -325,14 +344,6 @@ EOD
continue;
}
switch ($this->queriedContext) {
case 'By username':
if ($this->getInput('norep') && isset($tweet->in_reply_to_status_id)) {
continue 2;
}
break;
}
$item = [];
$realtweet = $tweet;
@ -341,11 +352,40 @@ EOD
$realtweet = $tweet->retweeted_status;
}
$item['username'] = $data->user_info->legacy->screen_name;
$item['fullname'] = $data->user_info->legacy->name;
$item['avatar'] = $data->user_info->legacy->profile_image_url_https;
if (isset($realtweet->truncated) && $realtweet->truncated) {
try {
$realtweet = $this->getFullText($realtweet->id_str);
} catch (HttpException $e) {
$realtweet = $tweet;
}
}
switch ($this->queriedContext) {
case 'By username':
if ($this->getInput('norep') && isset($tweet->in_reply_to_status_id)) {
continue 2;
}
$item['username'] = $data->user_info->legacy->screen_name;
$item['fullname'] = $data->user_info->legacy->name;
$item['avatar'] = $data->user_info->legacy->profile_image_url_https;
$item['id'] = $realtweet->id_str;
break;
case 'By list':
case 'By list ID':
$item['username'] = $data->userIds[$i]->legacy->screen_name;
$item['fullname'] = $data->userIds[$i]->legacy->name;
$item['avatar'] = $data->userIds[$i]->legacy->profile_image_url_https;
$item['id'] = $realtweet->conversation_id_str;
break;
case 'By keyword or hashtag':
$item['username'] = $realtweet->user->screen_name;
$item['fullname'] = $realtweet->user->name;
$item['avatar'] = $realtweet->user->profile_image_url_https;
$item['id'] = $realtweet->id_str;
break;
}
$item['timestamp'] = $realtweet->created_at;
$item['id'] = $realtweet->id_str;
$item['uri'] = self::URI . $item['username'] . '/status/' . $item['id'];
$item['author'] = (isset($tweet->retweeted_status) ? 'RT: ' : '')
. $item['fullname']
@ -353,7 +393,11 @@ EOD
. $item['username'] . ')';
// Convert plain text URLs into HTML hyperlinks
$fulltext = $realtweet->full_text;
if (isset($realtweet->full_text)) {
$fulltext = $realtweet->full_text;
} else {
$fulltext = $realtweet->text;
}
$cleanedTweet = $fulltext;
$foundUrls = false;
@ -385,7 +429,7 @@ EOD
if ($foundUrls === false) {
// fallback to regex'es
$reg_ex = '/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/';
if (preg_match($reg_ex, $realtweet->full_text, $url)) {
if (preg_match($reg_ex, $fulltext, $url)) {
$cleanedTweet = preg_replace(
$reg_ex,
"<a href='{$url[0]}' target='_blank'>{$url[0]}</a> ",
@ -410,10 +454,17 @@ EOD
EOD;
}
$medias = [];
if (isset($realtweet->extended_entities->media)) {
$medias = $realtweet->extended_entities->media;
} else if (isset($realtweet->mediaDetails)) {
$medias = $realtweet->mediaDetails;
}
// Get images
$media_html = '';
if (isset($realtweet->extended_entities->media) && !$this->getInput('noimg')) {
foreach ($realtweet->extended_entities->media as $media) {
if (!$this->getInput('noimg')) {
foreach ($medias as $media) {
switch ($media->type) {
case 'photo':
$image = $media->media_url_https . '?name=orig';
@ -496,6 +547,7 @@ EOD;
EOD;
// put out
$i++;
$this->items[] = $item;
}