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

Support twitter API v2 #337

Merged
merged 15 commits into from
Apr 13, 2021
Merged

Support twitter API v2 #337

merged 15 commits into from
Apr 13, 2021

Conversation

reliq
Copy link
Collaborator

@reliq reliq commented Mar 27, 2021

This adds support for Twitter API v2.

Highlights:

  • Easily switch between v2 endpoints and v1 endpoints by way of config.
  • Automatic use of oAuth1 or oAuth2 based on endpoint.

Todo:

  • oAuth2 support
  • tests
  • readme update

Supported v2 endpoints:

  • Tweet lookup
  • Timelines
  • Search Tweets
  • Hide replies
  • User lookup
  • Follows
  • Filtered Stream
  • Sampled stream

Try it out

composer require atymic/twitter:dev-twitter-api-v2

References

@codecov

This comment has been minimized.

@atymic
Copy link
Owner

atymic commented Mar 27, 2021

Quick comment, on my phone so can't really review the code, but I think we'd need to support 2.x as well as 1.x as there's things that only exist in either, but not both.

@reliq
Copy link
Collaborator Author

reliq commented Mar 27, 2021

Quick comment, on my phone so can't really review the code, but I think we'd need to support 2.x as well as 1.x as there's things that only exist in either, but not both.

Agree. v1 support will remain

@reliq reliq marked this pull request as draft March 28, 2021 01:58
@xward1
Copy link

xward1 commented Mar 28, 2021

Hi guys. I'm really glad you are fast-tracking this. I was planning to write a simple wrapper for the v2 API in native PHP, but if you guys can get this done faster (looks like you will) I'll gladly use this package instead.

One bit of feedback (well, more of a question). After looking through this PR I noticed you're not using async/event-driven methods in the streaming API classes, and I was wondering if/how you're planning to implement that functionality. I ask for two reasons:

  1. Because the real strength in the streaming aspects of v2 is the ability to connect and receive data immediately without having to poll all the time.
  2. It would satisfy my main requirement to actively listen for time-sensitive tweets from multiple accounts containing a number of v2 properties. I figure the time-sensitivity requirement means it has to be done using a live stream.

I realize some consumers of a widely-used Laravel package likely won't have the required PHP extensions installed to make something like ReactPHP work. Plus it's a Laravel package, which means it'll be tricky to make that play nicely in the framework unless you're comfortable using exec();. But if it's all the same to you, I would love it if you worked async functionality into this package, maybe as the primary connection to those endpoints but with a fallback.

Please let me know your thoughts. Thanks.

@reliq
Copy link
Collaborator Author

reliq commented Mar 28, 2021

Hello @xward1, thanks for your feedback.

Regarding streaming. I am still looking into whether we can actually have proper support for these endpoints this package. I'll consider your suggestion of ReactPHP.

I'm also open to any other suggestions/ideas.

@xward1
Copy link

xward1 commented Mar 28, 2021

Take a look at this older project by Spatie. He's connecting to the Twitter Labs streaming API endpoints, which is essentially what is now the v2 API (at least in most respects). I would use that project for ideas about how to deal with the rules as well.

@xward1
Copy link

xward1 commented Mar 28, 2021

Yeah, the reason we don't see too many PHP packages in the wild for the Twitter API, and others like the Discord API, is because the streaming aspects of those modern APIs would be handled much better by Node.js or Python. This is because of their native ability to perform asynchronously, whereas with PHP you'd need to install a language-level extension to handle it. There are a few PHP event-driven extensions that are considered stable: pcntl, uv, ev, event. But these are PECL extensions that won't be present in most legacy PHP hosted server environments. Modern deployment methods using Docker and CI/CD make this much easier to deal with, so maybe it's a non-issue these days, but on most traditional hosting environments this might be tricky.

That's really the only issue I see with implementing this in PHP. Laravel, however, might add another set of challenges, but I'm not sure about that TBH. We'd need to look into it further. For example, the PHP packages I've seen for the Discord API all need to be run at the command line, so I don't see any solution for the new Twitter API being any different in this regard.

@xward1
Copy link

xward1 commented Mar 28, 2021

This article says one could use queues in Laravel to effectively address this, but again, I'm not sure that applies to something like a live stream.

And here is the follow-up convo for this article on Reddit.

@xward1
Copy link

xward1 commented Mar 28, 2021

https://github.com/vuongxuongminh/laravel-async

https://dev.to/webong/using-asynchronous-processes-in-php-7io

https://github.com/spatie/async

Also, looks like Laravel Octane could be the ticket.

- fix php-di service container
- drop 'Configuration::createFromDefaults()'
@reliq
Copy link
Collaborator Author

reliq commented Mar 31, 2021

Thanks for your help on this @xward1. I've implemented a solution after looking at all those references you linked.

Example usage can be found here:

Feel free to check it out and let me know your thoughts.

@reliq reliq changed the title WIP: Support twitter API v2 Support twitter API v2 Apr 3, 2021
@reliq reliq marked this pull request as ready for review April 3, 2021 18:56
README.md Outdated Show resolved Hide resolved
README.md Show resolved Hide resolved
@atymic
Copy link
Owner

atymic commented Apr 11, 2021

Excellent work mate, this is awesome!

@xward1
Copy link

xward1 commented Apr 12, 2021 via email

@reliq
Copy link
Collaborator Author

reliq commented Apr 12, 2021

Hi. Apologies for the late response. Just had major surgery and my priorities have been offline. I briefly looked at your links, but it’s been quite a while since you sent them, so I won’t bother with feedback. Thanks for whipping this package into fighting shape! I plan to use it very soon on a project.

________________________________ From: Reliq @.> Sent: Wednesday, March 31, 2021 4:48:34 PM To: atymic/twitter @.> Cc: xward @.>; Mention @.> Subject: Re: [atymic/twitter] WIP: Support twitter API v2 (#337) Thanks for your help on this @xward1https://github.com/xward1. I've implemented a solution after looking at all those references you linked. Example usage can be found here: * Laravel: https://github.com/reliq/laravel-twitter-playground/blob/main/app/Console/Commands/Stream.php * without Laravel: https://github.com/reliq/php-di-twitter-playground/blob/main/src/SuperBlog/Command/StreamCommand.php Feel free to check it out and let me know your thoughts. — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub<#337 (comment)>, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ABT6O5FHAD5GSLDNQTXOV3DTGOKDFANCNFSM4Z4OBP2A.

No worries @xward1. Thank you for your help.

I wish you a speedy recovery! 🚤 💨

@atymic atymic merged commit d11436c into main Apr 13, 2021
@atymic atymic deleted the twitter-api-v2 branch April 13, 2021 02:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants