Convert comment posting system to flask framework
This commit is contained in:
parent
167483af21
commit
fc295ac93d
@ -6,7 +6,7 @@ from youtube import yt_app
|
|||||||
from youtube import util
|
from youtube import util
|
||||||
|
|
||||||
# these are just so the files get run - they import yt_app and add routes to it
|
# these are just so the files get run - they import yt_app and add routes to it
|
||||||
from youtube import watch, search, playlist, channel, local_playlist, comments
|
from youtube import watch, search, playlist, channel, local_playlist, comments, post_comment
|
||||||
|
|
||||||
import settings
|
import settings
|
||||||
|
|
||||||
|
@ -258,11 +258,16 @@ def get_comments_page():
|
|||||||
comments_info['comment_links'] = [(other_sort_text, other_sort_url)]
|
comments_info['comment_links'] = [(other_sort_text, other_sort_url)]
|
||||||
|
|
||||||
|
|
||||||
|
comment_posting_box_info = {
|
||||||
|
'form_action': '' if replies else util.URL_ORIGIN + '/post_comment',
|
||||||
|
'video_id': comments_info['video_id'],
|
||||||
|
'accounts': accounts.account_list_data(),
|
||||||
|
'include_video_id_input': not replies,
|
||||||
|
'replying': replies,
|
||||||
|
}
|
||||||
|
|
||||||
return flask.render_template('comments_page.html',
|
return flask.render_template('comments_page.html',
|
||||||
comments_info = comments_info,
|
comments_info = comments_info,
|
||||||
|
comment_posting_box_info = comment_posting_box_info,
|
||||||
form_action = '' if replies else util.URL_ORIGIN + '/post_comment',
|
|
||||||
include_video_id_input = not replies,
|
|
||||||
accounts = accounts.account_list_data(),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
# Contains functions having to do with posting/editing/deleting comments
|
# Contains functions having to do with posting/editing/deleting comments
|
||||||
from youtube import util, html_common, proto, comments, accounts
|
from youtube import util, proto, comments, accounts
|
||||||
|
from youtube import yt_app
|
||||||
import settings
|
import settings
|
||||||
|
|
||||||
import urllib
|
import urllib
|
||||||
@ -8,6 +9,9 @@ import re
|
|||||||
import traceback
|
import traceback
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
import flask
|
||||||
|
from flask import request
|
||||||
|
|
||||||
def _post_comment(text, video_id, session_token, cookiejar):
|
def _post_comment(text, video_id, session_token, cookiejar):
|
||||||
headers = {
|
headers = {
|
||||||
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1',
|
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1',
|
||||||
@ -109,108 +113,73 @@ def get_session_token(video_id, cookiejar):
|
|||||||
else:
|
else:
|
||||||
raise Exception("Couldn't find xsrf_token")
|
raise Exception("Couldn't find xsrf_token")
|
||||||
|
|
||||||
def delete_comment(env, start_response):
|
@yt_app.route('/delete_comment', methods=['POST'])
|
||||||
parameters = env['parameters']
|
def delete_comment():
|
||||||
video_id = parameters['video_id'][0]
|
video_id = request.values['video_id']
|
||||||
cookiejar = accounts.account_cookiejar(parameters['channel_id'][0])
|
cookiejar = accounts.account_cookiejar(request.values['channel_id'])
|
||||||
token = get_session_token(video_id, cookiejar)
|
token = get_session_token(video_id, cookiejar)
|
||||||
|
|
||||||
code = _delete_comment(video_id, parameters['comment_id'][0], parameters['author_id'][0], token, cookiejar)
|
code = _delete_comment(video_id, request.values['comment_id'], request.values['author_id'], token, cookiejar)
|
||||||
|
|
||||||
if code == "SUCCESS":
|
if code == "SUCCESS":
|
||||||
start_response('303 See Other', [('Location', util.URL_ORIGIN + '/comment_delete_success'),] )
|
return flask.redirect(util.URL_ORIGIN + '/comment_delete_success', 303)
|
||||||
else:
|
else:
|
||||||
start_response('303 See Other', [('Location', util.URL_ORIGIN + '/comment_delete_fail'),] )
|
return flask.redirect(util.URL_ORIGIN + '/comment_delete_fail', 303)
|
||||||
|
|
||||||
def post_comment(env, start_response):
|
@yt_app.route('/comment_delete_success')
|
||||||
parameters = env['parameters']
|
def comment_delete_success():
|
||||||
video_id = parameters['video_id'][0]
|
return 'Successfully deleted comment'
|
||||||
channel_id = parameters['channel_id'][0]
|
|
||||||
|
@yt_app.route('/comment_delete_fail')
|
||||||
|
def comment_delete_fail():
|
||||||
|
return 'Failed to delete comment'
|
||||||
|
|
||||||
|
@yt_app.route('/post_comment', methods=['POST'])
|
||||||
|
@yt_app.route('/comments', methods=['POST'])
|
||||||
|
def post_comment():
|
||||||
|
video_id = request.values['video_id']
|
||||||
|
channel_id = request.values['channel_id']
|
||||||
cookiejar = accounts.account_cookiejar(channel_id)
|
cookiejar = accounts.account_cookiejar(channel_id)
|
||||||
token = get_session_token(video_id, cookiejar)
|
token = get_session_token(video_id, cookiejar)
|
||||||
|
|
||||||
if 'parent_id' in parameters:
|
if 'parent_id' in request.values:
|
||||||
code = _post_comment_reply(parameters['comment_text'][0], parameters['video_id'][0], parameters['parent_id'][0], token, cookiejar)
|
code = _post_comment_reply(request.values['comment_text'], request.values['video_id'], request.values['parent_id'], token, cookiejar)
|
||||||
start_response('303 See Other', (('Location', util.URL_ORIGIN + '/comments?' + env['QUERY_STRING']),) )
|
return flask.redirect(util.URL_ORIGIN + '/comments?' + request.query_string.decode('utf-8'), 303)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
code = _post_comment(parameters['comment_text'][0], parameters['video_id'][0], token, cookiejar)
|
code = _post_comment(request.values['comment_text'], request.values['video_id'], token, cookiejar)
|
||||||
start_response('303 See Other', (('Location', util.URL_ORIGIN + '/comments?ctoken=' + comments.make_comment_ctoken(video_id, sort=1)),) )
|
return flask.redirect(util.URL_ORIGIN + '/comments?ctoken=' + comments.make_comment_ctoken(video_id, sort=1), 303)
|
||||||
|
|
||||||
return b''
|
@yt_app.route('/delete_comment', methods=['GET'])
|
||||||
|
def get_delete_comment_page():
|
||||||
|
parameters = [(parameter_name, request.args[parameter_name]) for parameter_name in ('video_id', 'channel_id', 'author_id', 'comment_id')]
|
||||||
|
return flask.render_template('delete_comment.html', parameters = parameters)
|
||||||
|
|
||||||
def get_delete_comment_page(env, start_response):
|
|
||||||
start_response('200 OK', [('Content-type','text/html'),])
|
|
||||||
parameters = env['parameters']
|
|
||||||
|
|
||||||
style = '''
|
@yt_app.route('/post_comment', methods=['GET'])
|
||||||
main{
|
def get_post_comment_page():
|
||||||
display: grid;
|
video_id = request.args['video_id']
|
||||||
grid-template-columns: minmax(0px, 3fr) 640px 40px 500px minmax(0px,2fr);
|
parent_id = request.args.get('parent_id', '')
|
||||||
align-content: start;
|
|
||||||
}
|
|
||||||
main > div, main > form{
|
|
||||||
margin-top:20px;
|
|
||||||
grid-column:2;
|
|
||||||
}
|
|
||||||
'''
|
|
||||||
|
|
||||||
page = '''
|
|
||||||
<div>Are you sure you want to delete this comment?</div>
|
|
||||||
<form action="" method="POST">'''
|
|
||||||
for parameter in ('video_id', 'channel_id', 'author_id', 'comment_id'):
|
|
||||||
page += '''\n <input type="hidden" name="''' + parameter + '''" value="''' + parameters[parameter][0] + '''">'''
|
|
||||||
page += '''
|
|
||||||
<input type="submit" value="Yes, delete it">
|
|
||||||
</form>'''
|
|
||||||
return html_common.yt_basic_template.substitute(
|
|
||||||
page_title = "Delete comment?",
|
|
||||||
style = style,
|
|
||||||
header = html_common.get_header(),
|
|
||||||
page = page,
|
|
||||||
).encode('utf-8')
|
|
||||||
|
|
||||||
def get_post_comment_page(env, start_response):
|
|
||||||
start_response('200 OK', [('Content-type','text/html'),])
|
|
||||||
parameters = env['parameters']
|
|
||||||
video_id = parameters['video_id'][0]
|
|
||||||
parent_id = util.default_multi_get(parameters, 'parent_id', 0, default='')
|
|
||||||
|
|
||||||
style = ''' main{
|
|
||||||
display: grid;
|
|
||||||
grid-template-columns: 3fr 2fr;
|
|
||||||
}
|
|
||||||
.left{
|
|
||||||
display:grid;
|
|
||||||
grid-template-columns: 1fr 640px;
|
|
||||||
}
|
|
||||||
textarea{
|
|
||||||
width: 460px;
|
|
||||||
height: 85px;
|
|
||||||
}
|
|
||||||
.comment-form{
|
|
||||||
grid-column:2;
|
|
||||||
justify-content:start;
|
|
||||||
}'''
|
|
||||||
if parent_id: # comment reply
|
if parent_id: # comment reply
|
||||||
comment_box = comments.comment_box_template.substitute(
|
form_action = util.URL_ORIGIN + '/comments?parent_id=' + parent_id + "&video_id=" + video_id
|
||||||
form_action = util.URL_ORIGIN + '/comments?parent_id=' + parent_id + "&video_id=" + video_id,
|
replying = True
|
||||||
video_id_input = '',
|
|
||||||
post_text = "Post reply",
|
|
||||||
options=comments.comment_box_account_options(),
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
comment_box = comments.comment_box_template.substitute(
|
form_action = ''
|
||||||
form_action = util.URL_ORIGIN + '/post_comment',
|
replying = False
|
||||||
video_id_input = '''<input type="hidden" name="video_id" value="''' + video_id + '''">''',
|
|
||||||
post_text = "Post comment",
|
|
||||||
options=comments.comment_box_account_options(),
|
comment_posting_box_info = {
|
||||||
)
|
'form_action': form_action,
|
||||||
|
'video_id': video_id,
|
||||||
|
'accounts': accounts.account_list_data(),
|
||||||
|
'include_video_id_input': not replying,
|
||||||
|
'replying': replying,
|
||||||
|
}
|
||||||
|
return flask.render_template('post_comment.html',
|
||||||
|
comment_posting_box_info = comment_posting_box_info,
|
||||||
|
replying = replying,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
page = '''<div class="left">\n''' + comment_box + '''</div>\n'''
|
|
||||||
return html_common.yt_basic_template.substitute(
|
|
||||||
page_title = "Post comment reply" if parent_id else "Post a comment",
|
|
||||||
style = style,
|
|
||||||
header = html_common.get_header(),
|
|
||||||
page = page,
|
|
||||||
).encode('utf-8')
|
|
||||||
|
@ -45,3 +45,26 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
</section>
|
</section>
|
||||||
{% endmacro %}
|
{% endmacro %}
|
||||||
|
|
||||||
|
{% macro comment_posting_box(info) %}
|
||||||
|
<form action="{{ info['form_action'] }}" method="post" class="comment-form">
|
||||||
|
<div id="comment-account-options">
|
||||||
|
<label for="account-selection">Account:</label>
|
||||||
|
<select id="account-selection" name="channel_id">
|
||||||
|
{% for account in info['accounts'] %}
|
||||||
|
<option value="{{ account[0] }}">{{ account[1] }}</option>
|
||||||
|
{% endfor %}
|
||||||
|
</select>
|
||||||
|
<a href="/https://youtube.com/login" target="_blank">Add account</a>
|
||||||
|
</div>
|
||||||
|
<textarea name="comment_text"></textarea>
|
||||||
|
{% if info['include_video_id_input'] %}
|
||||||
|
<input type="hidden" name="video_id" value="{{ info['video_id'] }}">
|
||||||
|
{% endif %}
|
||||||
|
<button type="submit" class="post-comment-button">{{ 'Post reply' if info['replying'] else 'Post comment' }}</button>
|
||||||
|
</form>
|
||||||
|
{% endmacro %}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -42,23 +42,7 @@
|
|||||||
</section>
|
</section>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
|
{{ comments.comment_posting_box(comment_posting_box_info) }}
|
||||||
<form action="{{ form_action }}" method="post" class="comment-form">
|
|
||||||
<div id="comment-account-options">
|
|
||||||
<label for="account-selection">Account:</label>
|
|
||||||
<select id="account-selection" name="channel_id">
|
|
||||||
{% for account in accounts %}
|
|
||||||
<option value="{{ account[0] }}">{{ account[1] }}</option>
|
|
||||||
{% endfor %}
|
|
||||||
</select>
|
|
||||||
<a href="/https://youtube.com/login" target="_blank">Add account</a>
|
|
||||||
</div>
|
|
||||||
<textarea name="comment_text"></textarea>
|
|
||||||
{% if include_video_id_input %}
|
|
||||||
<input type="hidden" name="video_id" value="{{ comments_info['video_id'] }}">
|
|
||||||
{% endif %}
|
|
||||||
<button type="submit" class="post-comment-button">{{ 'Post reply' if comments_info['is_replies'] else 'Post comment' }}</button>
|
|
||||||
</form>
|
|
||||||
|
|
||||||
{% if not comments_info['is_replies'] %}
|
{% if not comments_info['is_replies'] %}
|
||||||
<div class="comment-links">
|
<div class="comment-links">
|
||||||
|
26
youtube/templates/delete_comment.html
Normal file
26
youtube/templates/delete_comment.html
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
{% extends "base.html" %}
|
||||||
|
|
||||||
|
{% block page_title %}Delete comment?{% endblock %}
|
||||||
|
|
||||||
|
{% block style %}
|
||||||
|
main{
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: minmax(0px, 3fr) 640px 40px 500px minmax(0px,2fr);
|
||||||
|
align-content: start;
|
||||||
|
}
|
||||||
|
main > div, main > form{
|
||||||
|
margin-top:20px;
|
||||||
|
grid-column:2;
|
||||||
|
}
|
||||||
|
{% endblock style %}
|
||||||
|
|
||||||
|
{% block main %}
|
||||||
|
<div>Are you sure you want to delete this comment?</div>
|
||||||
|
<form action="" method="POST">
|
||||||
|
{% for parameter_name, parameter_value in parameters %}
|
||||||
|
<input type="hidden" name="{{ parameter_name }}" value="{{ parameter_value }}">
|
||||||
|
{% endfor %}
|
||||||
|
<input type="submit" value="Yes, delete it">
|
||||||
|
</form>
|
||||||
|
{% endblock %}
|
||||||
|
|
30
youtube/templates/post_comment.html
Normal file
30
youtube/templates/post_comment.html
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
{% extends "base.html" %}
|
||||||
|
{% import "comments.html" as comments %}
|
||||||
|
|
||||||
|
{% block page_title %}{{ 'Post reply' if replying else 'Post comment' }}{% endblock %}
|
||||||
|
|
||||||
|
{% block style %}
|
||||||
|
main{
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: 3fr 2fr;
|
||||||
|
}
|
||||||
|
.left{
|
||||||
|
display:grid;
|
||||||
|
grid-template-columns: 1fr 640px;
|
||||||
|
}
|
||||||
|
textarea{
|
||||||
|
width: 460px;
|
||||||
|
height: 85px;
|
||||||
|
}
|
||||||
|
.comment-form{
|
||||||
|
grid-column:2;
|
||||||
|
justify-content:start;
|
||||||
|
}
|
||||||
|
{% endblock style %}
|
||||||
|
|
||||||
|
{% block main %}
|
||||||
|
<div class="left">
|
||||||
|
{{ comments.comment_posting_box(comment_posting_box_info) }}
|
||||||
|
</div>
|
||||||
|
{% endblock %}
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user