Skip to content

Commit 44fb5b4

Browse files
Attempting to make docs clear and understandable
[ci skip]
1 parent 8559a1f commit 44fb5b4

File tree

7 files changed

+200
-28
lines changed

7 files changed

+200
-28
lines changed

docs/api.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Core Interface
1919
:special-members: __init__
2020
:inherited-members:
2121

22+
.. _streaming_interface:
23+
2224
Streaming Interface
2325
-------------------
2426

docs/index.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,25 @@ Features
1818
- Direct Messages
1919
- and anything found in `the Twitter API docs <https://dev.twitter.com/docs/api/1.1>`_.
2020
- Image Uploading!
21-
- **Update user status with an image**
21+
- Update user status with an image
2222
- Change user avatar
2323
- Change user background image
2424
- Change user banner image
25+
- OAuth 2 Application Only (read-only) Support
2526
- Support for Twitter's Streaming API
2627
- Seamless Python 3 support!
2728

2829
Usage
2930
-----
3031

3132
.. toctree::
32-
:maxdepth: 2
33+
:maxdepth: 3
3334

3435
usage/install
3536
usage/starting_out
3637
usage/basic_usage
38+
usage/advanced_usage
39+
usage/streaming_api
3740

3841
Twython API Documentation
3942
-------------------------

docs/usage/advanced_usage.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
.. _advanced-usage:
2+
3+
Advanced Usage
4+
==============
5+
6+
This section will cover how to use Twython and interact with some a little more advanced API calls
7+
8+
Before you make any API calls, make sure you :ref:`authenticated <starting-out>` the user!
9+
10+
.. note:: All sections on this page will assume you're using a Twython instance
11+
12+
*******************************************************************************
13+
14+
Create a Twython instance with your application keys and the users OAuth tokens::
15+
16+
from twython import Twython
17+
twitter = Twython(APP_KEY, APP_SECRET
18+
OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
19+
20+
Updating Status with Image
21+
--------------------------
22+
23+
Documentation: https://dev.twitter.com/docs/api/1.1/get/account/verify_credentials
24+
25+
::
26+
27+
photo = open('/path/to/file/image.jpg', 'rb')
28+
twitter.update_status_with_media(status='Checkout this cool image!', media=photo)
29+
30+
So now you can authenticate, update your status (with or without an image), search Twitter, and a few other things! Good luck!

docs/usage/basic_usage.rst

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,24 @@ This section will cover how to use Twython and interact with some basic Twitter
77

88
Before you make any API calls, make sure you :ref:`authenticated <starting-out>` the user!
99

10-
Create a Twython instance with your application keys and the users OAuth tokens::
10+
.. note:: All sections on this page will assume you're using a Twython instance
1111

12-
from twython import Twython
13-
twitter = Twython(APP_KEY, APP_SECRET
14-
OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
12+
*******************************************************************************
1513

16-
.. admonition:: Important
14+
Authenticated Calls
15+
-------------------
1716

18-
All sections on this page will assume you're using a Twython instance
17+
OAuth 1
18+
~~~~~~~
1919

20-
What Twython Returns
21-
--------------------
20+
Create a Twython instance with your application keys and the users OAuth tokens::
2221

23-
Twython returns a dictionary of JSON response from Twitter
22+
from twython import Twython
23+
twitter = Twython(APP_KEY, APP_SECRET
24+
OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
2425

2526
User Information
26-
----------------
27+
^^^^^^^^^^^^^^^^
2728

2829
Documentation: https://dev.twitter.com/docs/api/1.1/get/account/verify_credentials
2930

@@ -32,35 +33,52 @@ Documentation: https://dev.twitter.com/docs/api/1.1/get/account/verify_credentia
3233
twitter.verify_credentials()
3334

3435
Authenticated Users Home Timeline
35-
---------------------------------
36+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3637

3738
Documentation: https://dev.twitter.com/docs/api/1.1/get/statuses/home_timeline
3839

3940
::
4041

4142
twitter.get_home_timeline()
4243

43-
Search
44-
------
44+
Updating Status
45+
^^^^^^^^^^^^^^^
4546

46-
Documentation: https://dev.twitter.com/docs/api/1.1/get/search/tweets
47+
This method makes use of dynamic arguments, :ref:`read more about them <dynamicargexplaination>`
48+
49+
Documentation: https://dev.twitter.com/docs/api/1/post/statuses/update
4750

4851
::
4952

50-
twitter.search(q='python')
53+
twitter.update_status(status='See how easy using Twython is!')
54+
5155

52-
To help explain :ref:`dynamic function arguments <starting-out>` a little more, you can see that the previous call used the keyword argument ``q``, that is because Twitter specifies in their `search documentation <https://dev.twitter.com/docs/api/1.1/get/search/tweets>`_ that the search call accepts the parameter "q". You can pass mutiple keyword arguments. The search documentation also specifies that the call accepts the parameter "result_type"
56+
OAuth 2
57+
~~~~~~~
58+
59+
Create a Twython instance with your application key and access token::
60+
61+
from twython import Twython
62+
twitter = Twython(APP_KEY, access_token=ACCESS_TOKEN)
63+
64+
.. _howtosearch:
65+
66+
Searching
67+
---------
68+
69+
.. note:: Searching can be done whether you're authenticated via OAuth 1 or OAuth 2
70+
71+
Documentation: https://dev.twitter.com/docs/api/1.1/get/search/tweets
5372

5473
::
5574

56-
twitter.search(q='python', result_type='popular')
75+
twitter.search(q='python')
5776

58-
Updating Status
59-
---------------
77+
.. _dynamicargexplaination:
6078

61-
Documentation: https://dev.twitter.com/docs/api/1/post/statuses/update
79+
.. important:: To help explain :ref:`dynamic function arguments <dynamicfunctionarguments>` a little more, you can see that the previous call used the keyword argument ``q``, that is because Twitter specifies in their `search documentation <https://dev.twitter.com/docs/api/1.1/get/search/tweets>`_ that the search call accepts the parameter "q". You can pass mutiple keyword arguments. The search documentation also specifies that the call accepts the parameter "result_type"
6280

6381
::
6482

65-
twitter.update_status(status='See how easy using Twython is!')
83+
twitter.search(q='python', result_type='popular')
6684

docs/usage/install.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Installation
55

66
Information on how to properly install Twython
77

8+
*******************************************************************************
89

910
Pip or Easy Install
1011
-------------------
@@ -39,4 +40,8 @@ Feel free to clone the repository::
3940

4041
Now that you have the source code, install it into your site-packages directory::
4142

42-
$ python setup.py install
43+
$ python setup.py install
44+
45+
*******************************************************************************
46+
47+
So Twython is installed! Now, head over to the :ref:`starting out <starting-out>` section.

docs/usage/starting_out.rst

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Starting Out
55

66
This section is going to help you understand creating a Twitter Application, authenticating a user, and making basic API calls
77

8+
*******************************************************************************
9+
810
Beginning
911
---------
1012

@@ -17,6 +19,20 @@ Now you're ready to start authentication!
1719
Authentication
1820
--------------
1921

22+
Twython offers support for both OAuth 1 and OAuth 2 authentication.
23+
24+
The difference:
25+
26+
- :ref:`OAuth 1 <oauth1>` is for user authenticated calls (tweeting, following people, sneding DMs, etc.)
27+
- :ref:`OAuth 2 <oauth2>` is for application authenticated calls (when you don't want to authenticate a user and make read-only calls to Twitter, i.e. searching, reading a public users timeline)
28+
29+
.. _oauth1:
30+
31+
OAuth 1 (User Authentication)
32+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33+
34+
.. important:: Again, if your web app is planning on using interacting with users, this **IS** the authentication type for you. If you're not interested in authenticating a user and plan on making read-only calls, check out the :ref:`OAuth 2 <oauth2>` section.
35+
2036
First, you'll want to import Twython::
2137

2238
from twython import Twython
@@ -40,7 +56,7 @@ Send the user to the authentication url, you can obtain it by accessing::
4056
auth['auth_url']
4157

4258
Handling the Callback
43-
---------------------
59+
^^^^^^^^^^^^^^^^^^^^^
4460

4561
After they authorize your application to access some of their account details, they'll be redirected to the callback url you specified in ``get_autentication_tokens``
4662

@@ -64,16 +80,63 @@ Once you have the final user tokens, store them in a database for later use!::
6480
OAUTH_TOKEN = final_step['oauth_token']
6581
OAUTH_TOKEN_SECERT = final_step['oauth_token_secret']
6682

83+
.. _oauth2:
84+
85+
OAuth 2 (Application Authentication)
86+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
87+
88+
.. attention:: Just a reminder, this authentication type is for when you don't want to authenticate and interact with users and make read-only calls to Twitter
89+
90+
OAuth 2 authentication is 100x easier than OAuth 1.
91+
Let's say you *just* made your application and have your ``Consumer Key`` and ``Consumer Secret``
92+
93+
First, you'll want to import Twython::
94+
95+
from twython import Twython
96+
97+
Don't have an OAuth 2 `access_token`?
98+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
99+
100+
::
101+
102+
APP_KEY = 'YOUR_APP_KEY'
103+
APP_SECET = 'YOUR_APP_SECRET'
104+
105+
twitter = Twython(APP_KEY, APP_SECRET, oauth_version=2)
106+
ACCESS_TOKEN = twitter.obtain_access_token()
107+
108+
# Save ACCESS_TOKEN in a database or something for later use!
109+
110+
Already have one?
111+
^^^^^^^^^^^^^^^^^
112+
113+
::
114+
115+
APP_KEY = 'YOUR_APP_KEY'
116+
ACCESS_TOKEN = 'YOUR_ACCESS_TOKEN'
117+
118+
twitter = Twython(APP_KEY, access_token=ACCESS_TOKEN)
119+
120+
Now that you have your OAuth 2 access_token, maybe you'll want to perform a :ref:`search <howtosearch>` or something
121+
67122
The Twython API Table
68123
---------------------
69124

70-
In the Twython package is a file called ``endpoints.py`` which holds a dictionary of all Twitter API endpoints. This is so Twython's core ``api.py`` isn't cluttered with 50+ methods. We dynamically register these funtions when a Twython object is initiated.
125+
The Twython package contains a file ``endpoints.py`` which holds a Mixin of all Twitter API endpoints. This is so Twython's core ``api.py`` isn't cluttered with 50+ methods.
126+
127+
.. _dynamicfunctionarguments:
71128

72129
Dynamic Function Arguments
73130
--------------------------
74131

75132
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.
76133

77-
-----------------------
134+
What Twython Returns
135+
--------------------
136+
137+
Twython returns native Python objects. We convert the JSON sent to us from Twitter to an object so you don't have to.
138+
139+
140+
*******************************************************************************
78141

79-
Now that you have your application tokens and user tokens, check out the :ref:`basic usage <basic-usage>` section.
142+
Now that you have a little idea of the type of data you'll be receiving, briefed on how arguments are handled, and your application tokens and user oauth tokens (or access token if you're using OAuth 2), check out the :ref:`basic usage <basic-usage>` section.

docs/usage/streaming_api.rst

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
.. _streaming-api:
2+
3+
Streaming API
4+
=============
5+
6+
This section will cover how to use Twython and interact with the Twitter Streaming API.
7+
8+
Streaming Documentation: https://dev.twitter.com/docs/streaming-apis
9+
10+
.. important:: The Streaming API requires that you have OAuth 1 authentication credentials. If you don't have credentials, head over to the :ref:`authentication section <oauth1>` and find out how!
11+
12+
Setting Up Your Streamer
13+
------------------------
14+
15+
.. note:: When stream data is sent back to Twython, we send the data through signals (i.e. ``on_success``, ``on_error``, etc.)
16+
17+
Make sure you import ``TwythonStreamer``
18+
19+
::
20+
21+
from twython import TwythonStreamer
22+
23+
Now set up how you want to handle the signals.
24+
25+
::
26+
27+
class MyStreamer(TwythonStreamer):
28+
def on_success(self, data):
29+
if 'text' in data:
30+
print data['text'].encode('utf-8')
31+
32+
def on_error(self, status_code, data):
33+
print status_code
34+
35+
# Want to stop trying to get data because of the error?
36+
# Uncomment the next line!
37+
# self.disconnect()
38+
39+
More signals that you can extend on can be found in the Developer Interface section under :ref:`Streaming Interface <streaming_interface>`
40+
41+
Filtering Public Statuses
42+
-------------------------
43+
44+
::
45+
46+
stream = MyStreamer(APP_KEY, APP_SECRET,
47+
OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
48+
stream.statuses.filter(track='twitter')
49+
50+
With the code above, data should be flowing in.
51+

0 commit comments

Comments
 (0)