A simple type hinted Python client for interacting with Twitter's API.
pip -m install tweetipy
To use it, setup a developer account under developer.twitter.com.
After that, create an app from the developer dashboard and generate the needed tokens ("API Key and Secret").
Please note that the library does not yet implement the full Twitter API, but rather only some endpoints that are interesting for my projects. Also, although it is already working, please be aware that this library is still in early development phase and thus breaking changes might occur. In other words, don't rely on it for production just yet.
In any case, feel free to use it for your own projects. Do create issues if anything weird pops up. Pull requests and feature requests are welcome!
Examples
from tweetipy import Tweetipy
ttpy = Tweetipy(
'YOUR_TWITTER_API_KEY',
'YOUR_TWITTER_API_KEY_SECRET')
tweet = ttpy.tweets.write("I'm using Twitter API!")
print(tweet)
from tweetipy import Tweetipy
from tweetipy.types import MediaToUpload
ttpy = Tweetipy(
'YOUR_TWITTER_API_KEY',
'YOUR_TWITTER_API_KEY_SECRET')
with open('dog.jpeg', 'rb') as pic:
uploaded_media = ttpy.media.upload(
media_bytes=pic.read(),
media_type="image/jpeg")
ttpy.tweets.write(
"This tweet contains some media.",
media=MediaToUpload([uploaded_media.media_id_string]))
from tweetipy import Tweetipy
ttpy = Tweetipy(
'YOUR_TWITTER_API_KEY',
'YOUR_TWITTER_API_KEY_SECRET')
search_results = ttpy.tweets.search(query='space separated keywords')
for tweet in search_results:
print(tweet)
Doing advanced searches - Single condition
from tweetipy import Tweetipy
from tweetipy.helpers import QueryBuilder
ttpy = Tweetipy(
'YOUR_TWITTER_API_KEY',
'YOUR_TWITTER_API_KEY_SECRET')
t = QueryBuilder()
search_results = ttpy.tweets.search(
query=t.from_user('Randogs8'),
sort_order='recency'
)
for tweet in search_results:
print(tweet)
Doing advanced searches - Multiple conditions (AND)
from tweetipy import Tweetipy
from tweetipy.helpers import QueryBuilder
ttpy = Tweetipy(
'YOUR_TWITTER_API_KEY',
'YOUR_TWITTER_API_KEY_SECRET')
t = QueryBuilder()
search_results = ttpy.tweets.search(
query=t.with_all_keywords(['dogs', 'love']) & t.has.media,
sort_order='relevancy'
)
for tweet in search_results:
print(tweet)
Doing advanced searches - Multiple conditions (OR)
from tweetipy import Tweetipy
from tweetipy.helpers import QueryBuilder
ttpy = Tweetipy(
'YOUR_TWITTER_API_KEY',
'YOUR_TWITTER_API_KEY_SECRET')
t = QueryBuilder()
search_results = ttpy.tweets.search(
query=t.from_user('Randogs8') | t.from_user('cooldogfacts'),
sort_order='recency'
)
for tweet in search_results:
print(tweet)