From 48060b34062bd8c177bd4a6f49a70c52fc1b3e56 Mon Sep 17 00:00:00 2001 From: Jan Tojnar Date: Tue, 4 Aug 2020 03:39:17 +0200 Subject: [PATCH] spouts\twitter: Add Search This will allow following hashtags. See the Twitter docs for info about query: https://developer.twitter.com/en/docs/tweets/search/guides/standard-operators --- src/spouts/twitter/Search.php | 70 +++++++++++++++++++++++++++++ src/spouts/twitter/usertimeline.php | 5 +++ 2 files changed, 75 insertions(+) create mode 100644 src/spouts/twitter/Search.php diff --git a/src/spouts/twitter/Search.php b/src/spouts/twitter/Search.php new file mode 100644 index 0000000000..da100a08b9 --- /dev/null +++ b/src/spouts/twitter/Search.php @@ -0,0 +1,70 @@ + + * @copyright Jan Tojnar + * @license GPL-3.0-or-later + */ +class Search extends \spouts\twitter\usertimeline { + /** @var string name of source */ + public $name = 'Twitter: search'; + + /** @var string description of this source type */ + public $description = 'Fetch the search results for given query.'; + + /** @var array configurable parameters */ + public $params = [ + 'consumer_key' => [ + 'title' => 'Consumer Key', + 'type' => 'text', + 'default' => '', + 'required' => true, + 'validation' => ['notempty'] + ], + 'consumer_secret' => [ + 'title' => 'Consumer Secret', + 'type' => 'password', + 'default' => '', + 'required' => true, + 'validation' => ['notempty'] + ], + 'access_token' => [ + 'title' => 'Access Token (optional)', + 'type' => 'text', + 'default' => '', + 'required' => false, + 'validation' => [] + ], + 'access_token_secret' => [ + 'title' => 'Access Token Secret (optional)', + 'type' => 'password', + 'default' => '', + 'required' => false, + 'validation' => [] + ], + 'query' => [ + 'title' => 'Search query', + 'type' => 'text', + 'default' => '', + 'required' => true, + 'validation' => ['notempty'] + ], + ]; + + public function load(array $params) { + $this->client = $this->getHttpClient($params['consumer_key'], $params['consumer_secret'], $params['access_token'], $params['access_token_secret']); + + $this->items = $this->fetchTwitterTimeline('search/tweets', [ + 'q' => $params['query'], + 'result_type' => 'recent', + ]); + + $this->htmlUrl = 'https://twitter.com/search?q=' . urlencode($params['query']); + + $this->spoutTitle = "Search twitter for {$params['query']}"; + } +} diff --git a/src/spouts/twitter/usertimeline.php b/src/spouts/twitter/usertimeline.php index a9c5cf8a98..73d484c20f 100644 --- a/src/spouts/twitter/usertimeline.php +++ b/src/spouts/twitter/usertimeline.php @@ -135,6 +135,11 @@ protected function fetchTwitterTimeline($endpoint, array $params = []) { $timeline = json_decode((string) $response->getBody()); + + if (isset($timeline->statuses)) { + $timeline = $timeline->statuses; + } + if (!is_array($timeline)) { throw new \Exception('Invalid twitter response'); }