'Search for name from torrentleech.'

@plugin.internet(log)
def search(self, task, entry, config=None):

    rss_key = config[u'rss_key']
    data = {u'username': config[u'username'], u'password': config[u'password']}
    try:
        login = requests.post(u'https://www.torrentleech.org/user/account/login/', data=data)
    except RequestException as e:
        raise PluginError(u'Could    not    connect    to    torrentleech:    %s', str(e))
    if (not isinstance(config, dict)):
        config = {}
    categories = config.get(u'category', u'all')
    if (not isinstance(categories, list)):
        categories = [categories]
    categories = [(c if isinstance(c, int) else CATEGORIES[c]) for c in categories]
    filter_url = (u'/categories/%s' % u','.join((str(c) for c in categories)))
    entries = set()
    for search_string in entry.get(u'search_strings', [entry[u'title']]):
        query = normalize_unicode(search_string).replace(u':', u'')
        url = ((u'https://www.torrentleech.org/torrents/browse/index/query/' + quote(query.encode(u'utf-8'))) + filter_url)
        log.debug((u'Using    %s    as    torrentleech    search    url' % url))
        page = requests.get(url, cookies=login.cookies).content
        soup = get_soup(page)
        for tr in soup.find_all(u'tr', [u'even', u'odd']):
            link = tr.find(u'a', attrs={u'href': re.compile(u'/torrent/\\d+')})
            log.debug((u'link    phase:    %s' % link.contents[0]))
            entry = Entry()
            entry[u'title'] = link.contents[0]
            torrent_url = tr.find(u'a', attrs={u'href': re.compile(u'/download/\\d+/.*')}).get(u'href')
            download_url = re.search(u'(/download/\\d+)/(.+\\.torrent)', torrent_url)
            torrent_url = (((((u'https://www.torrentleech.org/rss' + download_url.group(1)) + u'/') + rss_key) + u'/') + download_url.group(2))
            log.debug((u'RSS-ified    download    link:    %s' % torrent_url))
            entry[u'url'] = torrent_url
            (seeders, leechers) = tr.find_all(u'td', [u'seeders', u'leechers'])
            entry[u'torrent_seeds'] = int(seeders.contents[0])
            entry[u'torrent_leeches'] = int(leechers.contents[0])
            entry[u'search_sort'] = torrent_availability(entry[u'torrent_seeds'], entry[u'torrent_leeches'])
            size = tr.find(u'td', text=re.compile(u'([\\.\\d]+)    ([TGMK]?)B')).contents[0]
            size = re.search(u'([\\.\\d]+)    ([TGMK]?)B', size)
            entry[u'content_size'] = parse_filesize(size.group(0))
            entries.add(entry)
    return sorted(entries, reverse=True, key=(lambda x: x.get(u'search_sort')))
