Skip to content

Commit

Permalink
Ads API v11 (#296)
Browse files Browse the repository at this point in the history
* updated cards class and examples

* added tests

* remove deprecated card types

* line item budget optimization changes

* bump version

* removed references to video website cards

* removed start/end times from campaigns

* Remove _str versions of parameters

Co-authored-by: Tom Osowski <[email protected]>
  • Loading branch information
tushdante and osowskit authored May 27, 2022
1 parent 2567d75 commit a3dd581
Show file tree
Hide file tree
Showing 15 changed files with 1,064 additions and 176 deletions.
80 changes: 70 additions & 10 deletions examples/cards.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,89 @@
from twitter_ads.client import Client
from twitter_ads.creative import Card
from twitter_ads.campaign import Tweet
from twitter_ads.restapi import UserIdLookup
from twitter_ads.http import Request


CONSUMER_KEY = 'your consumer key'
CONSUMER_SECRET = 'your consumer secret'
ACCESS_TOKEN = 'access token'
ACCESS_TOKEN_SECRET = 'access token secret'
ACCOUNT_ID = 'account id'
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)

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

# create the card
name = 'video website card'
components = [{"type":"MEDIA","media_key":"13_1191948012077092867"},{"type":"DETAILS","title":"Twitter","destination":{"type":"WEBSITE", "url":"http://twitter.com/"}}]
video_website_card = Card.create(account, name=name, components=components)
# fetch all
card = Card.all(account, card_ids="1502039998987587584").first

# fetch by card-id
card = Card.load(account=account, id="1502039998987587584")

# edit card destination.url
card.components= [
{
"media_key": "13_794652834998325248",
"media_metadata": {
"13_794652834998325248": {
"type": "VIDEO",
"url": "https://video.twimg.com/amplify_video/794652834998325248/vid/640x360/pUgE2UKcfPwF_5Uh.mp4",
"width": 640,
"height": 360,
"video_duration": 7967,
"video_aspect_ratio": "16:9"
}
},
"type": "MEDIA"
},
{
"title": "Twitter",
"destination": {
"url": "http://twitter.com/newvalue",
"type": "WEBSITE"
},
"type": "DETAILS"
}
]

card.save()
print(card.components)

# create new card
newcard = Card(account=account)
newcard.name="my new card"
components= [
{
"media_key": "13_794652834998325248",
"media_metadata": {
"13_794652834998325248": {
"type": "VIDEO",
"url": "https://video.twimg.com/amplify_video/794652834998325248/vid/640x360/pUgE2UKcfPwF_5Uh.mp4",
"width": 640,
"height": 360,
"video_duration": 7967,
"video_aspect_ratio": "16:9"
}
},
"type": "MEDIA"
},
{
"title": "Twitter",
"destination": {
"url": "http://twitter.com/login",
"type": "WEBSITE"
},
"type": "DETAILS"
}
]
newcard.components=components
newcard.save()
print(newcard.id)

# get user_id for as_user_id parameter
user_id = UserIdLookup.load(account, screen_name='your_twitter_handle_name').id

# create a tweet using this new card
Tweet.create(account, text='Created from the SDK', as_user_id=user_id, card_uri=video_website_card.card_uri)
Tweet.create(account, text='Created from the SDK', as_user_id=user_id, card_uri=card.card_uri)
# https://twitter.com/apimctestface/status/1372283476615958529
10 changes: 5 additions & 5 deletions examples/draft_tweet.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,27 @@
# fetch draft tweets from a given account
tweets = DraftTweet.all(account)
for tweet in tweets:
print(tweet.id_str)
print(tweet.id)
print(tweet.text)

# create a new draft tweet
draft_tweet = DraftTweet(account)
draft_tweet.text = 'draft tweet - new'
draft_tweet.as_user_id = user_id
draft_tweet = draft_tweet.save()
print(draft_tweet.id_str)
print(draft_tweet.id)
print(draft_tweet.text)

# fetch single draft tweet metadata
tweet_id = draft_tweet.id_str
tweet_id = draft_tweet.id
draft_tweet = draft_tweet.load(account, tweet_id)
print(draft_tweet.id_str)
print(draft_tweet.id)
print(draft_tweet.text)

# update (PUT) metadata
draft_tweet.text = 'draft tweet - update'
draft_tweet = draft_tweet.save()
print(draft_tweet.id_str)
print(draft_tweet.id)
print(draft_tweet.text)

# create a nullcasted tweet using draft tweet metadata
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/analytics_async_get.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"start_time": "2019-01-01T00:00:00Z",
"segmentation_type": null,
"url": "https://ton.twimg.com/advertiser-api-async-analytics/stats.json.gz",
"id_str": "111111111111111111",
"id": "111111111111111111",
"entity_ids": [
"aaaa"
],
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/analytics_async_post.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"start_time": "2019-01-01T00:00:00Z",
"segmentation_type": null,
"url": null,
"id_str": "111111111111111111",
"id": "111111111111111111",
"entity_ids": [
"aaaa"
],
Expand Down
Loading

0 comments on commit a3dd581

Please sign in to comment.