Skip to content

Commit 44e65c3

Browse files
committedFeb 26, 2016
Merge pull request #56 from twitterdev/image-upload
[minor] adding support for manual image media uploads, adding example
2 parents c13ad09 + a75a214 commit 44e65c3

File tree

5 files changed

+45
-9
lines changed

5 files changed

+45
-9
lines changed
 

‎examples/media_upload.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright (C) 2015 Twitter, Inc.
2+
3+
from twitter_ads.client import Client
4+
from twitter_ads.http import Request
5+
6+
CONSUMER_KEY = 'your consumer key'
7+
CONSUMER_SECRET = 'your consumer secret'
8+
ACCESS_TOKEN = 'user access token'
9+
ACCESS_TOKEN_SECRET = 'user access token secret'
10+
ADS_ACCOUNT = 'ads account id'
11+
12+
# initialize the twitter ads api client
13+
client = Client(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
14+
15+
# using the Request object you can manually call any twitter API resource including media uploads.
16+
17+
# note: video uploads a bit more specialized and should be done using the
18+
# twitter_ads.http.MediaUpload class.
19+
20+
# upload an image to POST media/upload
21+
resource = '/1.1/media/upload.json'
22+
domain = 'https://upload.twitter.com'
23+
files = {'media': (None, open('/path/to/file.jpg', 'rb'))}
24+
response = Request(client, 'post', resource, files=files, domain=domain).perform()
25+
26+
# extract the media_id value from the response
27+
media_id = response.body['media_id']

‎twitter_ads/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Copyright (C) 2015 Twitter, Inc.
22

3-
VERSION = (0, 2, 0)
3+
VERSION = (0, 2, 1)
44

55
from twitter_ads.utils import get_version
66

‎twitter_ads/campaign.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,16 @@ def preview(klass, account, **kwargs):
196196
"""
197197
Returns an HTML preview of a tweet, either new or existing.
198198
"""
199-
resource = klass.TWEET_ID_PREVIEW if kwargs.get('id') else klass.TWEET_PREVIEW
200-
resource = resource.format(account_id=account.id, id=kwargs.get('id'))
201-
response = Request(account.client, 'get', resource, params=kwargs).perform()
199+
params = {}
200+
params.update(kwargs)
201+
202+
# handles array to string conversion for media IDs
203+
if 'media_ids' in params and isinstance(params['media_ids'], list):
204+
params['media_ids'] = ','.join(map(params['media_ids']))
205+
206+
resource = klass.TWEET_ID_PREVIEW if params.get('id') else klass.TWEET_PREVIEW
207+
resource = resource.format(account_id=account.id, id=params.get('id'))
208+
response = Request(account.client, 'get', resource, params=params).perform()
202209
return response.body['data']
203210

204211
@classmethod
@@ -208,6 +215,11 @@ def create(klass, account, status, **kwargs):
208215
"""
209216
params = {'status': status}
210217
params.update(kwargs)
218+
219+
# handles array to string conversion for media IDs
220+
if 'media_ids' in params and isinstance(params['media_ids'], list):
221+
params['media_ids'] = ','.join(map(params['media_ids']))
222+
211223
resource = klass.TWEET_CREATE.format(account_id=account.id)
212224
response = Request(account.client, 'post', resource, params=params).perform()
213225
return response.body['data']

‎twitter_ads/error.py

-4
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,6 @@ def __init__(self, response, **kwargs):
9595
super(self, response, **kwargs)
9696
self._retry_after = response.headers.get('retry-after', None)
9797

98-
@property
99-
def reset_at(self):
100-
return self._reset_at
101-
10298
@property
10399
def retry_after(self):
104100
return self._retry_after

‎twitter_ads/http.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ def __oauth_request(self):
8181

8282
params = self.options.get('params', None)
8383
data = self.options.get('body', None)
84+
files = self.options.get('files', None)
8485

8586
consumer = OAuth1Session(
8687
self._client.consumer_key,
@@ -90,7 +91,7 @@ def __oauth_request(self):
9091

9192
url = self.__domain() + self._resource
9293
method = getattr(consumer, self._method)
93-
response = method(url, headers=headers, data=data, params=params)
94+
response = method(url, headers=headers, data=data, params=params, files=files)
9495

9596
return Response(response.status_code, response.headers,
9697
body=response.raw, raw_body=response.text)

0 commit comments

Comments
 (0)
Please sign in to comment.