'Search for entries on FileList.ro'

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

    entries = set()
    params = {u'cat': CATEGORIES[config[u'category']], u'incldead': int(config[u'include_dead']), u'order_by': SORTING[config[u'order_by']], u'searchin': SEARCH_IN[config[u'search_in']], u'asc': int(config[u'order_ascending'])}
    for search_string in entry.get(u'search_strings', [entry[u'title']]):
        params[u'search'] = search_string
        log.debug(u'Using    search    params:    %s', params)
        try:
            page = self.get((BASE_URL + u'browse.php'), params, config[u'username'], config[u'password'])
            log.debug(u'requesting:    %s', page.url)
        except RequestException as e:
            log.error(u'FileList.ro    request    failed:    %s', e)
            continue
        soup = get_soup(page.content)
        for result in soup.findAll(u'div', attrs={u'class': u'torrentrow'}):
            e = Entry()
            torrent_info = result.findAll(u'div', attrs={u'class': u'torrenttable'})
            genres = torrent_info[1].find(u'font')
            if genres:
                genres = genres.text.lstrip(u'[').rstrip(u']').replace(u'    ', u'')
                genres = genres.split(u'|')
            tags = torrent_info[1].findAll(u'img')
            freeleech = False
            internal = False
            for tag in tags:
                if (tag.get(u'alt', u'').lower() == u'freeleech'):
                    freeleech = True
                if (tag.get(u'alt', u'').lower() == u'internal'):
                    internal = True
            title = torrent_info[1].find(u'a').get(u'title')
            if re.match(u"\\<img    src='.*'\\>", title):
                title = torrent_info[1].find(u'b').text
                if title.endswith(u'...'):
                    url = (BASE_URL + torrent_info[1].find(u'a')[u'href'])
                    try:
                        request = self.get(url, {}, config[u'username'], config[u'password'])
                    except RequestException as e:
                        log.error(u'FileList.ro    request    failed:    %s', e)
                        continue
                    title_soup = get_soup(request.content)
                    title = title_soup.find(u'div', attrs={u'class': u'cblock-header'}).text
            e[u'title'] = title
            e[u'url'] = (((BASE_URL + torrent_info[3].find(u'a')[u'href']) + u'&passkey=') + config[u'passkey'])
            e[u'content_size'] = parse_filesize(torrent_info[6].find(u'font').text)
            e[u'torrent_snatches'] = int(torrent_info[7].find(u'font').text.replace(u'    ', u'').replace(u'times', u'').replace(u',', u''))
            e[u'torrent_seeds'] = int(torrent_info[8].find(u'span').text)
            e[u'torrent_leeches'] = int(torrent_info[9].find(u'span').text)
            e[u'torrent_internal'] = internal
            e[u'torrent_freeleech'] = freeleech
            if genres:
                e[u'torrent_genres'] = genres
            entries.add(e)
    return entries
