Skip to content

Commit 1d4158a

Browse files
committed
Search tweets
1 parent d82352d commit 1d4158a

3 files changed

+68
-0
lines changed

examples/search-tweets-async.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
use ApiClients\Client\Twitter\AsyncClient;
4+
use React\EventLoop\Factory;
5+
use function ApiClients\Foundation\resource_pretty_print;
6+
7+
require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor/autoload.php';
8+
$config = require 'resolve_config.php';
9+
10+
$loop = Factory::create();
11+
$client = (new AsyncClient(
12+
$config['consumer']['key'],
13+
$config['consumer']['secret'],
14+
$loop
15+
))->withAccessToken(
16+
$config['access_token']['token'],
17+
$config['access_token']['secret']
18+
)->stream();
19+
20+
$hashtags = [];
21+
22+
if (count($argv) > 1) {
23+
unset($argv[0]);
24+
foreach ($argv as $hashtag) {
25+
$hashtags[] = '#' . $hashtag;
26+
}
27+
}
28+
29+
$hashtags = array_unique($hashtags);
30+
31+
$client->searchTweets([
32+
// Any parameters from: https://developer.twitter.com/en/docs/tweets/search/api-reference/get-search-tweets.html
33+
'q' => 'nasa',
34+
'result_type' => 'popular',
35+
])->subscribe(function ($document) {
36+
resource_pretty_print($document);
37+
});
38+
39+
$loop->run();

src/AsyncStreamingClient.php

+27
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@
44

55
use ApiClients\Foundation\Client;
66
use ApiClients\Foundation\Hydrator\CommandBus\Command\HydrateCommand;
7+
use ApiClients\Foundation\Transport\CommandBus\Command\RequestCommand;
78
use ApiClients\Foundation\Transport\CommandBus\Command\StreamingRequestCommand;
9+
use ApiClients\Foundation\Transport\ParsedContentsInterface;
810
use GuzzleHttp\Psr7\Request;
911
use Psr\Http\Message\RequestInterface;
12+
use Psr\Http\Message\ResponseInterface;
1013
use React\EventLoop\LoopInterface;
1114
use Rx\Observable;
1215
use Rx\Operator\CutOperator;
1316
use Rx\React\Promise;
1417
use Rx\Scheduler\ImmediateScheduler;
18+
use function ApiClients\Tools\Rx\observableFromArray;
19+
use function RingCentral\Psr7\build_query;
1520

1621
final class AsyncStreamingClient implements AsyncStreamingClientInterface
1722
{
@@ -56,6 +61,28 @@ public function filtered(array $filter = []): Observable
5661
);
5762
}
5863

64+
public function searchTweets(array $filter = []): Observable
65+
{
66+
$query = build_query($filter);
67+
68+
return Promise::toObservable($this->client->handle(new RequestCommand(
69+
new Request(
70+
'GET',
71+
'https://api.twitter.com/1.1/search/tweets.json?' . $query,
72+
[]
73+
)
74+
))->then(function (ResponseInterface $response) {
75+
/** @var ParsedContentsInterface $body */
76+
$body = $response->getBody();
77+
78+
return $body->getParsedContents()['statuses'];
79+
}))->flatMap(function (array $statuses) {
80+
return observableFromArray($statuses);
81+
})->flatMap(function (array $document) {
82+
return Promise::toObservable($this->client->handle(new HydrateCommand('Tweet', $document)));
83+
});
84+
}
85+
5986
protected function stream(RequestInterface $request): Observable
6087
{
6188
return Promise::toObservable($this->client->handle(new StreamingRequestCommand(

src/AsyncStreamingClientInterface.php

+2
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ interface AsyncStreamingClientInterface
99
public function sample(): Observable;
1010

1111
public function filtered(array $filter = []): Observable;
12+
13+
public function searchTweets(array $filter): Observable;
1214
}

0 commit comments

Comments
 (0)