Skip to content

Commit a8dd886

Browse files
authored
combine field & location functions
1 parent 37934a7 commit a8dd886

File tree

1 file changed

+34
-30
lines changed

1 file changed

+34
-30
lines changed

ipapi/ipapi.py

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,55 +6,59 @@
66
import argparse
77
from requests import get
88

9-
headers = {'user-agent': 'ipapi/ipapi-python/0.4'}
9+
1010
API_KEY = ''
1111

12+
headers = {'user-agent': 'ipapi/ipapi-python/0.5.1'}
1213

13-
def location(ip=None, key=None):
14-
''' Get complete geolocation data (as JSON) for given IP address '''
15-
if ip:
16-
url = 'https://ipapi.co/{}/json/'.format(ip)
17-
else:
18-
url = 'https://ipapi.co/json/'
19-
if key or API_KEY:
20-
url = '{}?key={}'.format(url, (key or API_KEY))
14+
field_list = ['ip', 'city', 'region', 'country', 'postal',
15+
'latitude', 'longitude', 'timezone', 'latlong']
2116

22-
response = get(url, headers=headers)
23-
return response.json()
2417

2518

26-
def field(field, ip=None, key=None):
27-
''' Get specific geolocation field (as text) for given IP address '''
28-
if ip:
29-
url = 'https://ipapi.co/{}/{}/'.format(ip, field)
19+
def location(ip=None, key=None, field=None):
20+
''' Get geolocation data for a given IP address
21+
If field is specified, get specific field as text
22+
Else get complete location data as JSON
23+
'''
24+
25+
if field and (field not in field_list):
26+
return 'Invalid field'
27+
28+
if field:
29+
if ip:
30+
url = 'https://ipapi.co/{}/{}/'.format(ip, field)
31+
else:
32+
url = 'https://ipapi.co/{}/'.format(field)
3033
else:
31-
url = 'https://ipapi.co/{}/'.format(field)
34+
if ip:
35+
url = 'https://ipapi.co/{}/json/'.format(ip)
36+
else:
37+
url = 'https://ipapi.co/json/'
38+
3239
if key or API_KEY:
3340
url = '{}?key={}'.format(url, (key or API_KEY))
3441

3542
response = get(url, headers=headers)
36-
return response.text
43+
44+
if field:
45+
return response.text
46+
else:
47+
return response.json()
48+
3749

3850

39-
def main(argv=None):
40-
field_list = ['ip', 'city', 'region', 'country', 'postal', 'latitude', 'longitude', 'timezone', 'latlong']
41-
51+
def main(argv=None):
4252
argv = argv or sys.argv[1:]
4353
parser = argparse.ArgumentParser(description='IP address location API : https://ipapi.co')
44-
parser.add_argument('-i', '--ip', dest='ip', help='IP address')
45-
parser.add_argument('-f', '--field', dest='field', help='specific field e.g. {}'.format(', '.join(field_list)), default=None)
54+
parser.add_argument('-i', '--ip', dest='ip', help='IP address', default=None)
55+
parser.add_argument('-f', '--field', dest='field', help='specific field e.g. {}'.format(', '.join(field_list)))
4656
parser.add_argument('-k', '--key', dest='key', help='API key', default=None)
4757
args = parser.parse_args(argv)
4858

49-
if args.field and (args.field not in field_list):
50-
print 'Invalid field : {}'.format(args.field)
51-
return
52-
53-
if args.field:
54-
print field(args.field, args.ip, args.key)
55-
else:
56-
print location(args.ip, args.key)
59+
print location(args.ip, args.key, args.field)
5760

5861

5962
if __name__ == "__main__":
6063
sys.exit(main())
64+

0 commit comments

Comments
 (0)