#!/usr/bin/python3

import ipwhois
from ipwhois.net import Net
from ipwhois.asn import IPASN
import warnings

ipList = []

def lookupIP(inData):
    try:
        with warnings.catch_warnings():
            warnings.filterwarnings("ignore", category=UserWarning)
            ipnet = Net(inData)
            ipobj = IPASN(ipnet)
            results = ipobj.lookup()
            # print(results)
            return results

    except ipwhois.exceptions.IPDefinedError:
        pass # We do not care about local/invalid IPs
    except Exception as e:
        pass # Garbage data was given lol


def parseIPS():
    with open("ips.txt", 'r') as f:
        testdata = f.read()

    data = testdata.split('\n')
    for ip in data:
        ip = ip.strip()
        ipAddr = ip.split()
        if len(ipAddr) == 2:
            ipList.append(ipAddr)
        else:
            pass

parseIPS()

ff = lookupIP("122.106.224.146")

print(ff["asn_country_code"])

for ips in ipList:
    # ccode = ipCheck(lookupIP(ips[1]))
    ret = lookupIP(ips[1])
    if ret == None:
        continue
    cc = ret["asn_country_code"]
    desc = ret["asn_description"]
    if cc == "AU" or cc == "NZ" or cc == "US":
        continue
    print("IP: {}\t\tCountry: {}\tDesc: {}\tCount: {}".format(ips[1], cc, desc, ips[0]))




# print(ipList[0])
# lookupIP("123.23.23.12")

##{'asn_registry': 'apnic', 'asn': '4134', 'asn_cidr': '220.174.0.0/16', 'asn_country_code': 'CN', 'asn_date': '2002-10-30', 'asn_description': 'CHINANET-BACKBONE No.31,Jin-rong Street, CN'}