geocode.py (810B)
1 #!/usr/bin/env python3 2 3 # read human-readable locations on stdin, produce same list with added GPS 4 # coordinates on STDOUT 5 6 from geopy.geocoders import GeoNames 7 import os 8 import sys 9 from time import sleep 10 11 USER="a3nm" # user on geonames.org, serves as API key 12 geolocator = GeoNames(username=USER) 13 14 def searchGeonames(place): 15 # chatgpt 16 global geolocator 17 location = geolocator.geocode(place, exactly_one=True) 18 if (location == None): 19 print("ERROR: %s" % place, file=sys.stderr) 20 return None, None 21 else: 22 return (location.latitude, location.longitude) 23 24 for l in sys.stdin.readlines(): 25 l = l.strip() 26 origin_lat, origin_lng = searchGeonames(l) 27 if origin_lat is not None and origin_lng is not None: 28 print(origin_lat, origin_lng, l) 29 30 sleep(1) 31