Skip to content

Commit 7d44ec4

Browse files
committed
batch.py: better error when missing API key or empty lines in input
1 parent 9d39c98 commit 7d44ec4

4 files changed

Lines changed: 18 additions & 257 deletions

File tree

examples/batch.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# Requires Python 3.7 or newer. Tested with 3.8 and 3.9.
77

88
# Installation:
9-
# pip3 install opencage asyncio aiohttp backoff tqdm
9+
# pip3 install --upgrade opencage asyncio aiohttp backoff tqdm
1010

1111
import sys, random, time
1212
import csv
@@ -88,7 +88,11 @@ async def geocode_one_address(address, address_id):
8888
# note: you may also want to set other optional parameters like
8989
# countrycode, language, etc
9090
# see the full list: https://opencagedata.com/api#forward-opt
91-
geocoding_results = await geocoder.geocode_async(address, no_annotations=1)
91+
try:
92+
geocoding_results = await geocoder.geocode_async(address, no_annotations=1)
93+
except Exception as e:
94+
geocoding_results = None
95+
traceback.print_exception(e, file=sys.stderr)
9296

9397
# coordinates -> address, e.g. '40.78,-73.97' => 101, West 91st Street, New York
9498
# lon_lat = address.split(',')
@@ -134,9 +138,12 @@ async def main():
134138
##
135139
queue = asyncio.Queue(maxsize=max_items)
136140

137-
csv_reader = csv.reader(open(infile, 'r'))
141+
csv_reader = csv.reader(open(infile, 'r'), strict=True, skipinitialspace=True)
138142

139143
for row in csv_reader:
144+
if len(row) == 0:
145+
raise Exception("Empty line in input file at line number %d, aborting" % csv_reader.line_num)
146+
140147
work_item = {'id': row[0], 'address': row[1]}
141148
await queue.put(work_item)
142149
if queue.full():

0 commit comments

Comments
 (0)