Esempio n. 1
0
def set_report_time(api_client: BingReportClient,
                    current_date: datetime = None, all_time: bool = False):
    """
    Sets the report time for the BingAds API Client
    Args:
        api_client: BingApiClient object
        current_date: date for which the report object will be created
        all_time: include all days from the import start date
    Returns:
        A report time object with a specific date
    """

    report_time = api_client.factory.create('ReportTime')

    custom_date_range_end = api_client.factory.create('Date')

    if current_date is None:
        current_date = datetime.datetime.now()  # for example for downloading current campaign structure
    custom_date_range_end.Day = current_date.day
    custom_date_range_end.Month = current_date.month
    custom_date_range_end.Year = current_date.year

    report_time.CustomDateRangeEnd = custom_date_range_end
    if not all_time:
        report_time.CustomDateRangeStart = custom_date_range_end
    else:
        first_date = datetime.datetime.strptime(config.first_date(), '%Y-%m-%d')
        custom_date_range_start = api_client.factory.create('Date')
        custom_date_range_start.Day = first_date.day
        custom_date_range_start.Month = first_date.month
        custom_date_range_start.Year = first_date.year
        report_time.CustomDateRangeStart = custom_date_range_start
    report_time.PredefinedTime = None

    return report_time
Esempio n. 2
0
def download_ad_performance_data(api_client: BingReportClient):
    """
    Downloads BingAds performance reports by creating report objects
    for every day since config.first_date() till today
        Args:
         api_client: BingAdsApiClient
    """
    first_date = datetime.datetime.strptime(config.first_date(), '%Y-%m-%d')
    last_date = datetime.datetime.now() - datetime.timedelta(days=1)
    current_date = last_date
    remaining_attempts = config.total_attempts_for_single_file
    while current_date >= first_date:
        print(current_date)
        relative_filepath = Path('{date:%Y/%m/%d}/bing/'.format(
            date=current_date))
        filepath = ensure_data_directory(relative_filepath)

        if not filepath.is_dir() or (last_date - current_date).days < 31:
            report_request = build_ad_performance_request_for_single_day(api_client, current_date)
            with tempfile.TemporaryDirectory() as tmp_dir:
                tmp_filepath = Path(tmp_dir, relative_filepath)
                tmp_filepath.parent.mkdir(exist_ok=True, parents=True)
                try:
                    start_time = time.time()
                    submit_and_download(report_request, api_client, str(filepath))
                    print('Successfully downloaded data for {date:%Y-%m-%d} in {elapsed:.1f} seconds'
                          .format(date=current_date, elapsed=time.time() - start_time))
                    # date is decreased only if the download above does not fail
                    current_date -= datetime.timedelta(days=1)
                    remaining_attempts = config.total_attempts_for_single_file
                except urllib.error.URLError as url_error:
                    if remaining_attempts == 0:
                        print('Too many failed attempts while downloading this day, quitting', file=sys.stderr)
                        raise
                    print('ERROR WHILE DOWNLOADING REPORT, RETRYING in {} seconds, attempt {}#...'
                          .format(config.retry_timeout_interval, remaining_attempts), file=sys.stderr)
                    print(url_error, file=sys.stderr)
                    time.sleep(config.retry_timeout_interval)
                    remaining_attempts -= 1
        else:
            current_date -= datetime.timedelta(days=1)
def download_performance_data(api_client: BingReportClient):
    """
    Downloads BingAds Ads performance reports by creating report objects
    for every day since config.first_date() till today
        Args:
         api_client: BingAdsApiClient
    """
    first_date = datetime.datetime.strptime(config.first_date(), '%Y-%m-%d')
    last_date = datetime.datetime.now() - datetime.timedelta(days=1)
    current_date = last_date
    remaining_attempts = config.total_attempts_for_single_day
    while current_date >= first_date:
        print(current_date)
        relative_filepath = Path('{date:%Y/%m/%d}/bing/'.format(
            date=current_date))
        filepath = ensure_data_directory(relative_filepath)

        overwrite_if_exists = (last_date - current_date).days < 31
        if overwrite_if_exists:
            print('The data for {date:%Y-%m-%d} will be downloaded. Already present files will be overwritten'.format(
                date=current_date))
        report_request_ad = build_ad_performance_request(api_client, current_date)
        report_request_keyword = build_keyword_performance_request(api_client, current_date)
        report_request_campaign = build_campaign_performance_request(api_client, current_date)

        with tempfile.TemporaryDirectory() as tmp_dir:
            tmp_filepath = Path(tmp_dir, relative_filepath)
            tmp_filepath.parent.mkdir(exist_ok=True, parents=True)
            try:
                start_time = time.time()
                print('About to download ad data for {date:%Y-%m-%d}'
                      .format(date=current_date))
                submit_and_download(report_request_ad, api_client, str(filepath),
                                    'ad_performance_{}.csv.gz'.format(config.output_file_version()),
                                    overwrite_if_exists)
                print('Successfully downloaded ad data for {date:%Y-%m-%d} in {elapsed:.1f} seconds'
                      .format(date=current_date, elapsed=time.time() - start_time))
                start_time = time.time()
                print('About to download keyword data for {date:%Y-%m-%d}'
                      .format(date=current_date))
                submit_and_download(report_request_keyword, api_client, str(filepath),
                                    'keyword_performance_{}.csv.gz'.format(config.output_file_version()),
                                    overwrite_if_exists)
                print('Successfully downloaded keyword data for {date:%Y-%m-%d} in {elapsed:.1f} seconds'
                      .format(date=current_date, elapsed=time.time() - start_time))
                print('About to download campaign data for {date:%Y-%m-%d}'
                      .format(date=current_date))
                submit_and_download(report_request_campaign, api_client, str(filepath),
                                    'campaign_performance_{}.csv.gz'.format(config.output_file_version()),
                                    overwrite_if_exists)
                print('Successfully downloaded campaign data for {date:%Y-%m-%d} in {elapsed:.1f} seconds'
                      .format(date=current_date, elapsed=time.time() - start_time))
                # date is decreased only if the download above does not fail
                current_date -= datetime.timedelta(days=1)
                remaining_attempts = config.total_attempts_for_single_day
            except urllib.error.URLError as url_error:
                if remaining_attempts == 0:
                    print('Too many failed attempts while downloading this day, quitting', file=sys.stderr)
                    raise
                print('ERROR WHILE DOWNLOADING REPORT, RETRYING in {} seconds, attempt {}#...'
                      .format(config.retry_timeout_interval, remaining_attempts), file=sys.stderr)
                print(url_error, file=sys.stderr)
                time.sleep(config.retry_timeout_interval)
                remaining_attempts -= 1