-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_batch.py
More file actions
47 lines (37 loc) · 1.12 KB
/
Copy pathparse_batch.py
File metadata and controls
47 lines (37 loc) · 1.12 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
#!/usr/bin/env python3
"""
parse_batch.py - Parse ip-api.com batch JSON response.
Reads batch JSON from stdin.
For each IP passed as arguments, outputs a line:
ip|city|country|isp|lat|lon
Usage:
echo "$batch_json" | python3 parse_batch.py ip1 ip2 ip3 ...
"""
import sys
import json
def main():
ips = sys.argv[1:]
if not ips:
return
try:
batch = json.loads(sys.stdin.read())
except json.JSONDecodeError:
# Output unknown for all IPs if parsing fails
for ip in ips:
print(f"{ip}|Unknown|Unknown|Unknown||")
return
# Build lookup by query IP
lookup = {}
for entry in batch:
if isinstance(entry, dict):
lookup[entry.get("query", "")] = entry
for ip in ips:
entry = lookup.get(ip, {})
city = entry.get("city", "Unknown") or "Unknown"
country = entry.get("country", "Unknown") or "Unknown"
isp = entry.get("isp", "Unknown") or "Unknown"
lat = entry.get("lat", "")
lon = entry.get("lon", "")
print(f"{ip}|{city}|{country}|{isp}|{lat}|{lon}")
if __name__ == "__main__":
main()