Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Twitter API v2 compatibility #36

Merged
merged 1 commit into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,5 @@
# Ignore all test and documentation with "export-ignore".
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/phpunit.xml.dist export-ignore
/.scrutinizer.yml export-ignore
/tests export-ignore
File renamed without changes.
1 change: 0 additions & 1 deletion .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
github: spatie
11 changes: 11 additions & 0 deletions .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
blank_issues_enabled: false
contact_links:
- name: Ask a question
url: https://github.com/spatie/laravel-twitter-streaming-api/discussions/new?category=q-a
about: Ask the community for help
- name: Request a feature
url: https://github.com/spatie/laravel-twitter-streaming-api/discussions/new?category=ideas
about: Share ideas for new features
- name: Report a bug
url: https://github.com/spatie/laravel-twitter-streaming-api/issues/new
about: Report a reproducable bug
3 changes: 3 additions & 0 deletions .github/SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Security Policy

If you discover any security related issues, please email [email protected] instead of using the issue tracker.
11 changes: 2 additions & 9 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest]
php: [8.0 7.4]
laravel: [7.*, 8.*]
php: [8.0]
laravel: [8.*]
dependency-version: [prefer-lowest, prefer-stable]
include:
- laravel: 8.*
testbench: 6.*
- laravel: 7.*
testbench: 5.*

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }}

Expand All @@ -36,10 +34,5 @@ jobs:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick
coverage: none

- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest
- name: Execute tests
run: vendor/bin/phpunit
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
build
composer.lock
docs
vendor
.phpunit.result.cache
.php_cs.cache
.php-cs-fixer.cache
35 changes: 35 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

$finder = Symfony\Component\Finder\Finder::create()
->in([
__DIR__ . '/src',
__DIR__ . '/tests',
])
->name('*.php')
->notName('*.blade.php')
->ignoreDotFiles(true)
->ignoreVCS(true);

return (new PhpCsFixer\Config())
->setRules([
'@PSR2' => true,
'array_syntax' => ['syntax' => 'short'],
'ordered_imports' => ['sort_algorithm' => 'alpha'],
'no_unused_imports' => true,
'not_operator_with_successor_space' => true,
'trailing_comma_in_multiline' => true,
'phpdoc_scalar' => true,
'unary_operator_spaces' => true,
'binary_operator_spaces' => true,
'blank_line_before_statement' => [
'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'],
],
'phpdoc_single_line_var_spacing' => true,
'phpdoc_var_without_name' => true,
'method_argument_space' => [
'on_multiline' => 'ensure_fully_multiline',
'keep_multiple_spaces_after_comma' => true,
],
'single_trait_insert_per_statement' => true,
])
->setFinder($finder);
2 changes: 1 addition & 1 deletion .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -1 +1 @@
C:37:"PHPUnit\Runner\DefaultTestResultCache":234:{a:2:{s:7:"defects";a:0:{}s:5:"times";a:2:{s:80:"Spatie\LaravelTwitterStreamingApi\Test\FacadeTest::it_can_return_a_public_stream";d:0.067;s:78:"Spatie\LaravelTwitterStreamingApi\Test\FacadeTest::it_can_return_a_user_stream";d:0.003;}}}
C:37:"PHPUnit\Runner\DefaultTestResultCache":416:{a:2:{s:7:"defects";a:2:{s:80:"Spatie\LaravelTwitterStreamingApi\Test\FacadeTest::it_can_return_a_public_stream";i:4;s:78:"Spatie\LaravelTwitterStreamingApi\Test\FacadeTest::it_can_return_a_user_stream";i:4;}s:5:"times";a:2:{s:80:"Spatie\LaravelTwitterStreamingApi\Test\FacadeTest::it_can_return_a_public_stream";d:0.314;s:78:"Spatie\LaravelTwitterStreamingApi\Test\FacadeTest::it_can_return_a_user_stream";d:0.007;}}}
73 changes: 35 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
![Test Status](https://img.shields.io/github/workflow/status/spatie/laravel-twitter-streaming-api/run-tests?label=tests&style=flat-square)
[![Total Downloads](https://img.shields.io/packagist/dt/spatie/laravel-twitter-streaming-api.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-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.

```php
TwitterStreamingApi::publicStream()
Expand All @@ -15,15 +17,21 @@ TwitterStreamingApi::publicStream()
->startListening();
```

Here's [an example Laravel application](https://github.com/spatie/laravel-twitter-streaming-api-example-app) with the package pre-installed. It contains [an artisan command](https://github.com/spatie/laravel-twitter-streaming-api-example-app/blob/master/app/Console/Commands/ListenForHashTags.php) to kick off the listening process.
Here's [an example Laravel application](https://github.com/spatie/laravel-twitter-streaming-api-example-app) with the
package pre-installed. It
contains [an artisan command](https://github.com/spatie/laravel-twitter-streaming-api-example-app/blob/master/app/Console/Commands/ListenForHashTags.php)
to kick off the listening process.

## Support us

[<img src="https://github-ads.s3.eu-central-1.amazonaws.com/laravel-twitter-streaming-api.jpg?t=1" width="419px" />](https://spatie.be/github-ad-click/laravel-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).

## Installation

Expand All @@ -33,26 +41,6 @@ You can install the package via composer:
composer require spatie/laravel-twitter-streaming-api
```

You must install this service provider.

```php
// config/app.php
'providers' => [
...
Spatie\LaravelTwitterStreamingApi\TwitterStreamingApiServiceProvider::class,
];
```

This package also comes with a facade, which provides an easy way to call the class.

```php
// config/app.php
'aliases' => [
...
'TwitterStreamingApi' => Spatie\LaravelTwitterStreamingApi\TwitterStreamingApiFacade::class,
];
```

The config file must be published with this command:

```bash
Expand All @@ -64,33 +52,37 @@ It will be published in `config/laravel-twitter-streaming-api.php`
```php
return [

/**
/*
* To work with Twitter's Streaming API you'll need some credentials.
*
* If you don't have credentials yet, head over to https://apps.twitter.com/
* If you don't have credentials yet, head over to https://developers.twitter.com/
*/

'access_token' => env('TWITTER_ACCESS_TOKEN'),
'handle' => env('TWITTER_HANDLE'),

'access_token_secret' => env('TWITTER_ACCESS_TOKEN_SECRET'),
'api_key' => env('TWITTER_API_KEY'),

'consumer_key' => env('TWITTER_CONSUMER_KEY'),
'api_secret_key' => env('TWITTER_API_SECRET_KEY'),

'consumer_secret' => env('TWITTER_CONSUMER_SECRET'),
'bearer_token' => env('TWITTER_BEARER_TOKEN'),
];
```

## 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://developers.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`.
Once you've created your application, click on the `Keys and tokens` tab to retrieve your `bearer_token`, `api_key`
and `api_secret_key`.

![Keys and access tokens tab on Twitter](https://spatie.github.io/twitter-streaming-api/images/twitter.jpg)
![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.

In the example below a facade is used. If you don't like facades you can replace them with

Expand All @@ -102,7 +94,8 @@ app(Spatie\LaravelTwitterStreamingApi\TwitterStreamingApi::class)

The public stream can be used to listen for specific words that are being tweeted.

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
use TwitterStreamingApi;
Expand Down Expand Up @@ -130,11 +123,15 @@ TwitterStreamingApi::userStream()

## Suggestion on how to run in a production environment

When using this in production you could opt to create [an artisan command](https://github.com/spatie/laravel-twitter-streaming-api-example-app/blob/8175995/app/Console/Commands/ListenForHashTags.php) to listen for incoming events from Twitter. You can use [Supervisord](http://supervisord.org/) to make sure that command is running all the time.
When using this in production you could opt to
create [an artisan command](https://github.com/spatie/laravel-twitter-streaming-api-example-app/blob/8175995/app/Console/Commands/ListenForHashTags.php)
to listen for incoming events from Twitter. You can use [Supervisord](http://supervisord.org/) to make sure that command
is running all the time.

## 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.

## Changelog

Expand All @@ -148,7 +145,7 @@ $ composer test

## Contributing

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.

## Security

Expand Down
16 changes: 9 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
}
],
"require": {
"php": "^8.0|^7.3",
"illuminate/support": "^6.0|^7.0|^8.0",
"spatie/twitter-streaming-api": "^1.0.1"
"php": "^8.0",
"illuminate/support": "^8.0",
"spatie/twitter-streaming-api": "^2.0.0"
},
"require-dev": {
"phpunit/phpunit": "^8.0|^9.0",
"orchestra/testbench": "^4.0|^5.0|^6.0"
"friendsofphp/php-cs-fixer": "^3.0",
"orchestra/testbench": "^6.0",
"phpunit/phpunit": "^9.0"
},
"autoload": {
"psr-4": {
Expand All @@ -35,8 +36,9 @@
}
},
"scripts": {
"test": "vendor/bin/phpunit"
},
"lint": "./vendor/bin/php-cs-fixer fix",
"test": "./vendor/bin/phpunit",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage" },
"minimum-stability": "dev",
"prefer-stable": true,
"config": {
Expand Down
10 changes: 5 additions & 5 deletions config/laravel-twitter-streaming-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
/*
* To work with Twitter's Streaming API you'll need some credentials.
*
* If you don't have credentials yet, head over to https://apps.twitter.com/
* If you don't have credentials yet, head over to https://developers.twitter.com/
*/

'access_token' => env('TWITTER_ACCESS_TOKEN'),
'handle' => env('TWITTER_HANDLE'),

'access_token_secret' => env('TWITTER_ACCESS_TOKEN_SECRET'),
'api_key' => env('TWITTER_API_KEY'),

'consumer_key' => env('TWITTER_CONSUMER_KEY'),
'api_secret_key' => env('TWITTER_API_SECRET_KEY'),

'consumer_secret' => env('TWITTER_CONSUMER_SECRET'),
'bearer_token' => env('TWITTER_BEARER_TOKEN'),
];
Binary file added docs/tokens.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 9 additions & 10 deletions src/TwitterStreamingApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace Spatie\LaravelTwitterStreamingApi;

use Spatie\TwitterStreamingApi\UserStream;
use Illuminate\Contracts\Config\Repository;
use Spatie\TwitterStreamingApi\PublicStream;
use Spatie\TwitterStreamingApi\UserStream;

class TwitterStreamingApi
{
/** @var array */
protected $config;
protected array $config;

public function __construct(Repository $config)
{
Expand All @@ -19,20 +19,19 @@ public function __construct(Repository $config)
public function publicStream(): PublicStream
{
return new PublicStream(
$this->config['access_token'],
$this->config['access_token_secret'],
$this->config['consumer_key'],
$this->config['consumer_secret']
$this->config['api_key'],
$this->config['api_secret_key'],
$this->config['bearer_token']
);
}

public function userStream(): UserStream
{
return new UserStream(
$this->config['access_token'],
$this->config['access_token_secret'],
$this->config['consumer_key'],
$this->config['consumer_secret']
$this->config['handle'],
$this->config['api_key'],
$this->config['api_secret_key'],
$this->config['bearer_token']
);
}
}
7 changes: 1 addition & 6 deletions src/TwitterStreamingApiFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@
*/
class TwitterStreamingApiFacade extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
protected static function getFacadeAccessor(): string
{
return 'laravel-twitter-streaming-api';
}
Expand Down
6 changes: 0 additions & 6 deletions src/TwitterStreamingApiServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

class TwitterStreamingApiServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*/
public function boot()
{
if ($this->app->runningInConsole()) {
Expand All @@ -18,9 +15,6 @@ public function boot()
}
}

/**
* Register the application services.
*/
public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/laravel-twitter-streaming-api.php', 'laravel-twitter-streaming-api');
Expand Down
4 changes: 2 additions & 2 deletions tests/FacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Spatie\LaravelTwitterStreamingApi\Test;

use TwitterStreamingApi;
use Spatie\TwitterStreamingApi\UserStream;
use Spatie\TwitterStreamingApi\PublicStream;
use Spatie\TwitterStreamingApi\UserStream;
use TwitterStreamingApi;

class FacadeTest extends TestCase
{
Expand Down
Loading