<commit_before>from __future__ import unicode_literals, division, absolute_import
from builtins import *  # pylint: disable=unused-import, redefined-builtin

import logging

from flexget import plugin
from flexget.entry import Entry
from flexget.event import event
from flexget.utils.cached_input import cached
from flexget.utils.soup import get_soup

log = logging.getLogger('horriblesubs')


class HorribleSubs(object):
    """
    Give latest horriblesubs releases
    """
    schema = {'type': 'boolean'}

    @staticmethod
    def horrible_entries(requests, page_url):
        entries = []
        soup = get_soup(requests.get(page_url).content)
        
        for td_label in soup.findAll('td', attrs={'class': 'dl-label'}):
            title = '[HorribleSubs] {0}'.format(str(td_label.find('i').string))
            urls = []
            log.debug('Found title `{0}`'.format(title))
            for span in td_label.parent.findAll('span', attrs={'class': 'dl-link'}):
                # skip non torrent based links
                if 'hs-ddl-link' in span.parent.attrs['class']:
                    continue
                url = str(span.find('a').attrs['href'])
                log.debug('Found url `{0}`'.format(url))
                urls.append(url)
            # move magnets to last, a bit hacky
            for url in urls[:]:
                if url.startswith('magnet'):
                    urls.remove(url)
                    urls.append(url)
            entries.append(Entry(title=title, url=urls[0], urls=urls))
        return entries

    @cached('horriblesubs')
    def on_task_input(self, task, config):
        if not config:
            return
        return HorribleSubs.horrible_entries(
            task.requests, 'http://horriblesubs.info/lib/latest.php')

    def search(self, task, entry, config):
        if not config:
            return
        entries = []
        for search_string in entry.get('search_strings', [entry['title']]):
            log.debug('Searching `{0}`'.format(search_string))
            results = HorribleSubs.horrible_entries(
                task.requests, 'http://horriblesubs.info/lib/search.php?value={0}'.format(search_string))
            entries.extend(results)
        return entries


@event('plugin.register')
def register_plugin():
    plugin.register(HorribleSubs, 'horriblesubs', groups=['search'], api_ver=2)
<commit_msg>Wrapped the page request for RequestException<commit_after>from __future__ import unicode_literals, division, absolute_import
from builtins import *  # pylint: disable=unused-import, redefined-builtin

import logging

from flexget import plugin
from flexget.entry import Entry
from flexget.event import event
from flexget.utils.cached_input import cached
from flexget.utils.requests import RequestException
from flexget.utils.soup import get_soup

log = logging.getLogger('horriblesubs')


class HorribleSubs(object):
    """
    Give latest horriblesubs releases
    """
    schema = {'type': 'boolean'}

    @staticmethod
    def horrible_entries(requests, page_url):
        entries = []

        try:
            soup = get_soup(requests.get(page_url).content)
            log.debug('requesting: %s', page_url)
        except RequestException as e:
            log.error('HorribleSubs request failed: %s', e)

        for td_label in soup.findAll('td', attrs={'class': 'dl-label'}):
            title = '[HorribleSubs] {0}'.format(str(td_label.find('i').string))
            urls = []
            log.debug('Found title `{0}`'.format(title))
            for span in td_label.parent.findAll('span', attrs={'class': 'dl-link'}):
                # skip non torrent based links
                if 'hs-ddl-link' in span.parent.attrs['class']:
                    continue
                url = str(span.find('a').attrs['href'])
                log.debug('Found url `{0}`'.format(url))
                urls.append(url)
            # move magnets to last, a bit hacky
            for url in urls[:]:
                if url.startswith('magnet'):
                    urls.remove(url)
                    urls.append(url)
            entries.append(Entry(title=title, url=urls[0], urls=urls))
        return entries

    @cached('horriblesubs')
    def on_task_input(self, task, config):
        if not config:
            return
        return HorribleSubs.horrible_entries(
            task.requests, 'http://horriblesubs.info/lib/latest.php')

    def search(self, task, entry, config):
        if not config:
            return
        entries = []
        for search_string in entry.get('search_strings', [entry['title']]):
            log.debug('Searching `{0}`'.format(search_string))
            results = HorribleSubs.horrible_entries(
                task.requests, 'http://horriblesubs.info/lib/search.php?value={0}'.format(search_string))
            entries.extend(results)
        return entries


@event('plugin.register')
def register_plugin():
    plugin.register(HorribleSubs, 'horriblesubs', groups=['search'], api_ver=2)

{'fraction_non_alphanumeric': 0.12330946698488465, 'fraction_numerical': 0.0027844073190135244, 'mean_word_length': 2.463498622589532, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2961516', 'n_tokens_mistral': 1481, 'n_tokens_neox': 1450, 'n_words': 324}