Welcome, guest | Sign In | My Account | Store | Cart

Notice! PyPM is being replaced with the ActiveState Platform, which enhances PyPM’s build and deploy capabilities. Create your free Platform account to download ActivePython or customize Python with the packages you require and get automatic updates.

Download
ActivePython
INSTALL>
pypm install twitter

How to install twitter

  1. Download and install ActivePython
  2. Open Command Prompt
  3. Type pypm install twitter
 Python 2.7Python 3.2Python 3.3
Windows (32-bit)
1.8.0
1.10.0Never BuiltWhy not?
1.8.0 Available View build log
1.7.2 Available View build log
1.7.1 Available View build log
1.6.1 Available View build log
1.6 Available View build log
1.5.2 Available View build log
1.5.1 Available View build log
1.5 Available View build log
1.4.2 Available View build log
1.8.0
1.10.0Never BuiltWhy not?
1.8.0 Available View build log
1.7.2 Available View build log
1.7.1 Available View build log
1.6.1 Available View build log
1.6 Available View build log
Windows (64-bit)
1.8.0
1.10.0Never BuiltWhy not?
1.8.0 Available View build log
1.7.2 Available View build log
1.7.1 Available View build log
1.6.1 Available View build log
1.6 Available View build log
1.5.2 Available View build log
1.5.1 Available View build log
1.5 Available View build log
1.4.2 Available View build log
1.8.0
1.10.0Never BuiltWhy not?
1.8.0 Available View build log
1.7.2 Available View build log
1.7.1 Available View build log
1.6.1 Available View build log
1.6 Available View build log
Mac OS X (10.5+)
1.8.0
1.10.0Never BuiltWhy not?
1.8.0 Available View build log
1.7.2 Available View build log
1.7.1 Available View build log
1.6.1 Available View build log
1.6 Available View build log
1.5.2 Available View build log
1.5.1 Available View build log
1.5 Available View build log
1.4.2 Available View build log
1.9.0
1.10.0Never BuiltWhy not?
1.9.0 Available View build log
1.8.0 Available View build log
1.7.2 Available View build log
1.7.1 Available View build log
1.6.1 Available View build log
1.6 Available View build log
Linux (32-bit)
1.9.0
1.10.0Never BuiltWhy not?
1.9.0 Available View build log
1.8.0 Available View build log
1.7.2 Available View build log
1.7.1 Available View build log
1.6.1 Available View build log
1.6 Available View build log
1.5.2 Available View build log
1.5.1 Available View build log
1.5 Available View build log
1.4.2 Available View build log
1.9.0
1.10.0Never BuiltWhy not?
1.9.0 Available View build log
1.8.0 Available View build log
1.7.2 Available View build log
1.7.1 Available View build log
1.6.1 Available View build log
1.6 Available View build log
Linux (64-bit)
1.10.0 Available View build log
1.9.1 Available View build log
1.9.0 Available View build log
1.8.0 Available View build log
1.7.2 Available View build log
1.7.1 Available View build log
1.6.1 Available View build log
1.6 Available View build log
1.5.2 Available View build log
1.5.1 Available View build log
1.5 Available View build log
1.4.2 Available View build log
1.9.0
1.10.0Never BuiltWhy not?
1.9.0 Available View build log
1.8.0 Available View build log
1.7.2 Available View build log
1.7.1 Available View build log
1.6.1 Available View build log
1.6 Available View build log
1.10.0 Available View build log
1.9.1 Available View build log
 
Author
License
MIT License
Imports
Lastest release
version 1.10.0 on Sep 20th, 2013

Python Twitter Tools

The Minimalist Twitter API for Python is a Python API for Twitter, everyone's favorite Web 2.0 Facebook-style status updater for people on the go.

Also included is a twitter command-line tool for getting your friends' tweets and setting your own tweet from the safety and security of your favorite shell and an IRC bot that can announce Twitter updates to an IRC channel.

For more information, after installing the twitter package:

  • import the twitter package and run help() on it
  • run twitter -h for command-line tool help
twitter - The Command-Line Tool

The command-line tool lets you do some awesome things:

  • view your tweets, recent replies, and tweets in lists
  • view the public timeline
  • follow and unfollow (leave) friends
  • various output formats for tweet information

The bottom line: type twitter, receive tweets.

twitterbot - The IRC Bot

The IRC bot is associated with a twitter account (either your own account or an account you create for the bot). The bot announces all tweets from friends it is following. It can be made to follow or leave friends through IRC /msg commands.

twitter-log

twitter-log is a simple command-line tool that dumps all public tweets from a given user in a simple text format. It is useful to get a complete offsite backup of all your tweets. Run twitter-log and read the instructions.

twitter-archiver and twitter-follow

twitter-archiver will log all the tweets posted by any user since they started posting. twitter-follow will print a list of all of all the followers of a user (or all the users that user follows).

Programming with the Twitter api classes

The Twitter and TwitterStream classes are the key to building your own Twitter-enabled applications.

The Twitter class

The minimalist yet fully featured Twitter API class.

Get RESTful data by accessing members of this class. The result is decoded python objects (lists and dicts).

The Twitter API is documented at:

[http://dev.twitter.com/doc](http://dev.twitter.com/doc)

Examples:

```python

System Message: ERROR/3 (<string>, line 82)

Inconsistent literal block quoting.

from twitter import *

# see "Authentication" section below for tokens and keys t = Twitter(

System Message: ERROR/3 (<string>, line 86)

Unexpected indentation.
auth=OAuth(OAUTH_TOKEN, OAUTH_SECRET,
CONSUMER_KEY, CONSUMER_SECRET)

System Message: WARNING/2 (<string>, line 88)

Block quote ends without a blank line; unexpected unindent.

)

# Get your "home" timeline t.statuses.home_timeline()

# Get a particular friend's timeline t.statuses.friends_timeline(id="billybob")

# Also supported (but totally weird) t.statuses.friends_timeline.billybob()

# Update your status t.statuses.update(

System Message: ERROR/3 (<string>, line 101)

Unexpected indentation.
status="Using @sixohsix's sweet Python Twitter Tools.")

# Send a direct message t.direct_messages.new(

System Message: ERROR/3 (<string>, line 105)

Unexpected indentation.
user="billybob", text="I think yer swell!")

# Get the members of tamtar's list "Things That Are Rad" t._("tamtar")._("things-that-are-rad").members()

# Note how the magic _ method can be used to insert data # into the middle of a call. You can also use replacement: t.user.list.members(user="tamtar", list="things-that-are-rad")

# An optional _timeout parameter can also be used for API # calls which take much more time than normal or twitter stops # responding for some reasone t.users.lookup(screen_name=','.join(A_LIST_OF_100_SCREEN_NAMES), _timeout=1) ```

System Message: WARNING/2 (<string>, line 115); backlink

Inline literal start-string without end-string.

System Message: WARNING/2 (<string>, line 115); backlink

Inline interpreted text or phrase reference start-string without end-string.

Searching Twitter:

``` python

System Message: ERROR/3 (<string>, line 125)

Inconsistent literal block quoting.

# Search for the latest tweets about #pycon t.search.tweets(q="#pycon") ```

System Message: WARNING/2 (<string>, line 125); backlink

Inline literal start-string without end-string.

System Message: WARNING/2 (<string>, line 125); backlink

Inline interpreted text or phrase reference start-string without end-string.
Using the data returned

Twitter API calls return decoded JSON. This is converted into a bunch of Python lists, dicts, ints, and strings. For example:

```python

System Message: ERROR/3 (<string>, line 136)

Inconsistent literal block quoting.

x = twitter.statuses.home_timeline()

# The first 'tweet' in the timeline x[0]

# The screen name of the user who wrote the first 'tweet' x[0]['user']['screen_name'] ```

System Message: WARNING/2 (<string>, line 141); backlink

Inline literal start-string without end-string.

System Message: WARNING/2 (<string>, line 141); backlink

Inline interpreted text or phrase reference start-string without end-string.
Getting raw XML data

If you prefer to get your Twitter data in XML format, pass format="xml" to the Twitter object when you instantiate it:

```python

System Message: ERROR/3 (<string>, line 152)

Inconsistent literal block quoting.

twitter = Twitter(format="xml") ```

System Message: WARNING/2 (<string>, line 152); backlink

Inline literal start-string without end-string.

System Message: WARNING/2 (<string>, line 152); backlink

Inline interpreted text or phrase reference start-string without end-string.

The output will not be parsed in any way. It will be a raw string of XML.

The TwitterStream class

The TwitterStream object is an interface to the Twitter Stream API (stream.twitter.com). This can be used pretty much the same as the Twitter class except the result of calling a method will be an iterator that yields objects decoded from the stream. For example:

```python

System Message: ERROR/3 (<string>, line 169)

Inconsistent literal block quoting.

twitter_stream = TwitterStream(auth=UserPassAuth('joe', 'joespassword')) iterator = twitter_stream.statuses.sample()

for tweet in iterator:
# ...do something with this tweet...

System Message: WARNING/2 (<string>, line 174)

Definition list ends without a blank line; unexpected unindent.

```

System Message: WARNING/2 (<string>, line 174); backlink

Inline literal start-string without end-string.

System Message: WARNING/2 (<string>, line 174); backlink

Inline interpreted text or phrase reference start-string without end-string.

The iterator will yield tweets forever and ever (until the stream breaks at which point it raises a TwitterHTTPError.)

The block parameter controls if the stream is blocking. Default is blocking (True). When set to False, the iterator will occasionally yield None when there is no available message.

Twitter Response Objects

Response from a twitter request. Behaves like a list or a string (depending on requested format) but it has a few other interesting attributes.

headers gives you access to the response headers as an httplib.HTTPHeaders instance. You can do response.headers.getheader('h') to retrieve a header.

Authentication

You can authenticate with Twitter in three ways: NoAuth, OAuth, or UserPassAuth. Get help() on these classes to learn how to use them.

OAuth is probably the most useful.

Working with OAuth

Visit the Twitter developer page and create a new application:

[https://dev.twitter.com/apps/new](https://dev.twitter.com/apps/new)

This will get you a CONSUMER_KEY and CONSUMER_SECRET.

When users run your application they have to authenticate your app with their Twitter account. A few HTTP calls to twitter are required to do this. Please see the twitter.oauth_dance module to see how this is done. If you are making a command-line app, you can use the oauth_dance() function directly.

Performing the "oauth dance" gets you an ouath token and oauth secret that authenticate the user with Twitter. You should save these for later so that the user doesn't have to do the oauth dance again.

read_token_file and write_token_file are utility methods to read and write OAuth token and secret key values. The values are stored as strings in the file. Not terribly exciting.

Finally, you can use the OAuth authenticator to connect to Twitter. In code it all goes like this:

```python

System Message: ERROR/3 (<string>, line 230)

Inconsistent literal block quoting.

from twitter import *

MY_TWITTER_CREDS = os.path.expanduser('~/.my_app_credentials') if not os.path.exists(MY_TWITTER_CREDS):

System Message: ERROR/3 (<string>, line 234)

Unexpected indentation.
oauth_dance("My App Name", CONSUMER_KEY, CONSUMER_SECRET,
MY_TWITTER_CREDS)

oauth_token, oauth_secret = read_token_file(MY_TWITTER_CREDS)

twitter = Twitter(auth=OAuth(
oauth_token, oauth_secret, CONSUMER_KEY, CONSUMER_SECRET))

# Now work with Twitter twitter.statuses.update('Hello, world!') ```

System Message: WARNING/2 (<string>, line 242); backlink

Inline literal start-string without end-string.

System Message: WARNING/2 (<string>, line 242); backlink

Inline interpreted text or phrase reference start-string without end-string.

License

Python Twitter Tools are released under an MIT License.

Subscribe to package updates

Last updated Sep 20th, 2013

Download Stats

Last month:8

What does the lock icon mean?

Builds marked with a lock icon are only available via PyPM to users with a current ActivePython Business Edition subscription.

Need custom builds or support?

ActivePython Enterprise Edition guarantees priority access to technical support, indemnification, expert consulting and quality-assured language builds.

Plan on re-distributing ActivePython?

Get re-distribution rights and eliminate legal risks with ActivePython OEM Edition.