Last active
September 4, 2016 03:50
-
-
Save esurdam/a6800d1de42e1be2ed98ffd75d3935d8 to your computer and use it in GitHub Desktop.
sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import sys | |
import os | |
sys.path.insert(0, os.path.abspath('..')) | |
from facebookads.objects import ( | |
AdUser, | |
AdAccount, | |
AdSet, | |
Campaign, | |
TargetingSpecsField, | |
) | |
from audiences import ListCustomAudiences | |
from facebookads.api import FacebookAdsApi | |
from clint.textui import prompt, puts, colored, validators | |
import os | |
import json | |
this_dir = os.path.dirname(__file__) | |
config_filename = os.path.join(this_dir, 'config.json') | |
config_file = open(config_filename) | |
config = json.load(config_file) | |
config_file.close() | |
api = FacebookAdsApi.init(access_token=config['access_token']) | |
def SelectCampaign(selected=1): | |
campaigns = list(ad_account.get_campaigns(fields=[ | |
Campaign.Field.name, | |
Campaign.Field.id | |
])) | |
if len(campaigns) > 1: | |
opts = [i[Campaign.Field.name] for i in campaigns] | |
selected = prompt.options('Select a Campaign', opts) | |
campaign = campaigns[selected - 1 or 0] | |
return campaign | |
FIELDS = [AdSet.Field.name, | |
AdSet.Field.targeting, | |
AdSet.Field.campaign_id, | |
AdSet.Field.is_autobid, | |
AdSet.Field.daily_budget, | |
AdSet.Field.lifetime_budget, | |
AdSet.Field.billing_event, | |
AdSet.Field.end_time, | |
AdSet.Field.start_time, | |
AdSet.Field.optimization_goal, | |
# AdSet.Field.recommendations, | |
AdSet.Field.attribution_window_days, | |
# AdSet.Field.frequency_control_specs, | |
AdSet.Field.pacing_type, | |
AdSet.Field.promoted_object] | |
def SelectAdSet(campaign, opts=[]): | |
ad_sets = list(campaign.get_ad_sets(FIELDS)) | |
for ad_set in ad_sets: | |
opts.append(ad_set[AdSet.Field.name]) | |
ad_set_selection = prompt.options('Select an AdSet', opts) | |
ad_set_choice = ad_sets[ad_set_selection - 1 or 0] | |
return ad_set_choice | |
def SwitchGenderName(gender, name): | |
if gender == 1: | |
text = "Male" | |
opp = "Female" | |
elif gender == 2: | |
text = "Female" | |
opp = "Male" | |
else: | |
text = "Default" | |
if "Male" in name: | |
name = name.replace("Male", text) | |
if "Female" in name: | |
name = name.replace("Female", text) | |
if text not in name: | |
if opp in name: | |
output = name.replace(opp, text) | |
elif "Default" in name: | |
output = name.replace("Default", text) | |
else: | |
base_string = name.split(' ', 1)[0] | |
output = "%s - %s" % (base_string, text) | |
else: | |
output = name | |
return output | |
def SelectGender(ad_set, targeting): | |
prompt_string = "Gender Targeting? (current: %s)" % targeting[TargetingSpecsField.genders] | |
gender = prompt.options(prompt_string, ["Male", "Female", "Both"]) | |
ad_set_name = ad_set[AdSet.Field.name] | |
if gender == 3: | |
chosen = None | |
else: | |
chosen = [gender] | |
ad_set[AdSet.Field.name] = SwitchGenderName(gender, ad_set_name) | |
print ad_set[AdSet.Field.name] | |
return chosen | |
def DuplicateAdSet(account, orig): | |
ad_set = AdSet(parent_id=account.get_id_assured()) | |
for field in FIELDS: | |
ad_set[field] = orig[field] | |
ad_set[AdSet.Field.configured_status] = "PAUSED" | |
targeting = orig[AdSet.Field.targeting] | |
targeting[TargetingSpecsField.genders] = SelectGender(orig, targeting) | |
# | |
# default_dupe_name = "%s" % orig[AdSet.Field.name] | |
# name = prompt.query("AdSet Name?", default=default_dupe_name) | |
# ad_set[AdSet.Field.name] = name | |
# print targeting | |
# ads = ad_set.get_ads() | |
# print ads | |
if ad_set.remote_validate(): | |
print "VALID -> Creating AdSet" | |
print ad_set | |
#ad_set.remote_create() | |
else: | |
print "NOT VALID" | |
if __name__ == '__main__': | |
# Standard non-empty input | |
# name = prompt.query("What's your name?") | |
me = AdUser(fbid='me') | |
accounts = list(me.get_ad_accounts()) | |
if len(accounts) > 1: | |
account_opts = [] | |
for account in accounts: | |
account_opts.append(account[AdAccount.Field.account_id]) | |
acc = prompt.options('Select an AdAccount', account_opts) | |
account = accounts[acc - 1 or 0]["id"] | |
ad_account = AdAccount(account) | |
campaign = SelectCampaign() | |
ad_set = SelectAdSet(campaign) | |
print "Duplicate AdSet ->", ad_set[AdSet.Field.name] | |
DuplicateAdSet(ad_account, ad_set) | |
# ListCustomAudiences(ad_account) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<AdSet> { | |
"attribution_window_days": 1, | |
"billing_event": "IMPRESSIONS", | |
"campaign_id": "23842505138370490", | |
"configured_status": "PAUSED", | |
"daily_budget": "0", | |
"end_time": "2016-09-05T18:00:00-0700", | |
"is_autobid": true, | |
"lifetime_budget": "3000", | |
"name": "Visitors/AudiA3 - Female - Global", | |
"optimization_goal": "LEAD_GENERATION", | |
"pacing_type": [ | |
"standard" | |
], | |
"promoted_object": { | |
"page_id": "5798888989" | |
}, | |
"start_time": "2016-09-03T19:34:19-0700", | |
"targeting": { | |
"age_max": 65, | |
"age_min": 25, | |
"custom_audiences": [ | |
{ | |
"id": "6059235714396", | |
"name": "Audi A3 Visitors on Edmunds - 30 Days" | |
} | |
], | |
"device_platforms": [ | |
"desktop", | |
"mobile" | |
], | |
"excluded_geo_locations": { | |
"cities": [ | |
{ | |
"country": "US", | |
"distance_unit": "mile", | |
"key": "2420379", | |
"name": "Los Angeles", | |
"radius": 25, | |
"region": "California", | |
"region_id": "3847" | |
}, | |
{ | |
"country": "US", | |
"distance_unit": "mile", | |
"key": "2421830", | |
"name": "San Diego", | |
"radius": 25, | |
"region": "California", | |
"region_id": "3847" | |
}, | |
{ | |
"country": "US", | |
"distance_unit": "mile", | |
"key": "2421836", | |
"name": "San Francisco", | |
"radius": 25, | |
"region": "California", | |
"region_id": "3847" | |
} | |
], | |
"location_types": [ | |
"home" | |
] | |
}, | |
"facebook_positions": [ | |
"feed" | |
], | |
"genders": [ | |
1 | |
], | |
"geo_locations": { | |
"countries": [ | |
"US" | |
], | |
"location_types": [ | |
"home", | |
"recent" | |
] | |
}, | |
"publisher_platforms": [ | |
"facebook" | |
] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment