1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
.. _advanced-usage:
Advanced Usage
==============
This section will cover how to use Twython and interact with some more advanced API calls
Before you make any API calls, make sure you :ref:`authenticated the user <starting-out>` (or :ref:`app <oauth2>`)!
.. 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
.. code-block:: python
from twython import Twython
twitter = Twython(APP_KEY, APP_SECRET,
OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
Updating Status with Image
--------------------------
This uploads an image as a media object and associates it with a status update.
.. code-block:: python
photo = open('/path/to/file/image.jpg', 'rb')
response = twitter.upload_media(media=photo)
twitter.update_status(status='Checkout this cool image!', media_ids=[response['media_id']])
Documentation:
* https://developer.twitter.com/en/docs/api-reference-index
* https://developer.twitter.com/en/docs/media/upload-media/overview
Updating Status with Video
--------------------------
This uploads a video as a media object and associates it with a status update.
.. code-block:: python
video = open('/path/to/file/video.mp4', 'rb')
response = twitter.upload_video(media=video, media_type='video/mp4')
twitter.update_status(status='Checkout this cool video!', media_ids=[response['media_id']])
Documentation:
* https://developer.twitter.com/en/docs/api-reference-index
* https://developer.twitter.com/en/docs/media/upload-media/overview
Posting a Status with an Editing Image
--------------------------------------
This example resizes an image, then uploads it as a media object and associates it
with a status update.
.. code-block:: python
# Assume you are working with a JPEG
from PIL import Image
try:
# Python 3
from io import StringIO
except ImportError:
# Python 2
from StringIO import StringIO
photo = Image.open('/path/to/file/image.jpg')
basewidth = 320
wpercent = (basewidth / float(photo.size[0]))
height = int((float(photo.size[1]) * float(wpercent)))
photo = photo.resize((basewidth, height), Image.ANTIALIAS)
image_io = StringIO.StringIO()
photo.save(image_io, format='JPEG')
# If you do not seek(0), the image will be at the end of the file and
# unable to be read
image_io.seek(0)
response = twitter.upload_media(media=image_io)
twitter.update_status(status='Checkout this cool image!', media_ids=[response['media_id']])
Search Generator
----------------
So, if you're pretty into Python, you probably know about `generators <http://docs.python.org/2/tutorial/classes.html#generators>`_
That being said, Twython offers a generator for search results and can be accessed by using the following code:
.. code-block:: python
from twython import Twython
twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN,
OAUTH_TOKEN_SECRET)
results = twitter.cursor(twitter.search, q='python')
for result in results:
print(result)
Manipulate the Request (headers, proxies, etc.)
-----------------------------------------------
There are times when you may want to turn SSL verification off, send custom headers, or add proxies for the request to go through.
Twython uses the `requests <http://python-requests.org>`_ library to make API calls to Twitter. ``requests`` accepts a few parameters to allow developers to manipulate the acutal HTTP request.
Here is an example of sending custom headers to a Twitter API request:
.. code-block:: python
from twython import Twython
client_args = {
'headers': {
'User-Agent': 'My App Name'
}
}
twitter = Twython(APP_KEY, APP_SECRET,
OAUTH_TOKEN, OAUTH_TOKEN_SECRET,
client_args=client_args)
Here is an example of sending the request through proxies:
.. code-block:: python
from twython import Twython
client_args = {
'proxies': {
'http': 'http://10.0.10.1:8000',
'https': 'https://10.0.10.1:8001',
}
}
twitter = Twython(APP_KEY, APP_SECRET,
OAUTH_TOKEN, OAUTH_TOKEN_SECRET,
client_args=client_args)
or both (and set a timeout variable):
.. code-block:: python
from twython import Twython
client_args = {
'headers': {
'User-Agent': 'My App Name'
},
'proxies': {
'http': 'http://10.0.10.1:8000',
'https': 'https://10.0.10.1:8001',
}
'timeout': 300,
}
twitter = Twython(APP_KEY, APP_SECRET,
OAUTH_TOKEN, OAUTH_TOKEN_SECRET,
client_args=client_args)
Access Headers of Previous Call
-------------------------------
There are times when you may want to check headers from the previous call.
If you wish to access headers (ex. x-rate-limit-remaining, x-rate-limit-reset, content-type), you'll use the ``get_lastfunction_header`` method.
.. code-block:: python
from twython import Twython
twitter = Twython(APP_KEY, APP_SECRET,
OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
twitter.get_home_timeline()
twitter.get_lastfunction_header('x-rate-limit-remaining')
So now you can authenticate, update your status (with or without an image), search Twitter, and a few other things! Good luck!
|