'Search for name from piratebay.'

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

    if (not isinstance(config, dict)):
        config = {}
    sort = SORT.get(config.get(u'sort_by', u'seeds'))
    if config.get(u'sort_reverse'):
        sort += 1
    if isinstance(config.get(u'category'), int):
        category = config[u'category']
    else:
        category = CATEGORIES.get(config.get(u'category', u'all'))
    filter_url = (u'/0/%d/%d' % (sort, category))
    entries = set()
    for search_string in entry.get(u'search_strings', [entry[u'title']]):
        query = normalize_unicode(search_string)
        query = query.replace(u'-', u'    ').replace(u"'", u'    ')
        url = (u'http://thepiratebay.%s/search/%s%s' % (CUR_TLD, quote(query.encode(u'utf-8')), filter_url))
        log.debug((u'Using    %s    as    piratebay    search    url' % url))
        page = task.requests.get(url).content
        soup = get_soup(page)
        for link in soup.find_all(u'a', attrs={u'class': u'detLink'}):
            entry = Entry()
            entry[u'title'] = self.extract_title(link)
            if (not entry[u'title']):
                log.error(u'Malformed    search    result.    No    title    or    url    found.    Skipping.')
                continue
            entry[u'url'] = (u'http://thepiratebay.%s%s' % (CUR_TLD, link.get(u'href')))
            tds = link.parent.parent.parent.find_all(u'td')
            entry[u'torrent_seeds'] = int(tds[(-2)].contents[0])
            entry[u'torrent_leeches'] = int(tds[(-1)].contents[0])
            entry[u'search_sort'] = torrent_availability(entry[u'torrent_seeds'], entry[u'torrent_leeches'])
            size_text = link.find_next(attrs={u'class': u'detDesc'}).get_text()
            if size_text:
                size = re.search(u'Size    (\\d+(\\.\\d+)?\xa0(?:[PTGMK])?i?B)', size_text)
                if size:
                    entry[u'content_size'] = parse_filesize(size.group(1))
                else:
                    log.error(u'Malformed    search    result?    Title:    "%s",    No    size?    %s', entry[u'title'], size_text)
            entries.add(entry)
    return sorted(entries, reverse=True, key=(lambda x: x.get(u'search_sort')))
