Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
30 changes: 19 additions & 11 deletions examples/media_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
19 changes: 10 additions & 9 deletions examples/media_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
18 changes: 10 additions & 8 deletions twitter_ads/creative.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down