Skip to content

Commit

Permalink
spouts\twitter: Add Search
Browse files Browse the repository at this point in the history
This will allow following hashtags.

See the Twitter docs for info about query:

https://developer.twitter.com/en/docs/tweets/search/guides/standard-operators
  • Loading branch information
jtojnar committed Aug 4, 2020
1 parent cc9bdf1 commit 48060b3
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/spouts/twitter/Search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace spouts\twitter;

/**
* Spout for fetching a Twitter search
*
* @author Jan Tojnar <[email protected]>
* @copyright Jan Tojnar <[email protected]>
* @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']}";
}
}
5 changes: 5 additions & 0 deletions src/spouts/twitter/usertimeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down

0 comments on commit 48060b3

Please sign in to comment.