Skip to content

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+
importcsv
2+
importrequests
3+
4+
5+
defget_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+
withopen(filename, 'rb') asf:
13+
reader=csv.reader(f)
14+
forrowinreader:
15+
all_addresses.append(row)
16+
returnall_addresses
17+
18+
19+
defget_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+
forlineinall_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+
returnupdated_addresses
39+
40+
41+
defcreate_csv(updated_address_list):
42+
"""
43+
Given the updated lists of lists from `get_geolocation()`, this function
44+
creates a new CSV.
45+
"""
46+
withopen('output.csv', 'wb') asf:
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,[email protected]
3+
208.110.83.202,Tyrese Bartoletti,1,[email protected]
4+
108.162.199.95,Markus Sanford,2,[email protected]
5+
169.228.182.227,Anastasia Sawayn,3,[email protected]
6+
184.72.242.188,Ashly Howe,5,[email protected]

‎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)