예제 #1
0
def create_new_ad(adset, image_hash):

    link_data = AdCreativeLinkData()
    link_data[AdCreativeLinkData.Field.message] = '“Like” to find ways to help man’s best friend.'
    link_data[AdCreativeLinkData.Field.link] = 'https://www.facebook.com/caltech.clickmaniac'
    link_data[AdCreativeLinkData.Field.image_hash] = image_hash

    object_story_spec = AdCreativeObjectStorySpec()
    object_story_spec[AdCreativeObjectStorySpec.Field.page_id] = my_page_id
    object_story_spec[AdCreativeObjectStorySpec.Field.link_data] = link_data


    creative = AdCreative(parent_id=my_ad_account_id)
    creative[AdCreative.Field.name] = 'SDK Creative 1'
    creative[AdCreative.Field.object_story_spec] = object_story_spec

    # Finally, create your ad along with ad creative.
    # Please note that the ad creative is not created independently, rather its
    # data structure is appended to the ad group
    ad = Ad(parent_id=my_ad_account_id)
    ad[Ad.Field.name] = 'SDK Dog Ad 1'
    ad[Ad.Field.adset_id] = adset[AdSet.Field.id]
    ad[Ad.Field.creative] = creative
    ad[Ad.Field.status] = Ad.Status.active
    ad.remote_create()
    pass
예제 #2
0
    def createAdCreative(self, name, imageHash, message, headline, description,
                         caption, url, pageId):

        linkData = AdCreativeLinkData()
        linkData[AdCreativeLinkData.Field.message] = message
        linkData[AdCreativeLinkData.Field.link] = url
        linkData[AdCreativeLinkData.Field.caption] = caption
        linkData[AdCreativeLinkData.Field.description] = description
        linkData[AdCreativeLinkData.Field.name] = headline
        linkData[AdCreativeLinkData.Field.image_hash] = imageHash

        objectStorySpec = AdCreativeObjectStorySpec()
        objectStorySpec[AdCreativeObjectStorySpec.Field.page_id] = pageId
        objectStorySpec[AdCreativeObjectStorySpec.Field.link_data] = linkData

        params = {
            AdCreative.Field.image_hash: imageHash,
            AdCreative.Field.body: description,
            AdCreative.Field.title: headline,
            AdCreative.Field.actor_id: pageId,
            AdCreative.Field.object_story_spec: objectStorySpec,
            AdCreative.Field.name: name,
        }
        adCrea = AdCreative()
        return adCrea.api_create(parent_id=self.get_id_assured(),
                                 params=params)
예제 #3
0
    def ad_creative(self):
        creative = None
        error = None
        try:
            link_data = AdCreativeLinkData()
            link_data[AdCreativeLinkData.Field.message] = self.post['title']
            link_data[AdCreativeLinkData.Field.link] = self.post['link']
            link_data[AdCreativeLinkData.Field.caption] = self.post['link']

            object_story_spec = AdCreativeObjectStorySpec()
            object_story_spec[AdCreativeObjectStorySpec.Field.
                              page_id] = os.getenv("FACEBOOK_PAGE_ID")
            object_story_spec[
                AdCreativeObjectStorySpec.Field.link_data] = link_data

            creative = AdCreative(parent_id=self.ad_account_id)
            creative[
                AdCreative.Field.name] = 'AdCreative for ' + self.post['name']
            creative[AdCreative.Field.object_story_spec] = object_story_spec
            creative.remote_create()

        except Exception as err:
            error = err

        return creative, error
예제 #4
0
    def create_ad_creative(self, message, link, image_hash, page_id, act_id,
                           adcreative_name):
        link_data = AdCreativeLinkData()
        link_data[AdCreativeLinkData.Field.message] = message
        link_data[AdCreativeLinkData.Field.link] = link
        link_data[AdCreativeLinkData.Field.image_hash] = image_hash

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

        creative = AdCreative(parent_id='act_{}'.format(act_id))
        creative[AdCreative.Field.name] = 'AdCreative for Link Ad'
        creative[AdCreative.Field.object_story_spec] = object_story_spec
        creative.remote_create()

        return (creative)
예제 #5
0
def create_creative(account_id, name, caption, message, link, imagehash,
                    page_id):
    from facebookads.adobjects.adcreativelinkdata import AdCreativeLinkData
    from facebookads.adobjects.adcreativeobjectstoryspec \
        import AdCreativeObjectStorySpec

    link_data = AdCreativeLinkData()
    link_data[AdCreativeLinkData.Field.message] = message
    link_data[AdCreativeLinkData.Field.link] = link
    link_data[AdCreativeLinkData.Field.caption] = caption
    link_data[AdCreativeLinkData.Field.image_hash] = imagehash

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

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

    print(creative)
    return creative
def create_carousel_ad(caption, adset_id, ad_name, times, design_list,
                       account_id, land_on_design, url, campaign_tag):
    logger = logging.getLogger('testlogger')
    logger.debug("In create carousel")
    conn = None
    simple_list = []
    account_medium_list = {
        "act_940036526039709": "fb_ocpc",
        "act_938286879548007": "acpm",
        "act_1010404049002956": "acpm",
        "act_1385041538425866": "acpm",
        "act_1128744890502204": "jcpc",
        "act_10152414205523137": "int",
        "act_972844956092199": "exp"
    }
    utm_medium = account_medium_list[account_id]
    try:
        urlparse.uses_netloc.append("postgres")
        database_url = urlparse.urlparse(constants.database_url)
        conn = psycopg2.connect(database=database_url.path[1:],
                                user=database_url.username,
                                password=database_url.password,
                                host=database_url.hostname,
                                port=database_url.port)
        curr = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
        for i in xrange(times):
            design_id = design_list[i]
            curr.execute(
                'SELECT discount_percent,designer_id from designs where id=' +
                str(design_id))
            row = curr.fetchone()
            curr.execute(
                'SELECT id,photo_file_name FROM images where design_id = ' +
                str(design_id))
            rows = curr.fetchone()
            curr.execute(
                'SELECT name FROM "categories" INNER JOIN "categories_designs" ON "categories"."id" = "categories_designs"."category_id" WHERE design_id ='
                + str(design_id))
            category_name = curr.fetchone()
            image_link = image_hash.get_image_link(rows['photo_file_name'],
                                                   rows['id'])
            if row['discount_percent'] is None:
                row['discount_percent'] = 0
            product1 = AdCreativeLinkDataChildAttachment()
            if land_on_design:
                product1[
                    AdCreativeLinkDataChildAttachment.Field.
                    link] = 'www.mirraw.com/designers/' + str(
                        row['designer_id']
                    ) + '/designs/' + str(
                        design_id
                    ) + '?utm_source=facebook-auto&utm_medium=' + utm_medium + '&utm_campaign=' + campaign_tag
            else:
                product1[
                    AdCreativeLinkDataChildAttachment.Field.
                    link] = url + '?pid=' + str(
                        design_id
                    ) + '&utm_source=facebook&utm_medium=' + utm_medium + '&utm_campaign=' + campaign_tag
            product1[AdCreativeLinkDataChildAttachment.Field.
                     name] = category_name['name']
            action = AdCreativeLinkDataCallToAction()
            link_value = AdCreativeLinkDataCallToActionValue()
            link_value[AdCreativeLinkDataCallToActionValue.Field.
                       link_title] = 'Discount ' + str(
                           row['discount_percent']) + '%'
            action[AdCreativeLinkDataCallToAction.Field.
                   type] = AdCreativeLinkDataCallToAction.Type.shop_now
            action[AdCreativeLinkDataCallToAction.Field.value] = link_value
            product1[AdCreativeLinkDataChildAttachment.Field.
                     call_to_action] = action
            product1[AdCreativeLinkDataChildAttachment.Field.
                     description] = 'Discount ' + str(
                         row['discount_percent']) + '%'
            product1[AdCreativeLinkDataChildAttachment.Field.
                     image_hash] = image_hash.get_image_hash(
                         image_link, rows[1], account_id)
            sleep(0.5)
            simple_list.append(product1)

        link = AdCreativeLinkData()
        link[
            link.Field.
            link] = url + '&utm_source=facebook&utm_medium=' + utm_medium + '&utm_campaign=' + campaign_tag
        link[link.Field.child_attachments] = simple_list
        link[
            link.Field.
            caption] = url + '&utm_source=facebook&utm_medium=' + utm_medium + '&utm_campaign=' + campaign_tag
        call_to_action_button = AdCreativeLinkDataCallToAction()
        call_to_action_button[
            AdCreativeLinkDataCallToAction.Field.
            type] = AdCreativeLinkDataCallToAction.Type.shop_now
        link[link.Field.call_to_action] = call_to_action_button
        link[link.Field.message] = caption

        logger.info(link)
        story = AdCreativeObjectStorySpec()
        story[story.Field.page_id] = constants.page_id
        story[story.Field.link_data] = link

        creative = AdCreative(parent_id=account_id)
        creative[AdCreative.Field.name] = 'MPA Creative'
        creative[AdCreative.Field.object_story_spec] = story
        creative.remote_create()
        creative = json.loads(str(creative).replace('<AdCreative> ', ''))

        logger.info(creative)
        ad = Ad(parent_id=account_id)
        ad[Ad.Field.name] = ad_name
        ad[Ad.Field.adset_id] = adset_id
        ad[Ad.Field.status] = Campaign.Status.paused
        ad[Ad.Field.creative] = {'creative_id': str(creative['id'])}
        logger.info('Creating Ad')
        ad.remote_create()
        logger.info(ad)

    except psycopg2.DatabaseError, e:
        logger.error('Error %s' % e)
        return False
예제 #7
0
def create_carousel_ad(caption, adset_id, ad_name, design_list, land_on_design,
                       url, campaign_tag, country_code):
    FORMAT = '%(name)s:%(levelname)s:%(asctime)-15s:%(message)s'
    logging.basicConfig(filename='%s-facebook-automated.log' % date.today(),
                        format=FORMAT,
                        level=logging.DEBUG)
    logging.getLogger('create_carousel')
    logging.info('In create carousel')
    connection = None
    simple_list = []
    utm_medium = 'exp'
    try:
        connection = header.create_connection(
            os.environ['FB_APP_DATABASE_URL'])
        cursor = connection.cursor(cursor_factory=psycopg2.extras.DictCursor)
        for design_id in design_list:
            cursor.execute(
                'SELECT designer_id,discount_price from designs where id=' +
                str(design_id[0]))
            row = cursor.fetchone()
            cursor.execute(
                'SELECT id,photo_file_name FROM images where design_id = ' +
                str(design_id[0]))
            rows = cursor.fetchone()
            cursor.execute(
                'SELECT name FROM "categories" INNER JOIN "categories_designs" ON "categories"."id" = "categories_designs"."category_id" WHERE design_id ='
                + str(design_id[0]))
            category_name = cursor.fetchone()
            cursor.execute(
                "SELECT rate FROM currency_converts WHERE country_code = '" +
                country_code + "'")
            rate = cursor.fetchone()
            image_link = image_hash.get_image_link(rows['photo_file_name'],
                                                   rows['id'])
            product1 = AdCreativeLinkDataChildAttachment()
            if land_on_design:
                product1[
                    AdCreativeLinkDataChildAttachment.Field.
                    link] = 'www.mirraw.com/designers/' + str(
                        row['designer_id']
                    ) + '/designs/' + str(
                        design_id[0]
                    ) + '?utm_source=facebook-auto&utm_medium=' + utm_medium + '&utm_campaign=' + campaign_tag
            else:
                product1[
                    AdCreativeLinkDataChildAttachment.Field.
                    link] = url + '?pid=' + str(
                        design_id
                    ) + '&utm_source=facebook&utm_medium=' + utm_medium + '&utm_campaign=' + campaign_tag
            product1[AdCreativeLinkDataChildAttachment.Field.
                     name] = category_name['name']
            price = (row['discount_price'] * 1.0 / rate['rate'])
            product1[AdCreativeLinkDataChildAttachment.Field.
                     description] = 'Price: $' + str(round(price, 2))
            logging.info(image_link)
            logging.info(rows['photo_file_name'])
            product1[AdCreativeLinkDataChildAttachment.Field.
                     image_hash] = image_hash.get_image_hash(
                         image_link, rows['photo_file_name'])
            sleep(10)
            simple_list.append(product1)

        link = AdCreativeLinkData()
        link[link.Field.link] = 'www.mirraw.com'
        link[link.Field.child_attachments] = simple_list
        link[link.Field.caption] = caption

        story = AdCreativeObjectStorySpec()
        story[story.Field.page_id] = header.page_id
        story[story.Field.link_data] = link

        creative = AdCreative(parent_id=header.my_account['id'])
        creative[AdCreative.Field.name] = 'MPA Creative'
        creative[AdCreative.Field.object_story_spec] = story
        creative.remote_create()
        creative = json.loads(str(creative).replace('<AdCreative> ', ''))

        ad = Ad(parent_id=header.my_account['id'])
        ad[Ad.Field.name] = ad_name
        ad[Ad.Field.adset_id] = adset_id
        ad[Ad.Field.status] = Campaign.Status.paused
        ad[Ad.Field.creative] = {'creative_id': str(creative['id'])}
        logging.info('Creating Ad')
        ad.remote_create()
        logging.info(ad)

    except psycopg2.DatabaseError, e:
        logging.error('Error %s' % e)
        return False
            product1[AdCreativeLinkDataChildAttachment.Field.
                     image_hash] = image_hash.get_image_hash(
                         image_link, rows[1])
            simple_list.append(product1)

    except psycopg2.DatabaseError, e:
        print 'Error %s' % e
        sys.exit(1)

    finally:
        if con:
            con.close()
    caption = raw_input("Please enter a caption for the Ad.\n")
    link = AdCreativeLinkData()
    link[link.Field.link] = 'www.mirraw.com'
    link[link.Field.child_attachments] = simple_list
    link[link.Field.caption] = caption

    story = AdCreativeObjectStorySpec()
    story[story.Field.page_id] = header.page_id
    story[story.Field.link_data] = link

    creative = AdCreative(parent_id=header.my_account['id'])
    creative[AdCreative.Field.name] = 'MPA Creative'
    creative[AdCreative.Field.object_story_spec] = story
    creative.remote_create()
    creative = json.loads(str(creative).replace('<AdCreative> ', ''))
    print(creative)
    print creative['creative_id']
    print creative['success']
### Ad ###
print 'Creating Ad: ', ad_name

#Image
image = AdImage(parent_id=my_parent_id)
image[AdImage.Field.filename] = img_filename
image.remote_create()

#Creative
link_data = AdCreativeLinkData()
link_data[AdCreativeLinkData.Field.message] = link_message
link_data[AdCreativeLinkData.Field.link] = link_url
link_data[AdCreativeLinkData.Field.image_hash] = image[AdImage.Field.hash]

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

creative = AdCreative(parent_id=my_parent_id)
creative[AdCreative.Field.name] = 'AdCreative for Link Ad'
creative[AdCreative.Field.object_story_spec] = object_story_spec
creative.remote_create()

creative = AdCreative(parent_id=my_parent_id)
creative[AdCreative.Field.name] = 'AdCreative for Link Ad'
creative[AdCreative.Field.object_story_spec] = object_story_spec
creative.remote_create()
creative_id = creative.get_id()

# The Actual Ad
예제 #10
0
def create_new_ad_set():
    """
    Create a new adset
    """
    ad_account = AdAccount(fbid=my_ad_account_id)
    
    """
    Create and update your ad set here
    """
#https://developers.facebook.com/docs/marketing-api/reference/ad-campaign
    params = {
        AdSet.Field.name: 'joon_SDK_competition',
        AdSet.Field.promoted_object: {
            'page_id': my_page_id,
        },
        AdSet.Field.campaign_id: my_campaign_id,
        AdSet.Field.is_autobid: True,
        AdSet.Field.start_time: 1520560800, 
        AdSet.Field.end_time: 1520701200,
        AdSet.Field.daily_budget: 200,
        AdSet.Field.billing_event: AdSet.BillingEvent.impressions,
        AdSet.Field.optimization_goal: AdSet.OptimizationGoal.page_likes,
# https://developers.facebook.com/docs/marketing-api/targeting-specs
        AdSet.Field.targeting: {
            'geo_locations': {
                'countries': ['IN', 'ID'],
            },
            'age_min': 13,
            'age_max': 35,
            'flexible_spec': [
                {
            
                    'interests': [
                        {
                            'id': 6003332344237 ,
                            'name': 'Dogs',
                        },
                        {
                            'id': 6004037726009,
                            'name': 'Pets',
                        },
                        {
                            'id': 6003266061909,
                            'name': 'Food',
                        },
                        {
                            'id': 6003156370433,
                            'name': 'Cute Animals',
                        },
                        {
                            'id': 625163160959478,
                            'name': 'Adorable Animals',
                        },

                    ],
                },
            ]
        },
        AdSet.Field.status: AdSet.Status.active,
    }
    adset = ad_account.create_ad_set(params=params)



    image = AdImage(parent_id=my_ad_account_id)
    image[AdImage.Field.filename] = '/Users/deriknguyen/Desktop/CS144/clickmaniac/dog_ads/3.jpg'
    image.remote_create()

    # Output image Hash
    image_hash = image[AdImage.Field.hash]

    # First, upload the ad image that you will use in your ad creative
    # Please refer to Ad Image Create for details.

    link_data = AdCreativeLinkData()
    link_data[AdCreativeLinkData.Field.message] = '"Like" to find ways to help man\'s best friend.'
    link_data[AdCreativeLinkData.Field.name] = 'Puppy Love'
    link_data[AdCreativeLinkData.Field.link] = 'http://www.facebook.com/caltech.clickmaniac'
    link_data[AdCreativeLinkData.Field.image_hash] = image_hash

    # Then, use the image hash returned from above
    object_story_spec = AdCreativeObjectStorySpec()
    object_story_spec[AdCreativeObjectStorySpec.Field.page_id] = my_page_id
    object_story_spec[AdCreativeObjectStorySpec.Field.link_data] = link_data
    

    creative = AdCreative(parent_id='my_ad_account_id')
    # creative[AdCreative.Field.title] = 'Puppy Love'
    # creative[AdCreative.Field.body] = 'Cats are one of the deadliest local predators of birds. "Like" this post if you agree they\'re overated!'
    # creative[AdCreative.Field.link_url] = 'http://www.facebook.com/caltech.clickmaniac'
    # creative[AdCreative.Field.image_hash] = image_hash

    creative[AdCreative.Field.object_story_spec] = object_story_spec
    

    # Finally, create your ad along with ad creative.
    # Please note that the ad creative is not created independently, rather its
    # data structure is appended to the ad group
    ad = Ad(parent_id=my_ad_account_id)
    ad[Ad.Field.name] = 'joon_dog_pos_competition'
    ad[Ad.Field.adset_id] = adset[AdSet.Field.id]
    ad[Ad.Field.creative] = creative
    ad.remote_create(params={
        'status': Ad.Status.active,
    })
    return adset