import BeautifulSoup as bs
import urllib3
URL = "http://malc0de.com/database/?&page="


def malc0de_ips(url):
    '''

    :param url:
    :return A list of Lists (20x2) : IP Address, Rank :
    '''
    spamlist = []
    http = urllib3.PoolManager()
    doc = http.request('GET', url+"1").data

    soup = bs.BeautifulSoup(doc)
    soup_links = soup.findAll('a')

    pageNum = soup_links[len(soup_links)-2].string

    soup = soup.findAll('table')[0]
    for row in soup.findAll('tr'):

        aux = row.findAll('td')

        if len(aux) > 1 and aux[0].string is not None:
            spam = {}
            spam['ip_address'] = aux[2].find('a').string
            spam['listing_timestamp'] = aux[0].string
            spam['host_name'] = aux[1].string
            spam['country'] = aux[3].find('a').string
            spam['autonomous_system_number'] = aux[4].find('a').string
            spam['autonomous_system_name'] = aux[5].find('a').string

            spamlist.append(spam)


    for i in range(2, int(pageNum)):
        doc = http.request('GET', url+str(i)).data
        soup = bs.BeautifulSoup(doc)

        soup = soup.findAll('table')[0]
        for row in soup.findAll('tr'):

            aux = row.findAll('td')

            if len(aux) > 1 and aux[0].string is not None:
                spam = {}
                spam['ip_address'] = aux[2].find('a').string
                spam['listing_timestamp'] = aux[0].string
                spam['host_name'] = aux[1].string
                spam['country'] = aux[3].find('a').string
                spam['autonomous_system_number'] = aux[4].find('a').string
                spam['autonomous_system_name'] = aux[5].find('a').string

                spamlist.append(spam)



    return spamlist


#malc0de_ips(URL)