def search_piratebay(query):
    html = requests.get('https://thepiratebay.se/search/' + query)
    soup = BeautifulSoup(html.content)
    total_phrase = soup.find_all('h2')[0].contents[1].strip()
    results = []
    if total_phrase.startswith('No hits'):
        total = 0
    else:
        total = [int(s) for s in total_phrase.split() if s.isdigit()][-1]

    if total > 0:
        rows = soup.find_all('tr')[1:]

        for row in rows:
            torrent = Torrent(source='Pirate Bay')
            td = row.find_all('td')
            torrent.title = td[1].find('a').text
            torrent.url = td[1].find_all('a')[1].attrs['href']
            torrent.seeders = int(td[2].text)
            torrent.leechers = int(td[3].text)
            torrent.size = td[1].find('font').text.split(',')[1].replace(' Size ', '')

            results.append(torrent)

    if total > 30:
        total = 30

    return {'total': total, 'results': results}
def search_extra_torrent(query):
    html = requests.get('http://extratorrent.cc/search/?search=' + query)
    soup = BeautifulSoup(html.content)
    total = int(soup.find_all('b')[12].text)
    results = []
    if total > 0:
        r_rows = soup.find_all('tr', class_='tlr')
        z_rows = soup.find_all('tr', class_='tlz')
        all_rows = r_rows + z_rows

        for row in all_rows:
            torrent = Torrent(source='ExtraTorrent')
            attributes = row.find_all('a')[0].attrs
            title = attributes['title'].split()
            title.pop(0)
            title.pop()
            torrent.title = " ".join(title)
            download_link = attributes['href'].replace('torrent_download', 'download')
            torrent.url = 'http://extratorrent.cc' + download_link

            torrent.size = row.find_all('td')[-4].text

            if row.find('td', class_='sy'):
                torrent.seeders = int(row.find('td', class_='sy').text)
            else:
                torrent.seeders = 0

            if row.find('td', class_='ly'):
                torrent.leechers = int(row.find('td', class_='ly').text)
            else:
                torrent.leechers = 0

            results.append(torrent)

        if total > 50:
            total = 50

    return {'total': total, 'results': results}