2020-03-29 00:05:43 +00:00
|
|
|
import flask; from flask import request
|
2020-07-02 00:53:11 +00:00
|
|
|
import itertools
|
2020-03-29 00:05:43 +00:00
|
|
|
import traceback
|
|
|
|
|
2021-06-05 04:50:09 +00:00
|
|
|
from voussoirkit import flasktools
|
|
|
|
|
2020-03-29 00:05:43 +00:00
|
|
|
import ycdl
|
|
|
|
|
2020-05-22 03:04:02 +00:00
|
|
|
from .. import common
|
|
|
|
from .. import jsonify
|
2020-03-29 00:05:43 +00:00
|
|
|
|
|
|
|
site = common.site
|
|
|
|
|
2021-06-05 04:41:30 +00:00
|
|
|
@site.route('/all_channels.json')
|
|
|
|
def get_all_channel_names():
|
|
|
|
all_channels = {channel.id: channel.name for channel in common.ycdldb.get_channels()}
|
|
|
|
response = {'channels': all_channels}
|
2021-06-05 04:50:09 +00:00
|
|
|
return flasktools.make_json_response(response)
|
2021-06-05 04:41:30 +00:00
|
|
|
|
2020-03-29 00:05:43 +00:00
|
|
|
@site.route('/channels')
|
|
|
|
def get_channels():
|
|
|
|
channels = common.ycdldb.get_channels()
|
|
|
|
return flask.render_template('channels.html', channels=channels)
|
|
|
|
|
|
|
|
@site.route('/videos')
|
|
|
|
@site.route('/watch')
|
2020-09-04 22:55:48 +00:00
|
|
|
@site.route('/videos/<state>')
|
2020-03-29 00:05:43 +00:00
|
|
|
@site.route('/channel/<channel_id>')
|
2020-09-04 22:55:48 +00:00
|
|
|
@site.route('/channel/<channel_id>/<state>')
|
|
|
|
def get_channel(channel_id=None, state=None):
|
2020-03-29 00:05:43 +00:00
|
|
|
if channel_id is not None:
|
2020-11-16 05:10:01 +00:00
|
|
|
common.ycdldb.add_channel(channel_id)
|
2020-08-12 17:28:06 +00:00
|
|
|
try:
|
|
|
|
channel = common.ycdldb.get_channel(channel_id)
|
|
|
|
except ycdl.exceptions.NoSuchChannel:
|
|
|
|
flask.abort(404)
|
2020-03-29 00:05:43 +00:00
|
|
|
else:
|
|
|
|
channel = None
|
|
|
|
|
|
|
|
orderby = request.args.get('orderby', None)
|
|
|
|
|
|
|
|
video_id = request.args.get('v', '')
|
|
|
|
if video_id:
|
|
|
|
common.ycdldb.insert_video(video_id)
|
|
|
|
videos = [common.ycdldb.get_video(video_id)]
|
|
|
|
else:
|
|
|
|
videos = common.ycdldb.get_videos(
|
|
|
|
channel_id=channel_id,
|
2020-09-04 22:55:48 +00:00
|
|
|
state=state,
|
2020-03-29 00:05:43 +00:00
|
|
|
orderby=orderby,
|
|
|
|
)
|
|
|
|
|
|
|
|
search_terms = request.args.get('q', '').lower().strip().replace('+', ' ').split()
|
|
|
|
if search_terms:
|
2020-08-12 04:50:38 +00:00
|
|
|
videos = (v for v in videos if all(term in v.title.lower() for term in search_terms))
|
2020-03-29 00:05:43 +00:00
|
|
|
|
|
|
|
limit = request.args.get('limit', None)
|
|
|
|
if limit is not None:
|
|
|
|
try:
|
|
|
|
limit = int(limit)
|
2020-07-02 00:53:11 +00:00
|
|
|
videos = itertools.islice(videos, limit)
|
2020-03-29 00:05:43 +00:00
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
2020-07-02 00:53:11 +00:00
|
|
|
videos = list(videos)
|
|
|
|
|
2020-03-29 00:05:43 +00:00
|
|
|
all_states = common.ycdldb.get_all_states()
|
|
|
|
|
|
|
|
return flask.render_template(
|
|
|
|
'channel.html',
|
|
|
|
all_states=all_states,
|
|
|
|
channel=channel,
|
2020-09-04 22:55:48 +00:00
|
|
|
state=state,
|
2020-09-06 00:48:29 +00:00
|
|
|
orderby=orderby,
|
2020-03-29 00:05:43 +00:00
|
|
|
videos=videos,
|
|
|
|
)
|
|
|
|
|
2020-05-22 03:37:34 +00:00
|
|
|
@site.route('/add_channel', methods=['POST'])
|
|
|
|
def post_add_channel():
|
|
|
|
channel_id = request.form.get('channel_id', '')
|
2020-03-29 00:05:43 +00:00
|
|
|
channel_id = channel_id.strip()
|
|
|
|
if not channel_id:
|
|
|
|
flask.abort(400)
|
|
|
|
if not (len(channel_id) == 24 and channel_id.startswith('UC')):
|
|
|
|
# It seems they have given us a username instead.
|
|
|
|
try:
|
|
|
|
channel_id = common.ycdldb.youtube.get_user_id(username=channel_id)
|
2020-09-04 17:48:08 +00:00
|
|
|
except ycdl.ytapi.ChannelNotFound:
|
2021-06-05 04:50:09 +00:00
|
|
|
return flasktools.make_json_response({}, status=404)
|
2020-03-29 00:05:43 +00:00
|
|
|
|
2020-05-22 03:37:34 +00:00
|
|
|
channel = common.ycdldb.add_channel(channel_id, get_videos=True)
|
2021-06-05 04:50:09 +00:00
|
|
|
return flasktools.make_json_response(channel.jsonify())
|
2020-05-22 03:37:34 +00:00
|
|
|
|
2020-08-11 01:30:56 +00:00
|
|
|
@site.route('/channel/<channel_id>/delete', methods=['POST'])
|
|
|
|
def post_delete_channel(channel_id):
|
|
|
|
try:
|
|
|
|
channel = common.ycdldb.get_channel(channel_id)
|
|
|
|
except ycdl.exceptions.NoSuchChannel as exc:
|
2021-06-05 04:50:09 +00:00
|
|
|
return flasktools.make_json_response(exc.jsonify(), status=404)
|
2020-08-11 01:30:56 +00:00
|
|
|
|
|
|
|
channel.delete()
|
2021-06-05 04:50:09 +00:00
|
|
|
return flasktools.make_json_response({})
|
2020-08-11 01:30:56 +00:00
|
|
|
|
2020-05-22 03:37:34 +00:00
|
|
|
@site.route('/channel/<channel_id>/refresh', methods=['POST'])
|
|
|
|
def post_refresh_channel(channel_id):
|
2020-03-29 00:05:43 +00:00
|
|
|
force = request.form.get('force', False)
|
|
|
|
force = ycdl.helpers.truthystring(force)
|
2020-05-22 03:37:34 +00:00
|
|
|
try:
|
|
|
|
channel = common.ycdldb.get_channel(channel_id)
|
|
|
|
except ycdl.exceptions.NoSuchChannel as exc:
|
2021-06-05 04:50:09 +00:00
|
|
|
return flasktools.make_json_response(exc.jsonify(), status=404)
|
2020-05-22 03:37:34 +00:00
|
|
|
|
2020-04-04 22:13:01 +00:00
|
|
|
channel.refresh(force=force)
|
2021-06-05 04:50:09 +00:00
|
|
|
return flasktools.make_json_response(channel.jsonify())
|
2020-03-29 00:05:43 +00:00
|
|
|
|
2020-05-22 03:04:02 +00:00
|
|
|
@site.route('/refresh_all_channels', methods=['POST'])
|
|
|
|
def post_refresh_all_channels():
|
|
|
|
force = request.form.get('force', False)
|
|
|
|
force = ycdl.helpers.truthystring(force)
|
2021-04-22 04:31:03 +00:00
|
|
|
common.ycdldb.refresh_all_channels(force=force, skip_failures=True)
|
2021-06-05 04:50:09 +00:00
|
|
|
return flasktools.make_json_response({})
|
2020-05-22 03:04:02 +00:00
|
|
|
|
2020-05-22 00:28:34 +00:00
|
|
|
@site.route('/channel/<channel_id>/set_automark', methods=['POST'])
|
|
|
|
def post_set_automark(channel_id):
|
|
|
|
state = request.form['state']
|
|
|
|
channel = common.ycdldb.get_channel(channel_id)
|
|
|
|
|
|
|
|
try:
|
|
|
|
channel.set_automark(state)
|
2020-05-22 03:04:02 +00:00
|
|
|
except ycdl.exceptions.InvalidVideoState:
|
2020-05-22 00:28:34 +00:00
|
|
|
flask.abort(400)
|
|
|
|
|
2021-06-05 04:50:09 +00:00
|
|
|
return flasktools.make_json_response({})
|
2020-09-16 17:23:38 +00:00
|
|
|
|
|
|
|
@site.route('/channel/<channel_id>/set_queuefile_extension', methods=['POST'])
|
|
|
|
def post_set_queuefile_extension(channel_id):
|
|
|
|
extension = request.form['extension']
|
|
|
|
channel = common.ycdldb.get_channel(channel_id)
|
|
|
|
|
|
|
|
channel.set_queuefile_extension(extension)
|
|
|
|
|
2021-06-05 04:50:09 +00:00
|
|
|
return flasktools.make_json_response({})
|