BingAds / BingAds-Python-SDK

Other
115 stars 162 forks source link

Report not downloading to specified location in Azure One Lake #281

Open sunilchowdary9 opened 2 months ago

sunilchowdary9 commented 2 months ago

Hi @qitia

I am trying to download a report to Microsoft One Lake using the Bing Ads API through the Python SDK in Microsoft Fabric Notebooks, but the report is not downloading to the specified path in One Lake. Although the script is executing without issues and returning the count of records, no file is downloaded.

Here's the code I used:

`REPORT_FILE_FORMAT='Csv'

The directory for the report files.

result_file_directory_path='abfss://{workspace_name}/{path in one lake}'

The name of the report download file.

RESULT_FILE_NAME='KPR' + REPORT_FILE_FORMAT.lower()

The maximum amount of time (in milliseconds) that you want to wait for the report download.

TIMEOUT_IN_MILLISECONDS=3600000 def get_keyword_report(account_id): try: reporting_service = ServiceClient( service='ReportingService', version=13, authorization_data=authorization_data, environment='production', ) exclude_column_headers = False exclude_report_footer = False exclude_report_header = False time = reporting_service.factory.create('ReportTime') time.PredefinedTime='Yesterday' time.CustomDateRangeStart = None time.CustomDateRangeEnd = None return_only_complete_data=False time.ReportTimeZone = 'PacificTimeUSCanadaTijuana' report_request = reporting_service.factory.create('KeywordPerformanceReportRequest') report_request.Aggregation = 'Daily' report_request.ExcludeColumnHeaders = exclude_column_headers report_request.ExcludeReportFooter = exclude_report_footer report_request.ExcludeReportHeader = exclude_report_header report_request.Format = REPORT_FILE_FORMAT report_request.ReturnOnlyCompleteData = False report_request.Time = reporting_service.factory.create('ReportTime') report_request.Time.PredefinedTime='Yesterday'
report_request.Time.CustomDateRangeStart = None report_request.Time.CustomDateRangeEnd = None report_request.ReportName = "Keyword Performance Report" scope = reporting_service.factory.create('AccountThroughAdGroupReportScope') scope.AccountIds = {'long': [account_id] } scope.Campaigns = None report_request.Scope = scope
report_filter=reporting_service.factory.create('KeywordPerformanceReportFilter') report_request.Filter=report_filter report_columns = reporting_service.factory.create('ArrayOfKeywordPerformanceReportColumn') report_columns.KeywordPerformanceReportColumn.append([

        "AdGroupId",
        "CampaignId",
        "Clicks",
        "Conversions",
        "DeviceType",
        "Impressions",
        "Keyword",
        "KeywordId",
        "KeywordLabels",
        "KeywordStatus",
        "Network",
        "QualityScore",
        "Spend",
        "TimePeriod"
        ])
    report_request.Columns = report_columns

    return report_request
except Exception as e:
    print("\nMS_ADS_keyword_REPORT : report processing Failed : ", sys.exc_info())

    print(f'An error occurred in get_ads_report: {e}')

def download_keyword_report(report_request): try:

Initialize ReportingServiceManager with authorization_data

    reporting_service_manager=ReportingServiceManager(
        authorization_data=authorization_data, 
        poll_interval_in_milliseconds=5000, 
        environment='production',
    )
    print("*****************Checking If Path Exists*****************")
    if not os.path.exists(result_file_directory_path):
        os.makedirs(result_file_directory_path)
    else:
        print("Directory exists")

    print("result file directory:",os.path.exists(result_file_directory_path))

Download the report

    reporting_download_parameters = ReportingDownloadParameters(
            report_request=report_request,
            result_file_directory = result_file_directory_path,
            result_file_name = RESULT_FILE_NAME, 
            overwrite_result_file = True, # value true if you want to overwrite the same file.
            timeout_in_milliseconds=3600000 # cancel the download after a specified time interval.
        )

    print(f'Attempting to donload the report to {result_file_directory_path}{RESULT_FILE_NAME}')
    report_container=reporting_service_manager.download_report(reporting_download_parameters)
    print(f"Report downloaded")

    print("records retrieved:",report_container.record_count)

except Exception as e:
    print(f'An error occured in downloading keyword report:{e}')
    raise e

if name == 'main': authorization_data = auth_bing_ads(refresh_token,client_id,client_secret,developer_token) report_request = get_keyword_report(account_id) reporting_service_manager=ReportingServiceManager( authorization_data=authorization_data, poll_interval_in_milliseconds=5000, environment='production', )

        download_keyword_report(report_request)`