Last active
July 12, 2023 18:51
-
-
Save zudsniper/e31f7ef280311bb4f7f13515cd93b832 to your computer and use it in GitHub Desktop.
π get location data from IP with geolocation IPify API (REQUIRES API_KEY)
This file contains hidden or 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
#/usr/bin/python3 | |
# get_coords.py v1.0.0 | |
# -------------------- | |
# | |
# @zudsniper | |
import requests | |
import json | |
import os | |
import sys | |
from loguru import logger | |
from colorama import Fore, Style | |
from dotenv import load_dotenv | |
# Load environment variables from .env file | |
load_dotenv() | |
# init logger | |
logger.remove(0) | |
#logger.add(sys.stderr, format="{time:MMMM D, YYYY > HH:mm:ss!UTC-6} | [<level>{level}</level>]: {message}") | |
logger.add(sys.stderr, format="{time:HH:mm:ss} | [<level>{level}</level>]: {message}") | |
# https://stackoverflow.com/a/37757378/5768559 | |
def pp_json(json_thing, sort=True, indents=4): | |
if type(json_thing) is str: | |
logger.info(json.dumps(json.loads(json_thing), sort_keys=sort, indent=indents)) | |
else: | |
logger.info(json.dumps(json_thing, sort_keys=sort, indent=indents)) | |
return None | |
def get_location(ip_address): | |
api_key = os.getenv('API_KEY') | |
if not api_key: | |
raise Exception("API_KEY not found in environment variables") | |
logger.debug(f"{Fore.YELLOW}API Key: <REDACTED>{Style.RESET_ALL}") | |
logger.debug(f"{Fore.CYAN}IP Address: {ip_address}{Style.RESET_ALL}") | |
url = "https://geo.ipify.org/api/v1?apiKey={}&ipAddress={}".format(api_key, ip_address) | |
#logger.debug(f"{Fore.GREEN}URL: {url[:}{Style.RESET_ALL}") | |
response = requests.get(url) | |
logger.debug(f"{Fore.MAGENTA}Response Status Code: {response.status_code}{Style.RESET_ALL}") | |
logger.debug(f"{Fore.BLUE}Response Text...{Style.RESET_ALL}") | |
pp_json(response.text) | |
data = json.loads(response.text) | |
return data | |
if len(sys.argv) != 2: | |
logger.error(f"{Fore.RED}Usage: python script.py <ip_address>{Style.RESET_ALL}") | |
sys.exit(1) | |
def run(): | |
ip_address = sys.argv[1] # get IP address from command line argument | |
location_data = get_location(ip_address) | |
logger.info(f"{Fore.GREEN}Location Data{Style.RESET_ALL}") | |
pp_json(location_data) | |
if __name__ == '__main__': | |
run() |
This file contains hidden or 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
colorama==0.4.6 | |
loguru==0.7.0 | |
python-dotenv==1.0.0 | |
Requests==2.31.0 |
This file contains hidden or 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
API_KEY=geolocation-api-key-from-IPify |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment