From 8ab6ef868e1fcad15da2bc886e509b2c3b7cc5c6 Mon Sep 17 00:00:00 2001 From: smaeda-ks Date: Thu, 21 Nov 2019 18:17:36 +0900 Subject: [PATCH 1/2] Update example scripts --- examples/analytics.py | 4 ++-- examples/media_library.py | 30 +++++++++++++++++++----------- examples/media_upload.py | 19 ++++++++++--------- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/examples/analytics.py b/examples/analytics.py index 6f1d976..a85e95a 100644 --- a/examples/analytics.py +++ b/examples/analytics.py @@ -62,10 +62,10 @@ seconds = 30 time.sleep(seconds) -async_stats_job_results = LineItem.async_stats_job_result(account, queued_job_ids) +async_stats_job_results = LineItem.async_stats_job_result(account, job_ids=queued_job_ids) async_data = [] for result in async_stats_job_results: - async_data.append(LineItem.async_stats_job_data(account, result.url)) + async_data.append(LineItem.async_stats_job_data(account, url=result.url)) print(async_data) diff --git a/examples/media_library.py b/examples/media_library.py index 2d78fbc..d6535fb 100644 --- a/examples/media_library.py +++ b/examples/media_library.py @@ -4,11 +4,11 @@ from twitter_ads.http import Request -CONSUMER_KEY = '' -CONSUMER_SECRET = '' -ACCESS_TOKEN = '' -ACCESS_TOKEN_SECRET = '' -ACCOUNT_ID = '' +CONSUMER_KEY = 'your consumer key' +CONSUMER_SECRET = 'your consumer secret' +ACCESS_TOKEN = 'user access token' +ACCESS_TOKEN_SECRET = 'user access token secret' +ACCOUNT_ID = 'ads account id' # initialize the client client = Client(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET) @@ -17,16 +17,24 @@ account = client.accounts(ACCOUNT_ID) # upload an image to POST media/upload +# https://developer.twitter.com/en/docs/ads/creatives/guides/media-library resource = '/1.1/media/upload.json' +params = { + 'additional_owners': '756201191646691328', + 'media_category': MEDIA_CATEGORY.TWEET_IMAGE +} domain = 'https://upload.twitter.com' files = {'media': (None, open('/path/to/file.jpg', 'rb'))} -response = Request(client, 'post', resource, files=files, domain=domain).perform() -media_id = response.body['media_id'] +response = Request(client, 'post', resource, files=files, domain=domain, params=params).perform() +media_key = response.body['media_key'] # add to media library media_library = MediaLibrary(account) media_library.name = 'name' -media_library.file_name = 'name.jpeg' -media_library.media_id = media_id -media_library.media_category = MEDIA_CATEGORY.TWEET_IMAGE -media_library.save() +media_library.file_name = 'name.png' +media_library.media_key = media_key +data = media_library.add() + +# update the media +data.name = 'name - updated' +data.update() diff --git a/examples/media_upload.py b/examples/media_upload.py index ce204be..125578d 100644 --- a/examples/media_upload.py +++ b/examples/media_upload.py @@ -2,26 +2,27 @@ from twitter_ads.client import Client from twitter_ads.http import Request +from twitter_ads.enum import MEDIA_CATEGORY CONSUMER_KEY = 'your consumer key' CONSUMER_SECRET = 'your consumer secret' ACCESS_TOKEN = 'user access token' ACCESS_TOKEN_SECRET = 'user access token secret' -ADS_ACCOUNT = 'ads account id' +ACCOUNT_ID = 'ads account id' # initialize the twitter ads api client client = Client(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET) -# using the Request object you can manually call any twitter API resource including media uploads. - -# note: video uploads a bit more specialized and should be done using the -# twitter_ads.http.MediaUpload class. - # upload an image to POST media/upload +# https://developer.twitter.com/en/docs/ads/creatives/guides/media-library resource = '/1.1/media/upload.json' +params = { + 'additional_owners': '756201191646691328', + 'media_category': MEDIA_CATEGORY.TWEET_IMAGE +} domain = 'https://upload.twitter.com' files = {'media': (None, open('/path/to/file.jpg', 'rb'))} -response = Request(client, 'post', resource, files=files, domain=domain).perform() +response = Request(client, 'post', resource, files=files, domain=domain, params=params).perform() -# extract the media_id value from the response -media_id = response.body['media_id'] +# extract the media_key value from the response +media_key = response.body['media_key'] From 601e842d717dce9275f1e5e0f78ecd373e9f62c5 Mon Sep 17 00:00:00 2001 From: smaeda-ks Date: Thu, 21 Nov 2019 18:19:44 +0900 Subject: [PATCH 2/2] Fix MediaLibrary class - duplicate save() method and introduce add() = POST and update() = PUT methods --- twitter_ads/creative.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/twitter_ads/creative.py b/twitter_ads/creative.py index 9087913..87023d7 100644 --- a/twitter_ads/creative.py +++ b/twitter_ads/creative.py @@ -389,16 +389,18 @@ def reload(self, **kwargs): return self.from_response(response.body['data']) - def save(self): - if self.media_key: - method = 'put' - resource = self.RESOURCE.format(account_id=self.account.id, id=self.media_key) - else: - method = 'post' - resource = self.RESOURCE_COLLECTION.format(account_id=self.account.id) + def add(self): + resource = self.RESOURCE_COLLECTION.format(account_id=self.account.id) + response = Request( + self.account.client, 'post', + resource, params=self.to_params()).perform() + + return self.from_response(response.body['data']) + def update(self): + resource = self.RESOURCE.format(account_id=self.account.id, id=self.media_key) response = Request( - self.account.client, method, + self.account.client, 'put', resource, params=self.to_params()).perform() return self.from_response(response.body['data'])