def ensure_data_directory(relative_path: Path = None) -> Path: """Checks if a directory in the data dir path exists. Creates it if necessary Args: relative_path: A Path object pointing to a file relative to the data directory Returns: The absolute path Path object """ if relative_path is None: return Path(config.data_dir()) try: current_path = Path(config.data_dir(), relative_path) # if path points to a file, create parent directory instead if current_path.suffix: if not current_path.parent.exists(): current_path.parent.mkdir(exist_ok=True, parents=True) else: if not current_path.exists(): current_path.mkdir(exist_ok=True, parents=True) return current_path except OSError as exception: if exception.errno != errno.EEXIST: raise
def submit_and_download(report_request, api_client, data_dir, data_file, overwrite_if_exists, decompress: bool = False): """ Submit the download request and then use the ReportingDownloadOperation result to track status until the report is complete. Id the file already exists, do nothing Args: report_request: report_request object e.g. created by get_ad_performance api_client: BingApiClient object data_dir: target directory of the files containing the reports data_file: the name of the file containing the data overwrite_if_exists: if True, overwrite the file decompress: whether to decompress zip files Returns: result_file_path: the location of the result file """ target_file = data_dir + '/' + data_file if os.path.exists(target_file) and not overwrite_if_exists: print('The file {} already exists, skipping it'.format(target_file)) return current_reporting_service_manager = \ ReportingServiceManager( authorization_data=api_client.authorization_data, poll_interval_in_milliseconds=5000, environment='production', working_directory=config.data_dir(), ) reporting_download_operation = current_reporting_service_manager.submit_download(report_request) # You may optionally cancel the track() operation after a specified time interval. current_reporting_operation_status = reporting_download_operation.track( timeout_in_milliseconds=config.timeout()) # You can use ReportingDownloadOperation.track() to poll until complete as shown above, # or use custom polling logic with get_status() as shown below. for i in range(10): time.sleep(current_reporting_service_manager.poll_interval_in_milliseconds / 1000.0) download_status = reporting_download_operation.get_status() if download_status.status == 'Success': break print("Awaiting Download Results . . .") result_file_path = reporting_download_operation.download_result_file( result_file_directory=data_dir, result_file_name=data_file, decompress=decompress, overwrite=True, # Set this value true if you want to overwrite the same file. timeout_in_milliseconds=config.timeout() ) print("Download result file: {}".format(result_file_path)) print("Status: {}\n".format(current_reporting_operation_status.status)) return result_file_path
def submit_and_download(report_request, api_client, data_dir): """ Submit the download request and then use the ReportingDownloadOperation result to track status until the report is complete. Args: report_request: report_request object e.g. created by get_ad_performance_for_single_day api_client: BingApiClient object data_dir: target directory of the files containing the reports """ current_reporting_service_manager = \ ReportingServiceManager( authorization_data=api_client.authorization_data, poll_interval_in_milliseconds=5000, environment='production', working_directory=config.data_dir(), ) reporting_download_operation = current_reporting_service_manager.submit_download(report_request) # You may optionally cancel the track() operation after a specified time interval. current_reporting_operation_status = reporting_download_operation.track( timeout_in_milliseconds=config.timeout()) # You can use ReportingDownloadOperation.track() to poll until complete as shown above, # or use custom polling logic with get_status() as shown below. for i in range(10): time.sleep(current_reporting_service_manager.poll_interval_in_milliseconds / 1000.0) download_status = reporting_download_operation.get_status() if download_status.status == 'Success': break print("Awaiting Download Results . . .") result_file_path = reporting_download_operation.download_result_file( result_file_directory=data_dir, result_file_name=config.data_file(), decompress=False, overwrite=True, # Set this value true if you want to overwrite the same file. timeout_in_milliseconds=config.timeout() ) print("Download result file: {}".format(result_file_path)) print("Status: {}\n".format(current_reporting_operation_status.status))