diff --git a/.gitignore b/.gitignore index c2557eb..9977e30 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,5 @@ psalm.xml testbench.yaml vendor node_modules -.php-cs-fixer.cache \ No newline at end of file +.php-cs-fixer.cache +tinker.php diff --git a/README.md b/README.md index 534a2be..7e2928b 100644 --- a/README.md +++ b/README.md @@ -5,22 +5,24 @@ [![run-tests](https://github.com/spatie/twitter-streaming-api/actions/workflows/run-tests.yml/badge.svg)](https://github.com/spatie/twitter-streaming-api/actions/workflows/run-tests.yml) [![Total Downloads](https://img.shields.io/packagist/dt/spatie/twitter-streaming-api.svg?style=flat-square)](https://packagist.org/packages/spatie/twitter-streaming-api) -Twitter provides a streaming API with which you can do interesting things such as listening for tweets that contain specific strings or actions a user might take (e.g. liking a tweet, following someone,...). This package makes it very easy to work with the API. +Twitter provides a streaming API with which you can do interesting things such as listening for tweets that contain +specific strings or actions a user might take (e.g. liking a tweet, following someone,...). This package makes it very +easy to work with the API. Here's a quick example: ```php PublicStream::create( - $accessToken, - $accessTokenSecret, - $consumerKey, - $consumerSecret + $bearerToken, + $apiKey, + $apiSecretKey )->whenHears('@spatie_be', function(array $tweet) { echo "We got mentioned by {$tweet['user']['screen_name']} who tweeted {$tweet['text']}"; })->startListening(); ``` - There's no polling involved. The package will keep an open https connection with Twitter, events will be delivered in real time. +There's no polling involved. The package will keep an open https connection with Twitter, events will be delivered in +real time. Under the hood the [Phirehose package](https://github.com/fennb/phirehose) is used. @@ -28,13 +30,17 @@ Under the hood the [Phirehose package](https://github.com/fennb/phirehose) is us [](https://spatie.be/github-ad-click/twitter-streaming-api) -We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us). +We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can +support us by [buying one of our paid products](https://spatie.be/open-source/support-us). -We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards). +We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. +You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards +on [our virtual postcard wall](https://spatie.be/open-source/postcards). ## Postcardware -You're free to use this package (it's [MIT-licensed](LICENSE.md)), but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. +You're free to use this package (it's [MIT-licensed](LICENSE.md)), but if it makes it to your production environment we +highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium. @@ -50,30 +56,34 @@ composer require spatie/twitter-streaming-api ## Getting credentials -In order to use this package you'll need to get some credentials from Twitter. Head over to the [Application management on Twitter](https://apps.twitter.com/) to create an application. +In order to use this package you'll need to get some credentials from Twitter. Head over to +the [Developer Portal on Twitter](https://developer.twitter.com/) to create an application. -Once you've created your application, click on the `Keys and access tokens` tab to retrieve your `consumer_key`, `consumer_secret`, `access_token` and `access_token_secret`. - -![Keys and access tokens tab on Twitter](https://spatie.github.io/twitter-streaming-api/images/twitter.jpg) +Once you've created your application, click on the `Keys and tokens` tab to retrieve your `bearer_token` +, `api_key`, `api_secret_key`. +![Keys and tokens tab on Twitter](docs/tokens.png) ## Usage -Currently, this package works with the public stream and the user stream. Both the `PublicStream` and `UserStream` classes provide a `startListening` function that kicks of the listening process. Unless you cancel it your PHP process will execute that function forever. No code after the function will be run. +Currently, this package works with the public stream and the user stream. Both the `PublicStream` and `UserStream` +classes provide a `startListening` function that kicks of the listening process. Unless you cancel it your PHP process +will execute that function forever. No code after the function will be run. ### The public stream -The public stream can be used to listen for specific words that are being tweeted, receive Tweets that are being sent from specific locations or to follow one or more users tweets. +The public stream can be used to listen for specific words that are being tweeted, receive Tweets that are being sent +from specific locations or to follow one or more users tweets. #### Listen for Tweets containing specific words -The first parameter of `whenHears` must be a string or an array containing the word or words you want to listen for. The second parameter should be a callable that will be executed when one of your words is used on Twitter. +The first parameter of `whenHears` must be a string or an array containing the word or words you want to listen for. The +second parameter should be a callable that will be executed when one of your words is used on Twitter. ```php PublicStream::create( - $accessToken, - $accessTokenSecret, - $consumerKey, - $consumerSecret + $bearerToken, + $apiKey, + $apiSecretKey )->whenHears('@spatie_be', function(array $tweet) { echo "We got mentioned by {$tweet['user']['screen_name']} who tweeted {$tweet['text']}"; })->startListening(); @@ -81,16 +91,19 @@ PublicStream::create( #### Listen for Tweets from specific locations -The first parameter of `whenFrom` must be an array containing one or more bounding boxes, each as an array of 4 element lon/lat pairs (looking like `[, , , ]`). The second parameter should be a callable that will be executed when a Tweet from one of your tracked locations is being sent. +The first parameter of `whenFrom` must be an array containing one or more bounding boxes, each as an array of 4 element +lon/lat pairs (looking +like `[, , , ]`) +. The second parameter should be a callable that will be executed when a Tweet from one of your tracked locations is +being sent. **Track all tweets from San Francisco or New York:** ```php PublicStream::create( - $accessToken, - $accessTokenSecret, - $consumerKey, - $consumerSecret + $bearerToken, + $apiKey, + $apiSecretKey )->whenFrom([ [-122.75, 36.8, -121.75, 37.8], // San Francisco [-74, 40, -73, 41], // New York @@ -103,10 +116,9 @@ PublicStream::create( ```php PublicStream::create( - $accessToken, - $accessTokenSecret, - $consumerKey, - $consumerSecret + $bearerToken, + $apiKey, + $apiSecretKey )->whenFrom([ [-180, -90, 180, 90] // Whole world ], function(array $tweet) { @@ -116,59 +128,28 @@ PublicStream::create( #### Listen for Tweets from specific users -The first parameter of `whenTweets` must be a string or an array containing the Twitter user ID or IDs you wish to follow. The second parameter should be a callable that will be executed when one of your followed users tweets. Only public information relating to the Twitter user will be available. +The first parameter of `whenTweets` must be a string or an array containing the Twitter user ID or IDs you wish to +follow. The second parameter should be a callable that will be executed when one of your followed users tweets. Only +public information relating to the Twitter user will be available. ```php PublicStream::create( - $accessToken, - $accessTokenSecret, - $consumerKey, - $consumerSecret + $bearerToken, + $apiKey, + $apiSecretKey )->whenTweets('92947501', function(array $tweet) { echo "{$tweet['user']['screen_name']} just tweeted {$tweet['text']}"; })->startListening(); ``` -## Check filter predicates - -In most cases your script will interacts with the Twitter streaming API as a daemon. If you want to change the filters while it is running you can pass a callable to `checkFilterPredicates`. That callable will be called every ~5 seconds. - -Here's an example: - -```php -PublicStream::create( - $accessToken, - $accessTokenSecret, - $consumerKey, - $consumerSecret -)->whenHears('@spatie_be', function(array $tweet) { - echo "We got mentioned by {$tweet['user']['screen_name']} who tweeted {$tweet['text']}"; -})->checkFilterPredicates(function($stream) { - $trackIds = ExternalStorage::get('TwitterTrackIds'); - if ($trackIds != $stream->getTrack()) { - $stream->setTrack($trackIds); - } -})->startListening(); -``` - -If you run in an external script something like - -```php -ExternalStorage::set('TwitterTrackIds', ['@spatie_be', '@laravelphp']) -``` - -then the method in the example above will change the filter predicates and reconnect to the Twitter streaming API. - - `ExternalStorage::get/set` is just a dummy example. In real apps you'll probably use a file, in memory cache or db for this. - ### The user stream ```php UserStream::create( - $accessToken, - $accessTokenSecret, - $consumerKey, - $consumerSecret + 'your_handle', + $bearerToken, + $apiKey, + $apiSecretKey )->onEvent(function(array $event) { if ($event['event'] === 'favorite') { echo "Our tweet {$event['target_object']['text']} got favorited by {$event['source']['screen_name']}"; @@ -178,7 +159,11 @@ UserStream::create( ## A word to the wise -These APIs work in realtime, so they could report a lot of activity. If you need to do some heavy work processing that activity it's best to put that work in a queue to keep your listening process fast. +These APIs work in realtime, so they could report a lot of activity. If you need to do some heavy work processing that +activity, it's best to put that work in a queue to keep your listening process fast. + +If you need more advanced functionalities, consider checking +out [redwebcreation/twitter-streaming-api](https://github.com/redwebcreation/twitter-streaming-api). ## Changelog @@ -198,7 +183,9 @@ If you discover any security related issues, please email freek@spatie.be instea - [All Contributors](../../contributors) ## About Spatie -Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource). + +Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source +projects [on our website](https://spatie.be/opensource). ## License diff --git a/composer.json b/composer.json index a737e1e..e1135d6 100644 --- a/composer.json +++ b/composer.json @@ -17,11 +17,11 @@ ], "require": { "php": "^8.0", - "fennb/phirehose": "dev-master#405d125db9baa771ac0cd861f0b2b2032434d40c" + "redwebcreation/twitter-stream-api": "^0.2" }, "require-dev": { "phpunit/phpunit": "^9", - "spatie/laravel-ray": "^1", + "spatie/laravel-ray": "^1", "friendsofphp/php-cs-fixer": "^3.0", "vimeo/psalm": "^4" }, diff --git a/src/BaseStream.php b/src/BaseStream.php deleted file mode 100644 index c7aeeec..0000000 --- a/src/BaseStream.php +++ /dev/null @@ -1,35 +0,0 @@ -stream->startListening(); - } - - protected function createStream( - string $accessToken, - string $accessSecret, - string $consumerKey, - string $consumerSecret, - string $filter - ): PhirehoseWrapper { - return new PhirehoseWrapper( - $accessToken, - $accessSecret, - $consumerKey, - $consumerSecret, - $filter - ); - } -} diff --git a/src/PhirehoseWrapper.php b/src/PhirehoseWrapper.php deleted file mode 100644 index 66045db..0000000 --- a/src/PhirehoseWrapper.php +++ /dev/null @@ -1,68 +0,0 @@ -consumerKey = $consumerKey; - $this->consumerSecret = $consumerSecret; - - if ($method === Phirehose::METHOD_USER) { - /** @psalm-suppress InternalProperty */ - $this->URL_BASE = 'https://userstream.twitter.com/1.1/'; - } - - $this->onStreamActivity = function () { - }; - $this->checkFilterPredicates = function () { - }; - } - - /** - * @param mixed $status - */ - public function enqueueStatus($status): void - { - ($this->onStreamActivity)(json_decode($status, true)); - } - - public function performOnStreamActivity(callable $onStreamActivity) - { - $this->onStreamActivity = $onStreamActivity; - } - - public function startListening() - { - $this->consume(); - } - - public function setCheckFilterPredicates(callable $checkFilterPredicates) - { - $this->checkFilterPredicates = $checkFilterPredicates; - } - - public function checkFilterPredicates() - { - ($this->checkFilterPredicates)($this); - } -} diff --git a/src/PublicStream.php b/src/PublicStream.php index b4bcdf0..ebc6fe0 100644 --- a/src/PublicStream.php +++ b/src/PublicStream.php @@ -2,71 +2,80 @@ namespace Spatie\TwitterStreamingApi; -use Phirehose; +use RWC\TwitterStream\Rule; +use RWC\TwitterStream\RuleBuilder; +use RWC\TwitterStream\Sets; +use RWC\TwitterStream\Support\Arr; +use RWC\TwitterStream\TwitterStream; -class PublicStream extends BaseStream +class PublicStream { - public function __construct( - string $accessToken, - string $accessSecret, - string $consumerKey, - string $consumerSecret - ) { - $this->stream = $this->createStream( - $accessToken, - $accessSecret, - $consumerKey, - $consumerSecret, - Phirehose::METHOD_FILTER - ); + protected TwitterStream $stream; + + /** @var callable */ + protected $onTweet; + protected RuleBuilder $rule; + + public function __construct(string $bearerToken, string $apiKey, string $apiSecretKey) + { + $this->stream = new TwitterStream($bearerToken, $apiKey, $apiSecretKey); + $this->rule = RuleBuilder::create(); } - public function whenHears(string | array $listenFor, callable $whenHears): self + public static function create(string $bearerToken, string $apiKey, string $apiSecretKey): static { - if (! is_array($listenFor)) { - $listenFor = [$listenFor]; - } + return new self($bearerToken, $apiKey, $apiSecretKey); + } - $this->stream->setTrack($listenFor); + public function whenHears(string | array $listenFor, callable $whenHears): self + { + $this->rule->raw(array_reduce(Arr::wrap($listenFor), static function ($_, $listener) { + if (empty($_)) { + return $listener; + } - $this->stream->performOnStreamActivity($whenHears); + return $_ . ' OR ' . $listener; + })); + $this->onTweet = $whenHears; return $this; } public function whenFrom(array $boundingBoxes, callable $whenFrom): self { - $this->stream->setLocations($boundingBoxes); - - $this->stream->performOnStreamActivity($whenFrom); + $this->rule->boundingBox($boundingBoxes); + $this->onTweet = $whenFrom; return $this; } public function whenTweets(string | array $twitterUserIds, callable $whenTweets): self { - if (! is_array($twitterUserIds)) { - $twitterUserIds = [$twitterUserIds]; - } - - $this->stream->setFollow($twitterUserIds); - - $this->stream->performOnStreamActivity($whenTweets); + $this->rule->from($twitterUserIds); + $this->onTweet = $whenTweets; return $this; } public function setLocale(string $lang): self { - $this->stream->setLang($lang); + $this->rule->locale($lang); return $this; } - public function checkFilterPredicates(callable $checkFilterPredicates): self + public function startListening(Sets $sets = null): void { - $this->stream->setCheckFilterPredicates($checkFilterPredicates); + // Delete old rules + Rule::deleteBulk(...Rule::all()); + $this->rule->save(); - return $this; + foreach ($this->stream->filteredTweets($sets) as $tweet) { + if (is_null($tweet)) { + continue; + } + + call_user_func($this->onTweet, $tweet); + } } } diff --git a/src/UserStream.php b/src/UserStream.php index 87260d1..592d11f 100644 --- a/src/UserStream.php +++ b/src/UserStream.php @@ -2,29 +2,46 @@ namespace Spatie\TwitterStreamingApi; -use Phirehose; +use RWC\TwitterStream\Rule; +use RWC\TwitterStream\RuleBuilder; +use RWC\TwitterStream\Sets; +use RWC\TwitterStream\TwitterStream; -class UserStream extends BaseStream +class UserStream { - public function __construct( - string $accessToken, - string $accessSecret, - string $consumerKey, - string $consumerSecret - ) { - $this->stream = $this->createStream( - $accessToken, - $accessSecret, - $consumerKey, - $consumerSecret, - Phirehose::METHOD_USER - ); + protected string $handle; + protected TwitterStream $stream; + /** @var callable */ + protected $onTweet; + + public function __construct(string $handle, string $bearerToken, string $apiKey, string $apiSecretKey) + { + $this->handle = $handle; + $this->stream = new TwitterStream($bearerToken, $apiKey, $apiSecretKey); + } + + public static function create(string $handle, string $bearerToken, string $apiKey, string $apiSecretKey): static + { + return new self($handle, $bearerToken, $apiKey, $apiSecretKey); } - public function onEvent(callable $onEvent): self + public function onEvent(callable $onTweet): static { - $this->stream->performOnStreamActivity($onEvent); + $this->onTweet = $onTweet; return $this; } + + public function startListening(Sets $sets = null): void + { + // Delete old rules + Rule::deleteBulk(...Rule::all()); + RuleBuilder::create() + ->from($this->handle) + ->save(); + + foreach ($this->stream->filteredTweets($sets) as $tweet) { + call_user_func($this->onTweet, $tweet); + } + } }