Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
movie = db.MovieListMovie()
movie.title = title
movie.year = year
movie.ids = db.get_db_movie_identifiers(identifier_list=movie_identifiers, session=session)
movie.list_id = list_id
session.add(movie)
session.commit()
response = jsonify(movie.to_dict())
response.status_code = 201
return response
@movie_list_api.route('//movies//')
@api.doc(params={'list_id': 'ID of the list', 'movie_id': 'ID of the movie'})
@api.response(NotFoundError)
class MovieListMovieAPI(APIResource):
@etag
@api.response(200, model=movie_list_object_schema)
def get(self, list_id, movie_id, session=None):
""" Get a movie by list ID and movie ID """
try:
movie = db.get_movie_by_id(list_id=list_id, movie_id=movie_id, session=session)
except NoResultFound:
raise NotFoundError('could not find movie with id %d in list %d' % (movie_id, list_id))
return jsonify(movie.to_dict())
@api.response(200, model=base_message_schema)
def delete(self, list_id, movie_id, session=None):
""" Delete a movie by list ID and movie ID """
try:
movie = db.get_movie_by_id(list_id=list_id, movie_id=movie_id, session=session)
except NoResultFound:
approved = args.get('approved')
deleted = session.query(db.PendingEntry)
if task_name:
deleted = deleted.filter(db.PendingEntry.task_name == task_name)
if approved:
deleted = deleted.filter(db.PendingEntry.approved == approved)
deleted = deleted.delete()
return success_response('deleted %s pending entries'.format(deleted))
@pending_api.route('//')
@api.doc(params={'entry_id': 'ID of the entry'})
@api.response(NotFoundError)
class PendingEntryAPI(APIResource):
@etag
@api.response(200, model=pending_entry_schema)
def get(self, entry_id, session=None):
"""Get a pending entry by ID"""
try:
entry = db.get_entry_by_id(session, entry_id)
except NoResultFound:
raise NotFoundError('No pending entry with ID %s' % entry_id)
return jsonify(entry.to_dict())
@api.response(201, model=pending_entry_schema)
@api.response(BadRequest)
@api.validate(operation_schema, description=description)
def put(self, entry_id, session=None):
"""Approve/Reject the status of a pending entry"""
try:
"""Returns status of IRC connections"""
from flexget.plugins.daemon.irc import irc_manager
if irc_manager is None:
raise BadRequest('IRC daemon does not appear to be running')
args = irc_parser.parse_args()
name = args.get('name')
try:
status = irc_manager.status(name)
except ValueError as e:
raise NotFoundError(e.args[0])
return jsonify(status)
@irc_api.route('/connections/enums/')
class IRCEnums(APIResource):
@api.response(200, model=empty_response)
def get(self, session=None):
"""Get channel status enumeration meaning"""
try:
from irc_bot import simple_irc_bot
except ImportError:
raise BadRequest('irc_bot dep is not installed')
return jsonify(simple_irc_bot.IRCChannelStatus().enum_dict)
@irc_api.route('/restart/')
@api.doc(parser=irc_parser)
class IRCRestart(APIResource):
@api.response(200, model=base_message_schema)
@api.response(NotFoundError)
@api.response(BadRequest)
help='Filter by maximal end date. Example: \'2012-01-01\'')
status_parser.add_argument('limit', default=100, type=int,
help='Limit return of executions per task, as that number can be huge')
def _task_info_dict(task):
return {
'id': int(task.id),
'name': task.name,
'current_phase': task.current_phase,
'current_plugin': task.current_plugin,
}
@tasks_api.route('/queue/')
class TaskQueueAPI(APIResource):
@api.response(200, model=task_api_queue_schema)
def get(self, session=None):
""" List task(s) in queue for execution """
tasks = [_task_info_dict(task) for task in self.manager.task_queue.run_queue.queue]
if self.manager.task_queue.current_task:
tasks.insert(0, _task_info_dict(self.manager.task_queue.current_task))
return jsonify(tasks)
class ExecuteLog(Queue):
""" Supports task log streaming by acting like a file object """
def write(self, s):
self.put(json.dumps({'log': s}))
""" Supports task log streaming by acting like a file object """
def write(self, s):
self.put(json.dumps({'log': s}))
_streams = {}
# Another namespace for the same endpoint
inject_api = api.namespace('inject', description='Entry injection API')
@inject_api.route('/params/')
@tasks_api.route('/execute/params/')
@api.doc(description='Available payload parameters for task execute')
class TaskExecutionParams(APIResource):
@etag(cache_age=3600)
@api.response(200, model=task_execution_params)
def get(self, session=None):
""" Execute payload parameters """
return jsonify(ObjectsContainer.task_execution_input)
@inject_api.route('/')
@tasks_api.route('/execute/')
@api.doc(description='For details on available parameters query /params/ endpoint')
class TaskExecutionAPI(APIResource):
@api.response(NotFoundError)
@api.response(BadRequest)
@api.response(200, model=task_api_execute_schema)
@api.validate(task_execution_schema)
def post(self, session=None):
raise NotFoundError(e.args[0])
return jsonify(series.to_dict())
episode_parser = api.parser()
episode_parser.add_argument('season_num', type=int, help='Season number')
episode_parser.add_argument('ep_num', type=int, help='Episode number')
episode_parser.add_argument(
'air_date', type=inputs.date_from_iso8601, help="Air date in the format of '2012-01-01'"
)
@tvmaze_api.route('/episode//')
@api.doc(params={'tvmaze_id': 'TVMaze ID of show'})
@api.doc(parser=episode_parser)
class TVDBEpisodeSearchAPI(APIResource):
@etag(cache_age=3600)
@api.response(200, 'Successfully found episode', tvmaze_episode_schema)
@api.response(NotFoundError)
@api.response(BadRequest)
def get(self, tvmaze_id, session=None):
"""TVMaze episode lookup"""
args = episode_parser.parse_args()
air_date = args.get('air_date')
season_num = args.get('season_num')
ep_num = args.get('ep_num')
kwargs = {'tvmaze_id': tvmaze_id, 'session': session}
if air_date:
kwargs['series_id_type'] = 'date'
kwargs['series_date'] = air_date
elif season_num and ep_num:
delete_parser = api.parser()
delete_parser.add_argument(
'forget',
type=inputs.boolean,
default=False,
help="Enabling this will fire a 'forget' event that will delete the downloaded releases "
"from the entire DB, enabling to re-download them",
)
@series_api.route('//')
@api.doc(params={'show_id': 'ID of the show'})
@api.response(NotFoundError)
class SeriesShowAPI(APIResource):
@etag
@api.response(200, 'Show information retrieved successfully', show_details_schema)
@api.doc(description='Get a specific show using its ID', parser=base_series_parser)
def get(self, show_id, session):
""" Get show details by ID """
try:
show = db.show_by_id(show_id, session=session)
except NoResultFound:
raise NotFoundError('Show with ID %s not found' % show_id)
args = series_list_parser.parse_args()
begin = args.get('begin')
latest = args.get('latest')
return jsonify(series_details(show, begin, latest))
"type": "object",
"properties": {
"tasks": {
"type": "array",
"items": task_api_schema
}
},
'additionalProperties': False
}
tasks_api_schema = api.schema('tasks', tasks_api_schema)
task_api_schema = api.schema('task', task_api_schema)
@tasks_api.route('/')
class TasksAPI(APIResource):
@api.response(200, 'list of tasks', tasks_api_schema)
def get(self, session=None):
""" Show all tasks """
tasks = []
for name, config in self.manager.user_config.get('tasks', {}).iteritems():
tasks.append({'name': name, 'config': config})
return {'tasks': tasks}
@api.validate(task_api_schema)
@api.response(201, 'newly created task', task_api_schema)
@api.response(409, 'task already exists', task_api_schema)
def post(self, session=None):
""" Add new task """
data = request.json
release_list_parser = api.parser()
release_list_parser.add_argument('downloaded', choices=('downloaded', 'not_downloaded', 'all'), default='all',
help='Filter between release status')
@api.response(404, 'Show ID not found', default_error_schema)
@api.response(414, 'Episode ID not found', default_error_schema)
@api.response(400, 'Episode with ep_ids does not belong to show with show_id', default_error_schema)
@series_api.route('//episodes//releases')
@api.doc(params={'show_id': 'ID of the show', 'ep_id': 'Episode ID'},
parser=release_list_parser,
description='Use this endpoint to get or delete all information about seen releases for a specific episode of'
' a show. Deleting releases will trigger flexget to re-download an episode if a matching release'
' will be seen again for it.')
class SeriesReleasesAPI(APIResource):
@api.response(200, 'Releases retrieved successfully for episode', release_list_schema)
def get(self, show_id, ep_id, session):
""" Get all episodes releases by show ID and episode ID """
try:
show = series.show_by_id(show_id, session=session)
except NoResultFound:
return {'status': 'error',
'message': 'Show with ID %s not found' % show_id
}, 404
try:
episode = series.episode_by_id(ep_id, session)
except NoResultFound:
return {'status': 'error',
'message': 'Episode with ID %s not found' % ep_id
}, 414
if not series.episode_in_show(show_id, ep_id):
'year': {'type': 'number'},
'thumbnail': {'type': 'string'},
},
'required': ['imdb_id', 'match', 'name', 'url', 'year'],
'additionalProperties': False,
}
return_object = {'type': 'array', 'items': movie_object}
return_schema = api.schema_model('imdb_search_schema', ObjectsContainer.return_object)
@imdb_api.route('/search//')
@api.doc(params={'title': 'Movie name or IMDB ID'})
class IMDBMovieSearch(APIResource):
# noinspection PyUnusedLocal
@etag
@api.response(200, model=return_schema)
def get(self, title, session=None):
""" Get a list of IMDB search result by name or ID"""
raw_movies = ImdbSearch().smart_match(title, single_match=False)
if not raw_movies:
return jsonify([])
# Convert single movie to list to preserve consistent reply
if not isinstance(raw_movies, list):
raw_movies = [raw_movies]
return jsonify(raw_movies)