Add Channel.set_download_directory.

This commit is contained in:
voussoir 2021-09-01 23:37:43 -07:00
parent 38299db102
commit 2ad85ad69a
No known key found for this signature in database
GPG key ID: 5F7554F8C26DACCB
5 changed files with 87 additions and 5 deletions

View file

@ -2,6 +2,7 @@ import flask; from flask import request
import itertools
from voussoirkit import flasktools
from voussoirkit import pathclass
import ycdl
@ -157,6 +158,22 @@ def post_set_automark(channel_id):
return flasktools.make_json_response({})
@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:
channel.set_download_directory(download_directory)
except pathclass.NotDirectory:
exc = {
'error_type': 'NOT_DIRECTORY',
'error_message': f'"{download_directory}" is not a directory.',
}
return flasktools.make_json_response(exc, status=400)
return flasktools.make_json_response({})
@site.route('/channel/<channel_id>/set_queuefile_extension', methods=['POST'])
def post_set_queuefile_extension(channel_id):
extension = request.form['extension']

View file

@ -47,6 +47,15 @@ function set_automark(channel_id, state, callback)
return common.post(url, data, callback);
}
api.channels.set_download_directory =
function set_download_directory(channel_id, download_directory, callback)
{
const url = `/channel/${channel_id}/set_download_directory`;
const data = new FormData();
data.append("download_directory", download_directory);
return common.post(url, data, callback);
}
api.channels.set_queuefile_extension =
function set_queuefile_extension(channel_id, extension, callback)
{

View file

@ -274,6 +274,11 @@ https://stackoverflow.com/a/35153397
<button id="set_queuefile_extension_button" class="button_with_spinner" onclick="return set_queuefile_extension_form();">Set extension</button>
</div>
<div>
<input type="text" id="set_download_directory_input" placeholder="Queuefile directory" value="{{channel.download_directory or ''}}"/>
<button id="set_download_directory_button" class="button_with_spinner" onclick="return set_download_directory_form();">Set directory</button>
</div>
<div><a href="https://www.youtube.com/feeds/videos.xml?channel_id={{channel.id}}">Channel RSS</a></div>
<button class="red_button button_with_confirm"
@ -610,6 +615,21 @@ function set_automark_callback(response)
set_automark_spinner.hide();
}
function set_download_directory_form(event)
{
const download_directory = set_download_directory_input.value.trim();
api.channels.set_download_directory(CHANNEL_ID, download_directory, set_download_directory_callback);
}
function set_download_directory_callback(response)
{
if (response.meta.status != 200)
{
alert(JSON.stringify(response));
}
window[set_download_directory_button.dataset.spinnerCloser]();
}
function set_queuefile_extension_form(event)
{
const extension = set_queuefile_extension_input.value.trim();

View file

@ -1,6 +1,7 @@
import datetime
import typing
from voussoirkit import pathclass
from . import constants
from . import exceptions
@ -35,13 +36,38 @@ class Channel(Base):
self.id = db_row['id']
self.name = db_row['name']
self.uploads_playlist = db_row['uploads_playlist']
self.download_directory = db_row['download_directory']
self.download_directory = self.normalize_download_directory(
db_row['download_directory'],
do_assert=False,
)
self.queuefile_extension = self.normalize_queuefile_extension(db_row['queuefile_extension'])
self.automark = db_row['automark'] or 'pending'
def __repr__(self):
return f'Channel:{self.id}'
@staticmethod
def normalize_download_directory(
download_directory,
do_assert=True,
) -> typing.Optional[pathclass.Path]:
if download_directory is None:
return None
if not isinstance(download_directory, (str, pathclass.Path)):
raise TypeError(f'download_directory should be {str} or {pathclass.Path}, not {type(download_directory)}.')
if isinstance(download_directory, str):
download_directory = download_directory.strip()
if not download_directory:
return None
download_directory = pathclass.Path(download_directory)
if do_assert:
download_directory.assert_is_directory()
return download_directory
@staticmethod
def normalize_queuefile_extension(queuefile_extension) -> typing.Optional[str]:
if queuefile_extension is None:
@ -178,6 +204,18 @@ class Channel(Base):
if commit:
self.ycdldb.commit()
def set_download_directory(self, download_directory, commit=True):
download_directory = self.normalize_download_directory(download_directory)
pairs = {
'id': self.id,
'download_directory': download_directory.absolute_path if download_directory else None,
}
self.ycdldb.sql_update(table='channels', pairs=pairs, where_key='id')
self.download_directory = download_directory
if commit:
self.ycdldb.commit()
def set_queuefile_extension(self, queuefile_extension, commit=True):
queuefile_extension = self.normalize_queuefile_extension(queuefile_extension)

View file

@ -135,9 +135,7 @@ class YCDLDBChannelMixin:
if name is None:
name = self.youtube.get_user_name(channel_id)
if download_directory is not None:
download_directory = pathclass.Path(download_directory).absolute_path
download_directory = objects.Channel.normalize_download_directory(download_directory)
queuefile_extension = objects.Channel.normalize_queuefile_extension(queuefile_extension)
self.log.info('Adding channel %s %s', channel_id, name)
@ -146,7 +144,7 @@ class YCDLDBChannelMixin:
'id': channel_id,
'name': name,
'uploads_playlist': self.youtube.get_user_uploads_playlist_id(channel_id),
'download_directory': download_directory,
'download_directory': download_directory.absolute_path if download_directory else None,
'queuefile_extension': queuefile_extension,
'automark': "pending",
}