[ 'nopic' => [ 'name' => 'Hide profile pictures', 'type' => 'checkbox', 'title' => 'Activate to hide profile pictures in content' ], 'noimg' => [ 'name' => 'Hide images in tweets', 'type' => 'checkbox', 'title' => 'Activate to hide images in tweets' ], 'noimgscaling' => [ 'name' => 'Disable image scaling', 'type' => 'checkbox', 'title' => 'Activate to disable image scaling in tweets (keeps original image)' ] ], 'By keyword or hashtag' => [ 'q' => [ 'name' => 'Keyword or #hashtag', 'required' => true, 'exampleValue' => 'rss-bridge OR rssbridge', 'title' => << [ 'u' => [ 'name' => 'username', 'required' => true, 'exampleValue' => 'sebsauvage', 'title' => 'Insert a user name' ], 'norep' => [ 'name' => 'Without replies', 'type' => 'checkbox', 'title' => 'Only return initial tweets' ], 'noretweet' => [ 'name' => 'Without retweets', 'required' => false, 'type' => 'checkbox', 'title' => 'Hide retweets' ], 'nopinned' => [ 'name' => 'Without pinned tweet', 'required' => false, 'type' => 'checkbox', 'title' => 'Hide pinned tweet' ] ], 'By list' => [ 'user' => [ 'name' => 'User', 'required' => true, 'exampleValue' => 'Scobleizer', 'title' => 'Insert a user name' ], 'list' => [ 'name' => 'List', 'required' => true, 'exampleValue' => 'Tech-News', 'title' => 'Insert the list name' ], 'filter' => [ 'name' => 'Filter', 'exampleValue' => '#rss-bridge', 'required' => false, 'title' => 'Specify term to search for' ] ], 'By list ID' => [ 'listid' => [ 'name' => 'List ID', 'exampleValue' => '31748', 'required' => true, 'title' => 'Insert the list id' ], 'filter' => [ 'name' => 'Filter', 'exampleValue' => '#rss-bridge', 'required' => false, 'title' => 'Specify term to search for' ] ] ]; private $apiKey = null; private $guestToken = null; private $authHeaders = []; public function detectParameters($url) { $params = []; // By keyword or hashtag (search) $regex = '/^(https?:\/\/)?(www\.)?twitter\.com\/search.*(\?|&)q=([^\/&?\n]+)/'; if (preg_match($regex, $url, $matches) > 0) { $params['q'] = urldecode($matches[4]); return $params; } // By hashtag $regex = '/^(https?:\/\/)?(www\.)?twitter\.com\/hashtag\/([^\/?\n]+)/'; if (preg_match($regex, $url, $matches) > 0) { $params['q'] = urldecode($matches[3]); return $params; } // By list $regex = '/^(https?:\/\/)?(www\.)?twitter\.com\/([^\/?\n]+)\/lists\/([^\/?\n]+)/'; if (preg_match($regex, $url, $matches) > 0) { $params['user'] = urldecode($matches[3]); $params['list'] = urldecode($matches[4]); return $params; } // By username $regex = '/^(https?:\/\/)?(www\.)?twitter\.com\/([^\/?\n]+)/'; if (preg_match($regex, $url, $matches) > 0) { $params['u'] = urldecode($matches[3]); return $params; } return null; } public function getName() { switch ($this->queriedContext) { case 'By keyword or hashtag': $specific = 'search '; $param = 'q'; break; case 'By username': $specific = '@'; $param = 'u'; break; case 'By list': return $this->getInput('list') . ' - Twitter list by ' . $this->getInput('user'); case 'By list ID': return 'Twitter List #' . $this->getInput('listid'); default: return parent::getName(); } return 'Twitter ' . $specific . $this->getInput($param); } public function getURI() { switch ($this->queriedContext) { case 'By keyword or hashtag': return self::URI . 'search?q=' . urlencode($this->getInput('q')) . '&f=tweets'; case 'By username': return self::URI . urlencode($this->getInput('u')); // Always return without replies! // . ($this->getInput('norep') ? '' : '/with_replies'); case 'By list': return self::URI . urlencode($this->getInput('user')) . '/lists/' . str_replace(' ', '-', strtolower($this->getInput('list'))); case 'By list ID': return self::URI . 'i/lists/' . urlencode($this->getInput('listid')); default: return parent::getURI(); } } public function collectData() { // $data will contain an array of all found tweets (unfiltered) $data = null; // Contains user data (when in by username context) $user = null; // Array of all found tweets $tweets = []; // Get authentication information // Try to get all tweets switch ($this->queriedContext) { case 'By username': $cacheFactory = new CacheFactory(); $cache = $cacheFactory->create(); $cache->setScope('twitter'); $cache->setKey(['cache']); // todo: inspect mtime instead of purging with 3h $cache->purgeCache(60 * 60 * 3); $api = new TwitterClient($cache); $screenName = $this->getInput('u'); $screenName = trim($screenName); $screenName = ltrim($screenName, '@'); $data = $api->fetchUserTweets($screenName); break; 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', ]; $data = $this->makeApiCall('/1.1/search/tweets.json', $params)->statuses; 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', ]; $data = $this->makeApiCall('/1.1/lists/statuses.json', $params); break; case 'By list ID': // Does not work with the recent twitter changes $params = [ 'list_id' => $this->getInput('listid'), 'tweet_mode' => 'extended', ]; $data = $this->makeApiCall('/1.1/lists/statuses.json', $params); break; default: returnServerError('Invalid query context !'); } if (!$data) { switch ($this->queriedContext) { case 'By keyword or hashtag': returnServerError('twitter: No results for this query.'); // fall-through case 'By username': returnServerError('Requested username can\'t be found.'); // fall-through case 'By list': returnServerError('Requested username or list can\'t be found'); } } // Filter out unwanted tweets foreach ($data->tweets as $tweet) { if (!$tweet) { continue; } // Filter out retweets to remove possible duplicates of original tweet switch ($this->queriedContext) { case 'By keyword or hashtag': if (isset($tweet->retweeted_status) && substr($tweet->full_text, 0, 4) === 'RT @') { continue 2; } break; } $tweets[] = $tweet; } $hidePictures = $this->getInput('nopic'); $hidePinned = $this->getInput('nopinned'); if ($hidePinned) { $pinnedTweetId = null; if ($user && $user->pinned_tweet_ids_str) { $pinnedTweetId = $user->pinned_tweet_ids_str; } } 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; } switch ($this->queriedContext) { case 'By username': if ($this->getInput('norep') && isset($tweet->in_reply_to_status_id)) { continue 2; } break; } $item = []; $realtweet = $tweet; if (isset($tweet->retweeted_status)) { // Tweet is a Retweet, so set author based on original tweet and set realtweet for reference to the right content $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; $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'] . ' (@' . $item['username'] . ')'; // Convert plain text URLs into HTML hyperlinks $fulltext = $realtweet->full_text; $cleanedTweet = $fulltext; $foundUrls = false; if (substr($cleanedTweet, 0, 4) === 'RT @') { $cleanedTweet = substr($cleanedTweet, 3); } if (isset($realtweet->entities->media)) { foreach ($realtweet->entities->media as $media) { $cleanedTweet = str_replace( $media->url, '' . $media->display_url . '', $cleanedTweet ); $foundUrls = true; } } if (isset($realtweet->entities->urls)) { foreach ($realtweet->entities->urls as $url) { $cleanedTweet = str_replace( $url->url, '' . $url->display_url . '', $cleanedTweet ); $foundUrls = true; } } 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)) { $cleanedTweet = preg_replace( $reg_ex, "{$url[0]} ", $cleanedTweet ); } } // generate the title $item['title'] = strip_tags($cleanedTweet); // Add avatar $picture_html = ''; if (!$hidePictures) { $picture_html = << {$item['username']} EOD; } // Get images $media_html = ''; if (isset($realtweet->extended_entities->media) && !$this->getInput('noimg')) { foreach ($realtweet->extended_entities->media as $media) { switch ($media->type) { case 'photo': $image = $media->media_url_https . '?name=orig'; $display_image = $media->media_url_https; // add enclosures $item['enclosures'][] = $image; $media_html .= << EOD; break; case 'video': case 'animated_gif': if (isset($media->video_info)) { $link = $media->expanded_url; $poster = $media->media_url_https; $video = null; $maxBitrate = -1; foreach ($media->video_info->variants as $variant) { $bitRate = $variant->bitrate ?? -100; if ($bitRate > $maxBitrate) { $maxBitrate = $bitRate; $video = $variant->url; } } if (!is_null($video)) { // add enclosures $item['enclosures'][] = $video; $item['enclosures'][] = $poster; $media_html .= <<Video