Add separate API endpoint for add_channel, apply to web ui.

Previously, the "add channel" box was just calling refresh, which
implicitly adds the channel. This adds a separate endpoint for
add_channel, and as a bonus the web ui will navigate you to the
channel after it has been loaded.
This commit is contained in:
voussoir 2020-05-21 20:37:34 -07:00
parent 480eeb9ac3
commit 3e27e6e2e8
3 changed files with 28 additions and 16 deletions

View file

@ -71,11 +71,9 @@ def get_channel(channel_id=None, download_filter=None):
videos=videos,
)
@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']
@site.route('/add_channel', methods=['POST'])
def post_add_channel():
channel_id = request.form.get('channel_id', '')
channel_id = channel_id.strip()
if not channel_id:
flask.abort(400)
@ -86,11 +84,20 @@ def post_refresh_channel():
except IndexError:
flask.abort(404)
channel = common.ycdldb.add_channel(channel_id, get_videos=True)
return jsonify.make_json_response(ycdl.jsonify.channel(channel))
@site.route('/channel/<channel_id>/refresh', methods=['POST'])
def post_refresh_channel(channel_id):
force = request.form.get('force', False)
force = ycdl.helpers.truthystring(force)
channel = common.ycdldb.add_channel(channel_id, commit=False)
try:
channel = common.ycdldb.get_channel(channel_id)
except ycdl.exceptions.NoSuchChannel as exc:
return jsonify.make_json_response(ycdl.jsonify.exception(exc), status=404)
channel.refresh(force=force)
return jsonify.make_json_response({})
return jsonify.make_json_response(ycdl.jsonify.channel(channel))
@site.route('/refresh_all_channels', methods=['POST'])
def post_refresh_all_channels():

View file

@ -483,16 +483,14 @@ function receive_action_response(response)
function refresh_channel(channel_id, force, callback)
{
var url = "/refresh_channel";
var url = `/channel/${channel_id}/refresh`;
data = new FormData();
data.append("channel_id", channel_id);
data.append("force", force)
return common.post(url, data, callback);
}
function set_automark_hook(event)
{
console.log("What");
var url = "/channel/{{channel.id}}/set_automark";
data = new FormData();
data.append("state", event.target.value);

View file

@ -52,7 +52,7 @@
<span><button class="refresh_button" onclick="refresh_all_channels(true, function(){location.reload()})">Refresh everything</button></span>
<div>
<input type="text" id="new_channel_textbox">
<button id="new_channel_button" onclick="_new_channel_submit()">Add new channel</button>
<button id="new_channel_button" onclick="add_channel_form(event)">Add new channel</button>
</div>
{% for channel in channels %}
{% if channel.has_pending() %}
@ -75,20 +75,27 @@ var box = document.getElementById('new_channel_textbox');
var button = document.getElementById('new_channel_button');
common.bind_box_to_button(box, button);
function _new_channel_submit()
function add_channel_form(event)
{
if (box.value !== "")
{
refresh_channel(box.value, true, function(){location.reload()});
add_channel(box.value, add_channel_callback);
}
}
function refresh_channel(channel_id, force, callback)
function add_channel_callback(response)
{
var url = "/refresh_channel";
if (response["meta"]["status"] == 200)
{
window.location.href = "/channel/" + response["data"]["id"];
}
}
function add_channel(channel_id, callback)
{
var url = "/add_channel";
data = new FormData();
data.append("channel_id", channel_id);
data.append("force", force)
return common.post(url, data, callback);
}