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
35 changes: 35 additions & 0 deletions examples/cards_fetch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from twitter_ads.client import Client
from twitter_ads.creative import CardsFetch
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)

# retrieve a Tweet
tweet_id = '973002610033610753' # use one of your own Tweets
resource = '/1.1/statuses/show/{id}.json'.format(id=tweet_id)
domain = 'https://api.twitter.com'
params = {'include_card_uri' : 'true'}
response = Request(client, 'get', resource, domain=domain, params=params).perform()
card_uri = response.body['card_uri'] # Tweet must include a card_uri card

# fetch by card_uri
cf = CardsFetch(account)
card = cf.load(account, card_uri=card_uri)
card.card_type # 'VIDEO_POLLS'
card.id # '5g83p'

# fetch by card id
same_card = cf.load(account, card_id=card.id)
same_card.card_type # 'VIDEO_POLLS'
same_card.card_uri # 'card://973002559269974016'
93 changes: 93 additions & 0 deletions twitter_ads/creative.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,3 +486,96 @@ class PollCard(Resource, Persistence):
resource_property(PollCard, 'name')
resource_property(PollCard, 'second_choice')
resource_property(PollCard, 'third_choice')


class CardsFetch(Resource):

PROPERTIES = {}

FETCH_URI = '/' + API_VERSION + '/accounts/{account_id}/cards'
FETCH_ID = '/' + API_VERSION + '/accounts/{account_id}/cards/all/{id}'

def all(klass):
raise AttributeError("'CardsFetch' object has no attribute 'all'")

def load(klass, account, card_uri=None, card_id=None, with_deleted=None):
# check whether both are specified or neither are specified
if all([card_uri, card_id]) or not any([card_uri, card_id]):
raise ValueError('card_uri and card_id are exclusive parameters. ' +
'Please supply one or the other, but not both.')
params = {}
if card_uri:
params['card_uri'] = card_uri
resource = klass.FETCH_URI.format(account_id=account.id)
else:
resource = klass.FETCH_ID.format(account_id=account.id, id=card_id)
if with_deleted:
params['with_deleted'] = 'true'
response = Request(account.client, 'get', resource, params=params).perform()
return klass.from_response(response.body['data'])

def reload(self):
if self.id:
self.load(self.account, card_id=self.id)


# card properties
# read-only
resource_property(CardsFetch, 'app_country_code', readonly=True)
resource_property(CardsFetch, 'app_cta', readonly=True)
resource_property(CardsFetch, 'card_type', readonly=True)
resource_property(CardsFetch, 'card_uri', readonly=True)
resource_property(CardsFetch, 'content_duration_seconds', readonly=True)
resource_property(CardsFetch, 'created_at', readonly=True, transform=TRANSFORM.TIME)
resource_property(CardsFetch, 'deleted', readonly=True, transform=TRANSFORM.BOOL)
resource_property(CardsFetch, 'duration_in_minutes', readonly=True)
resource_property(CardsFetch, 'end_time', readonly=True, transform=TRANSFORM.TIME)
resource_property(CardsFetch, 'first_choice', readonly=True)
resource_property(CardsFetch, 'first_cta', readonly=True)
resource_property(CardsFetch, 'first_cta_tweet', readonly=True)
resource_property(CardsFetch, 'first_cta_welcome_message_id', readonly=True)
resource_property(CardsFetch, 'fouth_choice', readonly=True)
resource_property(CardsFetch, 'fouth_cta', readonly=True)
resource_property(CardsFetch, 'fouth_cta_tweet', readonly=True)
resource_property(CardsFetch, 'fourth_cta_welcome_message_id', readonly=True)
resource_property(CardsFetch, 'googleplay_app_id', readonly=True)
resource_property(CardsFetch, 'googleplay_deep_link', readonly=True)
resource_property(CardsFetch, 'id', readonly=True)
resource_property(CardsFetch, 'image', readonly=True)
resource_property(CardsFetch, 'image_display_height', readonly=True)
resource_property(CardsFetch, 'image_display_width', readonly=True)
resource_property(CardsFetch, 'ipad_app_id', readonly=True)
resource_property(CardsFetch, 'ipad_deep_link', readonly=True)
resource_property(CardsFetch, 'iphone_app_id', readonly=True)
resource_property(CardsFetch, 'iphone_deep_link', readonly=True)
resource_property(CardsFetch, 'name', readonly=True)
resource_property(CardsFetch, 'preview_url', readonly=True)
resource_property(CardsFetch, 'recipient_user_id', readonly=True)
resource_property(CardsFetch, 'second_choice', readonly=True)
resource_property(CardsFetch, 'second_cta', readonly=True)
resource_property(CardsFetch, 'second_cta_tweet', readonly=True)
resource_property(CardsFetch, 'second_cta_welcome_message_id', readonly=True)
resource_property(CardsFetch, 'start_time', readonly=True, transform=TRANSFORM.TIME)
resource_property(CardsFetch, 'thank_you_text', readonly=True)
resource_property(CardsFetch, 'thank_you_url', readonly=True)
resource_property(CardsFetch, 'third_choice', readonly=True)
resource_property(CardsFetch, 'third_cta', readonly=True)
resource_property(CardsFetch, 'third_cta_tweet', readonly=True)
resource_property(CardsFetch, 'third_cta_welcome_message_id', readonly=True)
resource_property(CardsFetch, 'title', readonly=True)
resource_property(CardsFetch, 'updated_at', readonly=True, transform=TRANSFORM.TIME)
resource_property(CardsFetch, 'video_content_id', readonly=True)
resource_property(CardsFetch, 'video_height', readonly=True)
resource_property(CardsFetch, 'video_hls_url', readonly=True)
resource_property(CardsFetch, 'video_owner_id', readonly=True)
resource_property(CardsFetch, 'video_poster_height', readonly=True)
resource_property(CardsFetch, 'video_poster_url', readonly=True)
resource_property(CardsFetch, 'video_poster_width', readonly=True)
resource_property(CardsFetch, 'video_width', readonly=True)
resource_property(CardsFetch, 'video_url', readonly=True)
resource_property(CardsFetch, 'website_dest_url', readonly=True)
resource_property(CardsFetch, 'website_display_url', readonly=True)
resource_property(CardsFetch, 'website_shortened_url', readonly=True)
resource_property(CardsFetch, 'website_title', readonly=True)
resource_property(CardsFetch, 'website_url', readonly=True)
resource_property(CardsFetch, 'wide_app_image', readonly=True)