def sdf_download(auth, version, partner_id, file_types, filter_type, filter_ids_obj): #Read Filter Ids filter_ids = list(get_rows(auth, filter_ids_obj)) body = { "version": version, "partnerId": partner_id, "parentEntityFilter": { "fileType": file_types, "filterType": filter_type, "filterIds": filter_ids }, "idFilter": None } operation = API_DV360_Beta(auth).sdfdownloadtasks().create(body=body).execute() if operation and 'name' in operation: request = API_DV360_Beta(auth).sdfdownloadtasks().operations().get(name=operation['name']) # This is the eng recommended way of getting the operation while True: response = request.execute() if 'done' in response and response['done']: break time.sleep(30) else: print('error') if 'error' in response: raise Exception(response['error']['message']) return download_media('user', response['response']['resourceName'])
def main(): parser = argparse.ArgumentParser( formatter_class=argparse.RawDescriptionHelpFormatter, description=textwrap.dedent("""\ Command line utility to download and print line items from the DV360 Beta API. Example: python helper.py --advertiser 3721733 --user user.json python helper.py --advertiser 3721733 --service service.json """)) # lineitem list requires an advertiser id parser.add_argument('--advertiser', help='Advertiser ID to pull line items from.') # initialize project project.from_commandline(parser=parser, arguments=('-u', '-c', '-s', '-v')) # determine auth based on parameters auth = 'service' if project.args.service else 'user' # pull the line items lineitems = API_DV360_Beta( auth, iterate=True).advertisers().lineItems().list( advertiserId=project.args.advertiser).execute() # print line items for lineitem in lineitems: print(json.dumps(lineitem, indent=2, sort_keys=True))
def dv360_api_patch(endpoint, row): kwargs = { 'advertiserId': str(row['advertiserId']), 'updateMask': 'displayName', 'body': row } print( API_DV360_Beta( project.task['auth']).call(endpoint).patch(**kwargs).execute())
def lineitem_get_v1(auth, advertiser_id, lineitem_id): """Gets a DV360 Line Item Args: auth: StarThinker authentication scheme advertiser_id: ID of the advertiser of the line item lineitem_id: ID of the line item Returns: Line Item from the DV360 API """ return API_DV360_Beta(auth).advertisers().lineItems().get(advertiserId = advertiser_id, lineItemId = lineitem_id).execute()
def lineitem_patch_v1(auth, patch, li): """Patches a DV360 Line Item Args: auth: StarThinker authentication scheme patch: List of field names to patch li: Line item with updates to push Returns: Updated Line Item """ return API_DV360_Beta(auth).advertisers().lineItems().patch(advertiserId = li['advertiserId'], lineItemId = li['lineItemId'], updateMask=patch, body=li).execute()
def dv360_api_list(endpoint): if 'partners' in project.task: partners = set(get_rows('user', project.task['partners'])) for partner in partners: kwargs = {'partnerId': partner} yield from API_DV360_Beta( project.task['auth'], iterate=True).call(endpoint).list(**kwargs).execute() if 'advertisers' in project.task: advertisers = set(get_rows('user', project.task['advertisers'])) for advertiser in advertisers: kwargs = {'advertiserId': str(advertiser)} yield from API_DV360_Beta( project.task['auth'], iterate=True).call(endpoint).list(**kwargs).execute()
def download_media(auth, resource_name): if project.verbose: print('SDF: Start Download'); downloadRequest = API_DV360_Beta(auth).media().download_media(resourceName=resource_name).execute(run=False) # Create output stream for downloaded file outStream = io.BytesIO() # Make downloader object downloader = MediaIoBaseDownload(outStream, downloadRequest) # Download media file in chunks until finished download_finished = False while download_finished is False: _, download_finished = downloader.next_chunk() if project.verbose: print('SDF: End Download'); return outStream
from starthinker.util.google_api import API_DV360_Beta """Command line to get a DV360 Line Item lists. This is a helper demonstrate the use of the new DV360 API. `python helper.py --advertiser # -u $STARTHINKER_USER -c $STARTHINKER_CLIENT` `python helper.py --advertiser 3721733 --user $STARTHINKER_USER` Prerequisite: https://github.com/google/starthinker/blob/master/tutorials/deploy_developer.md#command-line-deploy """ if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument('--advertiser', help='advertiser ID to pull', default=None) # initialize project project.from_commandline(parser=parser) auth = 'service' if project.args.service else 'user' # pull the line items lineitems = API_DV360_Beta( auth, iterate=True).advertisers().lineItems().list( advertiserId=project.args.advertiser).execute() for lineitem in lineitems: print(json.dumps(lineitem, indent=2, sort_keys=True))
def dv360_api_insert(endpoint, row): kwargs = {'advertiserId': str(row['advertiserId']), 'body': row} print( API_DV360_Beta( project.task['auth']).call(endpoint).create(**kwargs).execute())