diff options
author | James Taylor <user234683@users.noreply.github.com> | 2018-09-15 17:53:44 -0700 |
---|---|---|
committer | James Taylor <user234683@users.noreply.github.com> | 2018-09-15 17:53:44 -0700 |
commit | 3de6c50c67f44df5e72196b2f0b060c27d36c185 (patch) | |
tree | 7d941cfdbdd4711e7d5a0b2c54d95b084628a881 /youtube | |
parent | 182c6c7c8ab559aead9adb578ea54ff8a69c94f5 (diff) | |
download | yt-local-3de6c50c67f44df5e72196b2f0b060c27d36c185.tar.lz yt-local-3de6c50c67f44df5e72196b2f0b060c27d36c185.tar.xz yt-local-3de6c50c67f44df5e72196b2f0b060c27d36c185.zip |
Check whether comment posting was successful
Diffstat (limited to 'youtube')
-rw-r--r-- | youtube/account_functions.py | 35 | ||||
-rw-r--r-- | youtube/common.py | 19 |
2 files changed, 42 insertions, 12 deletions
diff --git a/youtube/account_functions.py b/youtube/account_functions.py index 985f7d1..f34e5f2 100644 --- a/youtube/account_functions.py +++ b/youtube/account_functions.py @@ -1,8 +1,11 @@ # 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 +import traceback + 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', @@ -30,6 +33,10 @@ def _post_comment(text, video_id, session_token, cookie): req = urllib.request.Request("https://m.youtube.com/service_ajax?name=createCommentEndpoint", headers=headers, data=data) response = urllib.request.urlopen(req, timeout = 5) content = response.read() + content = common.decode_content(content, response.getheader('Content-Encoding', default='identity')) + code = json.loads(content)['code'] + print("Comment posting code: " + code) + return code '''with open('debug/post_comment_response', 'wb') as f: f.write(content)''' @@ -61,6 +68,10 @@ def _post_comment_reply(text, video_id, parent_comment_id, session_token, cookie req = urllib.request.Request("https://m.youtube.com/service_ajax?name=createCommentReplyEndpoint", headers=headers, data=data) response = urllib.request.urlopen(req, timeout = 5) content = response.read() + content = common.decode_content(content, response.getheader('Content-Encoding', default='identity')) + code = json.loads(content)['code'] + print("Comment posting code: " + code) + return code '''with open('debug/post_comment_response', 'wb') as f: f.write(content)''' @@ -115,11 +126,27 @@ def post_comment(query_string, fields): 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) + code = _post_comment_reply(fields['comment_text'][0], parameters['video_id'][0], parameters['parent_id'][0], token, cookie_data) + try: + response = comments.get_comments_page(query_string) + except socket.error as e: + traceback.print_tb(e.__traceback__) + return b'Refreshing comment page yielded error 502 Bad Gateway.\nPost comment status code: ' + code.encode('ascii') + except Exception as e: + traceback.print_tb(e.__traceback__) + return b'Refreshing comment page yielded error 500 Internal Server Error.\nPost comment status code: ' + code.encode('ascii') + return response else: - _post_comment(fields['comment_text'][0], fields['video_id'][0], token, cookie_data) - return comments.get_comments_page('ctoken=' + comments.make_comment_ctoken(video_id, sort=1)) + code = _post_comment(fields['comment_text'][0], fields['video_id'][0], token, cookie_data) + try: + response = comments.get_comments_page('ctoken=' + comments.make_comment_ctoken(video_id, sort=1)) + except socket.error as e: + traceback.print_tb(e.__traceback__) + return b'Refreshing comment page yielded error 502 Bad Gateway.\nPost comment status code: ' + code.encode('ascii') + except Exception as e: + traceback.print_tb(e.__traceback__) + return b'Refreshing comment page yielded error 500 Internal Server Error.\nPost comment status code: ' + code.encode('ascii') + return response diff --git a/youtube/common.py b/youtube/common.py index 912cceb..76fd7c3 100644 --- a/youtube/common.py +++ b/youtube/common.py @@ -137,6 +137,16 @@ medium_channel_item_template = Template(''' </div> ''') +def decode_content(content, encoding_header): + encodings = encoding_header.replace(' ', '').split(',') + for encoding in reversed(encodings): + if encoding == 'identity': + continue + if encoding == 'br': + content = brotli.decompress(content) + elif encoding == 'gzip': + content = gzip.decompress(content) + return content def fetch_url(url, headers=(), timeout=15, report_text=None): if isinstance(headers, list): @@ -159,14 +169,7 @@ def fetch_url(url, headers=(), timeout=15, report_text=None): read_finish = time.time() if report_text: print(report_text, ' Latency:', response_time - start_time, ' Read time:', read_finish - response_time) - encodings = response.getheader('Content-Encoding', default='identity').replace(' ', '').split(',') - for encoding in reversed(encodings): - if encoding == 'identity': - continue - if encoding == 'br': - content = brotli.decompress(content) - elif encoding == 'gzip': - content = gzip.decompress(content) + content = decode_content(content, response.getheader('Content-Encoding', default='identity')) return content mobile_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' |