|
6 | 6 | import argparse
|
7 | 7 | from requests import get
|
8 | 8 |
|
9 |
| -headers = {'user-agent': 'ipapi/ipapi-python/0.4'} |
| 9 | + |
10 | 10 | API_KEY = ''
|
11 | 11 |
|
| 12 | +headers = {'user-agent': 'ipapi/ipapi-python/0.5.1'} |
12 | 13 |
|
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'] |
21 | 16 |
|
22 |
| - response = get(url, headers=headers) |
23 |
| - return response.json() |
24 | 17 |
|
25 | 18 |
|
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) |
30 | 33 | 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 | + |
32 | 39 | if key or API_KEY:
|
33 | 40 | url = '{}?key={}'.format(url, (key or API_KEY))
|
34 | 41 |
|
35 | 42 | response = get(url, headers=headers)
|
36 |
| - return response.text |
| 43 | + |
| 44 | + if field: |
| 45 | + return response.text |
| 46 | + else: |
| 47 | + return response.json() |
| 48 | + |
37 | 49 |
|
38 | 50 |
|
39 |
| -def main(argv=None): |
40 |
| - field_list = ['ip', 'city', 'region', 'country', 'postal', 'latitude', 'longitude', 'timezone', 'latlong'] |
41 |
| - |
| 51 | +def main(argv=None): |
42 | 52 | argv = argv or sys.argv[1:]
|
43 | 53 | 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))) |
46 | 56 | parser.add_argument('-k', '--key', dest='key', help='API key', default=None)
|
47 | 57 | args = parser.parse_args(argv)
|
48 | 58 |
|
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) |
57 | 60 |
|
58 | 61 |
|
59 | 62 | if __name__ == "__main__":
|
60 | 63 | sys.exit(main())
|
| 64 | + |
0 commit comments