Ability to delete comments
This commit is contained in:
parent
005c1239a6
commit
e3fac7da54
@ -113,10 +113,13 @@
|
|||||||
white-space: nowrap;
|
white-space: nowrap;
|
||||||
}
|
}
|
||||||
|
|
||||||
.comment .replies{
|
.comment .bottom-row{
|
||||||
grid-column:2 / span 2;
|
grid-column:2 / span 3;
|
||||||
grid-row:4;
|
grid-row:4;
|
||||||
justify-self:start;
|
justify-self:start;
|
||||||
|
display: grid;
|
||||||
|
grid-auto-flow: column;
|
||||||
|
grid-column-gap: 10px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.more-comments{
|
.more-comments{
|
||||||
|
@ -26,10 +26,14 @@ $avatar
|
|||||||
<address>
|
<address>
|
||||||
<a class="author" href="$author_url" title="$author">$author</a>
|
<a class="author" href="$author_url" title="$author">$author</a>
|
||||||
</address>
|
</address>
|
||||||
<span class="text">$text</span>
|
|
||||||
<time datetime="$datetime">$published</time>
|
<time datetime="$datetime">$published</time>
|
||||||
|
<span class="text">$text</span>
|
||||||
|
|
||||||
<span class="likes">$likes</span>
|
<span class="likes">$likes</span>
|
||||||
|
<div class="bottom-row">
|
||||||
$replies
|
$replies
|
||||||
|
$action_buttons
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
@ -162,6 +166,8 @@ def parse_comments_ajax(content, replies=False):
|
|||||||
comment = {
|
comment = {
|
||||||
'author': comment_raw['author']['runs'][0]['text'],
|
'author': comment_raw['author']['runs'][0]['text'],
|
||||||
'author_url': comment_raw['author_endpoint']['url'],
|
'author_url': comment_raw['author_endpoint']['url'],
|
||||||
|
'author_channel_id': '',
|
||||||
|
'author_id': '',
|
||||||
'author_avatar': comment_raw['author_thumbnail']['url'],
|
'author_avatar': comment_raw['author_thumbnail']['url'],
|
||||||
'likes': comment_raw['like_count'],
|
'likes': comment_raw['like_count'],
|
||||||
'published': comment_raw['published_time']['runs'][0]['text'],
|
'published': comment_raw['published_time']['runs'][0]['text'],
|
||||||
@ -207,6 +213,7 @@ def parse_comments_polymer(content, replies=False):
|
|||||||
video_title = comment_raw['commentTargetTitle']['runs'][0]['text']
|
video_title = comment_raw['commentTargetTitle']['runs'][0]['text']
|
||||||
|
|
||||||
parent_id = comment_raw['comment']['commentRenderer']['commentId']
|
parent_id = comment_raw['comment']['commentRenderer']['commentId']
|
||||||
|
# TODO: move this stuff into the comments_html function
|
||||||
if 'replies' in comment_raw:
|
if 'replies' in comment_raw:
|
||||||
#reply_ctoken = comment_raw['replies']['commentRepliesRenderer']['continuations'][0]['nextContinuationData']['continuation']
|
#reply_ctoken = comment_raw['replies']['commentRepliesRenderer']['continuations'][0]['nextContinuationData']['continuation']
|
||||||
#comment_id, video_id = get_ids(reply_ctoken)
|
#comment_id, video_id = get_ids(reply_ctoken)
|
||||||
@ -226,12 +233,16 @@ def parse_comments_polymer(content, replies=False):
|
|||||||
comment = {
|
comment = {
|
||||||
'author': common.get_plain_text(comment_raw['authorText']),
|
'author': common.get_plain_text(comment_raw['authorText']),
|
||||||
'author_url': comment_raw['authorEndpoint']['commandMetadata']['webCommandMetadata']['url'],
|
'author_url': comment_raw['authorEndpoint']['commandMetadata']['webCommandMetadata']['url'],
|
||||||
|
'author_channel_id': comment_raw['authorEndpoint']['browseEndpoint']['browseId'],
|
||||||
|
'author_id': comment_raw['authorId'],
|
||||||
'author_avatar': comment_raw['authorThumbnail']['thumbnails'][0]['url'],
|
'author_avatar': comment_raw['authorThumbnail']['thumbnails'][0]['url'],
|
||||||
'likes': comment_raw['likeCount'],
|
'likes': comment_raw['likeCount'],
|
||||||
'published': common.get_plain_text(comment_raw['publishedTimeText']),
|
'published': common.get_plain_text(comment_raw['publishedTimeText']),
|
||||||
'text': comment_raw['contentText'].get('runs', ''),
|
'text': comment_raw['contentText'].get('runs', ''),
|
||||||
'view_replies_text': view_replies_text,
|
'view_replies_text': view_replies_text,
|
||||||
'replies_url': replies_url,
|
'replies_url': replies_url,
|
||||||
|
'video_id': video_id,
|
||||||
|
'comment_id': comment_raw['commentId'],
|
||||||
}
|
}
|
||||||
comments.append(comment)
|
comments.append(comment)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@ -256,6 +267,16 @@ def get_comments_html(comments):
|
|||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
avatar = ''
|
avatar = ''
|
||||||
|
if comment['author_channel_id'] in accounts.accounts:
|
||||||
|
delete_url = (URL_ORIGIN + '/delete_comment?video_id='
|
||||||
|
+ comment['video_id']
|
||||||
|
+ '&channel_id='+ comment['author_channel_id']
|
||||||
|
+ '&author_id=' + comment['author_id']
|
||||||
|
+ '&comment_id=' + comment['comment_id'])
|
||||||
|
|
||||||
|
action_buttons = '''<a href="''' + delete_url + '''" target="_blank">Delete</a>'''
|
||||||
|
else:
|
||||||
|
action_buttons = ''
|
||||||
html_result += comment_template.substitute(
|
html_result += comment_template.substitute(
|
||||||
author=comment['author'],
|
author=comment['author'],
|
||||||
author_url = URL_ORIGIN + comment['author_url'],
|
author_url = URL_ORIGIN + comment['author_url'],
|
||||||
@ -264,8 +285,8 @@ def get_comments_html(comments):
|
|||||||
published = comment['published'],
|
published = comment['published'],
|
||||||
text = format_text_runs(comment['text']),
|
text = format_text_runs(comment['text']),
|
||||||
datetime = '', #TODO
|
datetime = '', #TODO
|
||||||
replies=replies,
|
replies = replies,
|
||||||
#replies='',
|
action_buttons = action_buttons,
|
||||||
)
|
)
|
||||||
return html_result
|
return html_result
|
||||||
|
|
||||||
|
@ -70,7 +70,7 @@ def _post_comment_reply(text, video_id, parent_comment_id, session_token, cookie
|
|||||||
'''with open('debug/post_comment_response', 'wb') as f:
|
'''with open('debug/post_comment_response', 'wb') as f:
|
||||||
f.write(content)'''
|
f.write(content)'''
|
||||||
|
|
||||||
def delete_comment(video_id, comment_id, author_id, session_token, cookiejar):
|
def _delete_comment(video_id, comment_id, author_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',
|
||||||
'Accept': '*/*',
|
'Accept': '*/*',
|
||||||
@ -91,7 +91,9 @@ def delete_comment(video_id, comment_id, author_id, session_token, cookiejar):
|
|||||||
data = urllib.parse.urlencode(data_dict).encode()
|
data = urllib.parse.urlencode(data_dict).encode()
|
||||||
|
|
||||||
content = common.fetch_url("https://m.youtube.com/service_ajax?name=performCommentActionEndpoint", headers=headers, data=data, cookiejar_send=cookiejar)
|
content = common.fetch_url("https://m.youtube.com/service_ajax?name=performCommentActionEndpoint", headers=headers, data=data, cookiejar_send=cookiejar)
|
||||||
|
code = json.loads(content)['code']
|
||||||
|
print("Comment deletion code: " + code)
|
||||||
|
return code
|
||||||
|
|
||||||
xsrf_token_regex = re.compile(r'''XSRF_TOKEN"\s*:\s*"([\w-]*(?:=|%3D){0,2})"''')
|
xsrf_token_regex = re.compile(r'''XSRF_TOKEN"\s*:\s*"([\w-]*(?:=|%3D){0,2})"''')
|
||||||
def get_session_token(video_id, cookiejar):
|
def get_session_token(video_id, cookiejar):
|
||||||
@ -107,6 +109,12 @@ 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(parameters, fields):
|
||||||
|
video_id = fields['video_id'][0]
|
||||||
|
cookiejar = accounts.account_cookiejar(fields['channel_id'][0])
|
||||||
|
token = get_session_token(video_id, cookiejar)
|
||||||
|
return _delete_comment(video_id, fields['comment_id'][0], fields['author_id'][0], token, cookiejar)
|
||||||
|
|
||||||
def post_comment(parameters, fields):
|
def post_comment(parameters, fields):
|
||||||
channel_id = fields['channel_id'][0]
|
channel_id = fields['channel_id'][0]
|
||||||
cookiejar = accounts.account_cookiejar(channel_id)
|
cookiejar = accounts.account_cookiejar(channel_id)
|
||||||
@ -144,6 +152,35 @@ def post_comment(parameters, fields):
|
|||||||
return response'''
|
return response'''
|
||||||
return code
|
return code
|
||||||
|
|
||||||
|
def get_delete_comment_page(query_string):
|
||||||
|
parameters = urllib.parse.parse_qs(query_string)
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
'''
|
||||||
|
|
||||||
|
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 common.yt_basic_template.substitute(
|
||||||
|
page_title = "Delete comment?",
|
||||||
|
style = style,
|
||||||
|
header = common.get_header(),
|
||||||
|
page = page,
|
||||||
|
)
|
||||||
|
|
||||||
def get_post_comment_page(query_string):
|
def get_post_comment_page(query_string):
|
||||||
parameters = urllib.parse.parse_qs(query_string)
|
parameters = urllib.parse.parse_qs(query_string)
|
||||||
|
@ -78,6 +78,18 @@ def youtube(env, start_response):
|
|||||||
start_response('200 OK', (('Content-type','text/html'),) )
|
start_response('200 OK', (('Content-type','text/html'),) )
|
||||||
return accounts.get_account_login_page(query_string=query_string).encode()
|
return accounts.get_account_login_page(query_string=query_string).encode()
|
||||||
|
|
||||||
|
elif path == "/delete_comment":
|
||||||
|
start_response('200 OK', (('Content-type','text/html'),) )
|
||||||
|
return post_comment.get_delete_comment_page(query_string).encode()
|
||||||
|
|
||||||
|
elif path == "/comment_delete_success":
|
||||||
|
start_response('200 OK', () )
|
||||||
|
return b'Successfully deleted comment'
|
||||||
|
|
||||||
|
elif path == "/comment_delete_fail":
|
||||||
|
start_response('200 OK', () )
|
||||||
|
return b'Failed to deleted comment'
|
||||||
|
|
||||||
else:
|
else:
|
||||||
start_response('200 OK', (('Content-type','text/html'),) )
|
start_response('200 OK', (('Content-type','text/html'),) )
|
||||||
return channel.get_channel_page_general_url(path, query_string=query_string).encode()
|
return channel.get_channel_page_general_url(path, query_string=query_string).encode()
|
||||||
@ -116,6 +128,14 @@ def youtube(env, start_response):
|
|||||||
start_response('303 See Other', (('Location', common.URL_ORIGIN + '/comments?ctoken=' + comments.make_comment_ctoken(video_id, sort=1)),) )
|
start_response('303 See Other', (('Location', common.URL_ORIGIN + '/comments?ctoken=' + comments.make_comment_ctoken(video_id, sort=1)),) )
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
|
elif path == "/delete_comment":
|
||||||
|
parameters = urllib.parse.parse_qs(query_string)
|
||||||
|
code = post_comment.delete_comment(parameters, fields)
|
||||||
|
if code == "SUCCESS":
|
||||||
|
start_response('303 See Other', (('Location', common.URL_ORIGIN + '/comment_delete_success'),) )
|
||||||
|
else:
|
||||||
|
start_response('303 See Other', (('Location', common.URL_ORIGIN + '/comment_delete_fail'),) )
|
||||||
|
|
||||||
elif path == "/login":
|
elif path == "/login":
|
||||||
if 'save' in fields and fields['save'][0] == "on":
|
if 'save' in fields and fields['save'][0] == "on":
|
||||||
save_account = True
|
save_account = True
|
||||||
|
Loading…
x
Reference in New Issue
Block a user