Skip to content

Commit 98f0baa

Browse files
committed
Update get_ip_geolocation.py
Add ip2location.io API as an option.
1 parent 4128f19 commit 98f0baa

File tree

1 file changed

+48
-14
lines changed

1 file changed

+48
-14
lines changed

web_programming/get_ip_geolocation.py

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,56 @@
22

33

44
# Function to get geolocation data for an IP address
5-
def get_ip_geolocation(ip_address: str) -> str:
5+
def get_ip_geolocation(ip_address: str, service_provider: str) -> str:
66
try:
7-
# Construct the URL for the IP geolocation API
8-
url = f"https://ipinfo.io/{ip_address}/json"
7+
# Using IP2Location.io
8+
if service_provider == "ip2location.io":
9+
url = f"https://api.ip2location.io/?ip={ip_address}"
910

10-
# Send a GET request to the API
11-
response = requests.get(url)
11+
# Send a GET request to the API
12+
response = requests.get(url)
1213

13-
# Check if the HTTP request was successful
14-
response.raise_for_status()
14+
# Check if the HTTP request was successful
15+
response.raise_for_status()
1516

16-
# Parse the response as JSON
17-
data = response.json()
17+
# Parse the response as JSON
18+
data = response.json()
1819

19-
# Check if city, region, and country information is available
20-
if "city" in data and "region" in data and "country" in data:
21-
location = f"Location: {data['city']}, {data['region']}, {data['country']}"
20+
# Check if city, region, and country information is available
21+
if "city_name" in data and "region_name" in data and "country_name" in data:
22+
location = (
23+
f"Location: {data['city_name']}, "
24+
f"{data['region_name']}, "
25+
f"{data['country_name']}"
26+
)
27+
else:
28+
location = "Location data not found."
29+
# Using ipinfo
30+
elif service_provider == "ipinfo":
31+
# Construct the URL for the IP geolocation API
32+
url = f"https://ipinfo.io/{ip_address}/json"
33+
34+
# Send a GET request to the API
35+
response = requests.get(url)
36+
37+
# Check if the HTTP request was successful
38+
response.raise_for_status()
39+
40+
# Parse the response as JSON
41+
data = response.json()
42+
43+
# Check if city, region, and country information is available
44+
if "city" in data and "region" in data and "country" in data:
45+
location = (
46+
f"Location: {data['city']}, {data['region']}, {data['country']}"
47+
)
48+
else:
49+
location = "Location data not found."
2250
else:
23-
location = "Location data not found."
51+
return (
52+
f"The service provider entered {service_provider} is not valid. "
53+
"Please choose from ip2location.io or ipinfo instead."
54+
)
2455

2556
return location
2657
except requests.exceptions.RequestException as e:
@@ -35,6 +66,9 @@ def get_ip_geolocation(ip_address: str) -> str:
3566
# Prompt the user to enter an IP address
3667
ip_address = input("Enter an IP address: ")
3768

69+
# Prompt the user to enter a service provider
70+
service_provider = input("Enter a service provider to use: ")
71+
3872
# Get the geolocation data and print it
39-
location = get_ip_geolocation(ip_address)
73+
location = get_ip_geolocation(ip_address, service_provider)
4074
print(location)

0 commit comments

Comments
 (0)