Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add script to get user geo location #191

Merged
merged 3 commits into from
Oct 16, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions Automation/src/user_geo_location/ugl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"""
User Geo Location
=================

Get Geo location of user using https://ipstack.com/

written by: [email protected] (vimm0)
original repository: https://github.com/vimm0/auto-script

USAGE:
=====
- Generate Free API key from https://ipstack.com/
- Obtain your ip address
- run the code

"""

import os
import requests
import re
import json


def check(Ip):
# Make a regular expression
# for validating an Ip-address
regex = '''^(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(
25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(
25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(
25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)'''
# pass the regular expression
# and the string in search() method
if (re.search(regex, Ip)):
return
else:
raise Exception('Invalid Ip address')


def screen_clear():
if os.name == 'nt':
os.system('cls')
else:
os.system('clear')


def get_geolocation_for_ip(ip_addr, access_key):
url = f"http://api.ipstack.com/{ip_addr}?access_key={access_key}"
response = requests.get(url)
response.raise_for_status()
formatted_json = json.dumps(response.json(), indent=4)
print(formatted_json)


def get_user_input():
ip_addr = input("Enter IP Address: ")
access_key = input("Enter IP Stack Access Code: ")
check(ip_addr)
get_geolocation_for_ip(ip_addr, access_key)


def main():
try:
screen_clear()
get_user_input()
except KeyboardInterrupt:
exit(1)


if __name__ == '__main__':
main()
exit(0)