Skip to content

Commit

Permalink
v9 update for TA->CA (#278)
Browse files Browse the repository at this point in the history
* Update audience.py

* Update test_targeted_audiences.py

* Update __init__.py

* Create custom_audience.py

* Update account.py

* Update campaign.py

* Update custom_audience.py

* Create custom_audience_load.json

* Rename custom_audience_load.json to custom_audiences_load.json

* Rename tailored_audiences_all.json to custom_audiences_all.json

* Update and rename tailored_audiences_permissions_all.json to custom_audiences_permissions_all.json
  • Loading branch information
BJapac authored May 6, 2021
1 parent ebb8202 commit 17ffd57
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 78 deletions.
39 changes: 39 additions & 0 deletions examples/custom_audience.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import hashlib
from twitter_ads.client import Client
from twitter_ads.audience import CustomAudience

CONSUMER_KEY = 'your consumer key'
CONSUMER_SECRET = 'your consumer secret'
ACCESS_TOKEN = 'access token'
ACCESS_TOKEN_SECRET = 'access token secret'
ACCOUNT_ID = '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 a new custom audience
audience = CustomAudience.create(account, 'test CA')

# sample user
# all values musth be sha256 hashed
email_hash = hashlib.sha256("[email protected]").hexdigest()

# create payload
user = [{
"operation_type": "Update",
"params": {
"users": [{
"email": [
email_hash
]
}]
}
}]

# update the custom audience
success_count, total_count = audience.users(user)
if success_count == total_count:
print(("Successfully added {total_count} users").format(total_count=total_count))
File renamed without changes.
29 changes: 29 additions & 0 deletions tests/fixtures/custom_audiences_load.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"data_type": "custom_audience",
"data": {
"targetable": false,
"name": "TA #2",
"targetable_types": [
"WEB",
"EXCLUDED_WEB"
],
"audience_type": "WEB",
"id": "abc2",
"reasons_not_targetable": [
"TOO_SMALL"
],
"list_type": null,
"created_at": "2014-03-09T20:35:41Z",
"updated_at": "2014-06-11T09:38:06Z",
"partner_source": "OTHER",
"deleted": false,
"audience_size": null
},
"request": {
"params": {
"account_id": "2iqph",
"name": "TA #2",
"list_type": "EMAIL"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"request": {
"params": {
"account_id": "2iqph",
"tailored_audience_id": "abc2"
"custom_audience_id": "abc2"
}
},
"data": [
{
"tailored_audience_id": "abc2",
"custom_audience_id": "abc2",
"permission_level": "READ_ONLY",
"id": "k",
"created_at": "2016-04-09T18:16:32Z",
Expand All @@ -16,7 +16,7 @@
"deleted": false
},
{
"tailored_audience_id": "abc2",
"custom_audience_id": "abc2",
"permission_level": "READ_ONLY",
"id": "l",
"created_at": "2016-04-09T18:22:33Z",
Expand All @@ -25,7 +25,7 @@
"deleted": false
}
],
"data_type": "tailored_audience_permission",
"data_type": "custom_audience_permission",
"total_count": 2,
"next_cursor": null
}
12 changes: 6 additions & 6 deletions tests/test_targeted_audiences.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from twitter_ads.account import Account
from twitter_ads.client import Client
from twitter_ads.audience import TailoredAudience
from twitter_ads.audience import CustomAudience
from twitter_ads.cursor import Cursor
from twitter_ads import API_VERSION

Expand All @@ -17,11 +17,11 @@ def test_targeted_audiences():
body=with_fixture('accounts_load'))

responses.add(responses.GET,
with_resource('/' + API_VERSION + '/accounts/2iqph/tailored_audiences/2906h'),
body=with_fixture('tailored_audiences_load'))
with_resource('/' + API_VERSION + '/accounts/2iqph/custom_audiences/2906h'),
body=with_fixture('custom_audiences_load'))

responses.add(responses.GET,
with_resource('/' + API_VERSION + '/accounts/2iqph/tailored_audiences/abc2/targeted?with_active=True'),
with_resource('/' + API_VERSION + '/accounts/2iqph/custom_audiences/abc2/targeted?with_active=True'),
body=with_fixture('targeted_audiences'))

client = Client(
Expand All @@ -33,7 +33,7 @@ def test_targeted_audiences():

account = Account.load(client, '2iqph')

audience = TailoredAudience.load(account, '2906h')
audience = CustomAudience.load(account, '2906h')
targeted_audiences = audience.targeted(
with_active=True
)
Expand All @@ -44,4 +44,4 @@ def test_targeted_audiences():
assert targeted_audiences.first.line_items[0]['id'] == '5gzog'
assert targeted_audiences.first.line_items[0]['name'] == 'test-line-item'
assert targeted_audiences.first.line_items[0]['servable'] == True
assert len(responses.calls) == 3
assert len(responses.calls) == 3
4 changes: 2 additions & 2 deletions twitter_ads/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (C) 2015 Twitter, Inc.

VERSION = (8, 0, 0)
API_VERSION = '8'
VERSION = (9, 0, 0)
API_VERSION = '9'

from twitter_ads.utils import get_version

Expand Down
8 changes: 4 additions & 4 deletions twitter_ads/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from twitter_ads.resource import resource_property, Resource
from twitter_ads.creative import (AccountMedia, MediaCreative, ScheduledTweet,
Card, PromotedTweet)
from twitter_ads.audience import TailoredAudience
from twitter_ads.audience import CustomAudience
from twitter_ads.campaign import (AppList, Campaign, FundingInstrument, LineItem,
PromotableUser, ScheduledPromotedTweet)

Expand Down Expand Up @@ -111,12 +111,12 @@ def app_lists(self, id=None, **kwargs):
"""
return self._load_resource(AppList, id, **kwargs)

def tailored_audiences(self, id=None, **kwargs):
def custom_audiences(self, id=None, **kwargs):
"""
Returns a collection of tailored audiences available to the
Returns a collection of custom audiences available to the
current account.
"""
return self._load_resource(TailoredAudience, id, **kwargs)
return self._load_resource(CustomAudience, id, **kwargs)

def account_media(self, id=None, **kwargs):
"""
Expand Down
Loading

0 comments on commit 17ffd57

Please sign in to comment.