forked from ipapi-co/ipapi-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipapi.py
More file actions
64 lines (43 loc) · 1.67 KB
/
ipapi.py
File metadata and controls
64 lines (43 loc) · 1.67 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# ipapi Python bindings (https://ipapi.co)
# API docs at https://ipapi.co/api
import sys
import argparse
from requests import get
API_KEY = ''
headers = {'user-agent': 'ipapi/ipapi-python/0.5.1'}
field_list = ['ip', 'city', 'region', 'country', 'postal',
'latitude', 'longitude', 'timezone', 'latlong']
def location(ip=None, key=None, field=None):
''' Get geolocation data for a given IP address
If field is specified, get specific field as text
Else get complete location data as JSON
'''
if field and (field not in field_list):
return 'Invalid field'
if field:
if ip:
url = 'https://ipapi.co/{}/{}/'.format(ip, field)
else:
url = 'https://ipapi.co/{}/'.format(field)
else:
if ip:
url = 'https://ipapi.co/{}/json/'.format(ip)
else:
url = 'https://ipapi.co/json/'
if key or API_KEY:
url = '{}?key={}'.format(url, (key or API_KEY))
response = get(url, headers=headers)
if field:
return response.text
else:
return response.json()
def main(argv=None):
argv = argv or sys.argv[1:]
parser = argparse.ArgumentParser(description='IP address location API : https://ipapi.co')
parser.add_argument('-i', '--ip', dest='ip', help='IP address', default=None)
parser.add_argument('-f', '--field', dest='field', help='specific field e.g. {}'.format(', '.join(field_list)))
parser.add_argument('-k', '--key', dest='key', help='API key', default=None)
args = parser.parse_args(argv)
print (location(args.ip, args.key, args.field))
if __name__ == "__main__":
sys.exit(main())