'Search for entries on Limetorrents'

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

    if (not isinstance(config, dict)):
        config = {u'category': config}
    order_by = u''
    if isinstance(config.get(u'order_by'), str):
        if (config[u'order_by'] != u'date'):
            order_by = u'{0}/1'.format(config[u'order_by'])
    category = u'all'
    if isinstance(config.get(u'category'), str):
        category = u'{0}'.format(config[u'category'])
    entries = set()
    for search_string in entry.get(u'search_strings', [entry[u'title']]):
        query = u'search/{0}/{1}/{2}'.format(category, quote(search_string.encode(u'utf8')), order_by)
        log.debug(u'Using    search:    %s;    category:    %s;    ordering:    %s', search_string, category, (order_by or u'default'))
        try:
            page = task.requests.get((self.base_url + query))
            log.debug(u'requesting:    %s', page.url)
        except RequestException as e:
            log.error(u'Limetorrents    request    failed:    %s', e)
            continue
        soup = get_soup(page.content)
        if (soup.find(u'a', attrs={u'class': u'csprite_dl14'}) is not None):
            for link in soup.findAll(u'a', attrs={u'class': u'csprite_dl14'}):
                row = link.find_parent(u'tr')
                info_url = str(link.get(u'href'))
                title = str(link.next_sibling.get(u'href'))
                title = title[:title.rfind(u'-torrent')].replace(u'-', u'    ')
                title = title[1:]
                data = row.findAll(u'td', attrs={u'class': u'tdnormal'})
                size = str(data[1].text).replace(u',', u'')
                seeds = int(row.find(u'td', attrs={u'class': u'tdseed'}).text.replace(u',', u''))
                leeches = int(row.find(u'td', attrs={u'class': u'tdleech'}).text.replace(u',', u''))
                size = parse_filesize(size)
                e = Entry()
                e[u'url'] = info_url
                e[u'title'] = title
                e[u'torrent_seeds'] = seeds
                e[u'torrent_leeches'] = leeches
                e[u'search_sort'] = torrent_availability(e[u'torrent_seeds'], e[u'torrent_leeches'])
                e[u'content_size'] = size
                entries.add(e)
    return entries
