fork(1) download
  1. import urllib.request, urllib.parse
  2. import json, ssl
  3.  
  4. # API endpoint with placeholder for your Geoapify API key
  5. serviceurl = 'https://a...content-available-to-author-only...y.com/v2/places?q={}&format=json&apiKey=YOUR_API_KEY'
  6.  
  7. # Ignore SSL certificate errors (not recommended for production)
  8. ctx = ssl.create_default_context()
  9. ctx.check_hostname = False
  10. ctx.verify_mode = ssl.CERT_NONE
  11.  
  12. while True:
  13. address = input('Enter location: ')
  14. if len(address) < 1: break
  15.  
  16. address = address.strip()
  17. parms = dict()
  18. parms['q'] = address
  19.  
  20. url = serviceurl.format(urllib.parse.urlencode(parms))
  21.  
  22. print('Retrieving', url)
  23. uh = urllib.request.urlopen(url, context=ctx)
  24. data = uh.read().decode()
  25. print('Retrieved', len(data), 'characters', data[:20].replace('\n', ' '))
  26.  
  27. try:
  28. js = json.loads(data)
  29. except json.JSONDecodeError:
  30. print('==== JSON decoding error ===')
  31. print('Failed to parse response data as JSON.')
  32. continue
  33.  
  34. if 'features' not in js or len(js['features']) == 0:
  35. print('==== Object not found or invalid response format ===')
  36. print(data[:100]) # Truncate output for brevity
  37. continue
  38.  
  39. # Extract location data with error handling
  40. location_data = js['features'][0]['properties']
  41. latitude = location_data.get('lat')
  42. longitude = location_data.get('lon')
  43. plus_code = location_data.get('plus_code')
  44. formatted_address = location_data.get('formatted')
  45.  
  46. # Check for data availability
  47. if not all([latitude, longitude, plus_code, formatted_address]):
  48. print('==== Missing data in response ===')
  49. print('Required data may be unavailable for the entered location.')
  50. continue
  51.  
  52. # Print results
  53. print('Latitude:', latitude)
  54. print('Longitude:', longitude)
  55. print('Plus code:', plus_code)
  56. print('Formatted address:', formatted_address)
  57.  
  58. # Example usage (assuming you have a function to process this data)
  59. # process_location_data(latitude, longitude, plus_code, formatted_address)
  60.  
Success #stdin #stdout 0.02s 25576KB
stdin
Standard input is empty
stdout
import urllib.request, urllib.parse
import json, ssl

# API endpoint with placeholder for your Geoapify API key
serviceurl = 'https://a...content-available-to-author-only...y.com/v2/places?q={}&format=json&apiKey=YOUR_API_KEY'

# Ignore SSL certificate errors (not recommended for production)
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

while True:
    address = input('Enter location: ')
    if len(address) < 1: break

    address = address.strip()
    parms = dict()
    parms['q'] = address

    url = serviceurl.format(urllib.parse.urlencode(parms))

    print('Retrieving', url)
    uh = urllib.request.urlopen(url, context=ctx)
    data = uh.read().decode()
    print('Retrieved', len(data), 'characters', data[:20].replace('\n', ' '))

    try:
        js = json.loads(data)
    except json.JSONDecodeError:
        print('==== JSON decoding error ===')
        print('Failed to parse response data as JSON.')
        continue

    if 'features' not in js or len(js['features']) == 0:
        print('==== Object not found or invalid response format ===')
        print(data[:100])  # Truncate output for brevity
        continue

    # Extract location data with error handling
    location_data = js['features'][0]['properties']
    latitude = location_data.get('lat')
    longitude = location_data.get('lon')
    plus_code = location_data.get('plus_code')
    formatted_address = location_data.get('formatted')

    # Check for data availability
    if not all([latitude, longitude, plus_code, formatted_address]):
        print('==== Missing data in response ===')
        print('Required data may be unavailable for the entered location.')
        continue

    # Print results
    print('Latitude:', latitude)
    print('Longitude:', longitude)
    print('Plus code:', plus_code)
    print('Formatted address:', formatted_address)

    # Example usage (assuming you have a function to process this data)
    # process_location_data(latitude, longitude, plus_code, formatted_address)