Skip to content

Instantly share code, notes, and snippets.

@tovask
Last active April 15, 2017 08:20
Show Gist options
  • Save tovask/4c81e11491885f37ca259e09b3ac6104 to your computer and use it in GitHub Desktop.
Save tovask/4c81e11491885f37ca259e09b3ac6104 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import urllib
import json
ips = set() # store only unique values
log = open('log.csv','r').read().split('\r\n')
for record in log:
if len(record)==0: # last one
continue
ips.add(record.split(";")[2][1:-1])
print 'Total: ',len(ips)
success = []
error = []
#ips = list(ips)[:3]
outcsv = open('ip_country_2.csv','w')
for ip in ips:
#url = 'http://ipinfo.io/208.80.152.201/json'
#url = 'http://ip-api.com/json/208.80.152.201'
print 'get ',ip
url = 'http://ip-api.com/json/'+ip
result = json.load(urllib.urlopen(url))
if result['status'] == 'success':
del result['status']
success.append(result)
# hope there isn't any apostrof in the result....
outcsv.write('"'+result['query']+'";"'+result['country']+'";"'+result['isp']+'";"'+result['org']+'";"'+result['as']+'"\r\n')
else:
error.append(result)
open('success.json','w').write(json.dumps(success))
#print json.dumps(success)
#print ""
open('error.json','w').write(json.dumps(error))
#print error
#print ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment