• Stars
    star
    1,849
  • Rank 24,073 (Top 0.5 %)
  • Language
    Python
  • License
    MIT License
  • Created about 15 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Actively maintained, pure Python wrapper for the Twitter API. Supports both normal and streaming Twitter APIs.

Twython

Twython is a Python library providing an easy way to access Twitter data. Supports Python 3. It's been battle tested by companies, educational institutions and individuals alike. Try it today!

Note: As of Twython 3.7.0, there's a general call for maintainers put out. If you find the project useful and want to help out, reach out to Ryan with the info from the bottom of this README. Great open source project to get your feet wet with!

Features

  • Query data for:
    • User information
    • Twitter lists
    • Timelines
    • Direct Messages
    • and anything found in the docs
  • Image Uploading:
    • Update user status with an image
    • Change user avatar
    • Change user background image
    • Change user banner image
  • OAuth 2 Application Only (read-only) Support
  • Support for Twitter's Streaming API
  • Seamless Python 3 support!

Installation

Install Twython via pip:

$ pip install twython

If you're on a legacy project that needs Python 2.7 support, you can install the last version of Twython that supported 2.7:

pip install twython==3.7.0`

Or, if you want the code that is currently on GitHub:

git clone git://github.com/ryanmcgrath/twython.git
cd twython
python setup.py install

Documentation

Documentation is available at https://twython.readthedocs.io/en/latest/

Starting Out

First, you'll want to head over to https://apps.twitter.com and register an application!

After you register, grab your applications Consumer Key and Consumer Secret from the application details tab.

The most common type of authentication is Twitter user authentication using OAuth 1. If you're a web app planning to have users sign up with their Twitter account and interact with their timelines, updating their status, and stuff like that this is the authentication for you!

First, you'll want to import Twython

from twython import Twython

Obtain Authorization URL

Now, you'll want to create a Twython instance with your Consumer Key and Consumer Secret:

  • Only pass callback_url to get_authentication_tokens if your application is a Web Application
  • Desktop and Mobile Applications do not require a callback_url
APP_KEY = 'YOUR_APP_KEY'
APP_SECRET = 'YOUR_APP_SECRET'

twitter = Twython(APP_KEY, APP_SECRET)

auth = twitter.get_authentication_tokens(callback_url='http://mysite.com/callback')

From the auth variable, save the oauth_token and oauth_token_secret for later use (these are not the final auth tokens). In Django or other web frameworks, you might want to store it to a session variable

OAUTH_TOKEN = auth['oauth_token']
OAUTH_TOKEN_SECRET = auth['oauth_token_secret']

Send the user to the authentication url, you can obtain it by accessing

auth['auth_url']

Handling the Callback

If your application is a Desktop or Mobile Application oauth_verifier will be the PIN code

After they authorize your application to access some of their account details, they'll be redirected to the callback url you specified in get_authentication_tokens.

You'll want to extract the oauth_verifier from the url.

Django example:

oauth_verifier = request.GET['oauth_verifier']

Now that you have the oauth_verifier stored to a variable, you'll want to create a new instance of Twython and grab the final user tokens

twitter = Twython(
    APP_KEY, APP_SECRET,
    OAUTH_TOKEN, OAUTH_TOKEN_SECRET
)

final_step = twitter.get_authorized_tokens(oauth_verifier)

Once you have the final user tokens, store them in a database for later use::

    OAUTH_TOKEN = final_step['oauth_token']
    OAUTH_TOKEN_SECRET = final_step['oauth_token_secret']

For OAuth 2 (Application Only, read-only) authentication, see our documentation.

Dynamic Function Arguments

Keyword arguments to functions are mapped to the functions available for each endpoint in the Twitter API docs. Doing this allows us to be incredibly flexible in querying the Twitter API, so changes to the API aren't held up from you using them by this library.

Basic Usage

Function definitions (i.e. get_home_timeline()) can be found by reading over twython/endpoints.py

Create a Twython instance with your application keys and the users OAuth tokens

from twython import Twython
twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

Authenticated Users Home Timeline

twitter.get_home_timeline()

Updating Status

This method makes use of dynamic arguments, read more about them.

twitter.update_status(status='See how easy using Twython is!')

Advanced Usage

Questions, Comments, etc?

My hope is that Twython is so simple that you'd never have to ask any questions, but if you feel the need to contact me for this (or other) reasons, you can hit me up at [email protected].

Or if I'm to busy to answer, feel free to ping [email protected] as well.

Follow us on Twitter:

Want to help?

Twython is useful, but ultimately only as useful as the people using it (say that ten times fast!). If you'd like to help, write example code, contribute patches, document things on the wiki, tweet about it. Your help is always appreciated!

More Repositories

1

cacao

Rust bindings for AppKit (macOS) and UIKit (iOS/tvOS). Experimental, but working!
Rust
1,560
star
2

wrench-js

Recursive file operations in Node.js
JavaScript
438
star
3

alchemy

An experimental GUI framework for Rust, backed by per-platform native widgets. React, AppKit/UIKit inspired. EXPERIMENTAL, runs on Cocoa right now. ;P
Rust
385
star
4

wii-js

A sane, documented, (hopefully) performant event-based library for Wiimote webpage interaction.
JavaScript
154
star
5

jelly

User authentication/sessions/etc for Actix-Web. More of a sample project than a crate, but probably useful to some people.
Rust
89
star
6

twython-django

An example Django application to showcase how to use OAuth with Twitter in Django using Twython.
Python
73
star
7

1.1.1.1-macOS

Experimenting with cloning CloudFlare's 1.1.1.1 app as a macOS status bar app.
Swift
34
star
8

cloudkit-sane-sharing

A dump of code that illustrates a better way to share CloudKit resources.
Swift
29
star
9

react-iconpack

A React Component for handling SVG icons, coupled with Babel and Browserify plugins to only bundle the icons you use.
JavaScript
25
star
10

django-rednoise

An opinionated addon for WhiteNoise, with a focus on Django environments.
Python
18
star
11

pythentic_jobs

A pure Python wrapper around the Authentic Jobs (http://www.authenticjobs.com) API.
Python
18
star
12

svgalib-1

It's not svgalib "dash" 1, it's svgalib negative 1, because if you still use this library there's probably something wrong with you. That said, this is a 'fork' of the most recent (haha) version found on the internets, with a ton of patches from various people around the internet cobbled together. I have not and will not make any outlandish efforts to credit people, but if you see something here you wrote and you want credit, message me. Should fix a lot of compiling issues under recent issues of Linux.
C
16
star
13

webpack-babel-react-setup-lesson

Walking through setting up Webpack, Babel, and React.
JavaScript
15
star
14

memelee

An unofficial smash.gg app, read-only.
JavaScript
13
star
15

node-utf8

utf8 encoding and decoding in Node.js
JavaScript
12
star
16

drinkkitcom

A Foursquare clone written in Django to let Redditors broadcast DC bar crawls.
Python
11
star
17

gitstatus

A Github-repository widget to display most recent commits on a given repository.
JavaScript
8
star
18

jTransliterate

Transliterate [Hirag/Katak]ana to Latin/English and back with Python. Convert half/full-width Japanese text.
Python
7
star
19

holidaycalendar

A demo holiday calendar, which shows how to make NSCollectionViewItem's swipeable.
Swift
6
star
20

franz

Client side color swatches.
JavaScript
4
star
21

beyond-react

Session 4, going beyond React and integrating with other libraries.
JavaScript
3
star
22

activity-scraper

A social media scraper written in Rust. Used for the data on my personal site.
Rust
3
star
23

shinekit

iOS/macOS/Windows UI in Rust. Highly experimental.
Rust
3
star
24

rubeclosures

Ruby wrapper for the foreclosurelistings.com API
Ruby
2
star
25

splash

A programming language aimed at kids. Builds on their existing writing skills and doesn't try to reinvent arcane logic.
JavaScript
2
star
26

react-flux-redux-lesson

Walkthrough guide for React session #2 via Codementor.
JavaScript
2
star
27

react-router-lesson

React Router & co.
JavaScript
1
star
28

jsmag

Random JSMag Code Samplings
JavaScript
1
star
29

katakana

An Android application that aims to teach people Katakana through a basic brain-timing-calculation/algorithmic method.
Java
1
star
30

redditimages

Reddit: Images in Comments hack
JavaScript
1
star
31

takeoff

A Chrome extension to randomly load up one of your bookmarks on new windows/tabs, built for user "hokku". Enjoy.
1
star
32

smashgg-upcoming-tournaments

A scraper example for finding upcoming tournaments on smash.gg. From an unreleased side project, might be fun for some people.
Rust
1
star