import csv
from geopy import distance
from tqdm import tqdm

with open("data.csv", 'r') as f:
    csv_data = csv.DictReader(f)

    with open("distances.csv", "w",  newline='') as wf:
        csv_writer = csv.writer(wf)
        csv_writer.writerow(['DbIpCity', 'HostIP', 'Ip2location', 'Ipstack', 'MaxMind_GeoLite2City', 'Eurek', 'GeobytesCityDetails', 'IP_Info', 'DbIpWeb', 'IP2Location_Web', 'MaxMindGeoIp2City',
                             'NeustarWeb', 'Skyhook', 'Average', 'Median', 'Clustering'])
        for row in tqdm(csv_data):
            orignal_loc = [row["Original: lat"], row["Original: lon"]]

            try:
                if row["DbIpCity: lat"] == "" or row["DbIpCity: lon"] == "" or row["DbIpCity: lat"] is None or row["DbIpCity: lon"] is None:
                    raise BaseException
                dist_to_db_ip_city = distance.distance(orignal_loc, [row["DbIpCity: lat"], row["DbIpCity: lon"]]).km
            except:
                dist_to_db_ip_city = None
            try:
                if row["HostIP: lat"] == "" or row["HostIP: lon"] == "" or row["HostIP: lat"] is None or row["HostIP: lon"] is None:
                    raise BaseException
                dist_to_hostip = distance.distance(orignal_loc, [row["HostIP: lat"], row["HostIP: lon"]]).km
            except:
                dist_to_hostip = None
            try:
                if row["Ip2location: lat"] == "" or row["Ip2location: lon"] == "" or row["Ip2location: lat"] is None or row["Ip2location: lon"] is None:
                    raise BaseException
                dist_to_ip2location = distance.distance(orignal_loc, [row["Ip2location: lat"], row["Ip2location: lon"]]).km
            except:
                dist_to_ip2location = None
            try:
                if row["Ipstack: lat"] == "" or row["Ipstack: lon"] == "" or row["Ipstack: lat"] is None or row["Ipstack: lon"] is None:
                    raise BaseException
                dist_to_ipstack = distance.distance(orignal_loc, [row["Ipstack: lat"], row["Ipstack: lon"]]).km
            except:
                dist_to_ipstack = None
            try:
                if row["MaxMind_GeoLite2City: lat"] == "" or row["MaxMind_GeoLite2City: lon"] == "" or row["MaxMind_GeoLite2City: lat"] is None or row["MaxMind_GeoLite2City: lon"] is None:
                    raise BaseException
                dist_to_maxmind_lite = distance.distance(orignal_loc, [row["MaxMind_GeoLite2City: lat"], row["MaxMind_GeoLite2City: lon"]]).km
            except:
                dist_to_maxmind_lite = None
            try:
                if row["Eurek: lat"] == "" or row["Eurek: lon"] == "" or row["Eurek: lat"] is None or row["Eurek: lon"] is None:
                    raise BaseException
                dist_to_eurek = distance.distance(orignal_loc, [row["Eurek: lat"], row["Eurek: lon"]]).km
            except:
                dist_to_eurek = None
            try:
                if row["GeobytesCityDetails: lat"] == "" or row["GeobytesCityDetails: lon"] == "" or row["GeobytesCityDetails: lat"] is None or row["GeobytesCityDetails: lon"] is None:
                    raise BaseException
                dist_to_geobytes = distance.distance(orignal_loc, [row["GeobytesCityDetails: lat"], row["GeobytesCityDetails: lon"]]).km
            except:
                dist_to_geobytes = None
            try:
                if row["IP_Info: lat"] == "" or row["IP_Info: lon"] == "" or row["IP_Info: lat"] is None or row["IP_Info: lon"] is None:
                    raise BaseException
                dist_to_ip_info = distance.distance(orignal_loc, [row["IP_Info: lat"], row["IP_Info: lon"]]).km
            except:
                dist_to_ip_info = None
            try:
                if row["DbIpWeb: lat"] == "" or row["DbIpWeb: lon"] == "" or row["DbIpWeb: lat"] is None or row["DbIpWeb: lon"] is None:
                    raise BaseException
                dist_to_db_ip_web = distance.distance(orignal_loc, [row["DbIpWeb: lat"], row["DbIpWeb: lon"]]).km
            except:
                dist_to_db_ip_web = None
            try:
                if row["IP2Location_Web: lat"] == "" or row["IP2Location_Web: lon"] == "" or row["IP2Location_Web: lat"] is None or row["IP2Location_Web: lon"] is None:
                    raise BaseException
                dist_to_ip2location_web = distance.distance(orignal_loc, [row["IP2Location_Web: lat"], row["IP2Location_Web: lon"]]).km
            except:
                dist_to_ip2location_web = None
            try:
                if row["MaxMindGeoIp2City: lat"] == "" or row["MaxMindGeoIp2City: lon"] == "" or row["MaxMindGeoIp2City: lat"] is None or row["MaxMindGeoIp2City: lon"] is None:
                    raise BaseException
                dist_to_maxmind = distance.distance(orignal_loc, [row["MaxMindGeoIp2City: lat"],row["MaxMindGeoIp2City: lon"]]).km
            except:
                dist_to_maxmind = None
            try:
                if row["NeustarWeb: lat"] == "" or row["NeustarWeb: lon"] == "" or row["NeustarWeb: lat"] is None or row["NeustarWeb: lon"] is None:
                    raise BaseException
                dist_to_neustar = distance.distance(orignal_loc, [row["NeustarWeb: lat"], row["NeustarWeb: lon"]]).km
            except:
                dist_to_neustar = None
            try:
                if row["Skyhook: lat"] == "" or row["Skyhook: lon"] == "" or row["Skyhook: lat"] is None or row["Skyhook: lon"] is None:
                    raise BaseException
                dist_to_skyhook = distance.distance(orignal_loc, [row["Skyhook: lat"], row["Skyhook: lon"]]).km
            except:
                dist_to_skyhook = None
            try:
                if row["Average: lat"] == "" or row["Average: lon"] == "" or row["Average: lat"] is None or row["Average: lon"] is None:
                    raise BaseException
                dist_to_average = distance.distance(orignal_loc, [row["Average: lat"], row["Average: lon"]]).km
            except:
                dist_to_average = None
            try:
                if row["Median: lat"] == "" or row["Median: lon"] == "" or row["Median: lat"] is None or row["Median: lon"] is None:
                    raise BaseException
                dist_to_median = distance.distance(orignal_loc, [row["Median: lat"], row["Median: lon"]]).km
            except:
                dist_to_median = None
            try:
                if row["Clustering: lat"] == "" or row["Clustering: lon"] == "" or row["Clustering: lat"] is None or row["Clustering: lon"] is None:
                    raise BaseException
                dist_to_clustering = distance.distance(orignal_loc, [row["Clustering: lat"], row["Clustering: lon"]]).km
            except:
                dist_to_clustering = None
        

            csv_writer.writerow([dist_to_db_ip_city, dist_to_hostip, dist_to_ip2location, dist_to_ipstack, dist_to_maxmind_lite, dist_to_eurek, dist_to_geobytes, dist_to_ip_info, dist_to_db_ip_web,
                                 dist_to_ip2location_web, dist_to_maxmind, dist_to_neustar, dist_to_skyhook, dist_to_average, dist_to_median, dist_to_clustering])
        