File: //home/arjun/projects/buyercall/buyercall/blueprints/user/utilities/get_request_location.py
import requests
from flask import current_app as app
def extract_first_ip(ip_list):
ips = ip_list.split(',') # Split the string by commas
first_ip = ips[0].strip() # Extract the first IP address and remove any leading or trailing whitespace
return first_ip
class UserUtilities:
@staticmethod
def get_request_location(ip_address):
# Use only the first ip in list
ip = extract_first_ip(ip_address)
key = app.config['IP_API_KEY']
url = f"https://pro.ip-api.com/json/{ip}?key={key}"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
return data.get('regionName')
return None
@staticmethod
def get_request_complete_details(ip_address):
# Use only the first ip in list
ip = extract_first_ip(ip_address)
key = app.config['IP_API_KEY']
url = (f"https://pro.ip-api.com/json/{ip}"
f"?key={key}&fields=status,message,country,regionName,city,zip,reverse,query")
response = requests.get(url)
if response.status_code == 200:
data = response.json()
return data if data.get('status') == "success" else {}
return {}