<filename>torrents.py
import pandas as pd
from web import soup




class EmptyTable(Exception):
    def __init__(self, message):
        self.message = message


class FailedDfCreation(Exception):
    def __init__(self, message):
        self.message = message


class HardToFixBetterSkip(Exception):
    def __init__(self, message):
        self.message = message


def get_torrent_table(movie, res_kwds, criteria):
    try:
        sizes = {
            'M': 1,
            'G': 1000
        }

        # get HTML table from 1337x.to
        link = f"https://1337x.to/category-search/{movie.title} {movie.year}/Movies/1/".replace(" ", "+")

        table_rows = soup(link).table.find_all('tr')
        if len(table_rows) < 2:
            raise Exception

        table_rows.pop(0)

        l = []
        for tr in table_rows:
            try:
                td = tr.find_all('td')
                row = [cell.text for cell in td] + [f"https://1337x.to{a['href']}" for a in tr.find_all(href=True)] +["","",""]
                row.pop(6)
                l.append(row)
            except:
                continue

        # Create, clean up and sort downloads_df

        downloads_df = pd.DataFrame(l, columns=['Title', 'Se', 'Le', 'Time', 'Size', 'Uploader', 'Item_link',
                                                    'User_link','Language','Id','Status']).dropna()

        downloads_df.Size = [val.replace(',', '').split('B')[0].split(' ') for val in downloads_df.Size]
        downloads_df[['Size', 'Unit']] = downloads_df.Size.tolist()
        downloads_df.Unit = downloads_df.Unit.map(sizes)
        downloads_df.Size = downloads_df.Size.map(float) * downloads_df.Unit

        downloads_df.Se = downloads_df.Se.map(int)
        downloads_df.Le = downloads_df.Le.map(int)
        downloads_df.Size = downloads_df.Size.map(int)

        downloads_df = downloads_df[downloads_df['Se'] >= criteria['seeders']].sort_values(['Size', 'Se'], ascending=False)

        downloads_df['Year'] = movie.year
            # Infer and add resolution to df
        downloads_df['Resolution'] = ''
        for res in res_kwds:  # add resolutions to df
            for i in range(len(downloads_df)):
                for keyword in res_kwds[res]:
                    if keyword in downloads_df.iloc[i, downloads_df.columns.get_loc('Title')]:
                        downloads_df.iloc[i, downloads_df.columns.get_loc('Resolution')] = res

        return downloads_df



    except:
        raise EmptyTable


def get_mirror(soup, mirror_site):
    info_hash = next(p.text.split(' ')[-1] for p in soup.find_all('p') if 'hash' in p.text.lower())
    return mirror_site.format(info_hash)

<filename>torrents.py
import pandas as pd
from web import soup




class EmptyTable(Exception):
    def __init__(self, message):
        self.message = message


class FailedDfCreation(Exception):
    def __init__(self, message):
        self.message = message


class HardToFixBetterSkip(Exception):
    def __init__(self, message):
        self.message = message


def get_torrent_table(movie, res_kwds, criteria):
    try:
        sizes = {
            'M': 1,
            'G': 1000
        }

        # get HTML table from 1337x.to
        link = f"https://1337x.to/category-search/{movie.title} {movie.year}/Movies/1/".replace(" ", "+")

        table_rows = soup(link).table.find_all('tr')
        if len(table_rows) < 2:
            raise Exception

        table_rows.pop(0)

        l = []
        for tr in table_rows:
            try:
                td = tr.find_all('td')
                row = [cell.text for cell in td] + [f"https://1337x.to{a['href']}" for a in tr.find_all(href=True)] +["","",""]
                row.pop(6)
                l.append(row)
            except:
                continue

        # Create, clean up and sort downloads_df

        downloads_df = pd.DataFrame(l, columns=['Title', 'Se', 'Le', 'Time', 'Size', 'Uploader', 'Item_link',
                                                    'User_link','Language','Id','Status']).dropna()

        downloads_df.Size = [val.replace(',', '').split('B')[0].split(' ') for val in downloads_df.Size]
        downloads_df[['Size', 'Unit']] = downloads_df.Size.tolist()
        downloads_df.Unit = downloads_df.Unit.map(sizes)
        downloads_df.Size = downloads_df.Size.map(float) * downloads_df.Unit

        downloads_df.Se = downloads_df.Se.map(int)
        downloads_df.Le = downloads_df.Le.map(int)
        downloads_df.Size = downloads_df.Size.map(int)

        downloads_df = downloads_df[downloads_df['Se'] >= criteria['seeders']].sort_values(['Size', 'Se'], ascending=False)

        downloads_df['Year'] = movie.year
            # Infer and add resolution to df
        downloads_df['Resolution'] = ''
        for res in res_kwds:  # add resolutions to df
            for i in range(len(downloads_df)):
                for keyword in res_kwds[res]:
                    if keyword in downloads_df.iloc[i, downloads_df.columns.get_loc('Title')]:
                        downloads_df.iloc[i, downloads_df.columns.get_loc('Resolution')] = res

        return downloads_df



    except:
        raise EmptyTable


def get_mirror(soup, mirror_site):
    info_hash = next(p.text.split(' ')[-1] for p in soup.find_all('p') if 'hash' in p.text.lower())
    return mirror_site.format(info_hash)

# get HTML table from 1337x.to # Create, clean up and sort downloads_df # Infer and add resolution to df # add resolutions to df