Skip to main content

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: Tweet = ttpy.tweets.write("Look mom, I'm using Twitter API!")

# See the uploaded tweet! :)
print(tweet)

Posting a tweet with media

from tweetipy import Tweetipy
from tweetipy.types import Media

# Initialize client
ttpy = Tweetipy(
    'YOUR_TWITTER_API_KEY',
    'YOUR_TWITTER_API_KEY_SECRET')

# Read some picture and upload the bytes to Twitter
pic_bytes = open('path/to/pic.jpeg', 'rb').read()
uploaded_media = ttpy.media.upload(pic_bytes, media_type="image/jpeg")

# Post media tweet to Twitter
tweet = ttpy.tweets.write(
    "This tweet contains some media!",
    media=Media([uploaded_media.media_id_string]))

# See the uploaded media tweet! :)
print(tweet)

Searching tweets

from tweetipy import Tweetipy

# Initialize client
ttpy = Tweetipy(
    'YOUR_TWITTER_API_KEY',
    'YOUR_TWITTER_API_KEY_SECRET')

# Find tweets containing some keywords
search_results = ttpy.tweets.search(query='space separated keywords')

# See the results
print(search_results)

Doing advanced searches - Single condition

from tweetipy import Tweetipy
from tweetipy.helpers import QueryBuilder

# Initialize client
ttpy = Tweetipy(
    'YOUR_TWITTER_API_KEY',
    'YOUR_TWITTER_API_KEY_SECRET')

# Initialize the query builder
t = QueryBuilder()

# Find tweets containing some keywords
search_results = ttpy.tweets.search(
    query=t.from_user('Randogs8'),
    sort_order='recency'
)

# See the results
print(search_results)

Doing advanced searches - Multiple conditions (AND)

from tweetipy import Tweetipy
from tweetipy.helpers import QueryBuilder

# Initialize client
ttpy = Tweetipy(
    'YOUR_TWITTER_API_KEY',
    'YOUR_TWITTER_API_KEY_SECRET')

# Initialize the query builder
t = QueryBuilder()

# Find tweets containing some keywords
search_results = ttpy.tweets.search(
    query=t.with_all_keywords(['dogs', 'love']) & t.has.media,
    sort_order='recency'
)

# See the results
print(search_results)

Doing advanced searches - Multiple conditions (OR)

from tweetipy import Tweetipy
from tweetipy.helpers import QueryBuilder

# Initialize client
ttpy = Tweetipy(
    'YOUR_TWITTER_API_KEY',
    'YOUR_TWITTER_API_KEY_SECRET')

# Initialize the query builder
t = QueryBuilder()

# Find tweets containing some keywords
search_results = ttpy.tweets.search(
    query=t.from_user('Randogs8') | t.from_user('cooldogfacts'),
    sort_order='recency'
)

# See the results
print(search_results)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tweetipy-0.1.7.tar.gz (14.6 kB view hashes)

Uploaded Source

Built Distribution

tweetipy-0.1.7-py3-none-any.whl (19.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page