A simple "type hinted" Python client for interacting with Twitter's API.
Project description
Tweetipy
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
Posting a tweet
from tweetipy import Tweetipy
# Initialize client
ttpy = Tweetipy(
'YOUR_TWITTER_API_KEY',
'YOUR_TWITTER_API_KEY_SECRET')
# Post tweet to Twitter
tweet = ttpy.tweets.write("I'm using Twitter API!")
# See the uploaded tweet! :)
print(tweet)
Posting a tweet with media
from tweetipy import Tweetipy
from tweetipy.types import MediaToUpload
ttpy = Tweetipy(
'YOUR_TWITTER_API_KEY',
'YOUR_TWITTER_API_KEY_SECRET')
# First upload the media to Twitter.
with open('dog.jpeg', 'rb') as pic:
uploaded_media = ttpy.media.upload(
media_bytes=pic.read(),
media_type="image/jpeg")
# Then post a tweet, adding the media_id as a parameter.
ttpy.tweets.write(
"This tweet contains some media.",
media=MediaToUpload([uploaded_media.media_id_string]))
Searching tweets
from tweetipy import Tweetipy
# Initialize the client
ttpy = Tweetipy(
'YOUR_TWITTER_API_KEY',
'YOUR_TWITTER_API_KEY_SECRET')
# Treat the 'query' argument as you would a search box.
search_results = ttpy.tweets.search(query='space separated keywords')
# See results 🤩
for tweet in search_results:
print(tweet)
Doing advanced searches - Single condition
from tweetipy import Tweetipy
from tweetipy.helpers import QueryBuilder
# Initialize the client
ttpy = Tweetipy(
'YOUR_TWITTER_API_KEY',
'YOUR_TWITTER_API_KEY_SECRET')
# The query builder is your friend :)
t = QueryBuilder()
# Define the search criteria using the query builder.
search_results = ttpy.tweets.search(
query=t.from_user('Randogs8'),
sort_order='recency'
)
# See results 🤩
for tweet in search_results:
print(tweet)
Doing advanced searches - Multiple conditions (AND)
from tweetipy import Tweetipy
from tweetipy.helpers import QueryBuilder
# Initialize the client
ttpy = Tweetipy(
'YOUR_TWITTER_API_KEY',
'YOUR_TWITTER_API_KEY_SECRET')
# The query builder is your friend :)
t = QueryBuilder()
# Use the 'and' operator (&) to define alternative criteria.
# The query builder will do some background work for you so this works as
# expected. 😎
search_results = ttpy.tweets.search(
query=t.with_all_keywords(['dogs', 'love']) & t.has.media,
sort_order='relevancy'
)
# See the results 🤩
for tweet in search_results:
print(tweet)
Doing advanced searches - Multiple conditions (OR)
from tweetipy import Tweetipy
from tweetipy.helpers import QueryBuilder
# Initialize the client
ttpy = Tweetipy(
'YOUR_TWITTER_API_KEY',
'YOUR_TWITTER_API_KEY_SECRET')
# The query builder is your friend :)
t = QueryBuilder()
# Use the pipe operator (|) to define alternative criteria.
# The query builder will do some background work for you so this works as
# expected. 😎
search_results = ttpy.tweets.search(
query=t.from_user('Randogs8') | t.from_user('cooldogfacts'),
sort_order='recency'
)
# See the results 🤩
for tweet in search_results:
print(tweet)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file tweetipy-0.1.10.tar.gz
.
File metadata
- Download URL: tweetipy-0.1.10.tar.gz
- Upload date:
- Size: 22.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.1 CPython/3.10.2 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8a6aa2bad9e440cc1dfe5301af2fd84fa68b8d29a802db08224412ee1a7bb7a3 |
|
MD5 | aa379ba82d8203b8502d8ea77c31ba2a |
|
BLAKE2b-256 | a605b71ba4e1ace80074e93368d6eee17eafb1c69545f715864f47e01d5d9218 |
File details
Details for the file tweetipy-0.1.10-py3-none-any.whl
.
File metadata
- Download URL: tweetipy-0.1.10-py3-none-any.whl
- Upload date:
- Size: 35.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.1 CPython/3.10.2 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a63dc5415b83df8731eef3487ee565ef648759ccd9a729df4fd3ae2340df11ad |
|
MD5 | ed9130ecb17105b90ddddec327876c6e |
|
BLAKE2b-256 | cc696757e3fdc480c2af127756e21145ebae0302569227e996d1ca415f4d03e0 |