<commit_before># coding=utf-8
from __future__ import unicode_literals, division, absolute_import
import logging
import re
from flexget.config_schema import one_or_more
from flexget.manager import Session
from flexget.plugins.api_t411 import T411Proxy, FriendlySearchQuery, ApiError

from flexget import plugin
from flexget.event import event

log = logging.getLogger('t411_plugin')


def escape_query(query):
    """
    Escaping some expression Grey's -> Grey's + Greys + Grey, Marvel's ->Marvel's + Marvels + Marvel etc
    :param query str:
    :return:
    """
    queries = [query]
    short_query = re.sub("'", "", query)
    if query != short_query:
        queries.append(short_query)
        very_short_query = re.sub("'[a-z]", "", query)
        if short_query != very_short_query:
            queries.append(very_short_query)
    return queries


class T411InputPlugin(object):
    """T411 search/Input plugin.
    Before any usage, please add your credential with
    "flexget t411 add-auth <username> <password>"

    t411:
      category: <see available categories on "flexget t411 list-cats">
      terms: <see available terms on "flexget t411 list-terms --category <category name>"
      max_resutls: XXX
    """

    def __init__(self):
        self.schema = {
            'type': 'object',
            'properties': {
                'category': {'type': 'string'},
                'terms': one_or_more({'type': 'string'}),
                'max_results': {'type': 'number', 'default': 100}
            },
            'additionalProperties': False
        }

    @staticmethod
    def build_request_from(config):
        """
        Build a query from plugin config dict
        :param config: dict
        :return:
        """
        query = FriendlySearchQuery()
        query.category_name = config.get('category')
        query.term_names = config.get('terms', [])
        query.max_results = config.get('max_results')
        return query

    @plugin.internet(log)
    def on_task_input(self, task, config):
        proxy = T411Proxy()
        proxy.set_credential()
        query = T411InputPlugin.build_request_from(config)
        try:
            return proxy.search(query)
        except ApiError as e:
            log.warning("Server send an error message : %d - %s", e.code, e.message)
            return []

    @classmethod
    @plugin.internet(log)
    def search(cls, task, entry, config=None):
        proxy = T411Proxy()
        proxy.set_credential()

        query = T411InputPlugin.build_request_from(config)
        if entry.get('series_season'):
            query.add_season_term(entry['series_season'])
            query.add_episode_term(entry['series_episode'])
            search_strings = escape_query(entry['series_name'])
        else:
            search_strings = entry.get('search_strings', [entry['title']])
            search_strings = escape_query(search_strings)

        produced_entries = set()
        for search_string in search_strings:
            query.expression = search_string
            try:
                search_result = proxy.search(query)
            except ApiError as e:
                log.warning("Server send an error message : %d - %s", e.code, e.message)
            produced_entries.update(search_result)

        return produced_entries


class T411LookupPlugin(object):
    schema = {'type': 'string', 'enum': ['fill', 'override']}
<commit_msg>Fix search for non-series<commit_after># coding=utf-8
from __future__ import unicode_literals, division, absolute_import
import logging
import re
from flexget.config_schema import one_or_more
from flexget.manager import Session
from flexget.plugins.api_t411 import T411Proxy, FriendlySearchQuery, ApiError

from flexget import plugin
from flexget.event import event

log = logging.getLogger('t411_plugin')


def escape_query(search_strings):
    """
    Escaping some expression Grey's -> Grey's + Greys + Grey, Marvel's ->Marvel's + Marvels + Marvel etc
    :param query str[]:
    :return:
    """
    result = []
    for search_string in search_strings:
        result.append(search_string)
        short_query = re.sub("'", "", search_string)
        if search_string != short_query:
            result.append(short_query)
            very_short_query = re.sub("'[a-z]", "", search_string)
            if short_query != very_short_query:
                result.append(very_short_query)
    return result


class T411InputPlugin(object):
    """T411 search/Input plugin.
    Before any usage, please add your credential with
    "flexget t411 add-auth <username> <password>"

    t411:
      category: <see available categories on "flexget t411 list-cats">
      terms: <see available terms on "flexget t411 list-terms --category <category name>"
      max_resutls: XXX
    """

    def __init__(self):
        self.schema = {
            'type': 'object',
            'properties': {
                'category': {'type': 'string'},
                'terms': one_or_more({'type': 'string'}),
                'max_results': {'type': 'number', 'default': 100}
            },
            'additionalProperties': False
        }

    @staticmethod
    def build_request_from(config):
        """
        Build a query from plugin config dict
        :param config: dict
        :return:
        """
        query = FriendlySearchQuery()
        query.category_name = config.get('category')
        query.term_names = config.get('terms', [])
        query.max_results = config.get('max_results')
        return query

    @plugin.internet(log)
    def on_task_input(self, task, config):
        proxy = T411Proxy()
        proxy.set_credential()
        query = T411InputPlugin.build_request_from(config)
        try:
            return proxy.search(query)
        except ApiError as e:
            log.warning("Server send an error message : %d - %s", e.code, e.message)
            return []

    @classmethod
    @plugin.internet(log)
    def search(cls, entry=None, config=None, task=None):
        proxy = T411Proxy()
        proxy.set_credential()

        query = T411InputPlugin.build_request_from(config)
        if entry.get('series_season'):
            query.add_season_term(entry['series_season'])
            query.add_episode_term(entry['series_episode'])
            search_strings = escape_query([entry['series_name']])
        else:
            search_strings = entry.get('search_strings', [entry['title']])
            search_strings = escape_query(search_strings)

        produced_entries = set()
        for search_string in search_strings:
            query.expression = search_string
            try:
                search_result = proxy.search(query)
                produced_entries.update(search_result)
            except ApiError as e:
                log.warning("Server send an error message : %d - %s", e.code, e.message)

        return produced_entries


class T411LookupPlugin(object):
    schema = {'type': 'string', 'enum': ['fill', 'override']}

{'fraction_non_alphanumeric': 0.10976306023408507, 'fraction_numerical': 0.013131601484441906, 'mean_word_length': 2.522875816993464, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, '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': '1195501', 'n_tokens_mistral': 2065, 'n_tokens_neox': 1957, 'n_words': 489}