|
| 1 | +""" |
| 2 | +User Geo Location |
| 3 | +================= |
| 4 | +
|
| 5 | +Get Geo location of user using https://ipstack.com/ |
| 6 | +
|
| 7 | +written by: [email protected] (vimm0) |
| 8 | +original repository: https://github.com/vimm0/auto-script |
| 9 | +
|
| 10 | +USAGE: |
| 11 | +===== |
| 12 | +- Generate Free API key from https://ipstack.com/ |
| 13 | +- Obtain your ip address |
| 14 | +- run the code |
| 15 | +
|
| 16 | +""" |
| 17 | + |
| 18 | +import os |
| 19 | +import requests |
| 20 | +import re |
| 21 | +import json |
| 22 | + |
| 23 | + |
| 24 | +def check(Ip): |
| 25 | + # Make a regular expression |
| 26 | + # for validating an Ip-address |
| 27 | + regex = '''^(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.( |
| 28 | + 25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.( |
| 29 | + 25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.( |
| 30 | + 25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)''' |
| 31 | + # pass the regular expression |
| 32 | + # and the string in search() method |
| 33 | + if (re.search(regex, Ip)): |
| 34 | + return |
| 35 | + else: |
| 36 | + raise Exception('Invalid Ip address') |
| 37 | + |
| 38 | + |
| 39 | +def screen_clear(): |
| 40 | + if os.name == 'nt': |
| 41 | + os.system('cls') |
| 42 | + else: |
| 43 | + os.system('clear') |
| 44 | + |
| 45 | + |
| 46 | +def get_geolocation_for_ip(ip_addr, access_key): |
| 47 | + url = f"http://api.ipstack.com/{ip_addr}?access_key={access_key}" |
| 48 | + response = requests.get(url) |
| 49 | + response.raise_for_status() |
| 50 | + formatted_json = json.dumps(response.json(), indent=4) |
| 51 | + print(formatted_json) |
| 52 | + |
| 53 | + |
| 54 | +def get_user_input(): |
| 55 | + ip_addr = input("Enter IP Address: ") |
| 56 | + access_key = input("Enter IP Stack Access Code: ") |
| 57 | + check(ip_addr) |
| 58 | + get_geolocation_for_ip(ip_addr, access_key) |
| 59 | + |
| 60 | + |
| 61 | +def main(): |
| 62 | + try: |
| 63 | + screen_clear() |
| 64 | + get_user_input() |
| 65 | + except KeyboardInterrupt: |
| 66 | + exit(1) |
| 67 | + |
| 68 | + |
| 69 | +if __name__ == '__main__': |
| 70 | + main() |
| 71 | + exit(0) |
0 commit comments