DEV Community

Saadman Rafat
Saadman Rafat

Posted on • Edited on

Streaming With Twitter-Stream.py

Twitter-Stream.py 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, twitter-stream.py 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 twitter-stream.py handles SampledStream.

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

Enter fullscreen mode Exit fullscreen mode
1. import json
2. from twitter_stream import SampledStream
3.
4. class Stream(SampledStream):
5.   user_fields =  ['name', 'location', 'public_metrics']
6.   expansions =   ['author_id']
7.   tweet_fields = ['created_at']
8.
9. stream = Stream()
10.
11.for tweet in stream.connect():
12.    print(json.dumps(tweet, indent=4, sort_keys=True))
13.
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 README.md page and our documentations twitivity.dev. We will continue to maintain the v1.1 Accounts Activity API Client while keep supporting and releasing new tools that help the community.


docs : twitivity.dev/docs
github : https://github.com/twitivity/twitter-stream.py
mail : mail@twitivity.dev
twitter: @twitivitydev
report an issue: Issues

Top comments (2)

Collapse
 
jessicagarson profile image
Jessica Garson

This is so cool!

Collapse
 
saadmanrafat profile image
Saadman Rafat

Thanks!