def get_image_hash(url,name): try: 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('image_hash') logging.info('Downloading image') urllib.urlretrieve(url,name) image1 = Img.open(header.PWD+'/'+name) width,height = image1.size size = width if width < height else height # The following code is for center cropping image1 = image1.crop(((width/2)-(size/2),(height/2)-(size/2),(width/2)+(size/2),(height/2)+(size/2))) image1 = image1.resize((1080,1080),Img.ANTIALIAS) image1.save(name) image1.close() image = AdImage(parent_id=header.my_account['id']) image[AdImage.Field.filename] = name logging.info('Uploading image') image.remote_create() sleep(20) os.remove(name) logging.info('Deleted image locally') except OSError, e: logging.error('Error %s' % e) return False
def create_adimage(account_id, imagepath): from facebookads.adobjects.adimage import AdImage image = AdImage(parent_id=account_id) image[AdImage.Field.filename] = imagepath image.remote_create() return image[AdImage.Field.hash]
def get_image_hash(url,name): urllib.urlretrieve(url,name) image = AdImage(parent_id=header.my_account['id']) image[AdImage.Field.filename] = name image.remote_create() os.remove(name) # Output image Hash return image[AdImage.Field.hash]
def create_new_ad_set(): """ Creating a new ad set for the ad account You can use the adset.update method specified here: https://developers.facebook.com/docs/marketing-api/reference/ad-campaign """ adset = AdSet(parent_id=my_ad_account_id) """ UPDATE/CREATE ADSET HERE """ adset[AdSet.Field.campaign_id] = my_campaign_id adset[AdSet.Field.name] = 'Toast_competition' adset[AdSet.Field.promoted_object] = { 'page_id': my_page_id, } adset[AdSet.Field.billing_event] = "IMPRESSIONS" adset[AdSet.Field.daily_budget] = 200 adset[AdSet.Field.is_autobid] = True adset[AdSet.Field.targeting] = { 'geo_locations': { 'countries': ['IN'] }, 'genders': [0], 'age_min': 18, 'age_max': 40, 'interests': [] } adset.remote_create() image = AdImage(parent_id=my_ad_account_id) image[AdImage.Field.filename] = "dog_ads/3.jpg" image.remote_create() image_hash = image[AdImage.Field.hash] creative = AdCreative(parent_id=my_ad_account_id) creative[AdCreative.Field.title] = 'Puppy Love' creative[AdCreative.Field. body] = '"Like" to find ways to help man\'s best friend.' creative[AdCreative.Field.object_id] = my_page_id # creative[AdCreative.Field.object_url] = 'https://www.facebook.com/caltech.clickmaniac/' creative[AdCreative.Field.image_hash] = image_hash ad = Ad(parent_id=my_ad_account_id) ad[Ad.Field.name] = 'Pos' ad[Ad.Field.adset_id] = adset[AdSet.Field.id] ad[Ad.Field.creative] = creative ad.remote_create(params={ 'status': Ad.Status.active, }) return adset
def create_ad_image(self, act_id, image_path): """ This creates the ad image. Have to put into it checking Note: Need to ad in checking to see if the image hash already exists Returns <AdImage> { "hash": "b7e7f2dbb0a6f81dd34537b3048039fc", "url": "https://scontent.xx.fbcdn.net/v/t45.1600-4/22395838_6090162528196_6778197604204281856_n.png?oh=7a10bb464945605d0a66162601383ee4&oe=5A66B82D" } """ image = AdImage(parent_id='act_{}'.format(act_id)) image[AdImage.Field.filename] = image_path image.remote_create() return image
def get_image_hash(url, name, account_id): try: logger = logging.getLogger('testlogger') logger.info('Downloading image') urllib.urlretrieve(url, name) image1 = Img.open(constants.PWD + '/' + name) width, height = image1.size size = width if width < height else height # The following is for center cropping image1 = image1.crop( ((width / 2) - (size / 2), (height / 2) - (size / 2), (width / 2) + (size / 2), (height / 2) + (size / 2))) image1 = image1.resize((1080, 1080), Img.ANTIALIAS) image1.save(name) image1.close() image = AdImage(parent_id=account_id) image[AdImage.Field.filename] = name logger.info('Uploading image') image.remote_create() os.remove(name) logger.info('Deleted image locally') except OSError, e: logger.error('Error %s' % e)
def add_image(self, image_path): image = AdImage(parent_id='act_' + self.id) image[AdImage.Field.filename] = image_path image.remote_create() return image[AdImage.Field.hash]
'status': adset_status, } # if manual bid use: #'bid_amount': '20', ad_account = my_account ad_set = ad_account.create_ad_set( params=params) #could have also used remote_create() method ad_set_id = ad_set.get_id() ### 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'
def create_new_ad_image(filepath): image = AdImage(parent_id=my_ad_account_id) image[AdImage.Field.filename] = filepath image.remote_create() return image[AdImage.Field.hash]
def create_multiple_website_clicks_ads( account, name, country, titles, bodies, urls, image_paths, optimization_goal, pixel_id, billing_event, bid_amount, daily_budget=None, lifetime_budget=None, start_time=None, end_time=None, age_min=None, age_max=None, genders=None, campaign=None, status=AdSet.Status.paused, ): # 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.' ) # Create campaign if campaign: campaign = Campaign(fbid=campaign)[Campaign.Field.id] else: campaign = Campaign(parent_id=account) campaign[Campaign.Field.name] = name + ' Campaign' campaign[Campaign.Field.objective] = \ Campaign.Objective.conversions campaign[Campaign.Field.status] = \ Campaign.Status.active if not status \ else Campaign.Status.paused campaign.remote_create() campaign = campaign[AdSet.Field.id] # Create ad set ad_set = AdSet(parent_id=account) ad_set[AdSet.Field.campaign_id] = campaign ad_set[AdSet.Field.name] = name + ' AdSet' ad_set[AdSet.Field.optimization_goal] = optimization_goal ad_set[AdSet.Field.promoted_object] = { 'pixel_id': pixel_id, 'custom_event_type': 'COMPLETE_REGISTRATION' } 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 targeting = {} targeting[Targeting.Field.geo_locations] = { 'countries': [country] } if age_max: targeting[Targeting.Field.age_max] = age_max if age_min: targeting[Targeting.Field.age_min] = age_min if genders: targeting[Targeting.Field.genders] = genders ad_set[AdSet.Field.targeting] = targeting ad_set.remote_create() # Upload the images first one by one image_hashes = [] for image_path in image_paths: img = AdImage(parent_id=account) img[AdImage.Field.filename] = image_path img.remote_create() image_hashes.append(img.get_hash()) ADGROUP_BATCH_CREATE_LIMIT = 10 ad_groups_created = [] def callback_failure(response): raise response.error() # For each creative permutation for creative_info_batch in generate_batches( itertools.product(titles, bodies, urls, image_hashes), ADGROUP_BATCH_CREATE_LIMIT ): api_batch = api.new_batch() for title, body, url, image_hash in creative_info_batch: # Create the ad ad = Ad(parent_id=account) ad[Ad.Field.name] = name + ' Ad' ad[Ad.Field.adset_id] = ad_set[AdSet.Field.id] ad[Ad.Field.creative] = { AdCreative.Field.title: title, AdCreative.Field.body: body, AdCreative.Field.object_url: url, AdCreative.Field.image_hash: image_hash, } ad[Ad.Field.status] = status ad.remote_create(batch=api_batch, failure=callback_failure) ad_groups_created.append(ad) api_batch.execute() return ad_groups_created
from facebook_business.adobjects.adaccount import AdAccount from facebook_business.adobjects.adcreative import AdCreative from facebook_business.api import FacebookAdsApi from facebookads.adobjects.adimage import AdImage # 4. 광고 크리에이티브 제공 (광고 형식 디자인) image = AdImage(parent_id='act_306011116959172') image[AdImage.Field.filename] = './examples/test.png' image.remote_create() # Output image Hash print(image[AdImage.Field.hash]) access_token = 'EAAg7zM8m1wQBAJk5CK770dtAxi4oiudZCNg7Rqj0fn4faKqL8SbSJYTWYfYrhZBctMBlT6A3kTQVJLcLVsWDf69TU0QcnL5vmygdoViZBTQGkC7U029JzOBmPi2YR7Thd46zon0OQHmaVHq01FgHCL2MxTW7SPEUMKMFhcqZBPN7iALMgKnD74lGCTGjGlUZD' app_secret = '4c1c34b9502793e4837a00f2a7b7f7aa' app_id = '2317550648481540' id = 'act_306011116959172' FacebookAdsApi.init(access_token=access_token) fields = [] params = { 'name': 'Sample Creative', 'object_story_spec': { 'page_id': '<pageID>', 'link_data': { 'image_hash': '<imageHash>', 'link': 'https:\/\/facebook.com\/<pageID>', 'message': 'try it out' } }, }
def hash_image(self): image = AdImage(parent_id=self.ad_account_id) image[AdImage.Field.filename] = self.post['image_path'] image.remote_create() return image[AdImage.Field.hash]
from facebookads.adobjects.adimage import AdImage import initialization AD_ACCOUNT_ID = initialization.account_id image = AdImage(parent_id=AD_ACCOUNT_ID) image[AdImage.Field.filename] = '../other_resources/image.png' image.remote_create() image_hash = image[AdImage.Field.hash]
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
targeting[Targeting.Field.interests] = [{ 'id': interest_id, 'name': interest_name, }] targeting[Targeting.Field.publisher_platforms] = [ 'facebook', 'instagram', 'messenger' ] targeting[Targeting.Field.facebook_positions] = ['feed'] targeting[Targeting.Field.instagram_positions] = ['stream'] targeting[Targeting.Field.messenger_positions] = ['messenger_home'] ad_set[AdSet.Field.targeting] = targeting ad_set.remote_create() img = AdImage(parent_id=account_publicitaire) img[AdImage.Field.filename] = image_path img.remote_create() image_hash = img.get_hash() link_data = AdCreativeLinkData() link_data[AdCreativeLinkData.Field.link] = url link_data[AdCreativeLinkData.Field.image_hash] = image_hash link_data[ AdCreativeLinkData.Field.message] = "Le texte de votre publicité ici." link_data[ AdCreativeLinkData.Field. description] = "Le petit message en dessous du titre de la publicité." link_data[AdCreativeLinkData.Field.name] = "Le titre de votre publicité" link_data[AdCreativeLinkData.Field.caption] = 'votredomaine.fr'