diff --git a/frontends/ycdl_flask/backend/endpoints/video_endpoints.py b/frontends/ycdl_flask/backend/endpoints/video_endpoints.py index 225ac67..3eb5675 100644 --- a/frontends/ycdl_flask/backend/endpoints/video_endpoints.py +++ b/frontends/ycdl_flask/backend/endpoints/video_endpoints.py @@ -1,6 +1,7 @@ import flask; from flask import request from voussoirkit import flasktools +from voussoirkit import stringtools import ycdl @@ -12,18 +13,18 @@ site = common.site @site.route('/mark_video_state', methods=['POST']) def post_mark_video_state(): video_ids = request.form['video_ids'] + video_ids = stringtools.comma_space_split(video_ids) state = request.form['state'] - try: - video_ids = video_ids.split(',') - for video_id in video_ids: - video = common.ycdldb.get_video(video_id) - video.mark_state(state, commit=False) - common.ycdldb.commit() + try: + videos = [common.ycdldb.get_video(id) for id in video_ids] except ycdl.exceptions.NoSuchVideo as exc: - common.ycdldb.rollback() return flasktools.json_response(exc.jsonify(), status=404) + try: + for video in videos: + video.mark_state(state, commit=False) + common.ycdldb.commit() except ycdl.exceptions.InvalidVideoState as exc: common.ycdldb.rollback() return flasktools.json_response(exc.jsonify(), status=400) @@ -34,14 +35,15 @@ def post_mark_video_state(): @site.route('/start_download', methods=['POST']) def post_start_download(): video_ids = request.form['video_ids'] - try: - video_ids = video_ids.split(',') - for video_id in video_ids: - common.ycdldb.download_video(video_id, commit=False) - common.ycdldb.commit() + video_ids = stringtools.comma_space_split(video_ids) - except ycdl.ytapi.VideoNotFound: - common.ycdldb.rollback() - flask.abort(404) + try: + videos = [common.ycdldb.get_video(id) for id in video_ids] + except ycdl.exceptions.NoSuchVideo as exc: + return flasktools.json_response(exc.jsonify(), status=404) + + for video in videos: + common.ycdldb.download_video(video, commit=False) + common.ycdldb.commit() return flasktools.json_response({'video_ids': video_ids, 'state': 'downloaded'})