def create_campaign(_name, _spend_cap=None, _objective='CONVERSIONS', _buying_type='AUCTION', _status='Paused', _ad_account= ad_account_id):
    from facebook_business.adobjects.campaign import Campaign
    if _spend_cap > 999:
        pr = input("Campaign spend cap is set to $" + str(_spend_cap) + " (y/n)?")
        if pr != "y":
            _spend_cap = input("Enter the correct spend cap in dollars: $")
    if _spend_cap is None:
        _spend_cap = 922337203685478 #10000 = $100
    else:
        _spend_cap = _spend_cap * 100
    print(_spend_cap)
    campaign = Campaign(parent_id=_ad_account)
    campaign.update({
        Campaign.Field.name: _name,
        Campaign.Field.objective: _objective, #APP_INSTALLS, BRAND_AWARENESS, CONVERSIONS, EVENT_RESPONSES, LEAD_GENERATION, LINK_CLICKS, LOCAL_AWARENESS, MESSAGES, OFFER_CLAIMS, PAGE_LIKES, POST_ENGAGEMENT, PRODUCT_CATALOG_SALES, REACH, VIDEO_VIEWS
        Campaign.Field.spend_cap: _spend_cap, #value of 922337203685478 will undo spend cap
        Campaign.Field.buying_type: _buying_type
    })
    if _status == 'Active':
        campaign.remote_create(params={ #remote_update to change and remote_delete to delete
            'status': Campaign.Status.active,
        })
    else:
        campaign.remote_create(params={  # remote_update to change and remote_delete to delete
            'status': Campaign.Status.paused,
        })
    return campaign
Beispiel #2
0
 def createCampaign(self, name):
     print("**Creating {} Campaign...**".format(name))
     campaign = Campaign()
     campaign['_parent_id'] = self.id
     campaign[Campaign.Field.name] = name
     campaign[Campaign.Field.objective] = 'LINK_CLICKS'
     campaign[Campaign.Field.status] = 'PAUSED'
     campaign.remote_create()
     self.campaign_id = str(campaign[Campaign.Field.id])
     print("**Campaign ID: {}**".format(self.campaign_id))
     return campaign
Beispiel #3
0
 def create_campaign(self, account_id, name):
     """
     Step 2: create a campaign. See [Campaign][1] for further details on
     the API used here.
     [1]: https://developers.facebook.com/docs/marketing-api/adcampaign
     """
     campaign = Campaign(parent_id=account_id)
     campaign[Campaign.Field.name] = name
     campaign[Campaign.Field.objective] = (Campaign.Objective.app_installs)
     campaign.remote_create(params={'status': Campaign.Status.paused})
     return campaign[Campaign.Field.id]
def create_campaign(request):
    FacebookAdsApi.init(access_token=access_token,
                        app_id=app_id,
                        app_secret=app_secret)

    my_account = AdAccount('act_' + user_id)

    campaign = Campaign(parent_id=my_account.get_id_assured())
    campaign[Campaign.Field.name] = "Campaign created by afnan -" + str(
        random.randint(1, 10))
    campaign[Campaign.Field.objective] = 'LINK_CLICKS'
    campaign[Campaign.Field.status] = 'ACTIVE'

    campaign.remote_create()

    result = {"id": campaign.get_id()}

    return redirect('view-campaign')
Beispiel #5
0
    "https://portal.synlighet.no/fb/accounts/").runtimeaccount()

for cu_data in data:
    act_cid = "act_" + cu_data['cid']
    print("Account: ", act_cid)

    if "season" in cu_data['services']:
        print("found api")

        campaign = Campaign(parent_id=act_cid)
        campaign[Campaign.Field.name] = 'TEST - Product Catalog Sales Campaign'
        objective = Campaign.Objective.product_catalog_sales
        campaign[Campaign.Field.objective] = 'LINK_CLICKS'

        campaign.remote_create(params={
            'status': Campaign.Status.paused,
        })

        print(campaign[Campaign.Field.id])

        # temp_campaign = Campaign(i["id"])
        # adsets = temp_campaign.adset(
        adset = AdSet(parent_id=act_cid)
        adset[AdSet.Field.name] = 'Product Catalog Sales Adset'
        adset[AdSet.Field.bid_amount] = 30
        adset[AdSet.Field.billing_event] = AdSet.BillingEvent.link_clicks
        adset[AdSet.Field.optimization_goal] = \
            AdSet.OptimizationGoal.link_clicks
        adset[AdSet.Field.daily_budget] = 1000
        adset[AdSet.Field.campaign_id] = campaign[Campaign.Field.id]
        adset[AdSet.Field.targeting] = {
Beispiel #6
0
    def create_lookalike_ads(
        self,
        account_id,
        name,
        tiered_lookalikes,
        optimization_goal,
        billing_event,
        tiered_bid_amounts,
        title,
        body,
        url,
        image_path,
        daily_budget=None,
        lifetime_budget=None,
        start_time=None,
        end_time=None,
        campaign=None,
    ):
        """
        Take the tiered lookalike audiences and create the ads
        """
        results = {
            'adsets': [],
            'ads': [],
        }

        tiers = len(tiered_lookalikes)
        if tiers != len(tiered_bid_amounts):
            raise TypeError('Audience and bid amount number mismatch.')

        # Create campaign
        if not campaign:
            campaign = Campaign(parent_id=account_id)
            campaign[Campaign.Field.name] = '{} Campaign'.format(name)
            campaign[Campaign.Field.objective] = \
                Campaign.Objective.link_clicks

            campaign.remote_create(params={
                'status': Campaign.Status.paused,
            })

        # Upload image
        img = AdImage(parent_id=account_id)
        img[AdImage.Field.filename] = image_path
        img.remote_create()
        image_hash = img.get_hash()

        # Inline creative for ads
        inline_creative = {
            AdCreative.Field.title: title,
            AdCreative.Field.body: body,
            AdCreative.Field.object_url: url,
            AdCreative.Field.image_hash: image_hash,
        }

        for tier in range(1, tiers + 1):
            # Create ad set
            ad_set = AdSet(parent_id=account_id)
            ad_set[AdSet.Field.campaign_id] = campaign.get_id_assured()
            ad_set[AdSet.Field.name] = '{0} AdSet tier {1}'.format(name, tier)
            ad_set[AdSet.Field.optimization_goal] = optimization_goal
            ad_set[AdSet.Field.billing_event] = billing_event
            ad_set[AdSet.Field.bid_amount] = tiered_bid_amounts[tier - 1]
            if daily_budget:
                ad_set[AdSet.Field.daily_budget] = daily_budget
            else:
                ad_set[AdSet.Field.lifetime_budget] = lifetime_budget
            if end_time:
                ad_set[AdSet.Field.end_time] = end_time
            if start_time:
                ad_set[AdSet.Field.start_time] = start_time

            audience = tiered_lookalikes[tier - 1]

            targeting = {
                Targeting.Field.custom_audiences: [{
                    'id':
                    audience[CustomAudience.Field.id],
                    'name':
                    audience[CustomAudience.Field.name],
                }]
            }

            ad_set[AdSet.Field.targeting] = targeting

            ad_set.remote_create(params={
                'status': AdSet.Status.paused,
            })

            # Create ad
            ad = Ad(parent_id=account_id)
            ad[Ad.Field.name] = '{0} Ad tier {1}'.format(name, tier)
            ad[Ad.Field.adset_id] = ad_set['id']
            ad[Ad.Field.creative] = inline_creative

            ad.remote_create(params={
                'status': Ad.Status.paused,
            })

            results['adsets'].append(ad_set)
            results['ads'].append(ad)

        return results
Beispiel #7
0
class FbApi(object):
    def __init__(self, config_file=None):
        self.config_file = config_file
        self.df = pd.DataFrame()
        self.config = None
        self.account = None
        self.campaign = None
        self.app_id = None
        self.app_secret = None
        self.access_token = None
        self.act_id = None
        self.config_list = []
        self.date_lists = None
        self.field_lists = None
        self.adset_dict = None
        self.cam_dict = None
        self.ad_dict = None
        self.pixel = None
        if self.config_file:
            self.input_config(self.config_file)

    def input_config(self, config_file):
        logging.info('Loading Facebook config file: ' + str(config_file))
        self.config_file = os.path.join(config_path, config_file)
        self.load_config()
        self.check_config()
        FacebookAdsApi.init(self.app_id, self.app_secret, self.access_token)
        self.account = AdAccount(self.config['act_id'])

    def load_config(self):
        try:
            with open(self.config_file, 'r') as f:
                self.config = json.load(f)
        except IOError:
            logging.error(self.config_file + ' not found.  Aborting.')
            sys.exit(0)
        self.app_id = self.config['app_id']
        self.app_secret = self.config['app_secret']
        self.access_token = self.config['access_token']
        self.act_id = self.config['act_id']
        self.config_list = [
            self.app_id, self.app_secret, self.access_token, self.act_id
        ]

    def check_config(self):
        for item in self.config_list:
            if item == '':
                logging.warning(item + 'not in FB config file.  Aborting.')
                sys.exit(0)

    def set_id_name_dict(self, fb_object):
        if fb_object == Campaign:
            fields = ['id', 'name']
            self.cam_dict = list(self.account.get_campaigns(fields=fields))
        elif fb_object == AdSet:
            fields = ['id', 'name', 'campaign_id']
            self.adset_dict = list(self.account.get_ad_sets(fields=fields))
        elif fb_object == Ad:
            fields = ['id', 'name', 'campaign_id', 'adset_id']
            self.ad_dict = list(self.account.get_ads(fields=fields))

    def campaign_to_id(self, campaigns):
        if not self.cam_dict:
            self.set_id_name_dict(Campaign)
        cids = [x['id'] for x in self.cam_dict if x['name'] in campaigns]
        return cids

    def adset_to_id(self, adsets, cids):
        as_and_cam = list(itertools.product(adsets, cids))
        if not self.adset_dict:
            self.set_id_name_dict(AdSet)
        asids = [
            tuple([x['id'], x['campaign_id']]) for x in self.adset_dict
            if tuple([x['name'], x['campaign_id']]) in as_and_cam
        ]
        return asids

    def create_campaign(self, campaign_name, objective, status, spend_cap):
        if not self.cam_dict:
            self.set_id_name_dict(Campaign)
        if campaign_name in ([x['name'] for x in self.cam_dict]):
            logging.warning(campaign_name + ' already in account.  This ' +
                            'campaign was not uploaded.')
            return None
        self.campaign = Campaign(parent_id=self.account.get_id_assured())
        self.campaign.update({
            Campaign.Field.name: campaign_name,
            Campaign.Field.objective: objective,
            Campaign.Field.effective_status: status,
            Campaign.Field.spend_cap: int(spend_cap),
            Campaign.Field.special_ad_categories: 'NONE'
        })
        self.campaign.remote_create()

    @staticmethod
    def geo_target_search(geos):
        all_geos = []
        for geo in geos:
            params = {
                'q': geo,
                'type': 'adgeolocation',
                'location_types': [Targeting.Field.country],
            }
            resp = TargetingSearch.search(params=params)
            all_geos.extend(resp)
        return all_geos

    @staticmethod
    def target_search(targets_to_search):
        all_targets = []
        for target in targets_to_search[1]:
            params = {
                'q': target,
                'type': 'adinterest',
            }
            resp = TargetingSearch.search(params=params)
            if not resp:
                logging.warning(target + ' not found in targeting search.  ' +
                                'It was not added to the adset.')
                continue
            if targets_to_search[0] == 'interest':
                resp = [resp[0]]
            new_tar = [dict((k, x[k]) for k in ('id', 'name')) for x in resp]
            all_targets.extend(new_tar)
        return all_targets

    @staticmethod
    def get_matching_saved_audiences(audiences):
        aud_list = []
        for audience in audiences:
            audience = CustomAudience(audience)
            val_aud = audience.remote_read(fields=['targeting'])
            aud_list.append(val_aud)
            aud_list = aud_list[0]['targeting']
        return aud_list

    def get_matching_custom_audiences(self, audiences):
        act_auds = self.account.get_custom_audiences(
            fields=[CustomAudience.Field.name, CustomAudience.Field.id])
        audiences = [{
            'id': x['id'],
            'name': x['name']
        } for x in act_auds if x['id'] in audiences]
        return audiences

    def set_target(self, geos, targets, age_min, age_max, gender, device,
                   publisher_platform, facebook_positions):
        targeting = {}
        if geos and geos != ['']:
            targeting[Targeting.Field.geo_locations] = {
                Targeting.Field.countries: geos
            }
        if age_min:
            targeting[Targeting.Field.age_min] = age_min
        if age_max:
            targeting[Targeting.Field.age_max] = age_max
        if gender:
            targeting[Targeting.Field.genders] = gender
        if device and device != ['']:
            targeting[Targeting.Field.device_platforms] = device
        if publisher_platform and publisher_platform != ['']:
            targeting[Targeting.Field.publisher_platforms] = publisher_platform
        if facebook_positions and facebook_positions != ['']:
            targeting[Targeting.Field.facebook_positions] = facebook_positions
        for target in targets:
            if target[0] == 'interest' or target[0] == 'interest-broad':
                int_targets = self.target_search(target)
                targeting[Targeting.Field.interests] = int_targets
            if target[0] == 'savedaudience':
                aud_target = self.get_matching_saved_audiences(target[1])
                targeting.update(aud_target)
            if target[0] == 'customaudience':
                aud_target = self.get_matching_custom_audiences(target[1])
                targeting[Targeting.Field.custom_audiences] = aud_target
        return targeting

    def create_adset(self, adset_name, cids, opt_goal, bud_type, bud_val,
                     bill_evt, bid_amt, status, start_time, end_time, prom_obj,
                     country, target, age_min, age_max, genders, device, pubs,
                     pos):
        if not self.adset_dict:
            self.set_id_name_dict(AdSet)
        for cid in cids:
            if adset_name in ([
                    x['name'] for x in self.adset_dict
                    if x['campaign_id'] == cid
            ]):
                logging.warning(adset_name + ' already in campaign.  This ' +
                                'ad set was not uploaded.')
                continue
            targeting = self.set_target(country, target, age_min, age_max,
                                        genders, device, pubs, pos)
            params = {
                AdSet.Field.name: adset_name,
                AdSet.Field.campaign_id: cid,
                AdSet.Field.billing_event: bill_evt,
                AdSet.Field.status: status,
                AdSet.Field.targeting: targeting,
                AdSet.Field.start_time: start_time,
                AdSet.Field.end_time: end_time,
            }
            if bid_amt == '':
                params['bid_strategy'] = 'LOWEST_COST_WITHOUT_CAP'
            else:
                params[AdSet.Field.bid_amount] = int(bid_amt)
            if opt_goal in [
                    'CONTENT_VIEW', 'SEARCH', 'ADD_TO_CART', 'ADD_TO_WISHLIST',
                    'INITIATED_CHECKOUT', 'ADD_PAYMENT_INFO', 'PURCHASE',
                    'LEAD', 'COMPLETE_REGISTRATION'
            ]:
                if not self.pixel:
                    pixel = self.account.get_ads_pixels()
                    self.pixel = pixel[0]['id']
                params[AdSet.Field.promoted_object] = {
                    'pixel_id': self.pixel,
                    'custom_event_type': opt_goal,
                    'page_id': prom_obj
                }
            elif 'APP_INSTALLS' in opt_goal:
                opt_goal = opt_goal.split('|')
                params[AdSet.Field.promoted_object] = {
                    'application_id': opt_goal[1],
                    'object_store_url': opt_goal[2],
                }
            else:
                params[AdSet.Field.optimization_goal] = opt_goal
                params[AdSet.Field.promoted_object] = {'page_id': prom_obj}
            if bud_type == 'daily':
                params[AdSet.Field.daily_budget] = int(bud_val)
            elif bud_type == 'lifetime':
                params[AdSet.Field.lifetime_budget] = int(bud_val)
            self.account.create_ad_set(params=params)

    def upload_creative(self, creative_class, image_path):
        cre = creative_class(parent_id=self.account.get_id_assured())
        if creative_class == AdImage:
            cre[AdImage.Field.filename] = image_path
            cre.remote_create()
            creative_hash = cre.get_hash()
        elif creative_class == AdVideo:
            cre[AdVideo.Field.filepath] = image_path
            cre.remote_create()
            creative_hash = cre.get_id()
        else:
            creative_hash = None
        return creative_hash

    def get_all_thumbnails(self, vid):
        video = AdVideo(vid)
        thumbnails = video.get_thumbnails()
        if not thumbnails:
            logging.warning('Could not retrieve thumbnail for vid: ' +
                            str(vid) + '.  Retrying in 120s.')
            time.sleep(120)
            thumbnails = self.get_all_thumbnails(vid)
        return thumbnails

    def get_video_thumbnail(self, vid):
        thumbnails = self.get_all_thumbnails(vid)
        thumbnail = [x for x in thumbnails if x['is_preferred'] is True]
        if not thumbnail:
            thumbnail = thumbnails[1]
        else:
            thumbnail = thumbnail[0]
        thumb_url = thumbnail['uri']
        return thumb_url

    @staticmethod
    def request_error(e):
        if e._api_error_code == 2:
            logging.warning(
                'Retrying as the call resulted in the following: ' + str(e))
        else:
            logging.error('Retrying in 120 seconds as the Facebook API call'
                          'resulted in the following error: ' + str(e))
            time.sleep(120)

    def create_ad(self,
                  ad_name,
                  asids,
                  title,
                  body,
                  desc,
                  cta,
                  durl,
                  url,
                  prom_obj,
                  ig_id,
                  view_tag,
                  ad_status,
                  creative_hash=None,
                  vid_id=None):
        if not self.ad_dict:
            self.set_id_name_dict(Ad)
        for asid in asids:
            if ad_name in [
                    x['name'] for x in self.ad_dict
                    if x['campaign_id'] == asid[1] and x['adset_id'] == asid[0]
            ]:
                logging.warning(ad_name + ' already in campaign/adset. ' +
                                'This ad was not uploaded.')
                continue
            if vid_id:
                params = self.get_video_ad_params(ad_name, asid, title, body,
                                                  desc, cta, url, prom_obj,
                                                  ig_id, creative_hash, vid_id,
                                                  view_tag, ad_status)
            elif isinstance(creative_hash, list):
                params = self.get_carousel_ad_params(ad_name, asid, title,
                                                     body, desc, cta, durl,
                                                     url, prom_obj, ig_id,
                                                     creative_hash, view_tag,
                                                     ad_status)
            else:
                params = self.get_link_ad_params(ad_name, asid, title, body,
                                                 desc, cta, durl, url,
                                                 prom_obj, ig_id,
                                                 creative_hash, view_tag,
                                                 ad_status)
            for attempt_number in range(100):
                try:
                    self.account.create_ad(params=params)
                    break
                except FacebookRequestError as e:
                    self.request_error(e)

    def get_video_ad_params(self, ad_name, asid, title, body, desc, cta, url,
                            prom_obj, ig_id, creative_hash, vid_id, view_tag,
                            ad_status):
        data = self.get_video_ad_data(vid_id, body, title, desc, cta, url,
                                      creative_hash)
        story = {
            AdCreativeObjectStorySpec.Field.page_id: str(prom_obj),
            AdCreativeObjectStorySpec.Field.video_data: data
        }
        if ig_id and str(ig_id) != 'nan':
            story[AdCreativeObjectStorySpec.Field.instagram_actor_id] = ig_id
        creative = {AdCreative.Field.object_story_spec: story}
        params = {
            Ad.Field.name: ad_name,
            Ad.Field.status: ad_status,
            Ad.Field.adset_id: asid[0],
            Ad.Field.creative: creative
        }
        if view_tag and str(view_tag) != 'nan':
            params['view_tags'] = [view_tag]
        return params

    def get_link_ad_params(self, ad_name, asid, title, body, desc, cta, durl,
                           url, prom_obj, ig_id, creative_hash, view_tag,
                           ad_status):
        data = self.get_link_ad_data(body, creative_hash, durl, desc, url,
                                     title, cta)
        story = {
            AdCreativeObjectStorySpec.Field.page_id: str(prom_obj),
            AdCreativeObjectStorySpec.Field.link_data: data
        }
        if ig_id and str(ig_id) != 'nan':
            story[AdCreativeObjectStorySpec.Field.instagram_actor_id] = ig_id
        creative = {AdCreative.Field.object_story_spec: story}
        params = {
            Ad.Field.name: ad_name,
            Ad.Field.status: ad_status,
            Ad.Field.adset_id: asid[0],
            Ad.Field.creative: creative
        }
        if view_tag and str(view_tag) != 'nan':
            params['view_tags'] = [view_tag]
        return params

    @staticmethod
    def get_video_ad_data(vid_id, body, title, desc, cta, url, creative_hash):
        data = {
            AdCreativeVideoData.Field.video_id: vid_id,
            AdCreativeVideoData.Field.message: body,
            AdCreativeVideoData.Field.title: title,
            AdCreativeVideoData.Field.link_description: desc,
            AdCreativeVideoData.Field.call_to_action: {
                'type': cta,
                'value': {
                    'link': url,
                },
            },
        }
        if creative_hash[:4] == 'http':
            data[AdCreativeVideoData.Field.image_url] = creative_hash
        else:
            data[AdCreativeVideoData.Field.image_hash] = creative_hash
        return data

    @staticmethod
    def get_link_ad_data(body, creative_hash, durl, desc, url, title, cta):
        data = {
            AdCreativeLinkData.Field.message: body,
            AdCreativeLinkData.Field.image_hash: creative_hash,
            AdCreativeLinkData.Field.caption: durl,
            AdCreativeLinkData.Field.description: desc,
            AdCreativeLinkData.Field.link: url,
            AdCreativeLinkData.Field.name: title,
            AdCreativeLinkData.Field.call_to_action: {
                'type': cta,
                'value': {
                    'link': url,
                },
            },
        }
        return data

    @staticmethod
    def get_carousel_ad_data(creative_hash,
                             desc,
                             url,
                             title,
                             cta,
                             vid_id=None):
        data = {
            AdCreativeLinkData.Field.description: desc,
            AdCreativeLinkData.Field.link: url,
            AdCreativeLinkData.Field.name: title,
            AdCreativeLinkData.Field.call_to_action: {
                'type': cta,
                'value': {
                    'link': url,
                },
            },
        }
        if creative_hash[:4] == 'http':
            data['picture'] = creative_hash
        else:
            data[AdCreativeVideoData.Field.image_hash] = creative_hash
        if vid_id:
            data[AdCreativeVideoData.Field.video_id] = vid_id
        return data

    @staticmethod
    def get_individual_carousel_param(param_list, idx):
        if idx < len(param_list):
            param = param_list[idx]
        else:
            logging.warning('{} does not have index {}.  Using last available.'
                            ''.format(param_list, idx))
            param = param_list[-1]
        return param

    def get_carousel_ad_params(self, ad_name, asid, title, body, desc, cta,
                               durl, url, prom_obj, ig_id, creative_hash,
                               view_tag, ad_status):
        data = []
        for idx, creative in enumerate(creative_hash):
            current_description = self.get_individual_carousel_param(desc, idx)
            current_url = self.get_individual_carousel_param(url, idx)
            current_title = self.get_individual_carousel_param(title, idx)
            if len(creative) == 1:
                data_ind = self.get_carousel_ad_data(creative_hash=creative[0],
                                                     desc=current_description,
                                                     url=current_url,
                                                     title=current_title,
                                                     cta=cta)
            else:
                data_ind = self.get_carousel_ad_data(creative_hash=creative[1],
                                                     desc=current_description,
                                                     url=current_url,
                                                     title=current_title,
                                                     cta=cta,
                                                     vid_id=creative[0])
            data.append(data_ind)
        link = {
            AdCreativeLinkData.Field.message: body,
            AdCreativeLinkData.Field.link: url[0],
            AdCreativeLinkData.Field.caption: durl,
            AdCreativeLinkData.Field.child_attachments: data,
            AdCreativeLinkData.Field.call_to_action: {
                'type': cta,
                'value': {
                    'link': url[0],
                },
            },
        }
        story = {
            AdCreativeObjectStorySpec.Field.page_id: str(prom_obj),
            AdCreativeObjectStorySpec.Field.link_data: link
        }
        if ig_id and str(ig_id) != 'nan':
            story[AdCreativeObjectStorySpec.Field.instagram_actor_id] = ig_id
        creative = {AdCreative.Field.object_story_spec: story}
        params = {
            Ad.Field.name: ad_name,
            Ad.Field.status: ad_status,
            Ad.Field.adset_id: asid[0],
            Ad.Field.creative: creative
        }
        if view_tag and str(view_tag) != 'nan':
            params['view_tags'] = [view_tag]
        return params
Beispiel #8
0
    def create_multiple_link_clicks_ads(
        self,
        accountid,
        pageid,
        name,
        titles,
        bodies,
        urls,
        image_paths,
        targeting,
        optimization_goal,
        billing_event,
        bid_amount,
        daily_budget=None,
        lifetime_budget=None,
        start_time=None,
        end_time=None,
    ):
        """
        There are 7 steps in this sample:

        1. Create a campaign
        2. Create an ad set
        3. Upload images
        4. Make combinations of specified creative elements
        5. Prepare an API batch
        6. For each creative combination, add a call to the batch to create a
        new ad
        7. Execute API batch

        Params:

        * `accountid` is your Facebook AdAccount id
        * `name` is a string of basename of your ads.
        * `page` is the Facebook page used to publish the ads
        * `titles` array of strings of what user will see as ad title
        * `bodies` array of strings of what user will see as ad body
        * `image_paths` array of image file paths
        * `targeting` is a JSON string specifying targeting info of your ad
          See [Targeting Specs](https://developers.facebook.com/docs/marketing-api/targeting-specs)
          for details.
        * `optimization_goal` the optimization goal for the adsets
        * `billing_event` what you want to pay for
        * `bid_amount` how much you want to pay per billing event
        * `daily_budget` is integer number in your currency. E.g., if your
           currency is USD, dailybudget=1000 says your budget is 1000 USD
        * `lifetime_budget` lifetime budget for created ads
        * `start_time` when the campaign should start
        * `end_time` when the campaign should end


        """
        my_account = AdAccount(fbid=accountid)
        """
          Take different title body url and image paths, create a batch of
          ads based on the permutation of these elements
        """
        # Check for bad specs
        if daily_budget is None:
            if lifetime_budget is None:
                raise TypeError(
                    'One of daily_budget or lifetime_budget must be defined.')
            elif end_time is None:
                raise TypeError(
                    'If lifetime_budget is defined, end_time must be defined.')
        """
        Step 1: Create new campaign with WEBSITE_CLICKS objective
        See
        [Campaign Group](https://developers.facebook.com/docs/marketing-api/reference/ad-campaign-group)
        for further details on the API used here.
        """
        campaign = Campaign(parent_id=accountid)
        campaign[Campaign.Field.name] = name + ' Campaign'
        campaign[Campaign.Field.objective] = \
            Campaign.Objective.link_clicks

        campaign.remote_create(params={
            'status': Campaign.Status.paused,
        })
        """
        Step 2: Create AdSet using specified optimization goal, billing event
        and bid.
        See
        [AdSet](https://developers.facebook.com/docs/marketing-api/reference/ad-campaign)
        for further details on the API used here.
        """
        # Create ad set
        ad_set = AdSet(parent_id=accountid)
        ad_set[AdSet.Field.campaign_id] = campaign.get_id_assured()
        ad_set[AdSet.Field.name] = name + ' AdSet'
        ad_set[AdSet.Field.optimization_goal] = optimization_goal
        ad_set[AdSet.Field.billing_event] = billing_event
        ad_set[AdSet.Field.bid_amount] = bid_amount
        if daily_budget:
            ad_set[AdSet.Field.daily_budget] = daily_budget
        else:
            ad_set[AdSet.Field.lifetime_budget] = lifetime_budget
        if end_time:
            ad_set[AdSet.Field.end_time] = end_time
        if start_time:
            ad_set[AdSet.Field.start_time] = start_time

        ad_set[AdSet.Field.targeting] = targeting
        ad_set.remote_create()
        """
        Step 3: Upload images and get image hashes for use in ad creative.
        See
        [Ad Image](https://developers.facebook.com/docs/marketing-api/reference/ad-image#Creating)
        for further details on the API used here.
        """
        # Upload the images first one by one
        image_hashes = []
        for image_path in image_paths:
            img = AdImage(parent_id=accountid)
            img[AdImage.Field.filename] = image_path
            img.remote_create()
            image_hashes.append(img.get_hash())

        ADGROUP_BATCH_CREATE_LIMIT = 10
        ads_created = []

        def callback_failure(response):
            raise response.error()

        """
        Step 4: Using itertools.product get combinations of creative
        elements.
        """
        for creative_info_batch in generate_batches(
                itertools.product(titles, bodies, urls, image_hashes),
                ADGROUP_BATCH_CREATE_LIMIT):
            """
            Step 5: Create an API batch so we can create all
            ad creatives with one HTTP request.
            See
            [Batch Requests](https://developers.facebook.com/docs/graph-api/making-multiple-requests#simple)
            for further details on batching API calls.
            """
            api_batch = my_account.get_api_assured().new_batch()

            for title, body, url, image_hash in creative_info_batch:
                # Create the ad
                """
                Step 6: For each combination of creative elements,
                add to the batch an API call to create a new Ad
                and specify the creative inline.
                See
                [AdGroup](https://developers.facebook.com/docs/marketing-api/adgroup/)
                for further details on creating Ads.
                """
                ad = Ad(parent_id=accountid)
                ad[Ad.Field.name] = name + ' Ad'
                ad[Ad.Field.adset_id] = ad_set.get_id_assured()
                ad[Ad.Field.creative] = {
                    AdCreative.Field.object_story_spec: {
                        "page_id": pageid,
                        "link_data": {
                            "message": body,
                            "link": url,
                            "caption": title,
                            "image_hash": image_hash
                        }
                    },
                }

                ad.remote_create(batch=api_batch, failure=callback_failure)
                ads_created.append(ad)
            """
            Step 7: Execute the batched API calls
            See
            [Batch Requests](https://developers.facebook.com/docs/graph-api/making-multiple-requests#simple)
            for further details on batching API calls.
            """
            api_batch.execute()

        return [campaign, ad_set, ads_created]
    def create_carousel_ad(
        self,
        accountid,
        page_id,
        site_link,
        caption,
        message,
        optimization_goal,
        billing_event,
        bid_amount,
        name,
        targeting,
        products,
        call_to_action_type=None,
    ):
        """
        There are 5 steps in this sample:

        1. Create a campaign
        2. Create an ad set
        3. For each product:
          a. Upload the product's image and get an image hash
          b. Create a story attachment using the product's creative elements
        4. Prepare the ad creative
        5. Create the ad using the ad creative
        """

        daily_budget = 10000
        """
        Step 1: Create new campaign with WEBSITE_CLICKS objective
        See
        [Campaign Group](https://developers.facebook.com/docs/marketing-api/reference/ad-campaign-group)
        for further details on the API used here.
        """
        campaign = Campaign(parent_id=accountid)
        campaign[Campaign.Field.name] = name + ' Campaign'
        campaign[Campaign.Field.objective] = \
            Campaign.Objective.link_clicks

        campaign.remote_create(params={'status': Campaign.Status.paused})
        """
        Step 2: Create AdSet using specified optimization goal, billing event
        and bid.
        See
        [AdSet](https://developers.facebook.com/docs/marketing-api/reference/ad-campaign)
        for further details on the API used here.
        """
        ad_set = AdSet(parent_id=accountid)
        ad_set[AdSet.Field.campaign_id] = campaign.get_id_assured()
        ad_set[AdSet.Field.name] = name + ' AdSet'
        ad_set[AdSet.Field.optimization_goal] = optimization_goal
        ad_set[AdSet.Field.billing_event] = billing_event
        ad_set[AdSet.Field.bid_amount] = bid_amount
        ad_set[AdSet.Field.daily_budget] = daily_budget
        ad_set[AdSet.Field.targeting] = targeting
        ad_set.remote_create()

        story_attachments = []
        """
        Step 3: Upload images and get image hashes for use in ad creative.
        See
        [Ad Image](https://developers.facebook.com/docs/marketing-api/reference/ad-image#Creating)
        for further details on the API used here.
        Then create a new attachment with the product's creative
        """
        call_to_action = {
            'type': call_to_action_type,
            'value': {
                'link': site_link,
                'link_caption': call_to_action_type,
            }
        } if call_to_action_type else None

        for product in products:
            img = AdImage(parent_id=accountid)
            img[AdImage.Field.filename] = product['image_path']
            img.remote_create()
            image_hash = img.get_hash()
            attachment = AttachmentData()
            attachment[AttachmentData.Field.link] = product['link']
            attachment[AttachmentData.Field.name] = product['name']
            attachment[
                AttachmentData.Field.description] = product['description']
            attachment[AttachmentData.Field.image_hash] = image_hash
            if call_to_action:
                attachment[
                    AttachmentData.Field.call_to_action] = call_to_action
            story_attachments.append(attachment)
        """
        Step 4: Prepare the ad creative including link information
        Note that here we specify multi_share_optimized = True
        this means you can add up to 10 products and Facebook will
        automatically select the best performing 5 to show in the ad. Facebook
        will also select the best ordering of those products.
        """
        link = LinkData()
        link[link.Field.link] = site_link
        link[link.Field.caption] = caption
        link[link.Field.child_attachments] = story_attachments
        link[link.Field.multi_share_optimized] = True
        link[link.Field.call_to_action] = call_to_action

        story = ObjectStorySpec()
        story[story.Field.page_id] = page_id
        story[story.Field.link_data] = link

        creative = AdCreative()
        creative[AdCreative.Field.name] = name + ' Creative'
        creative[AdCreative.Field.object_story_spec] = story
        """
        Step 5: Create the ad using the above creative
        """
        ad = Ad(parent_id=accountid)
        ad[Ad.Field.name] = name + ' Ad'
        ad[Ad.Field.adset_id] = ad_set.get_id_assured()
        ad[Ad.Field.creative] = creative

        ad.remote_create(params={'status': Ad.Status.paused})
        return (campaign, ad_set, ad)
    def create_lead_ad(
        self,
        account_id,
        name,
        page_id,
        form_id,
        optimization_goal,
        billing_event,
        bid_amount,
        daily_budget,
        targeting,
        image_path,
        message,
        link,
        caption,
        description,
        cta_type='SIGN_UP',
    ):
        """
        Create Campaign
        """
        campaign = Campaign(parent_id=account_id)
        campaign[Campaign.Field.name] = name + ' Campaign'
        campaign[Campaign.Field.objective] = \
            Campaign.Objective.lead_generation

        campaign.remote_create(params={
            'status': Campaign.Status.paused
        })

        """
        Create AdSet
        """
        adset = AdSet(parent_id=account_id)
        adset[AdSet.Field.campaign_id] = campaign.get_id_assured()
        adset[AdSet.Field.name] = name + ' AdSet'
        adset[AdSet.Field.promoted_object] = {
            'page_id': page_id,
        }
        adset[AdSet.Field.optimization_goal] = optimization_goal
        adset[AdSet.Field.billing_event] = billing_event
        adset[AdSet.Field.bid_amount] = bid_amount
        adset[AdSet.Field.daily_budget] = daily_budget
        adset[AdSet.Field.targeting] = targeting
        adset.remote_create()

        """
        Image
        """
        image = AdImage(parent_id=account_id)
        image[AdImage.Field.filename] = image_path
        image.remote_create()
        image_hash = image[AdImage.Field.hash]

        """
        Create Creative
        """
        link_data = LinkData()
        link_data[LinkData.Field.message] = message
        link_data[LinkData.Field.link] = link
        link_data[LinkData.Field.image_hash] = image_hash
        link_data[LinkData.Field.caption] = caption
        link_data[LinkData.Field.description] = description
        link_data[LinkData.Field.call_to_action] = {
            'type': cta_type,
            'value': {
                'lead_gen_form_id': form_id,
            },
        }

        object_story_spec = ObjectStorySpec()
        object_story_spec[ObjectStorySpec.Field.page_id] = page_id
        object_story_spec[ObjectStorySpec.Field.link_data] = link_data

        creative = AdCreative(parent_id=account_id)
        creative[AdCreative.Field.name] = name + ' Creative'
        creative[AdCreative.Field.object_story_spec] = object_story_spec
        creative.remote_create()

        """
        Create Ad
        """
        ad = Ad(parent_id=account_id)
        ad[Ad.Field.name] = name
        ad[Ad.Field.adset_id] = adset.get_id_assured()
        ad[Ad.Field.creative] = {'creative_id': str(creative.get_id_assured())}
        ad.remote_create()

        return {
            'image_hash': image_hash,
            'campaign_id': campaign['id'],
            'adset_id': adset['id'],
            'creative_id': creative['id'],
            'ad_id': ad['id'],
        }