]> jfr.im git - z_archive/twitter.git/blame - README
Version bump 1.8.0.
[z_archive/twitter.git] / README
CommitLineData
fdbae010 1Python Twitter Tools
a65893e4 2====================
fdbae010 3
f1a8ed67 4The Minimalist Twitter API for Python is a Python API for Twitter,
5everyone's favorite Web 2.0 Facebook-style status updater for people
6on the go.
fdbae010 7
f1a8ed67 8Also included is a twitter command-line tool for getting your friends'
9tweets and setting your own tweet from the safety and security of your
5b8b1ead 10favorite shell and an IRC bot that can announce Twitter updates to an
f1a8ed67 11IRC channel.
fdbae010 12
5f47b302 13For more information, after installing the `twitter` package:
fdbae010 14
15 * import the `twitter` package and run help() on it
16 * run `twitter -h` for command-line tool help
a65893e4 17
51e0b8f1
MV
18
19twitter - The Command-Line Tool
20-------------------------------
a65893e4 21
30913a4e 22The command-line tool lets you do some awesome things:
a65893e4 23
30913a4e 24 * view your tweets, recent replies, and tweets in lists
a65893e4
MV
25 * view the public timeline
26 * follow and unfollow (leave) friends
27 * various output formats for tweet information
51e0b8f1 28
a65893e4
MV
29The bottom line: type `twitter`, receive tweets.
30
31
32
51e0b8f1
MV
33twitterbot - The IRC Bot
34------------------------
a65893e4
MV
35
36The IRC bot is associated with a twitter account (either your own account or an
37account you create for the bot). The bot announces all tweets from friends
38it is following. It can be made to follow or leave friends through IRC /msg
39commands.
40
5f47b302 41
5f47b302 42twitter-log
51e0b8f1 43-----------
5f47b302
MV
44
45`twitter-log` is a simple command-line tool that dumps all public
46tweets from a given user in a simple text format. It is useful to get
47a complete offsite backup of all your tweets. Run `twitter-log` and
48read the instructions.
49
30913a4e
MV
50twitter-archiver and twitter-follow
51-----------------------------------
52
53twitter-archiver will log all the tweets posted by any user since they
54started posting. twitter-follow will print a list of all of all the
55followers of a user (or all the users that user follows).
56
5f47b302 57
51e0b8f1
MV
58Programming with the Twitter api classes
59========================================
60
61
62The Twitter and TwitterStream classes are the key to building your own
63Twitter-enabled applications.
64
65
66The Twitter class
67-----------------
68
69The minimalist yet fully featured Twitter API class.
70
71Get RESTful data by accessing members of this class. The result
72is decoded python objects (lists and dicts).
73
74The Twitter API is documented at:
75
76 http://dev.twitter.com/doc
77
78
79Examples::
80
d09c0dd3 81 t = Twitter(
51e0b8f1
MV
82 auth=OAuth(token, token_key, con_secret, con_secret_key)))
83
84 # Get the public timeline
d09c0dd3 85 t.statuses.public_timeline()
51e0b8f1
MV
86
87 # Get a particular friend's timeline
d09c0dd3 88 t.statuses.friends_timeline(id="billybob")
51e0b8f1
MV
89
90 # Also supported (but totally weird)
d09c0dd3
MV
91 t.statuses.friends_timeline.billybob()
92
93 # Update your status
94 t.statuses.update(
95 status="Using @sixohsix's sweet Python Twitter Tools.")
51e0b8f1
MV
96
97 # Send a direct message
d09c0dd3 98 t.direct_messages.new(
51e0b8f1
MV
99 user="billybob",
100 text="I think yer swell!")
101
d09c0dd3
MV
102 # Get the members of tamtar's list "Things That Are Rad"
103 t._("tamtar")._("things-that-are-rad").members()
104
105 # Note how the magic `_` method can be used to insert data
106 # into the middle of a call. You can also use replacement:
107 t.user.list.members(user="tamtar", list="things-that-are-rad")
51e0b8f1
MV
108
109
110Searching Twitter::
111
112 twitter_search = Twitter(domain="search.twitter.com")
113
114 # Find the latest search trends
115 twitter_search.trends()
116
117 # Search for the latest News on #gaza
118 twitter_search.search(q="#gaza")
119
120
121Using the data returned
122-----------------------
123
124Twitter API calls return decoded JSON. This is converted into
125a bunch of Python lists, dicts, ints, and strings. For example::
126
127 x = twitter.statuses.public_timeline()
128
129 # The first 'tweet' in the timeline
130 x[0]
131
132 # The screen name of the user who wrote the first 'tweet'
133 x[0]['user']['screen_name']
134
135
136Getting raw XML data
137--------------------
138
139If you prefer to get your Twitter data in XML format, pass
140format="xml" to the Twitter object when you instantiate it::
141
142 twitter = Twitter(format="xml")
143
144The output will not be parsed in any way. It will be a raw string
145of XML.
146
147
148The TwitterStream class
149-----------------------
150
151The TwitterStream object is an interface to the Twitter Stream API
152(stream.twitter.com). This can be used pretty much the same as the
153Twitter class except the result of calling a method will be an
154iterator that yields objects decoded from the stream. For
155example::
156
157 twitter_stream = TwitterStream(auth=UserPassAuth('joe', 'joespassword'))
158 iterator = twitter_stream.statuses.sample()
159
160 for tweet in iterator:
161 ...do something with this tweet...
162
163The iterator will yield tweets forever and ever (until the stream
164breaks at which point it raises a TwitterHTTPError.)
165
166The `block` parameter controls if the stream is blocking. Default
167is blocking (True). When set to False, the iterator will
168occasionally yield None when there is no available message.
169
170Twitter Response Objects
171------------------------
172
173Response from a twitter request. Behaves like a list or a string
174(depending on requested format) but it has a few other interesting
175attributes.
176
177`headers` gives you access to the response headers as an
178httplib.HTTPHeaders instance. You can do
179`response.headers.getheader('h')` to retrieve a header.
180
181Authentication
182--------------
183
184You can authenticate with Twitter in three ways: NoAuth, OAuth, or
185UserPassAuth. Get help() on these classes to learn how to use them.
186
187OAuth is probably the most useful.
188
189
190Working with OAuth
191------------------
192
193Visit the Twitter developer page and create a new application:
194
195 https://dev.twitter.com/apps/new
196
197This will get you a CONSUMER_KEY and CONSUMER_SECRET.
198
199When users run your application they have to authenticate your app
200with their Twitter account. A few HTTP calls to twitter are required
201to do this. Please see the twitter.oauth_dance module to see how this
202is done. If you are making a command-line app, you can use the
203oauth_dance() function directly.
204
205Performing the "oauth dance" gets you an ouath token and oauth secret
206that authenticate the user with Twitter. You should save these for
207later so that the user doesn't have to do the oauth dance again.
208
209read_token_file and write_token_file are utility methods to read and
210write OAuth token and secret key values. The values are stored as
211strings in the file. Not terribly exciting.
212
213Finally, you can use the OAuth authenticator to connect to Twitter. In
214code it all goes like this::
215
216 MY_TWITTER_CREDS = os.path.expanduser('~/.my_app_credentials')
217 if not os.path.exists(MY_TWITTER_CREDS):
218 oauth_dance("My App Name", CONSUMER_KEY, CONSUMER_SECRET,
219 MY_TWITTER_CREDS)
220
221 oauth_token, oauth_secret = read_token_file(MY_TWITTER_CREDS)
222
223 twitter = Twitter(auth=OAuth(
224 oauth_token, oauth_token_secret, CONSUMER_KEY, CONSUMER_SECRET))
225
226 # Now work with Twitter
227 twitter.statuses.update('Hello, world!')
228
229
230
231License
232=======
233
8be9a740 234Python Twitter Tools are released under an MIT License.