Skip to content

Commit 21a0c72

Browse files
authored
[minor] bug fix (#235)
* Update example scripts * Fix MediaLibrary class - duplicate save() method and introduce add() = POST and update() = PUT methods
1 parent c0ecae5 commit 21a0c72

File tree

4 files changed

+41
-30
lines changed

4 files changed

+41
-30
lines changed

examples/analytics.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@
6262
seconds = 30
6363
time.sleep(seconds)
6464

65-
async_stats_job_results = LineItem.async_stats_job_result(account, queued_job_ids)
65+
async_stats_job_results = LineItem.async_stats_job_result(account, job_ids=queued_job_ids)
6666

6767
async_data = []
6868
for result in async_stats_job_results:
69-
async_data.append(LineItem.async_stats_job_data(account, result.url))
69+
async_data.append(LineItem.async_stats_job_data(account, url=result.url))
7070

7171
print(async_data)

examples/media_library.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
from twitter_ads.http import Request
55

66

7-
CONSUMER_KEY = ''
8-
CONSUMER_SECRET = ''
9-
ACCESS_TOKEN = ''
10-
ACCESS_TOKEN_SECRET = ''
11-
ACCOUNT_ID = ''
7+
CONSUMER_KEY = 'your consumer key'
8+
CONSUMER_SECRET = 'your consumer secret'
9+
ACCESS_TOKEN = 'user access token'
10+
ACCESS_TOKEN_SECRET = 'user access token secret'
11+
ACCOUNT_ID = 'ads account id'
1212

1313
# initialize the client
1414
client = Client(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
@@ -17,16 +17,24 @@
1717
account = client.accounts(ACCOUNT_ID)
1818

1919
# upload an image to POST media/upload
20+
# https://developer.twitter.com/en/docs/ads/creatives/guides/media-library
2021
resource = '/1.1/media/upload.json'
22+
params = {
23+
'additional_owners': '756201191646691328',
24+
'media_category': MEDIA_CATEGORY.TWEET_IMAGE
25+
}
2126
domain = 'https://upload.twitter.com'
2227
files = {'media': (None, open('/path/to/file.jpg', 'rb'))}
23-
response = Request(client, 'post', resource, files=files, domain=domain).perform()
24-
media_id = response.body['media_id']
28+
response = Request(client, 'post', resource, files=files, domain=domain, params=params).perform()
29+
media_key = response.body['media_key']
2530

2631
# add to media library
2732
media_library = MediaLibrary(account)
2833
media_library.name = 'name'
29-
media_library.file_name = 'name.jpeg'
30-
media_library.media_id = media_id
31-
media_library.media_category = MEDIA_CATEGORY.TWEET_IMAGE
32-
media_library.save()
34+
media_library.file_name = 'name.png'
35+
media_library.media_key = media_key
36+
data = media_library.add()
37+
38+
# update the media
39+
data.name = 'name - updated'
40+
data.update()

examples/media_upload.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,27 @@
22

33
from twitter_ads.client import Client
44
from twitter_ads.http import Request
5+
from twitter_ads.enum import MEDIA_CATEGORY
56

67
CONSUMER_KEY = 'your consumer key'
78
CONSUMER_SECRET = 'your consumer secret'
89
ACCESS_TOKEN = 'user access token'
910
ACCESS_TOKEN_SECRET = 'user access token secret'
10-
ADS_ACCOUNT = 'ads account id'
11+
ACCOUNT_ID = 'ads account id'
1112

1213
# initialize the twitter ads api client
1314
client = Client(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
1415

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-
2016
# upload an image to POST media/upload
17+
# https://developer.twitter.com/en/docs/ads/creatives/guides/media-library
2118
resource = '/1.1/media/upload.json'
19+
params = {
20+
'additional_owners': '756201191646691328',
21+
'media_category': MEDIA_CATEGORY.TWEET_IMAGE
22+
}
2223
domain = 'https://upload.twitter.com'
2324
files = {'media': (None, open('/path/to/file.jpg', 'rb'))}
24-
response = Request(client, 'post', resource, files=files, domain=domain).perform()
25+
response = Request(client, 'post', resource, files=files, domain=domain, params=params).perform()
2526

26-
# extract the media_id value from the response
27-
media_id = response.body['media_id']
27+
# extract the media_key value from the response
28+
media_key = response.body['media_key']

twitter_ads/creative.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -389,16 +389,18 @@ def reload(self, **kwargs):
389389

390390
return self.from_response(response.body['data'])
391391

392-
def save(self):
393-
if self.media_key:
394-
method = 'put'
395-
resource = self.RESOURCE.format(account_id=self.account.id, id=self.media_key)
396-
else:
397-
method = 'post'
398-
resource = self.RESOURCE_COLLECTION.format(account_id=self.account.id)
392+
def add(self):
393+
resource = self.RESOURCE_COLLECTION.format(account_id=self.account.id)
394+
response = Request(
395+
self.account.client, 'post',
396+
resource, params=self.to_params()).perform()
397+
398+
return self.from_response(response.body['data'])
399399

400+
def update(self):
401+
resource = self.RESOURCE.format(account_id=self.account.id, id=self.media_key)
400402
response = Request(
401-
self.account.client, method,
403+
self.account.client, 'put',
402404
resource, params=self.to_params()).perform()
403405

404406
return self.from_response(response.body['data'])

0 commit comments

Comments
 (0)