Created
April 12, 2024 19:51
-
-
Save benmatselby/c739f2091b66243bf376e11e8925553c to your computer and use it in GitHub Desktop.
Geolocation investigation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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