-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
47 lines (43 loc) · 1.46 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Import the required packages and modules
import requests
from colorama import Style, Fore, Back
import time
from validate_IP import is_valid
# Ask the user to provide the IP address
ip_addr = input(f"{Back.GREEN}Please provide the IP address:{Style.RESET_ALL} ")
# Define a function to retrieve the location details
def get_loc():
try:
res = requests.get(f"https://ipapi.co/{ip_addr}/json/").json()
return res
except Exception:
return None
# Check if the IP address is provided by the user
if not ip_addr:
print(
f"{Fore.RED}ERROR:\nThe IP address must contain a value and cannot be left blank \nTerminating the program.....{Style.RESET_ALL}"
)
time.sleep(3)
elif not is_valid(ip_addr):
print(
f"{Fore.RED}ERROR:\nInvalid IP address \nTerminating the program.....{Style.RESET_ALL}"
)
time.sleep(3)
elif not get_loc():
print(
f"{Fore.RED}ERROR:\nAn error occurred while retrieving the location details \nTerminating the program.....{Style.RESET_ALL}"
)
time.sleep(3)
else:
# Retrieve the location details and display them
try:
output = str(get_loc())
print("\n")
for val in output.split(","):
print(f"{Fore.WHITE}{val}{Style.RESET_ALL}")
time.sleep(1)
except KeyboardInterrupt:
print(
f"{Fore.RED}ERROR:\nUser has interrupted the process \nTerminating the program.....{Style.RESET_ALL}"
)
time.sleep(3)