Beispiel #1
0
def get_accounts(result_queue, b_id, app_id):
    try:
        retry_count = 0
        while True:
            try:
                logger.debug('b_id:{} begin'.format(b_id))
                n = 0
                facebook_app = facebook_apps.get(app_id)
                api = FacebookAdsApi.init(facebook_app.app_id,
                                          facebook_app.app_secret,
                                          facebook_app.access_token)
                Business(fbid='me', api=api)
                at = Business(b_id).get_client_ad_accounts(
                    params={'limit': 1000})
                flag = True
                while flag:
                    for account in at:
                        b_a = {'BusinessId': b_id}
                        a_id = account['account_id']
                        b_a['AccountId'] = str(a_id)
                        result_queue.put(b_a)
                        n += 1
                    flag = at.load_next_page()
                logger.debug('b_id:{} len at:{}'.format(b_id, n))
                return
            except ConnectionError:
                if retry_count < MAX_RETRIES:
                    time.sleep(SLEEP_TIME)
                    continue
                else:
                    raise
    except Exception, ex:
        logger.error('get_accounts error,ex:{}'.format(ex))
Beispiel #2
0
def get_campaign_app(result_queue, campaign_info, app_id):
    try:
        logger.debug('campaign:{} get app'.format(
            campaign_info.get('CampaignId')))
        facebook_app = facebook_apps.get(app_id)
        api = FacebookAdsApi.init(facebook_app.app_id, facebook_app.app_secret,
                                  facebook_app.access_token)
        Campaign(api=api)
        promoted_object = Campaign(
            campaign_info.get('CampaignId')).get_ad_sets(
                fields=['promoted_object'], params={'limit': 1000})
        campaign_info['FBAppId'] = np.nan
        campaign_info['AppStoreUrl'] = np.nan
        campaign_info['CampaignName'] = np.nan
        for po in promoted_object:
            # try:
            if po.get('promoted_object') and po.get('promoted_object').get(
                    'object_store_url') and po.get('promoted_object').get(
                        'application_id'):
                campaign_info['FBAppId'] = str(
                    po['promoted_object']['application_id'])
                campaign_info['AppStoreUrl'] = po['promoted_object'][
                    'object_store_url']
                break
            # except:
            #     continue
        campaign_name = Campaign(campaign_info.get('CampaignId')).api_get(
            fields=[Campaign.Field.name], params={'limit': 1000})
        if campaign_name.get('name'):
            campaign_info['CampaignName'] = campaign_name['name']
        result_queue.put(campaign_info)
    except ConnectionError:
        raise
    except Exception, ex:
        logger.error('get_campaign_app error, ex:{}'.format(ex))
Beispiel #3
0
def start_insights_task(task_queue, e, datas, task_type):
    logger.info(
        '******************************  start_insights_task begin  ******************************'
    )
    report_define = report_defines.get(task_type)
    for app, business_account in datas.iteritems():
        facebook_app = facebook_apps.get(app)
        api = FacebookAdsApi.init(facebook_app.app_id, facebook_app.app_secret,
                                  facebook_app.access_token)
        AdAccount(api=api)
        retry_count = 0
        for b_a in business_account:
            while retry_count < MAX_RETRIES:
                try:
                    b_a['TokenId'] = app
                    i_async_job = AdAccount(
                        ACT + b_a.get('AccountId')).get_insights(
                            is_async=True,
                            params=report_define.get('params'),
                            fields=report_define.get('fields'))
                    task_queue.put((b_a, task_type, i_async_job))
                    time.sleep(0.5)
                    break
                except Exception, ex:
                    retry_count += 1
                    logger.error('[{}\{}] AccountId\Retry get error:{}'.format(
                        b_a.get('AccountId'), retry_count, ex))
                    time.sleep(retry_count * BACKOFF_FACTOR)
            else:
                logger.error('*****AccountId:{} error*****'.format(
                    b_a.get('AccountId')))
Beispiel #4
0
def get_account_campaign(result_queue, business_account, app_id):
    try:
        facebook_app = facebook_apps.get(app_id)
        api = FacebookAdsApi.init(facebook_app.app_id, facebook_app.app_secret,
                                  facebook_app.access_token)
        AdAccount(api=api)
        cp = AdAccount(ACT + business_account.get('AccountId')).get_campaigns(
            fields=[Campaign.Field.name], params={'limit': 1000})
        flag = True
        n = 0
        while flag:
            for campaign in cp:
                business_account['CampaignId'] = campaign['id']
                business_account['CampaignName'] = campaign['name']
                result_queue.put(business_account)
                n += 1
            flag = cp.load_next_page()
        logger.debug('a_id:{} len cp:{}'.format(
            business_account.get('AccountId'), n))
    except ConnectionError:
        raise
    except Exception, ex:
        logger.error('get_account_campaign error:{}'.format(ex))
Beispiel #5
0
def get_business_account_id(app_id):
    try:
        save_account_id = set()
        facebook_app = facebook_apps.get(app_id)
        api = FacebookAdsApi.init(facebook_app.app_id, facebook_app.app_secret,
                                  facebook_app.access_token)
        manager = multiprocessing.Manager()
        bs = get_business_users_all(api)
        # bs = bs[:1]
        logger.debug('app_id:{} get bs len:{}'.format(app_id, len(bs)))
        if not bs:
            return []
        result_queue = manager.Queue()
        len_pool = min(len(bs), MAX_PROCESSES)
        pool = multiprocessing.Pool(processes=len_pool)
        for b_id in bs:
            pool.apply_async(get_accounts, (result_queue, b_id, app_id))
        pool.close()
        pool.join()
        results = list()
        while True:
            try:
                result = result_queue.get(timeout=0.01)
                if result.get('AccountId') in save_account_id:
                    logger.debug('AccountId:{} have save'.format(
                        result.get('AccountId')))
                    continue
                else:
                    results.append(result)
                    save_account_id.add(result.get('AccountId'))
            except Empty:
                break
        logger.info('app:{} get business_account_id len:{}'.format(
            app_id, len(results)))
        return results
    except Exception:
        raise