2020-03-29 00:05:43 +00:00
|
|
|
import flask; from flask import request
|
2020-07-02 00:53:11 +00:00
|
|
|
import itertools
|
2022-03-15 20:53:40 +00:00
|
|
|
import os
|
|
|
|
import subprocess
|
2021-11-11 07:40:11 +00:00
|
|
|
import time
|
2020-03-29 00:05:43 +00:00
|
|
|
|
2021-06-05 04:50:09 +00:00
|
|
|
from voussoirkit import flasktools
|
2021-09-02 06:37:43 +00:00
|
|
|
from voussoirkit import pathclass
|
2021-09-05 08:22:21 +00:00
|
|
|
from voussoirkit import stringtools
|
2021-06-05 04:50:09 +00:00
|
|
|
|
2020-03-29 00:05:43 +00:00
|
|
|
import ycdl
|
|
|
|
|
2020-05-22 03:04:02 +00:00
|
|
|
from .. import common
|
2020-03-29 00:05:43 +00:00
|
|
|
|
|
|
|
site = common.site
|
|
|
|
|
2021-08-26 00:33:19 +00:00
|
|
|
def _get_or_insert_video(video_id):
|
|
|
|
try:
|
|
|
|
video = common.ycdldb.get_video(video_id)
|
|
|
|
except ycdl.exceptions.NoSuchVideo:
|
2022-07-16 05:30:06 +00:00
|
|
|
with common.ycdldb.transaction:
|
|
|
|
video = common.ycdldb.insert_video(video_id)['video']
|
2021-08-26 00:33:19 +00:00
|
|
|
return video
|
|
|
|
|
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-10-02 06:04:41 +00:00
|
|
|
return flasktools.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()
|
2023-06-24 19:01:11 +00:00
|
|
|
return common.render_template(request, 'channels.html', channels=channels)
|
2020-03-29 00:05:43 +00:00
|
|
|
|
2021-08-26 00:33:19 +00:00
|
|
|
def _render_videos_listing(videos, channel, state, orderby):
|
2020-03-29 00:05:43 +00:00
|
|
|
search_terms = request.args.get('q', '').lower().strip().replace('+', ' ').split()
|
|
|
|
if search_terms:
|
2021-08-26 00:33:19 +00:00
|
|
|
lowered = ((video, video.title.lower()) for video in videos)
|
|
|
|
videos = (
|
|
|
|
video for (video, title) in lowered
|
|
|
|
if all(term in title 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
|
|
|
|
|
2021-08-26 00:33:19 +00:00
|
|
|
if not isinstance(videos, list):
|
|
|
|
videos = list(videos)
|
2020-07-02 00:53:11 +00:00
|
|
|
|
2020-03-29 00:05:43 +00:00
|
|
|
all_states = common.ycdldb.get_all_states()
|
|
|
|
|
2023-06-24 19:01:11 +00:00
|
|
|
return common.render_template(
|
|
|
|
request,
|
2020-03-29 00:05:43 +00:00
|
|
|
'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,
|
|
|
|
)
|
|
|
|
|
2021-08-26 00:33:19 +00:00
|
|
|
@site.route('/channel/<channel_id>')
|
|
|
|
@site.route('/channel/<channel_id>/<state>')
|
|
|
|
def get_channel(channel_id, state=None):
|
|
|
|
try:
|
2022-07-16 05:30:06 +00:00
|
|
|
channel = common.ycdldb.get_channel(channel_id)
|
|
|
|
except ycdl.exceptions.NoSuchChannel:
|
|
|
|
try:
|
|
|
|
with common.ycdldb.transaction:
|
|
|
|
channel = common.ycdldb.add_channel(channel_id)
|
|
|
|
except ycdl.ytapi.ChannelNotFound:
|
|
|
|
flask.abort(404)
|
2021-08-26 00:33:19 +00:00
|
|
|
|
|
|
|
orderby = request.args.get('orderby', None)
|
|
|
|
|
|
|
|
videos = common.ycdldb.get_videos(
|
|
|
|
channel_id=channel.id,
|
|
|
|
orderby=orderby,
|
|
|
|
state=state,
|
|
|
|
)
|
|
|
|
return _render_videos_listing(videos, channel=channel, state=state, orderby=orderby)
|
|
|
|
|
|
|
|
@site.route('/videos')
|
|
|
|
@site.route('/videos/<state>')
|
|
|
|
def get_videos(state=None):
|
|
|
|
orderby = request.args.get('orderby', None)
|
|
|
|
|
|
|
|
videos = common.ycdldb.get_videos(
|
|
|
|
orderby=orderby,
|
|
|
|
state=state,
|
|
|
|
)
|
|
|
|
return _render_videos_listing(videos, channel=None, state=state, orderby=orderby)
|
|
|
|
|
|
|
|
@site.route('/watch')
|
|
|
|
def get_watch():
|
|
|
|
video_id = request.args.get('v', '')
|
|
|
|
if not video_id:
|
|
|
|
return flask.redirect('/')
|
|
|
|
|
|
|
|
try:
|
|
|
|
video = _get_or_insert_video(video_id)
|
|
|
|
except ycdl.ytapi.VideoNotFound:
|
|
|
|
flask.abort(404)
|
|
|
|
|
|
|
|
videos = [video]
|
|
|
|
return _render_videos_listing(videos, channel=None, state=None, orderby=None)
|
|
|
|
|
2021-10-26 19:18:55 +00:00
|
|
|
@flasktools.required_fields(['channel_id'], forbid_whitespace=True)
|
2020-05-22 03:37:34 +00:00
|
|
|
@site.route('/add_channel', methods=['POST'])
|
|
|
|
def post_add_channel():
|
2021-10-29 06:30:38 +00:00
|
|
|
channel_id = request.form['channel_id']
|
2020-03-29 00:05:43 +00:00
|
|
|
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-10-02 06:04:41 +00:00
|
|
|
return flasktools.json_response({}, status=404)
|
2020-03-29 00:05:43 +00:00
|
|
|
|
2022-07-16 05:30:06 +00:00
|
|
|
with common.ycdldb.transaction:
|
|
|
|
channel = common.ycdldb.add_channel(channel_id, get_videos=True)
|
2021-10-02 06:04:41 +00:00
|
|
|
return flasktools.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-10-02 06:04:41 +00:00
|
|
|
return flasktools.json_response(exc.jsonify(), status=404)
|
2020-08-11 01:30:56 +00:00
|
|
|
|
2022-07-16 05:30:06 +00:00
|
|
|
with common.ycdldb.transaction:
|
|
|
|
channel.delete()
|
2021-10-26 19:03:40 +00:00
|
|
|
response = {'id': channel.id, 'deleted': channel.deleted}
|
|
|
|
return flasktools.json_response(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)
|
2021-09-05 08:22:21 +00:00
|
|
|
force = stringtools.truthystring(force, False)
|
2020-05-22 03:37:34 +00:00
|
|
|
try:
|
|
|
|
channel = common.ycdldb.get_channel(channel_id)
|
|
|
|
except ycdl.exceptions.NoSuchChannel as exc:
|
2021-10-02 06:04:41 +00:00
|
|
|
return flasktools.json_response(exc.jsonify(), status=404)
|
2020-05-22 03:37:34 +00:00
|
|
|
|
2022-07-16 05:30:06 +00:00
|
|
|
with common.ycdldb.transaction:
|
|
|
|
channel.refresh(force=force)
|
2021-10-02 06:04:41 +00:00
|
|
|
return flasktools.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)
|
2021-09-05 08:22:21 +00:00
|
|
|
force = stringtools.truthystring(force, False)
|
2022-07-16 05:30:06 +00:00
|
|
|
with common.ycdldb.transaction:
|
|
|
|
common.ycdldb.refresh_all_channels(force=force, skip_failures=True)
|
2021-11-11 07:40:11 +00:00
|
|
|
common.last_refresh = time.time()
|
2021-10-02 06:04:41 +00:00
|
|
|
return flasktools.json_response({})
|
2020-05-22 03:04:02 +00:00
|
|
|
|
2021-10-26 18:57:55 +00:00
|
|
|
@flasktools.required_fields(['state'], forbid_whitespace=True)
|
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:
|
2022-07-16 05:30:06 +00:00
|
|
|
with common.ycdldb.transaction:
|
|
|
|
channel.set_automark(state)
|
2021-10-26 19:18:55 +00:00
|
|
|
except ycdl.exceptions.InvalidVideoState as exc:
|
|
|
|
return flasktools.json_response(exc.jsonify(), status=400)
|
2020-05-22 00:28:34 +00:00
|
|
|
|
2021-10-26 19:03:40 +00:00
|
|
|
response = {'id': channel.id, 'automark': channel.automark}
|
|
|
|
return flasktools.json_response(response)
|
2020-09-16 17:23:38 +00:00
|
|
|
|
2021-09-05 08:26:34 +00:00
|
|
|
@flasktools.required_fields(['autorefresh'], forbid_whitespace=True)
|
|
|
|
@site.route('/channel/<channel_id>/set_autorefresh', methods=['POST'])
|
|
|
|
def post_set_autorefresh(channel_id):
|
|
|
|
autorefresh = request.form['autorefresh']
|
|
|
|
channel = common.ycdldb.get_channel(channel_id)
|
|
|
|
|
|
|
|
try:
|
|
|
|
autorefresh = stringtools.truthystring(autorefresh)
|
2022-07-16 05:30:06 +00:00
|
|
|
with common.ycdldb.transaction:
|
|
|
|
channel.set_autorefresh(autorefresh)
|
2021-09-05 08:26:34 +00:00
|
|
|
except (ValueError, TypeError):
|
|
|
|
flask.abort(400)
|
|
|
|
|
2021-10-26 19:03:40 +00:00
|
|
|
response = {'id': channel.id, 'autorefresh': channel.autorefresh}
|
|
|
|
return flasktools.json_response(response)
|
2021-09-05 08:26:34 +00:00
|
|
|
|
2021-10-26 18:57:55 +00:00
|
|
|
@flasktools.required_fields(['download_directory'], forbid_whitespace=False)
|
2021-09-02 06:37:43 +00:00
|
|
|
@site.route('/channel/<channel_id>/set_download_directory', methods=['POST'])
|
|
|
|
def post_set_download_directory(channel_id):
|
|
|
|
download_directory = request.form['download_directory']
|
|
|
|
channel = common.ycdldb.get_channel(channel_id)
|
|
|
|
|
|
|
|
try:
|
2022-07-16 05:30:06 +00:00
|
|
|
with common.ycdldb.transaction:
|
|
|
|
channel.set_download_directory(download_directory)
|
2021-09-02 06:37:43 +00:00
|
|
|
except pathclass.NotDirectory:
|
|
|
|
exc = {
|
|
|
|
'error_type': 'NOT_DIRECTORY',
|
|
|
|
'error_message': f'"{download_directory}" is not a directory.',
|
|
|
|
}
|
2021-10-02 06:04:41 +00:00
|
|
|
return flasktools.json_response(exc, status=400)
|
2021-09-02 06:37:43 +00:00
|
|
|
|
2021-09-16 01:55:14 +00:00
|
|
|
abspath = channel.download_directory.absolute_path if channel.download_directory else None
|
2021-10-26 19:03:40 +00:00
|
|
|
response = {'id': channel.id, 'download_directory': abspath}
|
2021-10-02 06:04:41 +00:00
|
|
|
return flasktools.json_response(response)
|
2021-09-02 06:37:43 +00:00
|
|
|
|
2023-08-10 06:24:47 +00:00
|
|
|
@flasktools.required_fields(['ignore_shorts'], forbid_whitespace=True)
|
|
|
|
@site.route('/channel/<channel_id>/set_ignore_shorts', methods=['POST'])
|
|
|
|
def post_set_ignore_shorts(channel_id):
|
|
|
|
ignore_shorts = request.form['ignore_shorts']
|
|
|
|
channel = common.ycdldb.get_channel(channel_id)
|
|
|
|
|
|
|
|
try:
|
|
|
|
ignore_shorts = stringtools.truthystring(ignore_shorts)
|
|
|
|
with common.ycdldb.transaction:
|
|
|
|
channel.set_ignore_shorts(ignore_shorts)
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
flask.abort(400)
|
|
|
|
|
|
|
|
response = {'id': channel.id, 'ignore_shorts': channel.ignore_shorts}
|
|
|
|
return flasktools.json_response(response)
|
|
|
|
|
2021-10-26 18:53:49 +00:00
|
|
|
@flasktools.required_fields(['name'], forbid_whitespace=False)
|
|
|
|
@site.route('/channel/<channel_id>/set_name', methods=['POST'])
|
|
|
|
def post_set_name(channel_id):
|
|
|
|
name = request.form['name']
|
|
|
|
channel = common.ycdldb.get_channel(channel_id)
|
|
|
|
|
2022-07-16 05:30:06 +00:00
|
|
|
with common.ycdldb.transaction:
|
|
|
|
channel.set_name(name)
|
2021-10-26 18:53:49 +00:00
|
|
|
|
2021-10-26 19:03:40 +00:00
|
|
|
response = {'id': channel.id, 'name': channel.name}
|
2021-10-26 18:53:49 +00:00
|
|
|
return flasktools.json_response(response)
|
|
|
|
|
2021-10-26 18:57:55 +00:00
|
|
|
@flasktools.required_fields(['extension'], forbid_whitespace=False)
|
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)
|
|
|
|
|
2022-07-16 05:30:06 +00:00
|
|
|
with common.ycdldb.transaction:
|
|
|
|
channel.set_queuefile_extension(extension)
|
2020-09-16 17:23:38 +00:00
|
|
|
|
2021-10-26 19:03:40 +00:00
|
|
|
response = {'id': channel.id, 'queuefile_extension': channel.queuefile_extension}
|
2021-10-02 06:04:41 +00:00
|
|
|
return flasktools.json_response(response)
|
2022-03-15 20:53:40 +00:00
|
|
|
|
|
|
|
@site.route('/channel/<channel_id>/show_download_directory', methods=['POST'])
|
|
|
|
def post_show_download_directory(channel_id):
|
|
|
|
if not request.is_localhost:
|
|
|
|
flask.abort(403)
|
|
|
|
|
|
|
|
channel = common.ycdldb.get_channel(channel_id)
|
|
|
|
if channel.download_directory:
|
|
|
|
abspath = channel.download_directory.absolute_path
|
|
|
|
else:
|
|
|
|
abspath = common.ycdldb.config['download_directory']
|
|
|
|
|
|
|
|
if not os.path.exists(abspath):
|
|
|
|
return flask.abort(400)
|
|
|
|
|
|
|
|
if os.name == 'nt':
|
|
|
|
command = f'explorer.exe "{abspath}"'
|
|
|
|
subprocess.Popen(command, shell=True)
|
|
|
|
return flasktools.json_response({})
|
|
|
|
else:
|
|
|
|
command = ['xdg-open', abspath]
|
|
|
|
subprocess.Popen(command, shell=True)
|
|
|
|
return flasktools.json_response({})
|
|
|
|
|
2022-03-25 02:35:21 +00:00
|
|
|
flask.abort(501)
|