8000 added ip -> geolocation script · SLKTH/python-scripts@5bb3679 · GitHub
[go: up one dir, main page]

Skip to content
Sign in

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5bb3679

Browse files
committed
added ip -> geolocation script
1 parent 25e5b3a commit 5bb3679

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

25_ip2geolocation.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import csv
2+
import requests
3+
4+
5+
def get_addresses(filename):
6+
"""
7+
Given a CSV file, this function returns a list of lists
8+
where each element (list) in the outer list contains the
9+
row info from the csv file.
10+
"""
11+
all_addresses = []
12+
with open(filename, 'rb') as f:
13+
reader = csv.reader(f)
14+
for row in reader:
15+
all_addresses.append(row)
16+
return all_addresses
17+
18+
19+
def get_geolocation(all_the_ip_address):
20+
"""
21+
Given a list of lists from `get_addresses()`, this function
22+
returns an updated lists of lists containing the geolocation.
23+
"""
24+
print("Getting geo information...")
25+
updated_addresses = []
26+
counter = 1
27+
# update header
28+
header_row = all_the_ip_address.pop(0)
29+
header_row.extend(['Country', 'City'])
30+
# get geolocation
31+
for line in all_the_ip_address:
32+
print "Grabbing geo info for row # {0}".format(counter)
33+
r = requests.get('https://freegeoip.net/json/{0}'.format(line[0]))
34+
line.extend([str(r.json()['country_name']), str(r.json()['city'])])
35+
updated_addresses.append(line)
36+
counter += 1
37+
updated_addresses.insert(0, header_row)
38+
return updated_addresses
39+
40+
41+
def create_csv(updated_address_list):
42+
"""
43+
Given the updated lists of lists from `get_geolocation()`, this function
44+
creates a new CSV.
45+
"""
46+
with open('output.csv', 'wb') as f:
47+
writer = csv.writer(f)
48+
writer.writerows(updated_address_list)
49+
print "All done!"
50+
51+
52+
if __name__ == '__main__':
53+
csv_file = '25_sample_csv.csv'
54+
all_the_ip_address = get_addresses(csv_file)
55+
updated_address_list = get_geolocation(all_the_ip_address)
56+
create_csv(updated_address_list)

25_sample_csv.csv

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
IP Address,Full Name,Id,Email
2+
162.252.85.172,Virgie Simonis,0,Tatyana_Barton@domenico.net
3+
208.110.83.202,Tyrese Bartoletti,1,Birdie.Greenholt@annetta.co.uk
4+
108.162.199.95,Markus Sanford,2,Lela_Homenick@philip.net
5+
169.228.182.227,Anastasia Sawayn,3,Abe@camylle.name
6+
184.72.242.188,Ashly Howe,5,Kieran.Bashirian@ansley.com

readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@
2323
1. **21_twitter_bot.py**: Twitter Bot
2424
1. **22_git_tag.py**: Create Git Tag based on a commit
2525
1. **23_flask_session_test.py**: Just a simple app to see if the sessions are working
26-
1. **24_sql2csv.py**: SQL to CSV.
26+
1. **24_sql2csv.py**: SQL to CSV.
27+
1. **25_ip2geolocation.py**: Given a CSV file with an ip address (see sample - *25_sample_csv.csv*), return the geolocation based on the ip.

0 commit comments

Comments
 (0)
0