#!/usr/bin/python3
import sys 
import geoip2.database

rank = sys.argv[1]
domain = sys.argv[2]
ip = sys.argv[3]

iso_code=""
name=""
cobject=False

# This reader object should be reused across lookups as creation of it is
# expensive.
with geoip2.database.Reader('/usr/share/GeoIP/GeoLite2-Country.mmdb') as reader:
    
    try:
        response = reader.country(ip);
        if 'country' in response.raw:
            cobject = True
            iso_code = response.country.iso_code
            name = response.country.names['en']
            iseurope = response.country.is_in_european_union
        else:
            # Country object does not exist use registered_country print(ip," ",name,": country object does not exist")

            if 'registered_country' in response.raw:
                cobject = False
                iso_code = response.registered_country.iso_code
                name = response.registered_country.names['en']

    except geoip2.errors.AddressNotFoundError:
        print(f"The address {ip} is not in the database.")
    except Exception as e:
        print(f"An error occurred: {e}")
    print(f"{rank},{domain},{ip},{iso_code},{name},(maxmind)")
    #print(response)

reader.close()
