Exemplo n.º 1
0
class Accounts(object):
    def __init__(self,task_data):
        super(Accounts).__init__()
        self.database = DatabaseOperation()
        my_app_id = task_data.app_id
        my_app_secret = task_data.app_secret
        my_access_token = task_data.access_token
        business_id = task_data.business_id

        # Start the connection to the facebook API
        FacebookAdsApi.init(my_app_id, my_app_secret, my_access_token)

        # Create a business object for the business account
        business = Business(business_id)

        # Get all ad accounts on the business account
        accounts = business.get_owned_ad_accounts(
            fields=[
                AdAccount.Field.account_id ,
                AdAccount.Field.account_status ,
                AdAccount.Field.name ,
                AdAccount.Field.age ,
                AdAccount.Field.amount_spent ,
                AdAccount.Field.business ,
                AdAccount.Field.business_city ,
                AdAccount.Field.business_country_code ,
                AdAccount.Field.business_name ,
                AdAccount.Field.business_state ,
                AdAccount.Field.business_street ,
                AdAccount.Field.business_street2 ,
                AdAccount.Field.business_zip
            ]
        )

        # To keep track of rows added to file
        rows = 0

        #Iterate through the adaccounts
        for account in accounts:
            # Write all ad info to the file, and increment the number of rows that will display
            #Construct the params of time
            id_account = self.database.select('account', 'account_id',account[AdAccount.Field.account_id])
            if len(id_account) == 0:
                value = {
                    'fb_id':account[AdAccount.Field.id],
                    'account_id':account[AdAccount.Field.account_id],
                    'name':account[AdAccount.Field.name],
                    'account_status':account[AdAccount.Field.account_status],
                    'age':account[AdAccount.Field.age],
                    'amount_spent':account[AdAccount.Field.amount_spent]
                }
                rows = rows + 1
                self.database.insert('account',value)

        print(str(rows) + " rows added ")
Exemplo n.º 2
0
    def __init__(self):
        super(ConversationApp).__init__()
        #Log
        now = datetime.now()
        print(' Start to run Conversation', now.year, now.month, now.day,
              now.hour, now.minute, now.second)

        self.database = DatabaseOperation()
        page_data = self.database.select('token')

        for page in page_data:
            hilo = threading.Thread(target=self.run_conversations_thread,
                                    kwargs={'page_data': page})
            hilo.start()

        now = datetime.now()
        print(' End to run Post', now.year, now.month, now.day, now.hour,
              now.minute, now.second)
Exemplo n.º 3
0
#File to run all data
# Import system necesary library
import threading, datetime, sys, json, os, argparse, logging, time, shutil

#conexion a la base de datos
from datab import DatabaseOperation
from pagedata import PageApp
from postdata import PostApp

#Database conection
DB = DatabaseOperation()

# TODO: use the latest version available
VERSION = '3.1'


def remove_folder(path):
    if os.path.exists(path):
        shutil.rmtree(path)
    else:
        print('No existe temp')


def run_page_thread(**page_data):
    page_data = page_data['page_data']
    page_id = page_data.id_page
    now = datetime.datetime.now()
    deltaCurrent = now.strftime('%Y-%m-%d %X')
    deltalog = now.strftime('%Y-%m-%d-%X')
    print(' Start to run Page ', page_id, now.year, now.month, now.day,
          now.hour, now.minute, now.second)
Exemplo n.º 4
0
class ConversationApp(object):
    def __init__(self):
        super(ConversationApp).__init__()
        #Log
        now = datetime.now()
        print(' Start to run Conversation', now.year, now.month, now.day,
              now.hour, now.minute, now.second)

        self.database = DatabaseOperation()
        page_data = self.database.select('token')

        for page in page_data:
            hilo = threading.Thread(target=self.run_conversations_thread,
                                    kwargs={'page_data': page})
            hilo.start()

        now = datetime.now()
        print(' End to run Post', now.year, now.month, now.day, now.hour,
              now.minute, now.second)

    def run_conversations_thread(self, page_data):
        page_id = page_data.id_page
        page_token = page_data.token
        graph = facebook.GraphAPI(access_token=page_token, version=VERSION)
        args = {
            'fields':
            'id,message_count,updated_time,link,messages.limit(100){id,from,message,sticker,created_time}',
            'limit': 499
        }  #requested fields
        conv = graph.get_object(page_id + '/conversations', **args)
        # print(conv)
        # Wrap this block in a while loop so we can keep paginating requests until
        # finished.
        while True:
            try:
                # Perform some action on each post in the collection we receive from
                # Facebook.
                #[self.conversation_process(mess=convv) for convv in conv['data']]
                for mess in conv['data']:
                    conversation_id = mess['id']
                    message_count = mess['message_count']
                    conversation_db = self.database.select(
                        'conversation', 'conversation_id', conversation_id)
                    # print(conversation_db)
                    lon = len(conversation_db)
                    if (lon > 0):
                        #Existe (No tiene que traer mas)
                        print('Existe')
                        raise GetOutOfLoop
                    else:
                        #No Existe
                        data_to_database = {}
                        data_to_database['conversation_id'] = conversation_id
                        data_to_database['id_page'] = page_id
                        data_to_database['link'] = mess['link']
                        data_to_database['message_count'] = message_count
                        data_to_database['updated_time'] = mess['updated_time']
                        self.database.insert('conversation', data_to_database)
                        del data_to_database
                        self.message_process(conversation_id,
                                             mess['messages']['data'])
                # Attempt to make a request to the next page of data, if it exists.
                time.sleep(1)
                conv = requests.get(conv['paging'][next]).json()
            except KeyError:
                # When there are no more pages (['paging']['next']), break from the
                # loop and end the script.
                break
            except GetOutOfLoop:
                break

    def message_process(self, conversation_id, message_data):
        print(conversation_id)
        for message in message_data:
            data_to_database = {}
            data_to_database['message_id'] = message['id']
            data_to_database['conversation_id'] = conversation_id
            data_to_database['from_id'] = message['from']['id']
            data_to_database['from_name'] = message['from']['name']
            data_to_database['from_email'] = message['from']['email']
            try:
                msn = message['message']
            except:
                msn = ''
            try:
                strk = message['sticker']
            except:
                strk = ''
            data_to_database['message'] = msn
            data_to_database['sticker'] = strk
            data_to_database['message_created_time'] = message['created_time']
            self.database.insert('message', data_to_database)
            del data_to_database
Exemplo n.º 5
0
    def __init__(self, task_data, account_id):
        super(Adset).__init__()
        print('Run Adset')
        self.database = DatabaseOperation()
        my_app_id = task_data.app_id
        my_app_secret = task_data.app_secret
        my_access_token = task_data.access_token
        business_id = task_data.business_id
        since = task_data.since
        until = task_data.until
        period = task_data.period
        since = since.strftime('%Y-%m-%d')
        until = until.strftime('%Y-%m-%d')

        # Start the connection to the facebook API
        FacebookAdsApi.init(my_app_id, my_app_secret, my_access_token)

        #Construct the params of time
        if not period:
            params = {
                'level': 'adset',
                'time_range': {
                    'since': since,
                    'until': until,
                }
            }
        else:
            params = {
                'level': 'adset',
            }

        # Get all ad accounts on the business account
        # accounts = self.database.select('account','id_task', id_task)

        # Get Fields Configuration
        fields = self.database.select('field', 'type_configuration', 'adset')
        fieldsList = []
        rows = 0
        for field in fields:
            if (field.active == 'Y'):
                fieldsList.append(field.name_field)

        del fields
        adset_insights = self.database.select('field', 'type_configuration',
                                              'adset-insight')
        adset_insight_fields_list = []
        for adset_insight in adset_insights:
            if (adset_insight.active == 'Y'):
                adset_insight_fields_list.append(adset_insight.name_field)
                rows = rows + 1
        del adset_insight

        # Grab insight info for all camp in the adaccount
        adsets = AdAccount('act_' + account_id).get_ad_sets(params=params,
                                                            fields=fieldsList)
        for adset in adsets:

            data_adset = {'id_platform': 1}
            for field in fieldsList:
                if (field in adset):
                    if (field == 'targeting'):
                        data_adset[field] = superSerialize(adset[field])
                    else:
                        data_adset[field] = adset[field]
            #Save in data base
            try:
                self.database.insert('adset', data_adset)
                del data_adset
            except:
                del data_adset

        # Hace el llamado
        insights_list = AdAccount('act_' + account_id).get_insights(
            params=params, fields=adset_insight_fields_list)
        if (len(insights_list) > 0):
            for insight in insights_list:
                insights_data = {
                    'level_insight': 'adset',
                    'time_increment': task_data.increment
                }
                for adset_insight_field in adset_insight_fields_list:
                    if (adset_insight_field in insight):
                        insights_data[adset_insight_field] = insight[
                            adset_insight_field]

                self.database.insert('insight', insights_data)
                del insights_data
            #Check if you reached 75% of the limit, if yes then back-off for 5 minutes (put this chunk in your 'for ad is ads' loop, every 100-200 iterations)
            if (check_limit(account_id, my_access_token) > 75):
                print('75% Rate Limit Reached. Cooling Time 5 Minutes. ' +
                      account_id)
                logging.debug(
                    '75% Rate Limit Reached. Cooling Time 5 Minutes.')
                time.sleep(300)
                print('Cooling finish.' + account_id)
Exemplo n.º 6
0
    def __init__(self, task_data, account_id):
        super(AdsCreativites).__init__()
        print('Runnig creatives')
        self.database = DatabaseOperation()
        my_app_id = task_data.app_id
        my_app_secret = task_data.app_secret
        my_access_token = task_data.access_token
        business_id = task_data.business_id
        #Fields for creativities
        fields = [
            'account_id', 'actor_id', 'adlabels', 'applink_treatment',
            'asset_feed_spec', 'authorization_category', 'auto_update', 'body',
            'branded_content_sponsor_page_id', 'bundle_folder_id',
            'call_to_action_type', 'categorization_criteria',
            'category_media_source', 'destination_set_id', 'dynamic_ad_voice',
            'effective_authorization_category', 'effective_instagram_story_id',
            'effective_object_story_id', 'enable_direct_install',
            'enable_launch_instant_app', 'id', 'image_crops', 'image_hash',
            'image_url', 'instagram_actor_id', 'instagram_permalink_url',
            'instagram_story_id', 'interactive_components_spec',
            'link_deep_link_url', 'link_og_id', 'link_url',
            'messenger_sponsored_message', 'name', 'object_id',
            'object_store_url', 'object_story_id', 'object_story_spec',
            'object_type', 'object_url', 'place_page_set_id',
            'platform_customizations', 'playable_asset_id',
            'portrait_customizations', 'product_set_id',
            'recommender_settings', 'status', 'template_url',
            'template_url_spec', 'thumbnail_url', 'title', 'url_tags',
            'use_page_actor_override', 'video_id', 'image_file',
            'is_dco_internal'
        ]

        #Get ads by task
        ads = self.database.select('ad', 'read', 'N')
        # print(ads)

        # To keep track of rows added to file
        rows = 0

        for adid in ads:
            creatives = AdAccount(adid['ad_id']).get_ad_creatives(
                fields=fields, )

            for creative in creatives:
                # print(creative)
                account_id = ""
                actor_id = ""
                adlabels = ""
                applink_treatment = ""
                asset_feed_spec = ""
                authorization_category = ""
                auto_update = ""
                body = ""
                branded_content_sponsor_page_id = ""
                bundle_folder_id = ""
                call_to_action_type = ""
                categorization_criteria = ""
                category_media_source = ""
                destination_set_id = ""
                dynamic_ad_voice = ""
                effective_authorization_category = ""
                effective_instagram_story_id = ""
                effective_object_story_id = ""
                enable_direct_install = ""
                enable_launch_instant_app = ""
                creative_id = ""
                image_crops = ""
                image_hash = ""
                image_url = ""
                instagram_actor_id = ""
                instagram_permalink_url = ""
                instagram_story_id = ""
                interactive_components_spec = ""
                link_deep_link_url = ""
                link_og_id = ""
                link_url = ""
                messenger_sponsored_message = ""
                name = ""
                object_id = ""
                object_store_url = ""
                object_story_id = ""
                object_story_spec = ""
                object_type = ""
                object_url = ""
                place_page_set_id = ""
                platform_customizations = ""
                playable_asset_id = ""
                portrait_customizations = ""
                product_set_id = ""
                recommender_settings = ""
                status = ""
                template_url = ""
                template_url_spec = ""
                thumbnail_url = ""
                title = ""
                url_tags = ""
                use_page_actor_override = ""
                video_id = ""
                image_file = ""
                is_dco_internal = ""
                url_post = ""

                if 'account_id' in creative:
                    account_id = creative[AdCreative.Field.account_id]
                if 'actor_id' in creative:
                    actor_id = creative[AdCreative.Field.actor_id]
                if 'adlabels' in creative:
                    adlabels = creative[AdCreative.Field.adlabels]
                if 'applink_treatment' in creative:
                    applink_treatment = creative[
                        AdCreative.Field.applink_treatment]
                if 'asset_feed_spec' in creative:
                    asset_feed_spec = creative[
                        AdCreative.Field.asset_feed_spec]
                if 'authorization_category' in creative:
                    authorization_category = creative[
                        AdCreative.Field.authorization_category]
                if 'auto_update' in creative:
                    auto_update = creative[AdCreative.Field.auto_update]
                if 'body' in creative:
                    body = creative[AdCreative.Field.body]
                if 'branded_content_sponsor_page_id' in creative:
                    branded_content_sponsor_page_id = creative[
                        AdCreative.Field.branded_content_sponsor_page_id]
                if 'bundle_folder_id' in creative:
                    bundle_folder_id = creative[
                        AdCreative.Field.bundle_folder_id]
                if 'call_to_action_type' in creative:
                    call_to_action_type = creative[
                        AdCreative.Field.call_to_action_type]
                if 'categorization_criteria' in creative:
                    categorization_criteria = creative[
                        AdCreative.Field.categorization_criteria]
                if 'category_media_source' in creative:
                    category_media_source = creative[
                        AdCreative.Field.category_media_source]
                if 'destination_set_id' in creative:
                    destination_set_id = creative[
                        AdCreative.Field.destination_set_id]
                if 'dynamic_ad_voice' in creative:
                    dynamic_ad_voice = creative[
                        AdCreative.Field.dynamic_ad_voice]
                if 'effective_authorization_category' in creative:
                    effective_authorization_category = creative[
                        AdCreative.Field.effective_authorization_category]
                if 'effective_instagram_story_id' in creative:
                    effective_instagram_story_id = creative[
                        AdCreative.Field.effective_instagram_story_id]
                if 'effective_object_story_id' in creative:
                    effective_object_story_id = creative[
                        AdCreative.Field.effective_object_story_id]
                if 'enable_direct_install' in creative:
                    enable_direct_install = creative[
                        AdCreative.Field.enable_direct_install]
                if 'enable_launch_instant_app' in creative:
                    enable_launch_instant_app = creative[
                        AdCreative.Field.enable_launch_instant_app]
                if 'id' in creative:
                    creative_id = creative[AdCreative.Field.id]
                if 'image_crops' in creative:
                    image_crops = creative[AdCreative.Field.image_crops]
                if 'image_hash' in creative:
                    image_hash = creative[AdCreative.Field.image_hash]
                if 'image_url' in creative:
                    image_url = creative[AdCreative.Field.image_url]
                if 'instagram_actor_id' in creative:
                    instagram_actor_id = creative[
                        AdCreative.Field.instagram_actor_id]
                if 'instagram_permalink_url' in creative:
                    instagram_permalink_url = creative[
                        AdCreative.Field.instagram_permalink_url]
                if 'instagram_story_id' in creative:
                    instagram_story_id = creative[
                        AdCreative.Field.instagram_story_id]
                if 'interactive_components_spec' in creative:
                    interactive_components_spec = creative[
                        AdCreative.Field.interactive_components_spec]
                if 'link_deep_link_url' in creative:
                    link_deep_link_url = creative[
                        AdCreative.Field.link_deep_link_url]
                if 'link_og_id' in creative:
                    link_og_id = creative[AdCreative.Field.link_og_id]
                if 'link_url' in creative:
                    link_url = creative[AdCreative.Field.link_url]
                if 'messenger_sponsored_message' in creative:
                    messenger_sponsored_message = creative[
                        AdCreative.Field.messenger_sponsored_message]
                if 'name' in creative:
                    name = creative[AdCreative.Field.name]
                if 'object_id' in creative:
                    object_id = creative[AdCreative.Field.object_id]
                if 'object_store_url' in creative:
                    object_store_url = creative[
                        AdCreative.Field.object_store_url]
                if 'object_story_id' in creative:
                    object_story_id = creative[
                        AdCreative.Field.object_story_id]
                    object_id_split = object_story_id.split('_')
                    url_post = "https://www.facebook.com/" + object_id_split[
                        0] + "/posts/" + object_id_split[1]
                if 'object_story_spec' in creative:
                    object_story_spec = creative[
                        AdCreative.Field.object_story_spec]
                if 'object_type' in creative:
                    object_type = creative[AdCreative.Field.object_type]
                if 'object_url' in creative:
                    object_url = creative[AdCreative.Field.object_url]
                if 'place_page_set_id' in creative:
                    place_page_set_id = creative[
                        AdCreative.Field.place_page_set_id]
                if 'platform_customizations' in creative:
                    platform_customizations = creative[
                        AdCreative.Field.platform_customizations]
                if 'playable_asset_id' in creative:
                    playable_asset_id = creative[
                        AdCreative.Field.playable_asset_id]
                if 'portrait_customizations' in creative:
                    portrait_customizations = creative[
                        AdCreative.Field.portrait_customizations]
                if 'product_set_id' in creative:
                    product_set_id = creative[AdCreative.Field.product_set_id]
                if 'recommender_settings' in creative:
                    recommender_settings = creative[
                        AdCreative.Field.recommender_settings]
                if 'status' in creative:
                    status = creative[AdCreative.Field.status]
                if 'template_url' in creative:
                    template_url = creative[AdCreative.Field.template_url]
                if 'template_url_spec' in creative:
                    template_url_spec = creative[
                        AdCreative.Field.template_url_spec]
                if 'thumbnail_url' in creative:
                    thumbnail_url = creative[AdCreative.Field.thumbnail_url]
                if 'title' in creative:
                    title = creative[AdCreative.Field.title]
                if 'url_tags' in creative:
                    url_tags = creative[AdCreative.Field.url_tags]
                if 'use_page_actor_override' in creative:
                    use_page_actor_override = creative[
                        AdCreative.Field.use_page_actor_override]
                if 'video_id' in creative:
                    video_id = creative[AdCreative.Field.video_id]
                if 'image_file' in creative:
                    image_file = creative[AdCreative.Field.image_file]

                creative_data = {
                    'id_ad':
                    adid['id'],
                    'ad_id':
                    adid['ad_id'],
                    'actor_id':
                    str(actor_id),
                    'adlabels':
                    str(adlabels),
                    'applink_treatment':
                    str(applink_treatment),
                    'asset_feed_spec':
                    str(asset_feed_spec),
                    'authorization_category':
                    str(authorization_category),
                    'auto_update':
                    str(auto_update),
                    'body':
                    str(body),
                    'branded_content_sponsor_page_id':
                    str(branded_content_sponsor_page_id),
                    'bundle_folder_id':
                    str(bundle_folder_id),
                    'call_to_action_type':
                    str(call_to_action_type),
                    'categorization_criteria':
                    str(categorization_criteria),
                    'category_media_source':
                    str(category_media_source),
                    'destination_set_id':
                    str(destination_set_id),
                    'dynamic_ad_voice':
                    str(dynamic_ad_voice),
                    'effective_authorization_category':
                    str(effective_authorization_category),
                    'effective_instagram_story_id':
                    str(effective_instagram_story_id),
                    'effective_object_story_id':
                    str(effective_object_story_id),
                    'enable_direct_install':
                    str(enable_direct_install),
                    'enable_launch_instant_app':
                    str(enable_launch_instant_app),
                    'creative_id':
                    str(creative_id),
                    'image_crops':
                    str(image_crops),
                    'image_hash':
                    str(image_hash),
                    'image_url':
                    str(image_url),
                    'instagram_actor_id':
                    str(instagram_actor_id),
                    'instagram_permalink_url':
                    str(instagram_permalink_url),
                    'instagram_story_id':
                    str(instagram_story_id),
                    'interactive_components_spec':
                    str(interactive_components_spec),
                    'link_deep_link_url':
                    str(link_deep_link_url),
                    'link_og_id':
                    str(link_og_id),
                    'link_url':
                    str(link_url),
                    'messenger_sponsored_message':
                    str(messenger_sponsored_message),
                    'name':
                    str(name),
                    'object_id':
                    str(object_id),
                    'object_store_url':
                    str(object_store_url),
                    'object_story_id':
                    str(object_story_id),
                    'object_story_spec':
                    str(object_story_spec),
                    'object_type':
                    str(object_type),
                    'object_url':
                    str(object_url),
                    'place_page_set_id':
                    str(place_page_set_id),
                    'platform_customizations':
                    str(platform_customizations),
                    'playable_asset_id':
                    str(playable_asset_id),
                    'portrait_customizations':
                    str(portrait_customizations),
                    'product_set_id':
                    str(product_set_id),
                    'recommender_settings':
                    str(recommender_settings),
                    'status':
                    str(status),
                    'template_url':
                    str(template_url),
                    'template_url_spec':
                    str(template_url_spec),
                    'thumbnail_url':
                    str(thumbnail_url),
                    'title':
                    str(title),
                    'url_tags':
                    str(url_tags),
                    'use_page_actor_override':
                    str(use_page_actor_override),
                    'video_id':
                    str(video_id),
                    'image_file':
                    str(image_file),
                    'is_dco_internal':
                    str(is_dco_internal),
                    'url_post':
                    url_post,
                    'created_time':
                    datetime.datetime.now().strftime('%Y-%m-%d %X')
                }

                # convert to string
                #creative_data = json.dumps(creative_data)

                self.database.insert('creative', creative_data)
                self.database.update('ad', 'ad_id', adid['ad_id'],
                                     {'read': 'Y'})
                rows += 1
                #Check if you reached 75% of the limit, if yes then back-off for 5 minutes (put this chunk in your 'for ad is ads' loop, every 100-200 iterations)
                if (check_limit(account_id, my_access_token) > 75):
                    print('75% Rate Limit Reached. Cooling Time 5 Minutes. ' +
                          account_id)
                    logging.debug(
                        '75% Rate Limit Reached. Cooling Time 5 Minutes.')
                    time.sleep(300)
                    print('Cooling finish.' + account_id)

                del actor_id
                del adlabels
                del applink_treatment
                del asset_feed_spec
                del authorization_category
                del auto_update
                del body
                del branded_content_sponsor_page_id
                del bundle_folder_id
                del call_to_action_type
                del categorization_criteria
                del category_media_source
                del destination_set_id
                del dynamic_ad_voice
                del effective_authorization_category
                del effective_instagram_story_id
                del effective_object_story_id
                del enable_direct_install
                del enable_launch_instant_app
                del creative_id
                del image_crops
                del image_hash
                del image_url
                del instagram_actor_id
                del instagram_permalink_url
                del instagram_story_id
                del interactive_components_spec
                del link_deep_link_url
                del link_og_id
                del link_url
                del messenger_sponsored_message
                del name
                del object_id
                del object_store_url
                del object_story_id
                del object_story_spec
                del object_type
                del object_url
                del place_page_set_id
                del platform_customizations
                del playable_asset_id
                del portrait_customizations
                del product_set_id
                del recommender_settings
                del status
                del template_url
                del template_url_spec
                del thumbnail_url
                del title
                del url_tags
                del use_page_actor_override
                del video_id
                del image_file
                del is_dco_internal
                del url_post
Exemplo n.º 7
0
    def __init__(self,task_data,account_id):
        super(Campain).__init__()
        self.database = DatabaseOperation()
        print('Running Campains')
        my_app_id = task_data.app_id
        my_app_secret = task_data.app_secret
        my_access_token = task_data.access_token
        since=task_data.since
        until=task_data.until
        period=task_data.period
        since=since.strftime('%Y-%m-%d')
        until=until.strftime('%Y-%m-%d')

        # Start the connection to the facebook API
        FacebookAdsApi.init(my_app_id, my_app_secret, my_access_token)

        # Get Fields Configuration
        fields = self.database.select('field','type_configuration','campaing')
        fields_list = []
        fields_list_names = []

        for field in fields:
            if (field.active == 'Y'):
                fields_list.append(field)
                fields_list_names.append(field.name_field)

        del fields

        # Get field for insights from data base 
        campaing_insights = self.database.select('field','type_configuration','campaing-insight')
        campaing_insights_fields_list = []
        campaing_insights_fields_names = []
        for campaing_insights in campaing_insights:
            if (campaing_insights.active == 'Y'):
                campaing_insights_fields_list.append(campaing_insights)
                campaing_insights_fields_names.append(campaing_insights.name_field)
        del campaing_insights

        #Construct the params of time
        if not period:
            params = {
                'level':'campaign',
                'time_range': {
                    'since': since,
                    'until': until,
                }
            }
        else:
            params = {
                'level':'campaign',
                'date_preset': period
            }
        # Grab insight info for all camp in the adaccount
        camps = AdAccount('act_'+account_id).get_campaigns(
            fields=fields_list_names,
            params=params
        )
        
        for camp in camps:
            campaing_data = {
                'id_platform': 1
            }
            for field in fields_list:
                if(field.name_field in camp):
                    if(field.serialize == 'Y'):
                            campaing_data[field.name_field] = superSerialize(camp[field.name_field])
                    else:
                        campaing_data[field.name_field] = camp[field.name_field]

            try:
                self.database.insert('campaing',campaing_data)
                del campaing_data 
            except:
                del campaing_data 
        
        params['time_increment'] = task_data.increment
        insights_list =  AdAccount('act_'+account_id).get_insights(
            params = params,
            fields = campaing_insights_fields_names
        )
        if(len(insights_list)>0):
            for insight in insights_list:
                insights_data = {
                    'level_insight':'campaign',
                    'time_increment': task_data.increment
                }
                for campaing_insights_field in campaing_insights_fields_list:
                    if (campaing_insights_field.name_field in insight):
                        if(campaing_insights_field.serialize == 'Y'):
                            insights_data[campaing_insights_field.name_field] = superSerialize(insight[campaing_insights_field.name_field])
                        else:
                            insights_data[campaing_insights_field.name_field] = insight[campaing_insights_field.name_field]
                try:
                    self.database.insert('insight',insights_data)
                    del insights_data 
                except:
                    print('falla')            
                    del insights_data
        
            #Check if you reached 75% of the limit, if yes then back-off for 5 minutes (put this chunk in your 'for ad is ads' loop, every 100-200 iterations)
            if (check_limit(account_id,my_access_token)>75):
                print('75% Rate Limit Reached. Cooling Time 5 Minutes.'+ account_id)
                logging.debug('75% Rate Limit Reached. Cooling Time 5 Minutes.')
                time.sleep(300)
                print('Cooling finish.'+ account_id)
Exemplo n.º 8
0
    def __init__(self, task_data, account_id):
        super(Ads).__init__()
        print('Running AD')
        self.database = DatabaseOperation()
        my_app_id = task_data.app_id
        my_app_secret = task_data.app_secret
        my_access_token = task_data.access_token
        since = task_data.since
        until = task_data.until
        period = task_data.period
        since = since.strftime('%Y-%m-%d')
        until = until.strftime('%Y-%m-%d')
        # Start the connection to the facebook API
        FacebookAdsApi.init(my_app_id, my_app_secret, my_access_token)

        # Arma la lista para llenar la tabla AD
        fields = self.database.select('field', 'type_configuration', 'ad')
        ad_field_list = []

        for field in fields:
            if (field.active == 'Y'):
                ad_field_list.append(field.name_field)

        # Arma la lista para llenar la tabla de insight
        fields = self.database.select('field', 'type_configuration',
                                      'ad-insight')
        ad_field_insight_list = []

        for field in fields:
            if (field.active == 'Y'):
                ad_field_insight_list.append(field.name_field)

        del fields
        # Merge de las 2 listas
        ad_field_insight_list = ad_field_insight_list + ad_field_list
        # Define los parametros para el llamado
        if not period:
            params_ad = {
                'level': 'ad',
                'time_range': {
                    'since': since,
                    'until': until
                },
                'time_increment': task_data.increment
            }
        else:
            params_ad = {
                'level': 'ad',
                'date_preset': period,
                'time_increment': task_data.increment
            }

        # Arma el diccionrio de Breakdowns si está activo
        if (task_data.breakdown == 'Y'):
            breakdowns = self.database.select('breakdown')
            breakdown_names = []
            breakdown_list = []

            for breakdown in breakdowns:
                if (breakdown.active == 'Y'):
                    breakdown_names.append(breakdown.name)
                    breakdown_list.append(breakdown)
            del breakdowns
            params_ad['breakdowns'] = breakdown_names

        # Hace el llamado
        try:
            data_list = AdAccount("act_" + account_id).get_insights(
                params=params_ad, fields=ad_field_insight_list)
        except:
            data_list = []

        if (len(data_list) > 0):
            # Recorre el llamado
            for data_single in data_list:
                # data para la tabla ad
                ad_data = {'read': 'N', 'id_platform': 1}
                # Extrae los datos para la tabla AD
                for ad_field in ad_field_list:
                    if (ad_field in data_single):
                        ad_data[ad_field] = data_single[ad_field]
                # Inserta en la tabla add
                try:
                    self.database.insert('ad', ad_data)
                    ad_id = ad_data['ad_id']
                    del ad_data
                except:
                    ad_id = ad_data['ad_id']

                # Extrae los datos para la tabla Insight
                if (task_data.breakdown == 'Y'):
                    #Datos con breakdown
                    ad_field_insight_list = ad_field_insight_list + breakdown_names
                # Datos Estaticos
                insights_data = {
                    'ad_id': ad_id,
                    'level_insight': 'ad',
                    'time_increment': task_data.increment
                }
                # Extrae los datos para la tabla Insight
                for ad_field_insight in ad_field_insight_list:
                    if (ad_field_insight in data_single):
                        if ad_field_insight == 'relevance_score':
                            insights_data[ad_field_insight] = superSerialize(
                                data_single[ad_field_insight])
                        else:
                            insights_data[ad_field_insight] = data_single[
                                ad_field_insight]

                # Inserta los datos para la tabla Insight
                try:
                    self.database.insert('insight', insights_data)
                    del insights_data
                except:
                    print('falla')
                #Check if you reached 75% of the limit, if yes then back-off for 5 minutes (put this chunk in your 'for ad is ads' loop, every 100-200 iterations)
                if (check_limit(account_id, my_access_token) > 75):
                    print('75% Rate Limit Reached. Cooling Time 5 Minutes.' +
                          account_id)
                    logging.debug(
                        '75% Rate Limit Reached. Cooling Time 5 Minutes.')
                    time.sleep(300)
                    print('Cooling finish.' + account_id)