geocode.py (630B)
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 return (location.latitude, location.longitude) 19 20 for l in sys.stdin.readlines(): 21 l = l.strip() 22 origin_lat, origin_lng = searchGeonames(l) 23 24 print(origin_lat, origin_lng, l) 25 26 sleep(1) 27