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'] 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'])