Attempting to make docs clear and understandable
[ci skip]
This commit is contained in:
parent
8559a1f1ce
commit
44fb5b4a6e
7 changed files with 206 additions and 34 deletions
|
|
@ -19,6 +19,8 @@ Core Interface
|
|||
:special-members: __init__
|
||||
:inherited-members:
|
||||
|
||||
.. _streaming_interface:
|
||||
|
||||
Streaming Interface
|
||||
-------------------
|
||||
|
||||
|
|
|
|||
|
|
@ -18,10 +18,11 @@ Features
|
|||
- Direct Messages
|
||||
- and anything found in `the Twitter API docs <https://dev.twitter.com/docs/api/1.1>`_.
|
||||
- Image Uploading!
|
||||
- **Update user status with an image**
|
||||
- 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!
|
||||
|
||||
|
|
@ -29,11 +30,13 @@ Usage
|
|||
-----
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 2
|
||||
:maxdepth: 3
|
||||
|
||||
usage/install
|
||||
usage/starting_out
|
||||
usage/basic_usage
|
||||
usage/advanced_usage
|
||||
usage/streaming_api
|
||||
|
||||
Twython API Documentation
|
||||
-------------------------
|
||||
|
|
|
|||
30
docs/usage/advanced_usage.rst
Normal file
30
docs/usage/advanced_usage.rst
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
.. _advanced-usage:
|
||||
|
||||
Advanced Usage
|
||||
==============
|
||||
|
||||
This section will cover how to use Twython and interact with some a little more advanced API calls
|
||||
|
||||
Before you make any API calls, make sure you :ref:`authenticated <starting-out>` the user!
|
||||
|
||||
.. note:: All sections on this page will assume you're using a Twython instance
|
||||
|
||||
*******************************************************************************
|
||||
|
||||
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)
|
||||
|
||||
Updating Status with Image
|
||||
--------------------------
|
||||
|
||||
Documentation: https://dev.twitter.com/docs/api/1.1/get/account/verify_credentials
|
||||
|
||||
::
|
||||
|
||||
photo = open('/path/to/file/image.jpg', 'rb')
|
||||
twitter.update_status_with_media(status='Checkout this cool image!', media=photo)
|
||||
|
||||
So now you can authenticate, update your status (with or without an image), search Twitter, and a few other things! Good luck!
|
||||
|
|
@ -7,23 +7,24 @@ This section will cover how to use Twython and interact with some basic Twitter
|
|||
|
||||
Before you make any API calls, make sure you :ref:`authenticated <starting-out>` the user!
|
||||
|
||||
.. note:: All sections on this page will assume you're using a Twython instance
|
||||
|
||||
*******************************************************************************
|
||||
|
||||
Authenticated Calls
|
||||
-------------------
|
||||
|
||||
OAuth 1
|
||||
~~~~~~~
|
||||
|
||||
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)
|
||||
|
||||
.. admonition:: Important
|
||||
|
||||
All sections on this page will assume you're using a Twython instance
|
||||
|
||||
What Twython Returns
|
||||
--------------------
|
||||
|
||||
Twython returns a dictionary of JSON response from Twitter
|
||||
|
||||
User Information
|
||||
----------------
|
||||
^^^^^^^^^^^^^^^^
|
||||
|
||||
Documentation: https://dev.twitter.com/docs/api/1.1/get/account/verify_credentials
|
||||
|
||||
|
|
@ -32,7 +33,7 @@ Documentation: https://dev.twitter.com/docs/api/1.1/get/account/verify_credentia
|
|||
twitter.verify_credentials()
|
||||
|
||||
Authenticated Users Home Timeline
|
||||
---------------------------------
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
Documentation: https://dev.twitter.com/docs/api/1.1/get/statuses/home_timeline
|
||||
|
||||
|
|
@ -40,23 +41,10 @@ Documentation: https://dev.twitter.com/docs/api/1.1/get/statuses/home_timeline
|
|||
|
||||
twitter.get_home_timeline()
|
||||
|
||||
Search
|
||||
------
|
||||
|
||||
Documentation: https://dev.twitter.com/docs/api/1.1/get/search/tweets
|
||||
|
||||
::
|
||||
|
||||
twitter.search(q='python')
|
||||
|
||||
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"
|
||||
|
||||
::
|
||||
|
||||
twitter.search(q='python', result_type='popular')
|
||||
|
||||
Updating Status
|
||||
---------------
|
||||
^^^^^^^^^^^^^^^
|
||||
|
||||
This method makes use of dynamic arguments, :ref:`read more about them <dynamicargexplaination>`
|
||||
|
||||
Documentation: https://dev.twitter.com/docs/api/1/post/statuses/update
|
||||
|
||||
|
|
@ -64,3 +52,33 @@ Documentation: https://dev.twitter.com/docs/api/1/post/statuses/update
|
|||
|
||||
twitter.update_status(status='See how easy using Twython is!')
|
||||
|
||||
|
||||
OAuth 2
|
||||
~~~~~~~
|
||||
|
||||
Create a Twython instance with your application key and access token::
|
||||
|
||||
from twython import Twython
|
||||
twitter = Twython(APP_KEY, access_token=ACCESS_TOKEN)
|
||||
|
||||
.. _howtosearch:
|
||||
|
||||
Searching
|
||||
---------
|
||||
|
||||
.. note:: Searching can be done whether you're authenticated via OAuth 1 or OAuth 2
|
||||
|
||||
Documentation: https://dev.twitter.com/docs/api/1.1/get/search/tweets
|
||||
|
||||
::
|
||||
|
||||
twitter.search(q='python')
|
||||
|
||||
.. _dynamicargexplaination:
|
||||
|
||||
.. 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"
|
||||
|
||||
::
|
||||
|
||||
twitter.search(q='python', result_type='popular')
|
||||
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ Installation
|
|||
|
||||
Information on how to properly install Twython
|
||||
|
||||
*******************************************************************************
|
||||
|
||||
Pip or Easy Install
|
||||
-------------------
|
||||
|
|
@ -40,3 +41,7 @@ Feel free to clone the repository::
|
|||
Now that you have the source code, install it into your site-packages directory::
|
||||
|
||||
$ python setup.py install
|
||||
|
||||
*******************************************************************************
|
||||
|
||||
So Twython is installed! Now, head over to the :ref:`starting out <starting-out>` section.
|
||||
|
|
@ -5,6 +5,8 @@ Starting Out
|
|||
|
||||
This section is going to help you understand creating a Twitter Application, authenticating a user, and making basic API calls
|
||||
|
||||
*******************************************************************************
|
||||
|
||||
Beginning
|
||||
---------
|
||||
|
||||
|
|
@ -17,6 +19,20 @@ Now you're ready to start authentication!
|
|||
Authentication
|
||||
--------------
|
||||
|
||||
Twython offers support for both OAuth 1 and OAuth 2 authentication.
|
||||
|
||||
The difference:
|
||||
|
||||
- :ref:`OAuth 1 <oauth1>` is for user authenticated calls (tweeting, following people, sneding DMs, etc.)
|
||||
- :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)
|
||||
|
||||
.. _oauth1:
|
||||
|
||||
OAuth 1 (User Authentication)
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. 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.
|
||||
|
||||
First, you'll want to import Twython::
|
||||
|
||||
from twython import Twython
|
||||
|
|
@ -40,7 +56,7 @@ Send the user to the authentication url, you can obtain it by accessing::
|
|||
auth['auth_url']
|
||||
|
||||
Handling the Callback
|
||||
---------------------
|
||||
^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
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``
|
||||
|
||||
|
|
@ -64,16 +80,63 @@ Once you have the final user tokens, store them in a database for later use!::
|
|||
OAUTH_TOKEN = final_step['oauth_token']
|
||||
OAUTH_TOKEN_SECERT = final_step['oauth_token_secret']
|
||||
|
||||
.. _oauth2:
|
||||
|
||||
OAuth 2 (Application Authentication)
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. 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
|
||||
|
||||
OAuth 2 authentication is 100x easier than OAuth 1.
|
||||
Let's say you *just* made your application and have your ``Consumer Key`` and ``Consumer Secret``
|
||||
|
||||
First, you'll want to import Twython::
|
||||
|
||||
from twython import Twython
|
||||
|
||||
Don't have an OAuth 2 `access_token`?
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
::
|
||||
|
||||
APP_KEY = 'YOUR_APP_KEY'
|
||||
APP_SECET = 'YOUR_APP_SECRET'
|
||||
|
||||
twitter = Twython(APP_KEY, APP_SECRET, oauth_version=2)
|
||||
ACCESS_TOKEN = twitter.obtain_access_token()
|
||||
|
||||
# Save ACCESS_TOKEN in a database or something for later use!
|
||||
|
||||
Already have one?
|
||||
^^^^^^^^^^^^^^^^^
|
||||
|
||||
::
|
||||
|
||||
APP_KEY = 'YOUR_APP_KEY'
|
||||
ACCESS_TOKEN = 'YOUR_ACCESS_TOKEN'
|
||||
|
||||
twitter = Twython(APP_KEY, access_token=ACCESS_TOKEN)
|
||||
|
||||
Now that you have your OAuth 2 access_token, maybe you'll want to perform a :ref:`search <howtosearch>` or something
|
||||
|
||||
The Twython API Table
|
||||
---------------------
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
.. _dynamicfunctionarguments:
|
||||
|
||||
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.
|
||||
|
||||
-----------------------
|
||||
What Twython Returns
|
||||
--------------------
|
||||
|
||||
Now that you have your application tokens and user tokens, check out the :ref:`basic usage <basic-usage>` section.
|
||||
Twython returns native Python objects. We convert the JSON sent to us from Twitter to an object so you don't have to.
|
||||
|
||||
|
||||
*******************************************************************************
|
||||
|
||||
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.
|
||||
|
|
|
|||
51
docs/usage/streaming_api.rst
Normal file
51
docs/usage/streaming_api.rst
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
.. _streaming-api:
|
||||
|
||||
Streaming API
|
||||
=============
|
||||
|
||||
This section will cover how to use Twython and interact with the Twitter Streaming API.
|
||||
|
||||
Streaming Documentation: https://dev.twitter.com/docs/streaming-apis
|
||||
|
||||
.. 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!
|
||||
|
||||
Setting Up Your Streamer
|
||||
------------------------
|
||||
|
||||
.. note:: When stream data is sent back to Twython, we send the data through signals (i.e. ``on_success``, ``on_error``, etc.)
|
||||
|
||||
Make sure you import ``TwythonStreamer``
|
||||
|
||||
::
|
||||
|
||||
from twython import TwythonStreamer
|
||||
|
||||
Now set up how you want to handle the signals.
|
||||
|
||||
::
|
||||
|
||||
class MyStreamer(TwythonStreamer):
|
||||
def on_success(self, data):
|
||||
if 'text' in data:
|
||||
print data['text'].encode('utf-8')
|
||||
|
||||
def on_error(self, status_code, data):
|
||||
print status_code
|
||||
|
||||
# Want to stop trying to get data because of the error?
|
||||
# Uncomment the next line!
|
||||
# self.disconnect()
|
||||
|
||||
More signals that you can extend on can be found in the Developer Interface section under :ref:`Streaming Interface <streaming_interface>`
|
||||
|
||||
Filtering Public Statuses
|
||||
-------------------------
|
||||
|
||||
::
|
||||
|
||||
stream = MyStreamer(APP_KEY, APP_SECRET,
|
||||
OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
|
||||
stream.statuses.filter(track='twitter')
|
||||
|
||||
With the code above, data should be flowing in.
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue