Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

Commit 69e1112

Browse files
committed
WIP on README
1 parent a50ec37 commit 69e1112

File tree

1 file changed

+161
-68
lines changed

1 file changed

+161
-68
lines changed

README

Lines changed: 161 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
Python Twitter Tools
2-
====================
1+
# Python Twitter Tools
32

43
[![Tests](https://github.com/python-twitter-tools/twitter/workflows/Tests/badge.svg)](https://github.com/python-twitter-tools/twitter/actions)
54
[![Coverage Status](https://coveralls.io/repos/github/python-twitter-tools/twitter/badge.svg?branch=master)](https://coveralls.io/github/python-twitter-tools/twitter?branch=master)
@@ -12,17 +11,7 @@ For more information:
1211
* import the `twitter` package and run `help()` on it
1312

1413

15-
Programming with the Twitter API classes
16-
========================================
17-
18-
The `Twitter` and `TwitterStream` classes are the key to building your own
19-
Twitter-enabled applications.
20-
The `Twitter2` and `TwitterStream2` classes are helpers preconfigured to
21-
handle the specificities of Twitter API v2.
22-
23-
24-
The `Twitter` class
25-
-------------------
14+
## Programming with the Twitter API classes
2615

2716
The minimalist yet fully featured Twitter API class.
2817

@@ -37,6 +26,150 @@ The list of most accessible functions is listed at:
3726

3827
**[https://developer.twitter.com/en/docs/api-reference-index](https://developer.twitter.com/en/docs/api-reference-index)**
3928

29+
The `Twitter` and `TwitterStream` classes are the key to building your own
30+
Twitter-enabled applications.
31+
32+
The `Twitter2` and `TwitterStream2` classes are helpers preconfigured to
33+
handle the specificities of Twitter API v2.
34+
35+
```python
36+
from twitter import Twitter, Twitter2, TwitterStream, TwitterStream2
37+
```
38+
39+
### Authenticating
40+
41+
Querying the Twitter API requires you to [authenticate](https://developer.twitter.com/en/docs/authentication/overview) using individual credentials.
42+
43+
In order to obtain these, visit first the [Twitter developer page](https://developer.twitter.com/)
44+
and [apply for a developer acount](https://developer.twitter.com/en/apply-for-access)
45+
(which now requires you to provide a cellphone number in your account settings
46+
and to fill a form describing your intentions in using the Twitter API),
47+
then [create an app](https://developer.twitter.com/en/portal/apps/new).
48+
49+
Once your app is configured, you can generate your API keys set:
50+
- API `KEY` and `SECRET`
51+
- Access `OAUTH_TOKEN` and `OAUTH_TOKEN_SECRET`
52+
- OAuth2 `BEARER_TOKEN`
53+
54+
You can authenticate with Twitter in three ways: NoAuth, OAuth, or
55+
OAuth2 (app-only and API v2). Get `help()` on these classes to learn how to use them.
56+
57+
OAuth and OAuth2 are probably the most useful.
58+
59+
- OAuth requires a complete set of `KEY`, `SECRET`, `OAUTH_TOKEN` and
60+
`OAUTH_TOKEN_SECRET`:
61+
62+
```python
63+
from twitter import OAuth
64+
65+
auth = OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET, KEY, SECRET)
66+
```
67+
68+
- OAuth2 can be used directly with just the `BEARER_TOKEN`:
69+
70+
```python
71+
from twitter import OAuth2
72+
73+
auth2 = OAuth2(bearer_token=BEARER_TOKEN)
74+
```
75+
76+
Note also that you can configure your app to enable "3-legged OAuth" if you
77+
want to allow other users to interact with Twitter through your app. In this
78+
case, only your KEY and SECRET will be required and your application will need
79+
to be authorized by the user by performing the "OAuth Dance" (see dedicated
80+
[documentation below](#oauth-dance).
81+
82+
83+
### Instantiating
84+
85+
Now that you have credentials allowing to use either OAuth or OAuth2
86+
authentification, you can instantiate the Twitter classes like this:
87+
88+
```python
89+
# For the API v1.1:
90+
tw = Twitter(auth=auth)
91+
# and
92+
tw_app = Twitter(auth=auth2)
93+
94+
# And for the v2:
95+
tw2 = Twitter2(auth=auth)
96+
tw2_app = Twitter2(auth=auth2)
97+
# (which are actually the same as:)
98+
tw2 = Twitter(auth=auth, api_version = "2", format="")
99+
tw2_app = Twitter(auth=auth2, api_version = "2", format="")
100+
```
101+
102+
Note that the different API routes proposed can sometimes require always
103+
OAuth2 while certain will only work with regular OAuth and other will accept
104+
both (but potentially with different rate limits).
105+
Please read the [official documentation](https://developer.twitter.com/en/docs/api-reference-index)
106+
to know which authentication to use for which routes.
107+
108+
109+
## Querying the API
110+
111+
The philosophy of this wrapper is to be as flexible as possible and allow
112+
users to call any new routes added by Twitter without requiring to add
113+
specific code for each route within the library first. One only needs to know
114+
the url of an API route to call it as if it were an attribute of the Twitter
115+
classes.
116+
117+
Hence, for instance, to use the v2 search tweets route
118+
[`https://api.twitter.com/2/tweets/search/recent`](https://developer.twitter.com/en/docs/twitter-api/tweets/search/api-reference/get-tweets-search-recent),
119+
you can call it as such:
120+
121+
```python
122+
tw2_app.tweets.search.recent(query="python", max_results=50)
123+
```
124+
125+
Identically with the v1.1 search route
126+
[`https://api.twitter.com/1.1/search/tweets.json`](https://developer.twitter.com/en/docs/twitter-api/v1/tweets/search/api-reference/get-search-tweets):
127+
128+
```python
129+
tw.search.tweets(q="python", result_type="recent", count=50)
130+
```
131+
132+
API routes sometimes include an argument within them. In such cases the
133+
positionnal argument should be prefixed with an underscore and reused as
134+
such in the function's arguments. For instance, to get a user's id when
135+
knowing only its screen_name, one can use [`https://api.twitter.com/2/users/by/username/:username`](https://developer.twitter.com/en/docs/twitter-api/users/lookup/api-reference/get-users-by-username-username):
136+
137+
```
138+
res = tw2.users.by.username._username(_username="PythonTwitTools")
139+
user_id = res["data"]["id"]
140+
```
141+
142+
Similarly, to collect all recent tweets of this account (his "timeline"), using [`https://api.twitter.com/2/users/:id/tweets`](https://developer.twitter.com/en/docs/twitter-api/tweets/timelines/api-reference/get-users-id-tweets);
143+
144+
```python
145+
tw2.users._id.tweets(_id=user_id, exclude="retweets")
146+
```
147+
148+
Some routes also require an `id` to be given at the end of the route url. In those cases, one can directly use the 'id` argument without prefixing it with an underscore and it will magically be added at the end of the route. For instance to get more metadata on the previous user, thanks to the route [`https://api.twitter.com/2/users/:id`](https://developer.twitter.com/en/docs/twitter-api/users/lookup/api-reference/get-users-id)
149+
150+
```python
151+
tw2.users(id=user_id)
152+
# which is identical to
153+
tw2.users._id(_id=user_id)
154+
```
155+
156+
Consequently, when calling one of the rare routes raking an argument named "id" but that is not supposed to appear within the route url, it needs to be renamed as `_id` within the function's argument. For instance, to call [`https://api.twitter.com/1.1/collections/show.json`](https://developer.twitter.com/en/docs/twitter-api/v1/tweets/curate-a-collection/api-reference/get-collections-show):
157+
158+
```python
159+
tw.collections.show(_id="custom-388061495298244609")
160+
# instead of the following which will return a 404 error:
161+
tw.collections.show(id="custom-388061495298244609")
162+
```
163+
164+
Finally, some routes require to force the HTTP method used to call them (`GET`, `POST`, `DELETE`, etc.). The library uses GET by default and tries to guess most other cases, but it required sometimes to be forced, wghich can be done by using the extra `_method` argument. For instance, the route [`https://api.twitter.com/2/users/:id/muting`](https://developer.twitter.com/en/docs/twitter-api/users/mutes/api-reference/get-users-muting) can be called with GET to collect a list of muted users, and with `POST` to add a user to this list:
165+
166+
```python
167+
# To see the list of all users muted by our previous user:
168+
tw2.users._id.muting(_id=user_id)
169+
# And instead to mute this specific user:
170+
tw2.users._id.muting(_id=user_id, _method="POST")
171+
```
172+
40173

41174
Examples with Twitter API v2:
42175
+++++++++++++++++++++++++++++
@@ -295,29 +428,14 @@ attributes.
295428
`httplib.HTTPHeaders` instance. Use `response.headers.get('h')`
296429
to retrieve a header.
297430

298-
Authentication
299-
--------------
300-
301-
You can authenticate with Twitter in three ways: NoAuth, OAuth, or
302-
OAuth2 (app-only). Get `help()` on these classes to learn how to use them.
303-
304-
OAuth and OAuth2 are probably the most useful.
305-
306-
307-
Working with OAuth
308-
------------------
309431

310-
Visit the Twitter developer page and create a new application:
432+
### OAuth Danse
311433

312-
**[https://dev.twitter.com/apps/new](https://dev.twitter.com/apps/new)**
313-
314-
This will get you a `CONSUMER_KEY` and `CONSUMER_SECRET`.
315-
316-
When users run your application they have to authenticate your app
317-
with their Twitter account. A few HTTP calls to Twitter are required
318-
to do this. Please see the `twitter.oauth_dance` module to see how this
319-
is done. If you are making a command-line app, you can use the
320-
`oauth_dance()` function directly.
434+
When users run your application they have to authenticate your app with
435+
their Twitter account. A few HTTP calls to Twitter are required to do this.
436+
Please see the twitter.oauth_dance module to see how this is done.
437+
If you are making a command-line app, you can use the oauth_dance() function
438+
directly.
321439

322440
Performing the "oauth dance" gets you an oauth token and oauth secret
323441
that authenticate the user with Twitter. You should save these for
@@ -331,50 +449,25 @@ Finally, you can use the `OAuth` authenticator to connect to Twitter. In
331449
code it all goes like this:
332450

333451
```python
334-
from twitter import *
452+
import os
453+
from twitter import oauth_dance, read_token_file, Twitter, OAuth
454+
KEY = "xxxxxxxx"
455+
SECRET = "xxxxxxxx"
335456

336457
MY_TWITTER_CREDS = os.path.expanduser('~/.my_app_credentials')
337458
if not os.path.exists(MY_TWITTER_CREDS):
338-
oauth_dance("My App Name", CONSUMER_KEY, CONSUMER_SECRET,
339-
MY_TWITTER_CREDS)
459+
oauth_dance("My App Name", KEY, SECRET, MY_TWITTER_CREDS)
340460

341461
oauth_token, oauth_secret = read_token_file(MY_TWITTER_CREDS)
342462

343-
twitter = Twitter(auth=OAuth(
344-
oauth_token, oauth_secret, CONSUMER_KEY, CONSUMER_SECRET))
463+
twitter = Twitter(auth=OAuth(oauth_token, oauth_secret, KEY, SECRET))
345464

346465
# Now work with Twitter
347466
twitter.statuses.update(status='Hello, world!')
348467
```
349468

350-
Working with `OAuth2`
351-
---------------------
352-
353-
Twitter only supports the application-only flow of OAuth2 for certain
354-
API endpoints. This OAuth2 authenticator only supports the application-only
355-
flow right now.
356-
357-
To authenticate with OAuth2, visit the Twitter developer page and create a new
358-
application:
359-
360-
**[https://dev.twitter.com/apps/new](https://dev.twitter.com/apps/new)**
361-
362-
This will get you a `CONSUMER_KEY` and `CONSUMER_SECRET`.
363-
364-
Exchange your `CONSUMER_KEY` and `CONSUMER_SECRET` for a bearer token using the
365-
`oauth2_dance` function.
366-
367-
Finally, you can use the `OAuth2` authenticator and your bearer token to connect
368-
to Twitter. In code it goes like this::
369-
370-
```python
371-
twitter = Twitter(auth=OAuth2(bearer_token=BEARER_TOKEN))
372-
373-
# Now work with Twitter
374-
twitter.search.tweets(q='keyword')
375-
```
376469

377-
License
378-
=======
470+
## License
379471

380-
Python Twitter Tools are released under an MIT License.
472+
Python Twitter Tools and the Twitter API Python wrapper are released
473+
under an MIT License.

0 commit comments

Comments
 (0)