Skip to content

Instantly share code, notes, and snippets.

@benmatselby
Created April 12, 2024 19:51
Show Gist options
  • Save benmatselby/c739f2091b66243bf376e11e8925553c to your computer and use it in GitHub Desktop.
Save benmatselby/c739f2091b66243bf376e11e8925553c to your computer and use it in GitHub Desktop.
Geolocation investigation
# Script to get locations based on lat/long or IP address
#
# Installation:
# pip install geopy
# pip install ip2geotools
from geopy.geocoders import Nominatim
from ip2geotools.databases.noncommercial import DbIpCity
# ------------------------------
# Geo
# ------------------------------
geolocator = Nominatim(user_agent="andrew")
# An address
address = "175 5th Avenue NYC"
location = geolocator.geocode(address)
print(f"Address: {address}")
print(location.address)
print((location.latitude, location.longitude))
print("")
# Coordinates
geo_location = geolocator.reverse("51.503368, -0.127721")
print("Coordinates: 51.503368, -0.127721")
print(geo_location.address)
print("")
# ------------------------------
# IP
# ------------------------------
response = DbIpCity.get("144.193.202.86", api_key="free")
print(response)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment