DEV Community

Saadman Rafat
Saadman Rafat

Posted on • Updated on

Streaming With a python API client for Twitter API v2 now supports
FilteredStream, SampledStream, RecentSearch, TweetLookUp, and UserLookUp. It makes it easier to get started with Twitter's New API.

If you are following #Twitter here, you probably already know about Sampled Stream. But armed with Twitter's next-generation API, makes streaming a lot more seamless.

Sampled Stream delivers about 1% of Twitter's publicly available tweets in real-time and paints a picture of general sentiments, recent trends, and global events. So let's see an example of how handles SampledStream.

~$ mkdir twitterStream && cd twitterStream
~$ pipenv install # or use pip3
~$ pipenv shell
(twitter-stream) ~$ export $BEARER_TOKEN=BEARER TOKEN
(twitter-stream) ~$ touch
(twitter-stream) ~$ tree
├── Pipfile
└── Pipfile.lock
0 directories, 3 files

Enter fullscreen mode Exit fullscreen mode
1. import json
2. from twitter_stream import SampledStream
4. class Stream(SampledStream):
5.   user_fields =  ['name', 'location', 'public_metrics']
6.   expansions =   ['author_id']
7.   tweet_fields = ['created_at']
9. stream = Stream()
11.for tweet in stream.connect():
12.    print(json.dumps(tweet, indent=4, sort_keys=True))
Enter fullscreen mode Exit fullscreen mode

Is this all you have to do to start streaming? Yes. Are these all the data points available to you? No. Let's discuss line number 5-7. Twitter's Official Documentation lists an elaborate set of query parameters. You can use these queries to get the data you need. We are subclassing SampledStream and carefully constructing clear and eloquent queries in line 5-7. And you can do this for all the query parameters listed in the SampledStream API Reference.

To get more insights into other API endpoints. Please Visit our page and our documentations We will continue to maintain the v1.1 Accounts Activity API Client while keep supporting and releasing new tools that help the community.

docs :
github :
mail :
twitter: @twitivitydev
report an issue: Issues

Top comments (2)

jessicagarson profile image
Jessica Garson

This is so cool!

saadmanrafat profile image
Saadman Rafat