aboutsummaryrefslogtreecommitdiffstats
path: root/youtube
diff options
context:
space:
mode:
Diffstat (limited to 'youtube')
-rw-r--r--youtube/account_functions.py135
-rw-r--r--youtube/comments.py7
-rw-r--r--youtube/watch.py4
-rw-r--r--youtube/youtube.py2
4 files changed, 146 insertions, 2 deletions
diff --git a/youtube/account_functions.py b/youtube/account_functions.py
new file mode 100644
index 0000000..9dd3d10
--- /dev/null
+++ b/youtube/account_functions.py
@@ -0,0 +1,135 @@
+# Contains functions having to do with logging in or requiring that one is logged in
+import urllib
+import json
+from youtube import common, proto, comments
+import re
+def _post_comment(text, video_id, session_token, cookie):
+ 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',
+ 'Accept': '*/*',
+ 'Accept-Language': 'en-US,en;q=0.5',
+ 'Accept-Encoding': 'gzip, deflate, br',
+ 'X-YouTube-Client-Name': '2',
+ 'X-YouTube-Client-Version': '2.20180823',
+ 'Content-Type': 'application/x-www-form-urlencoded',
+ 'Cookie': cookie,
+ }
+
+ comment_params = proto.string(2, video_id) + proto.nested(5, proto.uint(1, 0)) + proto.uint(10, 1)
+ comment_params = proto.percent_b64encode(comment_params).decode('ascii')
+
+ sej = json.dumps({"clickTrackingParams":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=", "commandMetadata":{"webCommandMetadata":{"url":"/service_ajax","sendPost":True}},"createCommentEndpoint":{"createCommentParams": comment_params}})
+
+ data_dict = {
+ 'comment_text': text,
+ 'sej': sej,
+ 'session_token': session_token,
+ }
+ data = urllib.parse.urlencode(data_dict).encode()
+
+ req = urllib.request.Request("https://m.youtube.com/service_ajax?name=createCommentEndpoint", headers=headers, data=data)
+ response = urllib.request.urlopen(req, timeout = 5)
+ '''with open('debug/post_comment_response', 'wb') as f:
+ f.write(response.read())'''
+
+
+def _post_comment_reply(text, video_id, parent_comment_id, session_token, cookie):
+ 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',
+ 'Accept': '*/*',
+ 'Accept-Language': 'en-US,en;q=0.5',
+ 'Accept-Encoding': 'gzip, deflate, br',
+ 'X-YouTube-Client-Name': '2',
+ 'X-YouTube-Client-Version': '2.20180823',
+ 'Content-Type': 'application/x-www-form-urlencoded',
+ 'Cookie': cookie,
+ }
+
+ comment_params = proto.string(2, video_id) + proto.string(4, parent_comment_id) + proto.nested(5, proto.uint(1, 0)) + proto.uint(6,0) + proto.uint(10, 1)
+ comment_params = proto.percent_b64encode(comment_params).decode('ascii')
+
+ sej = json.dumps({"clickTrackingParams":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=", "commandMetadata":{"webCommandMetadata":{"url":"/service_ajax","sendPost":True}},"createCommentReplyEndpoint":{"createReplyParams": comment_params}})
+
+ data_dict = {
+ 'comment_text': text,
+ 'sej': sej,
+ 'session_token': session_token,
+ }
+ data = urllib.parse.urlencode(data_dict).encode()
+
+ req = urllib.request.Request("https://m.youtube.com/service_ajax?name=createCommentReplyEndpoint", headers=headers, data=data)
+ response = urllib.request.urlopen(req, timeout = 5)
+ '''with open('debug/post_comment_response', 'wb') as f:
+ f.write(response.read())'''
+
+
+
+xsrf_token_regex = re.compile(r'''XSRF_TOKEN"\s*:\s*"([\w-]*(?:=|%3D){0,2})"''')
+def post_comment(query_string, fields):
+ with open('data/cookie.txt', 'r', encoding='utf-8') as f:
+ cookie_data = f.read()
+
+ parameters = urllib.parse.parse_qs(query_string)
+ try:
+ video_id = fields['video_id'][0]
+ except KeyError:
+ video_id = parameters['video_id'][0]
+
+ # Get session token for mobile
+ # youtube-dl uses disable_polymer=1 which uses a different request format which has an obfuscated javascript algorithm to generate a parameter called "bgr"
+ # Tokens retrieved from disable_polymer pages only work with that format. Tokens retrieved on mobile only work using mobile requests
+ # Additionally, tokens retrieved without sending the same cookie won't work. So this is necessary even if the bgr and stuff was reverse engineered.
+ headers = {'User-Agent': common.mobile_user_agent,
+ 'Cookie': cookie_data,}
+ mobile_page = common.fetch_url('https://m.youtube.com/watch?v=' + video_id, headers, report_text="Retrieved session token for comment").decode()
+ match = xsrf_token_regex.search(mobile_page)
+ if match:
+ token = match.group(1).replace("%3D", "=")
+ else:
+ raise Exception("Couldn't find xsrf_token")
+
+ if 'parent_id' in parameters:
+ _post_comment_reply(fields['comment_text'][0], parameters['video_id'][0], parameters['parent_id'][0], token, cookie_data)
+ return comments.get_comments_page(query_string)
+ else:
+ _post_comment(fields['comment_text'][0], fields['video_id'][0], token, cookie_data)
+ return common.yt_basic_template.substitute(
+ page_title = "Success",
+ style = '',
+ header = common.get_header(),
+ page = 'Comment sucessfully posted',
+ )
+
+
+def get_post_comment_page(query_string):
+ parameters = urllib.parse.parse_qs(query_string)
+ video_id = parameters['v'][0]
+ style = ''' main{
+ display: grid;
+ grid-template-columns: 3fr 2fr;
+}
+.left{
+ display:grid;
+ grid-template-columns: 1fr 640px;
+}
+textarea{
+ width: 462px;
+ height: 85px;
+}
+.comment-form{
+ grid-column:2;
+}'''
+ page = '''<div class="left">
+ <form action="" method="post" class="comment-form">
+ <textarea name="comment_text"></textarea>
+ <input type="hidden" name="video_id" value="''' + video_id + '''">
+ <button type="submit">Post comment</button>
+ </form>
+</div>
+'''
+ return common.yt_basic_template.substitute(
+ page_title = "Post a comment",
+ style = style,
+ header = common.get_header(),
+ page = page,
+ ) \ No newline at end of file
diff --git a/youtube/comments.py b/youtube/comments.py
index 5490938..a1f08bf 100644
--- a/youtube/comments.py
+++ b/youtube/comments.py
@@ -206,7 +206,6 @@ def parse_comments_polymer(content, replies=False):
print('Error parsing comments: ' + str(e))
comments = ()
ctoken = ''
- raise
else:
print("Finished getting and parsing comments")
return {'ctoken': ctoken, 'comments': comments, 'video_title': video_title}
@@ -273,6 +272,10 @@ def get_comments_page(query_string):
if replies:
page_title = 'Replies'
video_metadata = ''
+ comment_box = '''<form action="" method="post" class="comment-form">
+ <textarea name="comment_text"></textarea>
+ <button type="submit" class="post-comment-button">Post reply</button>
+</form>'''
else:
page_number = str(int(metadata['offset']/20) + 1)
page_title = 'Comments page ' + page_number
@@ -283,6 +286,7 @@ def get_comments_page(query_string):
url = common.URL_ORIGIN + '/watch?v=' + metadata['video_id'],
thumbnail = '/i.ytimg.com/vi/'+ metadata['video_id'] + '/mqdefault.jpg',
)
+ comment_box = ''
comments_html, ctoken = get_comments_html(parsed_comments)
@@ -293,6 +297,7 @@ def get_comments_page(query_string):
return yt_comments_template.substitute(
header = common.get_header(),
+ comment_box = comment_box,
video_metadata = video_metadata,
comments = comments_html,
page_title = page_title,
diff --git a/youtube/watch.py b/youtube/watch.py
index ad3c448..ff7d6df 100644
--- a/youtube/watch.py
+++ b/youtube/watch.py
@@ -385,6 +385,10 @@ def get_watch_page(query_string):
music_list_html += '''</tr>\n'''
music_list_html += '''</table>\n'''
+ with open('data/googlevideo-domains.txt', 'a+', encoding='utf-8') as f:
+ url = info['formats'][0]['url']
+ subdomain = url[0:url.find(".googlevideo.com")]
+ f.write(subdomain + "\n")
download_options = ''
for format in info['formats']:
diff --git a/youtube/youtube.py b/youtube/youtube.py
index 3c31fa4..29fa6a0 100644
--- a/youtube/youtube.py
+++ b/youtube/youtube.py
@@ -80,7 +80,7 @@ def youtube(env, start_response):
else:
start_response('400 Bad Request', ())
return b'400 Bad Request'
- elif path == "/post_comment":
+ elif path in ("/post_comment", "/comments"):
start_response('200 OK', () )
return account_functions.post_comment(query_string, fields).encode()