diff --git a/25_ip2geolocation.py b/25_ip2geolocation.py new file mode 100644 index 0000000..f312989 --- /dev/null +++ b/25_ip2geolocation.py @@ -0,0 +1,56 @@ +import csv +import requests + + +def get_addresses(filename): + """ + Given a CSV file, this function returns a list of lists + where each element (list) in the outer list contains the + row info from the csv file. + """ + all_addresses = [] + with open(filename, 'rb') as f: + reader = csv.reader(f) + for row in reader: + all_addresses.append(row) + return all_addresses + + +def get_geolocation(all_the_ip_address): + """ + Given a list of lists from `get_addresses()`, this function + returns an updated lists of lists containing the geolocation. + """ + print("Getting geo information...") + updated_addresses = [] + counter = 1 + # update header + header_row = all_the_ip_address.pop(0) + header_row.extend(['Country', 'City']) + # get geolocation + for line in all_the_ip_address: + print "Grabbing geo info for row # {0}".format(counter) + r = requests.get('https://freegeoip.net/json/{0}'.format(line[0])) + line.extend([str(r.json()['country_name']), str(r.json()['city'])]) + updated_addresses.append(line) + counter += 1 + updated_addresses.insert(0, header_row) + return updated_addresses + + +def create_csv(updated_address_list): + """ + Given the updated lists of lists from `get_geolocation()`, this function + creates a new CSV. + """ + with open('output.csv', 'wb') as f: + writer = csv.writer(f) + writer.writerows(updated_address_list) + print "All done!" + + +if __name__ == '__main__': + csv_file = '25_sample_csv.csv' + all_the_ip_address = get_addresses(csv_file) + updated_address_list = get_geolocation(all_the_ip_address) + create_csv(updated_address_list) diff --git a/25_sample_csv.csv b/25_sample_csv.csv new file mode 100644 index 0000000..9943c95 --- /dev/null +++ b/25_sample_csv.csv @@ -0,0 +1,6 @@ +IP Address,Full Name,Id,Email +162.252.85.172,Virgie Simonis,0,Tatyana_Barton@domenico.net +208.110.83.202,Tyrese Bartoletti,1,Birdie.Greenholt@annetta.co.uk +108.162.199.95,Markus Sanford,2,Lela_Homenick@philip.net +169.228.182.227,Anastasia Sawayn,3,Abe@camylle.name +184.72.242.188,Ashly Howe,5,Kieran.Bashirian@ansley.com \ No newline at end of file diff --git a/readme.md b/readme.md index 33caedd..ad5d157 100644 --- a/readme.md +++ b/readme.md @@ -23,4 +23,5 @@ 1. **21_twitter_bot.py**: Twitter Bot 1. **22_git_tag.py**: Create Git Tag based on a commit 1. **23_flask_session_test.py**: Just a simple app to see if the sessions are working -1. **24_sql2csv.py**: SQL to CSV. \ No newline at end of file +1. **24_sql2csv.py**: SQL to CSV. +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. \ No newline at end of file