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
32 changes: 32 additions & 0 deletions examples/media_library.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from twitter_ads.client import Client
from twitter_ads.creative import MediaLibrary
from twitter_ads.enum import MEDIA_CATEGORY
from twitter_ads.http import Request


CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_TOKEN = ''
ACCESS_TOKEN_SECRET = ''
ACCOUNT_ID = ''

# initialize the client
client = Client(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

# load the advertiser account instance
account = client.accounts(ACCOUNT_ID)

# upload an image to POST media/upload
resource = '/1.1/media/upload.json'
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']

# 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()
59 changes: 59 additions & 0 deletions twitter_ads/creative.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,62 @@ def preview(self):
resource_property(ScheduledTweet, 'nullcast', transform=TRANSFORM.BOOL)
resource_property(ScheduledTweet, 'scheduled_at', transform=TRANSFORM.TIME)
resource_property(ScheduledTweet, 'text')


class MediaLibrary(Resource, Persistence):

PROPERTIES = {}

RESOURCE_COLLECTION = '/' + API_VERSION + '/accounts/{account_id}/media_library'
RESOURCE = '/' + API_VERSION + '/accounts/{account_id}/media_library/{id}'

def reload(self, **kwargs):
if not self.media_key:
return self

resource = self.RESOURCE.format(account_id=self.account.id, id=self.media_key)
response = Request(self.account.client, 'get', resource, params=kwargs).perform()

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)

response = Request(
self.account.client, method,
resource, params=self.to_params()).perform()

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

def delete(self):
resource = self.RESOURCE.format(account_id=self.account.id, id=self.media_key)
response = Request(self.account.client, 'delete', resource).perform()
self.from_response(response.body['data'])


# media library properties
# read-only
resource_property(MediaLibrary, 'aspect_ratio', readonly=True)
resource_property(MediaLibrary, 'created_at', readonly=True, transform=TRANSFORM.TIME)
resource_property(MediaLibrary, 'deleted', readonly=True, transform=TRANSFORM.BOOL)
resource_property(MediaLibrary, 'duration', readonly=True, transform=TRANSFORM.INT)
resource_property(MediaLibrary, 'media_status', readonly=True)
resource_property(MediaLibrary, 'media_type', readonly=True)
resource_property(MediaLibrary, 'media_url', readonly=True)
resource_property(MediaLibrary, 'tweeted', readonly=True, transform=TRANSFORM.BOOL)
resource_property(MediaLibrary, 'updated_at', readonly=True, transform=TRANSFORM.TIME)
# writable
resource_property(MediaLibrary, 'media_category')
resource_property(MediaLibrary, 'media_id')
resource_property(MediaLibrary, 'media_key')
resource_property(MediaLibrary, 'description')
resource_property(MediaLibrary, 'file_name')
resource_property(MediaLibrary, 'name')
resource_property(MediaLibrary, 'poster_image_media_id')
resource_property(MediaLibrary, 'poster_image_media_key')
resource_property(MediaLibrary, 'title')
13 changes: 13 additions & 0 deletions twitter_ads/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,19 @@ def enum(**enums):
TOTAL='TOTAL'
)

MEDIA_CATEGORY = enum(
AMPLIFY_VIDEO='AMPLIFY_VIDEO',
TWEET_GIF='TWEET_GIF',
TWEET_IMAGE='TWEET_IMAGE',
TWEET_VIDEO='TWEET_VIDEO'
)

MEDIA_TYPE = enum(
GIF='GIF',
IMAGE='IMAGE',
VIDEO='VIDEO'
)

METRIC_GROUP = enum(
ENGAGEMENT='ENGAGEMENT',
WEB_CONVERSION='WEB_CONVERSION',
Expand Down