def testFetchUrls(): import json with open('credentials.json', 'rb') as fcredentials: credentials = json.load(fcredentials) api_key = credentials['key'].encode('utf-8') api_secret = credentials['secret'].encode('utf-8') pass directories = ('google', 'citysearch', 'dexknows', 'kudzu', 'manta') # setup api wrappers api = BrightLocalAPI(key=api_key, secret=api_secret) batchapi = BrightLocalBatch(api=api) # Step 1: create a new batch batch_id = batchapi.create() assert( batch_id ) print( 'Created batch ID {}'.format(batch_id) ) # Step 2: add directory jobs to batch for directory in directories: result = api.call( method='/v4/ld/fetch-profile-url', params={ 'batch-id': batch_id, 'business-names': 'Eleven Madison Park', 'country': 'USA', 'city': 'New York', 'postcode': '10010', 'local-directory': directory } ) if ( result['success'] ): print( 'Added job with ID {}'.format(result['job-id']) ); pass pass # Step 3: Commit batch (to signal all jobs added, processing starts) success_or_failure = batchapi.commit(batch_id) if success_or_failure: print( 'Committed batch successfully.' ) pass return
def testFetchUrls(): import json with open('credentials.json', 'rb') as fcredentials: credentials = json.load(fcredentials) api_key = credentials['key'].encode('utf-8') api_secret = credentials['secret'].encode('utf-8') pass directories = ('google', 'citysearch', 'dexknows', 'kudzu', 'manta') # setup api wrappers api = BrightLocalAPI(key=api_key, secret=api_secret) batchapi = BrightLocalBatch(api=api) # Step 1: create a new batch batch_id = batchapi.create() assert( batch_id ) print( 'Created batch ID {}'.format(batch_id) ) # Step 2: add directory jobs to batch for directory in directories: result = api.call( method='/v4/ld/fetch-profile-url', params={ 'batch-id': batch_id, 'local-directory': directory, 'business-names': 'Eleven Madison Park', 'country': 'USA', 'city': 'New York', 'postcode': '10010' } ) if ( result['success'] ): print( 'Added job with ID {}'.format(result['job-id']) ); pass pass # Step 3: Commit batch (to signal all jobs added, processing starts) success_or_failure = batchapi.commit(batch_id) if success_or_failure: print( 'Committed batch successfully.' ) pass return
def testGetLocations(): count = 0 with open('credentials.json', 'rb') as creds: credentials = json.load(creds) api_key = credentials['key'].encode('utf-8') api_secret = credentials['secret'].encode('utf-8') pass fhand = open('testGetLocations.csv', 'w', newline='', encoding='utf-8') csvfhand = csv.writer(fhand) csvfhand.writerow([ 'Batch ID', 'Hospital Name', 'Hospital Address', 'Hospital City', 'Hospital State', 'Hospital Zip' ]) # Set up API Wrappers api = BrightLocalAPI(key=api_key, secret=api_secret) batchapi = BrightLocalBatch(api=api) # Step 1: create a new batch batch_id = batchapi.create() print('Created Batch ID {}'.format(batch_id)) pp = pprint.PrettyPrinter(indent=4) # Step 2: Make GET request and retrieve results with open('LocationID.csv') as fh: next(fh) for location_id in fh: print(str(location_id)) result = api.call(method='/v1/clients-and-locations/locations/' + location_id, http_method=BrightLocalAPI.HTTP_METHOD_GET) pp.pprint(result) count += 1 # if count == 1: # break return
def testGetAllReports(): with open('credentials.json', 'rb') as creds: credentials = json.load(creds) api_key = credentials['key'].encode('utf-8') api_secret = credentials['secret'].encode('utf-8') pass fhand = open('testGetAllReports.csv', 'w', newline='', encoding='utf-8') csvfhand = csv.writer(fhand) csvfhand.writerow( ['Batch ID', 'Hospital Name', 'Campaign ID', 'Location ID']) # Set up API Wrappers api = BrightLocalAPI(key=api_key, secret=api_secret) batchapi = BrightLocalBatch(api=api) # Step 1: create a new batch batch_id = batchapi.create() print('Created Batch ID {}'.format(batch_id)) csvfhand.writerow([batch_id, '', '', '']) # Step 2: add directory jobs to batch result = api.call(method='/v2/lsrc/get-all') pp = pprint.PrettyPrinter(indent=4) # pp.pprint(result['response']['results']) for item in result['response']['results']: campaign_id = item['campaign_id'] location_id = item['location_id'] hospname = item['name'] print('Hosptial Name: ', hospname) print('Campaign ID: ', campaign_id) print('Location ID: ', location_id) print('') csvfhand.writerow(['', hospname, campaign_id, location_id]) pass print('testGetAllReports.csv write with campaign information') fhand.close() return
def testBatchResults(): batch_id = 9834613 with open('credentials.json', 'rb') as fcredentials: credentials = json.load(fcredentials) api_key = credentials['key'].encode('utf-8') api_secret = credentials['secret'].encode('utf-8') pass directories = ('google', 'citysearch', 'dexknows', 'kudzu', 'manta') # setup api wrappers api = BrightLocalAPI(key=api_key, secret=api_secret) batchapi = BrightLocalBatch(api=api) # get results batchresponse = batchapi.get_results(batch_id) batchresults = batchresponse['results'] pp = pprint.PrettyPrinter(depth=4) for jobset_key, jobset in batchresults.items(): print jobset_key print for job in jobset: print 'Job', job['job-id'] print pp.pprint(job['payload']) pp.pprint(job['results']) pass pass return
def testBatchResults(): batch_id = 9834613 with open('credentials.json', 'rb') as fcredentials: credentials = json.load(fcredentials) api_key = credentials['key'].encode('utf-8') api_secret = credentials['secret'].encode('utf-8') pass directories = ('google', 'citysearch', 'dexknows', 'kudzu', 'manta') # setup api wrappers api = BrightLocalAPI(key=api_key, secret=api_secret) batchapi = BrightLocalBatch(api=api) # get results batchresponse = batchapi.get_results(batch_id) batchresults = batchresponse['results'] pp = pprint.PrettyPrinter(depth=4) for jobset_key, jobset in batchresults.items(): print jobset_key print for job in jobset: print 'Job', job['job-id'] print pp.pprint( job['payload'] ) pp.pprint( job['results'] ) pass pass return
def testBatchReportResults(): count = 0 fhand = open('KeywordRankings.csv', 'w', newline='', encoding='utf-8') csvfhand = csv.writer(fhand, delimiter=',') csvfhand.writerow( ['Hospital Name', 'Location', 'Campaign ID', 'Keyword', 'Ranking']) with open('credentials.json', 'rb') as creds: credentials = json.load(creds) api_key = credentials['key'].encode('utf-8') api_secret = credentials['secret'].encode('utf-8') pass # Set up API wrappers api = BrightLocalAPI(key=api_key, secret=api_secret) batchapi = BrightLocalBatch(api=api) # Step 1: create a new batch batch_id = batchapi.create() print('Created Batch ID {}'.format(batch_id)) pp = pprint.PrettyPrinter(indent=4) # Step 3: add campaign_id jobs to batch with open('allReports.csv') as fh: next(fh) for campaign_id in fh: print('Campaign ID: ', campaign_id) result = api.call(method='/v2/lsrc/results/get', params={'campaign-id': campaign_id}) try: campaign_id = result['response']['result']['campaign_details'][ 'campaign_id'] googloc = result['response']['result']['campaign_details'][ 'google_location'] lastprocess = result['response']['result']['campaign_details'][ 'last_processed'] name = result['response']['result']['campaign_details']['name'] zipcode = result['response']['result']['campaign_details'][ 'postcode'] # rankings = result['response']['result']['rankings'] kwrankings = result['response']['result']['rankings'][ 'keywords_num_rankings'] print(campaign_id) print(name) print(zipcode) print(googloc) print(lastprocess) csvfhand.writerow([name, googloc, campaign_id, '', '']) # print(rankings.keys()) # print(rankingNum.values()) for keyword, ranking in kwrankings.items(): print('Keyword: ', keyword, ' -- ', ranking) # print(ranking) csvfhand.writerow( [name, googloc, campaign_id, keyword, ranking]) pass count += 1 print('Total records accessed: ', count) except: pass print('campaignRankings.csv written with keyword rankings') fhand.close() return