Bing ads API using python is tremendous slow

Sagar Sarkar 1 Reputation point
2021-03-16T11:12:54.12+00:00

Hi, I am using python sdk(https://pypi.org/project/bingads/) for fetching campaigns, adgroups and keywords. But it is tremendous slow. on the other hand google bing api is so fast. Specially Bing keywords process taking too much time. i am using GetKeywordsByAdGroupId method to fetch all keywords for each adgroup id. i am also fetching all keyword labels by calling another python function as below:
def get_labels(campaign_service, keywordId):
keyword_associated_lables = campaign_service.GetLabelAssociationsByEntityIds(
EntityIds={'long': [keywordId]}, EntityType="Keyword")

associated_label_ids = []
associated_label_names = []

for associated_label in keyword_associated_lables.LabelAssociations:
    for label in associated_label[1]:
        associated_label_ids.append(label.LabelId)

get_labels_by_ids_response = campaign_service.GetLabelsByIds(
    LabelIds={'long': associated_label_ids}
)

for data_object in get_labels_by_ids_response.Labels['Label']:
    associated_label_names.append(data_object.Name)

return ','.join(associated_label_names)

Now for fetching 12633 keywords and their lables total time taken 4hours 28 min. on the other hand using google ads api(python) fetching google keywords with labels take only 35 min. Even from console i can see the difference of fetching records. is there any way to optimise the process?
Thanks in advance
Sagar

Bing Web Search
Bing Web Search
A Bing service that gives you enhanced search details from billions of web documents.
147 questions
Bing Custom Search
Bing Custom Search
An easy-to-use, ad-free, commercial-grade search tool that lets you deliver the results you want.
83 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Sagar Sarkar 1 Reputation point
    2021-03-17T09:48:47.523+00:00

    Hello YotonTie, this is script i am using for fetching keywords with labels.
    Thank for looking into my problem.

    authentication process code which i found in bing python example.

    from process.scripts.bing_auth_helper import *
    from bingads.authorization import *

    my Models

    from remote.models import AdvertisingAccounts, CampaignsDetails, AdGroupDetails, KeywordDetails
    import environ
    from django.core.exceptions import FieldError, ObjectDoesNotExist
    from suds import WebFault
    from process.log_message import *
    import logging

    my Models

    from process.models import Log, Process
    import sys
    from django.db import connection, OperationalError

    env = environ.Env()

    reading .env file

    environ.Env.read_env()

    def get_labels(campaign_service, keywordId):
    keyword_associated_lables = campaign_service.GetLabelAssociationsByEntityIds(
    EntityIds={'long': [keywordId]}, EntityType="Keyword")

    associated_label_ids = []
    associated_label_names = []
    
    for associated_label in keyword_associated_lables.LabelAssociations:
        for label in associated_label[1]:
            associated_label_ids.append(label.LabelId)
    
    get_labels_by_ids_response = campaign_service.GetLabelsByIds(
        LabelIds={'long': associated_label_ids}
    )
    
    for data_object in get_labels_by_ids_response.Labels['Label']:
        associated_label_names.append(data_object.Name)
    
    return ','.join(associated_label_names)
    

    def get_keywords(adgroup, log_id):
    campaign = CampaignsDetails.objects.filter(
    id=adgroup.Campaign_id).select_related('Account').using('remote_staging')
    account_id = campaign[0].Account.account_id
    # print(campaign.query)
    # print("account_id=%s" % (account_id))
    # print("adgroup ID=%s" % (adgroup.Adgroup_Id))
    created = 0
    updated = 0
    total = 0
    # return_additional_fields = 'AdGroupType'
    authorization_data = AuthorizationData(
    account_id=account_id,
    customer_id=None,
    developer_token=env('DEVELOPER_TOKEN'),
    authentication=None,
    )
    campaign_service = ServiceClient(
    service='CampaignManagementService',
    version=13,
    authorization_data=authorization_data,
    environment=env('ENVIRONMENT'),
    )
    try:
    authenticate(authorization_data)
    keywords = campaign_service.GetKeywordsByAdGroupId(
    AdGroupId=adgroup.Adgroup_Id)
    if keywords != "":
    for keyword in keywords.Keyword:
    total = total+1
    keyword_id = keyword.Id
    keyword_name = keyword.Text
    keyword_status = keyword.Status
    keyword_matchtype = keyword.MatchType
    lbl_names = get_labels(campaign_service, keyword_id)
    print("%s Keyword found for Keyword ID %s" %
    (keyword_name, keyword_id))
    print("%s Keyword found" % (total))
    print("Keyword id=%s Name=%s Status=%s Match Type=%s labels=%s" %
    (keyword.Id, keyword.Text, keyword.Status, keyword.MatchType, lbl_names))
    try:
    keyword_details = KeywordDetails.objects.filter(
    Adgroup_id=adgroup.id, Keyword_Id=keyword_id
    ).using('remote_staging')
    except FieldError as e:
    saveLog(log_id=log_id, status=Log.Statuses.FAILURE,
    log_message=e)
    sys.exit()
    if keyword_details.count() != 0:
    if(keyword_details[0].Name != keyword_name or keyword_details[0].Match_Type != keyword_matchtype or keyword_details[0].Status != keyword_status):
    print("updating Bing Keywords %s" % (keyword_name))
    keyword_details.update(
    Name=keyword_name,
    Match_Type=keyword_matchtype,
    Status=keyword_status,
    Labels=lbl_names,
    )
    updated = updated + 1
    else:
    # insert
    print('inserting Bing Keywords' + keyword_name)
    try:
    newKeyword = KeywordDetails(
    Keyword_Id=keyword_id,
    Name=keyword_name,
    Match_Type=keyword_matchtype,
    Status=keyword_status,
    Labels=lbl_names,
    Adgroup_id=adgroup.id,
    )
    newKeyword.save(using='remote_staging')
    created = created + 1
    except OperationalError:
    print(connection.queries[-1])
    except Exception as e:
    template = "An exception of type {0} occurred. Arguments:\n{1!r}"
    print(template.format(type(e).name, e.args))
    else:
    print("No Keywords found for Adgroup ID %s" % (adgroup.Adgroup_Id))
    return [created, total, updated]
    except WebFault as ex:
    print(ex)
    except Exception as ex:
    print(ex)

    def run_keyword_task(log_id=0):

    created = 0
    updated = 0
    total = 0
    message = []
    logProcessStarted(log_id)
    
    adgroups = AdGroupDetails.objects.filter(
        Campaign__Account__source="bing_ads_search", Campaign__Account__isActive=1).distinct().using('remote_staging')
    \# print(adgroups.query)
    
    for adgroup in adgroups:
        \# print(adgroup.Adgroup_Id)
        \# print(adgroup.Campaign_id)
        try:
            print("Fetching  Bing Keywords  for Adgroup ID=%s" %
                  (adgroup.Adgroup_Id))
            stats = get_keywords(adgroup, log_id)
            if stats != False:
                created += stats[0]
                total += stats[1]
                updated += stats[2]
            logProcessStats(log_id, created, total, updated)
        except ValueError:
            message.append("ValueError in Account '%s' " %
                           (adgroup.Adgroup_Id))
            saveLog(log_id=log_id, status=Log.Statuses.RUNNING,
                    log_message="ValueError in Account '%s' " % (adgroup.Adgroup_Id))
        except KeyboardInterrupt:
            saveLog(log_id=log_id, status=Log.Statuses.FAILURE,
                    log_message="Forcefully Stopped")
            sys.exit()
        except Exception as e:
            template = "An exception of type {0} occurred. Arguments:\n{1!r}"
            msg = template.format(type(e).__name__, e.args)
            log_message = msg + \
                "Bing Keywords:Error in Adgroup ID '%s' " % (
                    adgroup.Adgroup_Id)
            message.append(log_message)
            pass
    
    if len(message) == 0:
        logProcessFinished(log_id)
    elif len(message) > 0:
        message.append("Success with some errors")
        log_msg = ','.join(message)[0:244]
        saveLog(log_id=log_id, status=Log.Statuses.FAILURE,
                log_message=log_msg)
    print("Bing Keywords Process Finished")
    
    0 comments No comments