2016-11-29 04:16:16 +00:00
|
|
|
'''
|
|
|
|
Do not execute this file directly.
|
|
|
|
Use ycdl_launch.py to start the server with gevent.
|
|
|
|
'''
|
2017-10-09 04:39:07 +00:00
|
|
|
import logging
|
|
|
|
logging.getLogger('googleapicliet.discovery_cache').setLevel(logging.ERROR)
|
|
|
|
|
2016-11-29 04:16:16 +00:00
|
|
|
import datetime
|
|
|
|
import flask
|
|
|
|
from flask import request
|
|
|
|
import json
|
|
|
|
import mimetypes
|
|
|
|
import os
|
2017-10-09 04:39:07 +00:00
|
|
|
import traceback
|
2016-11-29 04:16:16 +00:00
|
|
|
|
|
|
|
import bot
|
|
|
|
import ycdl
|
|
|
|
|
2017-10-09 04:39:07 +00:00
|
|
|
from voussoirkit import pathclass
|
|
|
|
|
2018-12-18 03:17:53 +00:00
|
|
|
from . import jinja_filters
|
|
|
|
|
2017-10-09 04:39:07 +00:00
|
|
|
root_dir = pathclass.Path(__file__).parent.parent
|
|
|
|
|
|
|
|
TEMPLATE_DIR = root_dir.with_child('templates')
|
|
|
|
STATIC_DIR = root_dir.with_child('static')
|
|
|
|
FAVICON_PATH = STATIC_DIR.with_child('favicon.png')
|
|
|
|
|
2020-01-07 06:01:23 +00:00
|
|
|
youtube_core = ycdl.ytapi.Youtube(bot.get_youtube_key())
|
2016-11-29 04:16:16 +00:00
|
|
|
youtube = ycdl.YCDL(youtube_core)
|
|
|
|
|
2017-10-09 04:39:07 +00:00
|
|
|
site = flask.Flask(
|
|
|
|
__name__,
|
|
|
|
template_folder=TEMPLATE_DIR.absolute_path,
|
|
|
|
static_folder=STATIC_DIR.absolute_path,
|
|
|
|
)
|
2016-11-29 04:16:16 +00:00
|
|
|
site.config.update(
|
|
|
|
SEND_FILE_MAX_AGE_DEFAULT=180,
|
|
|
|
TEMPLATES_AUTO_RELOAD=True,
|
|
|
|
)
|
|
|
|
site.jinja_env.add_extension('jinja2.ext.do')
|
2018-12-18 03:17:53 +00:00
|
|
|
site.jinja_env.filters['seconds_to_hms'] = jinja_filters.seconds_to_hms
|
2016-11-29 04:16:16 +00:00
|
|
|
site.debug = True
|
|
|
|
|
|
|
|
####################################################################################################
|
|
|
|
####################################################################################################
|
|
|
|
####################################################################################################
|
|
|
|
####################################################################################################
|
|
|
|
|
|
|
|
def make_json_response(j, *args, **kwargs):
|
|
|
|
dumped = json.dumps(j)
|
|
|
|
response = flask.Response(dumped, *args, **kwargs)
|
|
|
|
response.headers['Content-Type'] = 'application/json;charset=utf-8'
|
|
|
|
return response
|
|
|
|
|
|
|
|
def send_file(filepath):
|
|
|
|
'''
|
|
|
|
Range-enabled file sending.
|
|
|
|
'''
|
|
|
|
try:
|
|
|
|
file_size = os.path.getsize(filepath)
|
|
|
|
except FileNotFoundError:
|
|
|
|
flask.abort(404)
|
|
|
|
|
|
|
|
outgoing_headers = {}
|
|
|
|
mimetype = mimetypes.guess_type(filepath)[0]
|
|
|
|
if mimetype is not None:
|
|
|
|
if 'text/' in mimetype:
|
|
|
|
mimetype += '; charset=utf-8'
|
|
|
|
outgoing_headers['Content-Type'] = mimetype
|
|
|
|
|
|
|
|
if 'range' in request.headers:
|
|
|
|
desired_range = request.headers['range'].lower()
|
|
|
|
desired_range = desired_range.split('bytes=')[-1]
|
|
|
|
|
|
|
|
int_helper = lambda x: int(x) if x.isdigit() else None
|
|
|
|
if '-' in desired_range:
|
|
|
|
(desired_min, desired_max) = desired_range.split('-')
|
|
|
|
range_min = int_helper(desired_min)
|
|
|
|
range_max = int_helper(desired_max)
|
|
|
|
else:
|
|
|
|
range_min = int_helper(desired_range)
|
|
|
|
|
|
|
|
if range_min is None:
|
|
|
|
range_min = 0
|
|
|
|
if range_max is None:
|
|
|
|
range_max = file_size
|
|
|
|
|
|
|
|
# because ranges are 0-indexed
|
|
|
|
range_max = min(range_max, file_size - 1)
|
|
|
|
range_min = max(range_min, 0)
|
|
|
|
|
|
|
|
range_header = 'bytes {min}-{max}/{outof}'.format(
|
|
|
|
min=range_min,
|
|
|
|
max=range_max,
|
|
|
|
outof=file_size,
|
|
|
|
)
|
|
|
|
outgoing_headers['Content-Range'] = range_header
|
|
|
|
status = 206
|
|
|
|
else:
|
|
|
|
range_max = file_size - 1
|
|
|
|
range_min = 0
|
|
|
|
status = 200
|
|
|
|
|
|
|
|
outgoing_headers['Accept-Ranges'] = 'bytes'
|
|
|
|
outgoing_headers['Content-Length'] = (range_max - range_min) + 1
|
|
|
|
|
|
|
|
if request.method == 'HEAD':
|
|
|
|
outgoing_data = bytes()
|
|
|
|
else:
|
2017-10-09 04:39:07 +00:00
|
|
|
outgoing_data = ycdl.helpers.read_filebytes(filepath, range_min=range_min, range_max=range_max)
|
2016-11-29 04:16:16 +00:00
|
|
|
|
|
|
|
response = flask.Response(
|
|
|
|
outgoing_data,
|
|
|
|
status=status,
|
|
|
|
headers=outgoing_headers,
|
|
|
|
)
|
|
|
|
return response
|
|
|
|
|
|
|
|
####################################################################################################
|
|
|
|
####################################################################################################
|
|
|
|
####################################################################################################
|
|
|
|
####################################################################################################
|
|
|
|
|
|
|
|
@site.route('/')
|
|
|
|
def root():
|
|
|
|
return flask.render_template('root.html')
|
|
|
|
|
|
|
|
@site.route('/favicon.ico')
|
|
|
|
@site.route('/favicon.png')
|
|
|
|
def favicon():
|
2017-10-09 04:39:07 +00:00
|
|
|
return flask.send_file(FAVICON_PATH.absolute_path)
|
|
|
|
|
2016-11-29 04:16:16 +00:00
|
|
|
|
|
|
|
@site.route('/channels')
|
|
|
|
def get_channels():
|
|
|
|
channels = youtube.get_channels()
|
|
|
|
for channel in channels:
|
|
|
|
channel['has_pending'] = youtube.channel_has_pending(channel['id'])
|
|
|
|
return flask.render_template('channels.html', channels=channels)
|
|
|
|
|
2017-05-21 20:51:15 +00:00
|
|
|
@site.route('/videos')
|
2017-10-09 04:39:07 +00:00
|
|
|
@site.route('/watch')
|
2017-05-21 20:51:15 +00:00
|
|
|
@site.route('/videos/<download_filter>')
|
2016-11-29 04:16:16 +00:00
|
|
|
@site.route('/channel/<channel_id>')
|
|
|
|
@site.route('/channel/<channel_id>/<download_filter>')
|
2017-05-21 20:51:15 +00:00
|
|
|
def get_channel(channel_id=None, download_filter=None):
|
|
|
|
if channel_id is not None:
|
2017-10-09 04:39:07 +00:00
|
|
|
try:
|
|
|
|
youtube.add_channel(channel_id)
|
|
|
|
except Exception:
|
|
|
|
traceback.print_exc()
|
2017-05-21 20:51:15 +00:00
|
|
|
channel = youtube.get_channel(channel_id)
|
|
|
|
else:
|
|
|
|
channel = None
|
|
|
|
|
2016-12-07 06:11:09 +00:00
|
|
|
videos = youtube.get_videos(channel_id=channel_id, download_filter=download_filter)
|
|
|
|
|
2017-10-09 04:39:07 +00:00
|
|
|
search_terms = request.args.get('q', '').lower().strip().replace('+', ' ').split()
|
|
|
|
if search_terms:
|
|
|
|
videos = [v for v in videos if all(term in v['title'].lower() for term in search_terms)]
|
|
|
|
|
|
|
|
video_id = request.args.get('v', '')
|
|
|
|
if video_id:
|
|
|
|
youtube.insert_video(video_id)
|
|
|
|
videos = [youtube.get_video(video_id)]
|
2017-05-21 20:51:15 +00:00
|
|
|
|
|
|
|
limit = request.args.get('limit', None)
|
|
|
|
if limit is not None:
|
|
|
|
try:
|
|
|
|
limit = int(limit)
|
|
|
|
videos = videos[:limit]
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
2016-11-29 04:16:16 +00:00
|
|
|
for video in videos:
|
|
|
|
published = video['published']
|
|
|
|
published = datetime.datetime.utcfromtimestamp(published)
|
|
|
|
published = published.strftime('%Y %m %d')
|
|
|
|
video['_published_str'] = published
|
2017-10-09 04:39:07 +00:00
|
|
|
return flask.render_template(
|
|
|
|
'channel.html',
|
|
|
|
channel=channel,
|
2019-01-25 23:54:31 +00:00
|
|
|
download_filter=download_filter,
|
2017-10-09 04:39:07 +00:00
|
|
|
videos=videos,
|
|
|
|
query_string='?' + request.query_string.decode('utf-8'),
|
|
|
|
)
|
2016-11-29 04:16:16 +00:00
|
|
|
|
|
|
|
@site.route('/mark_video_state', methods=['POST'])
|
|
|
|
def post_mark_video_state():
|
2019-01-24 05:22:09 +00:00
|
|
|
if 'video_ids' not in request.form or 'state' not in request.form:
|
2016-11-29 04:16:16 +00:00
|
|
|
flask.abort(400)
|
2019-01-24 05:22:09 +00:00
|
|
|
video_ids = request.form['video_ids']
|
2016-11-29 04:16:16 +00:00
|
|
|
state = request.form['state']
|
|
|
|
try:
|
2019-01-24 05:22:09 +00:00
|
|
|
video_ids = video_ids.split(',')
|
|
|
|
for video_id in video_ids:
|
|
|
|
youtube.mark_video_state(video_id, state, commit=False)
|
|
|
|
youtube.sql.commit()
|
2016-12-07 06:11:09 +00:00
|
|
|
|
|
|
|
except ycdl.NoSuchVideo:
|
2019-01-24 05:22:09 +00:00
|
|
|
youtube.rollback()
|
2018-12-18 03:17:53 +00:00
|
|
|
traceback.print_exc()
|
2016-11-29 04:16:16 +00:00
|
|
|
flask.abort(404)
|
2016-12-07 06:11:09 +00:00
|
|
|
|
|
|
|
except ycdl.InvalidVideoState:
|
2019-01-24 05:22:09 +00:00
|
|
|
youtube.rollback()
|
2016-11-29 04:16:16 +00:00
|
|
|
flask.abort(400)
|
2016-12-07 06:11:09 +00:00
|
|
|
|
2019-01-24 05:22:09 +00:00
|
|
|
return make_json_response({'video_ids': video_ids, 'state': state})
|
2016-11-29 04:16:16 +00:00
|
|
|
|
|
|
|
@site.route('/refresh_all_channels', methods=['POST'])
|
|
|
|
def post_refresh_all_channels():
|
|
|
|
force = request.form.get('force', False)
|
2017-10-09 04:39:07 +00:00
|
|
|
force = ycdl.helpers.truthystring(force)
|
2016-11-29 04:16:16 +00:00
|
|
|
youtube.refresh_all_channels(force=force)
|
|
|
|
return make_json_response({})
|
|
|
|
|
|
|
|
@site.route('/refresh_channel', methods=['POST'])
|
|
|
|
def post_refresh_channel():
|
|
|
|
if 'channel_id' not in request.form:
|
|
|
|
flask.abort(400)
|
|
|
|
channel_id = request.form['channel_id']
|
|
|
|
channel_id = channel_id.strip()
|
|
|
|
if not channel_id:
|
|
|
|
flask.abort(400)
|
2016-12-07 06:11:09 +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 = youtube.youtube.get_user_id(username=channel_id)
|
|
|
|
except IndexError:
|
|
|
|
flask.abort(404)
|
|
|
|
|
2016-11-29 04:16:16 +00:00
|
|
|
force = request.form.get('force', False)
|
2017-10-09 04:39:07 +00:00
|
|
|
force = ycdl.helpers.truthystring(force)
|
2016-11-29 04:16:16 +00:00
|
|
|
youtube.refresh_channel(channel_id, force=force)
|
|
|
|
return make_json_response({})
|
|
|
|
|
|
|
|
@site.route('/start_download', methods=['POST'])
|
|
|
|
def post_start_download():
|
2019-01-24 05:22:09 +00:00
|
|
|
if 'video_ids' not in request.form:
|
2016-11-29 04:16:16 +00:00
|
|
|
flask.abort(400)
|
2019-01-24 05:22:09 +00:00
|
|
|
video_ids = request.form['video_ids']
|
2016-12-07 06:11:09 +00:00
|
|
|
try:
|
2019-01-24 05:22:09 +00:00
|
|
|
video_ids = video_ids.split(',')
|
|
|
|
for video_id in video_ids:
|
|
|
|
youtube.download_video(video_id, commit=False)
|
|
|
|
youtube.sql.commit()
|
|
|
|
|
2017-10-09 04:39:07 +00:00
|
|
|
except ycdl.ytapi.VideoNotFound:
|
2019-01-24 05:22:09 +00:00
|
|
|
youtube.rollback()
|
2016-11-29 04:16:16 +00:00
|
|
|
flask.abort(404)
|
2016-12-07 06:11:09 +00:00
|
|
|
|
2019-01-24 05:22:09 +00:00
|
|
|
return make_json_response({'video_ids': video_ids, 'state': 'downloaded'})
|
2016-11-29 04:16:16 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
pass
|