<commit_before>from __future__ import unicode_literals, division, absolute_import

import copy
import logging

from flask import jsonify
from flask import request
from sqlalchemy.orm.exc import NoResultFound

from flexget.api import api, APIResource
from flexget.plugins.list import movie_list as ml
from flexget.utils.tools import split_title_year

log = logging.getLogger('movie_list')

movie_list_api = api.namespace('movie_list', description='Movie List operations')

default_error_schema = {
    'type': 'object',
    'properties': {
        'status': {'type': 'string'},
        'message': {'type': 'string'}
    }
}
empty_response = api.schema('empty', {'type': 'object'})

default_error_schema = api.schema('default_error_schema', default_error_schema)
empty_response = api.schema('empty_response', empty_response)

allowed_ids = ml.SUPPORTED_IDS

input_movie_list_id_object = {
    'type': 'array',
    'items': {
        'type': 'object',
        'minProperties': 1,
        'additionalProperties': True
    }
}

input_movie_entry = {
    'type': 'object',
    'properties': {
        'title': {'type': 'string'},
        'original_url': {'type': 'string'},
        'movie_name': {'type': 'string'},
        'movie_year': {'type': 'integer'},
        'movie_identifiers': input_movie_list_id_object
    },
    'additionalProperties': True,
    'required': ['original_url'],
    'anyOf': [
        {'required': ['title']},
        {'required': ['movie_name', 'movie_year']}
    ]
}

return_movie_list_id_object = copy.deepcopy(input_movie_list_id_object)
return_movie_list_id_object.update(
    {'properties': {
        'id': {'type': 'integer'},
        'added_on': {'type': 'string'},
        'movie_id': {'type': 'integer'}
    }})

movie_list_object = {
    'type': 'object',
    'properties': {
        'title': {'type': 'string'},
        'added_on': {'type': 'string'},
        'year': {'type': 'integer'},
        'list_id': {'type': 'integer'},
        'movie_list_ids': {
            'type': 'array',
            'items': return_movie_list_id_object
        },
    }
}

list_object = {
    'type': 'object',
    'properties': {
        'id': {'type': 'integer'},
        'added_on': {'type': 'string'},
        'name': {'type': 'string'}
    }
}

list_input = copy.deepcopy(list_object)
del list_input['properties']['id']
del list_input['properties']['added_on']

return_movies = {
    'type': 'object',
    'properties': {
        'movies': {
            'type': 'array',
            'items': movie_list_object
        },
        'number_of_movies': {'type': 'integer'}
    }
}
return_lists = {'type': 'array', 'items': list_object}

input_movie_entry_schema = api.schema('input_movie_entry', input_movie_entry)
input_movie_list_id_schema = api.schema('input_movie_list_id_object', input_movie_list_id_object)

movie_list_id_object_schema = api.schema('movie_list_id_object', return_movie_list_id_object)
movie_list_object_schema = api.schema('movie_list_object', movie_list_object)
list_object_schema = api.schema('list_object', list_object)
return_lists_schema = api.schema('return_lists', return_lists)
return_movies_schema = api.schema('return_movies', return_movies)

new_list_schema = api.schema('new_list', list_input)

movie_list_parser = api.parser()
movie_list_parser.add_argument('name', help='Filter results by list name')


@movie_list_api.route('/')
class MovieListAPI(APIResource):
    @api.response(200, model=return_lists_schema)
    @api.doc(parser=movie_list_parser)
    def get(self, session=None):
        """ Gets movies lists """
        args = movie_list_parser.parse_args()
        name = args.get('name')
        movie_lists = [movie_list.to_dict() for movie_list in ml.get_movie_lists(name=name, session=session)]
        return jsonify({'movie_lists': movie_lists})

    @api.validate(new_list_schema)
    @api.response(201, model=list_object_schema)
    @api.response(500, description='List already exist', model=default_error_schema)
    def post(self, session=None):
        """ Create a new list """
        data = request.json
        name = data.get('name')
        movie_list = ml.get_list_by_exact_name(name=name, session=session)
        if movie_list:
            return {'status': 'error',
                    'message': "list with name '%s' already exists" % name}, 500
        movie_list = ml.MovieListList(name=name)
        session.add(movie_list)
        session.commit()
        resp = jsonify(movie_list.to_dict())
        resp.status_code = 201
        return resp


@movie_list_api.route('/<int:list_id>/')
@api.doc(params={'list_id': 'ID of the list'})
class MovieListListAPI(APIResource):
    @api.response(404, model=default_error_schema)<commit_msg>Fixed found movie logic<commit_after>from __future__ import unicode_literals, division, absolute_import

import copy
import logging

from flask import jsonify
from flask import request
from sqlalchemy.orm.exc import NoResultFound

from flexget.api import api, APIResource
from flexget.plugins.list import movie_list as ml
from flexget.utils.tools import split_title_year

log = logging.getLogger('movie_list')

movie_list_api = api.namespace('movie_list', description='Movie List operations')

default_error_schema = {
    'type': 'object',
    'properties': {
        'status': {'type': 'string'},
        'message': {'type': 'string'}
    }
}
empty_response = api.schema('empty', {'type': 'object'})

default_error_schema = api.schema('default_error_schema', default_error_schema)
empty_response = api.schema('empty_response', empty_response)

allowed_ids = ml.SUPPORTED_IDS

input_movie_list_id_object = {
    'type': 'array',
    'items': {
        'type': 'object',
        'minProperties': 1,
        'additionalProperties': True
    }
}

input_movie_entry = {
    'type': 'object',
    'properties': {
        'title': {'type': 'string'},
        'original_url': {'type': 'string'},
        'movie_name': {'type': 'string'},
        'movie_year': {'type': 'integer'},
        'movie_identifiers': input_movie_list_id_object
    },
    'additionalProperties': True,
    'required': ['original_url'],
    'anyOf': [
        {'required': ['title']},
        {'required': ['movie_name', 'movie_year']}
    ]
}

return_movie_list_id_object = copy.deepcopy(input_movie_list_id_object)
return_movie_list_id_object.update(
    {'properties': {
        'id': {'type': 'integer'},
        'added_on': {'type': 'string'},
        'movie_id': {'type': 'integer'}
    }})

movie_list_object = {
    'type': 'object',
    'properties': {
        'title': {'type': 'string'},
        'added_on': {'type': 'string'},
        'year': {'type': 'integer'},
        'list_id': {'type': 'integer'},
        'movie_list_ids': {
            'type': 'array',
            'items': return_movie_list_id_object
        },
    }
}

list_object = {
    'type': 'object',
    'properties': {
        'id': {'type': 'integer'},
        'added_on': {'type': 'string'},
        'name': {'type': 'string'}
    }
}

list_input = copy.deepcopy(list_object)
del list_input['properties']['id']
del list_input['properties']['added_on']

return_movies = {
    'type': 'object',
    'properties': {
        'movies': {
            'type': 'array',
            'items': movie_list_object
        },
        'number_of_movies': {'type': 'integer'}
    }
}
return_lists = {'type': 'array', 'items': list_object}

input_movie_entry_schema = api.schema('input_movie_entry', input_movie_entry)
input_movie_list_id_schema = api.schema('input_movie_list_id_object', input_movie_list_id_object)

movie_list_id_object_schema = api.schema('movie_list_id_object', return_movie_list_id_object)
movie_list_object_schema = api.schema('movie_list_object', movie_list_object)
list_object_schema = api.schema('list_object', list_object)
return_lists_schema = api.schema('return_lists', return_lists)
return_movies_schema = api.schema('return_movies', return_movies)

new_list_schema = api.schema('new_list', list_input)

movie_list_parser = api.parser()
movie_list_parser.add_argument('name', help='Filter results by list name')


@movie_list_api.route('/')
class MovieListAPI(APIResource):
    @api.response(200, model=return_lists_schema)
    @api.doc(parser=movie_list_parser)
    def get(self, session=None):
        """ Gets movies lists """
        args = movie_list_parser.parse_args()
        name = args.get('name')
        movie_lists = [movie_list.to_dict() for movie_list in ml.get_movie_lists(name=name, session=session)]
        return jsonify({'movie_lists': movie_lists})

    @api.validate(new_list_schema)
    @api.response(201, model=list_object_schema)
    @api.response(500, description='List already exist', model=default_error_schema)
    def post(self, session=None):
        """ Create a new list """
        data = request.json
        name = data.get('name')
        try:
            movie_list = ml.get_list_by_exact_name(name=name, session=session)
        except NoResultFound:
            movie_list = None
        if movie_list:
            return {'status': 'error',
                    'message': "list with name '%s' already exists" % name}, 500
        movie_list = ml.MovieListList(name=name)
        session.add(movie_list)
        session.commit()
        resp = jsonify(movie_list.to_dict())
        resp.status_code = 201
        return resp


@movie_list_api.route('/<int:list_id>/')
@api.doc(params={'list_id': 'ID of the list'})
class MovieListListAPI(APIResource):
    @api.response(404, model=default_error_schema)
{'fraction_non_alphanumeric': 0.14807429950676881, 'fraction_numerical': 0.003987826634484206, 'mean_word_length': 3.74601593625498, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2993954', 'n_tokens_mistral': 3142, 'n_tokens_neox': 3018, 'n_words': 583}