Rename cookie_jar to cookiejar

This commit is contained in:
James Taylor 2018-12-26 18:27:15 -08:00
parent 2b4b5e284c
commit 90f19d2d5b
3 changed files with 27 additions and 27 deletions

View File

@ -22,23 +22,23 @@ def save_accounts():
f.write(json.dumps(to_save))
def add_account(username, password, save):
cookie_jar = http.cookiejar.LWPCookieJar()
condition = _login(username, password, cookie_jar)
cookiejar = http.cookiejar.LWPCookieJar()
condition = _login(username, password, cookiejar)
accounts[username] = {
"save":save,
"cookies":cookie_jar.as_lwp_str(ignore_discard=False, ignore_expires=False),
"cookies":cookiejar.as_lwp_str(ignore_discard=False, ignore_expires=False),
}
return condition
def cookie_jar_from_lwp_str(lwp_str):
cookie_jar = http.cookiejar.LWPCookieJar()
def cookiejar_from_lwp_str(lwp_str):
cookiejar = http.cookiejar.LWPCookieJar()
# HACK: cookiejar module insists on using filenames and reading files for you,
# so present a StringIO to this internal method which takes a filelike object
cookie_jar._really_load(self, io.StringIO(lwp_str), "", False, False)
return cookie_jar
cookiejar._really_load(self, io.StringIO(lwp_str), "", False, False)
return cookiejar
def account_cookie_jar(username):
return cookie_jar_from_lwp_str(accounts[username]['cookies'])
def account_cookiejar(username):
return cookiejar_from_lwp_str(accounts[username]['cookies'])
def get_account_login_page(query_string):
style = '''
@ -172,7 +172,7 @@ _TWOFACTOR_URL = 'https://accounts.google.com/signin/challenge'
_LOOKUP_URL = 'https://accounts.google.com/_/signin/sl/lookup'
_CHALLENGE_URL = 'https://accounts.google.com/_/signin/sl/challenge'
_TFA_URL = 'https://accounts.google.com/_/signin/challenge?hl=en&TL={0}'
def _login(username, password, cookie_jar):
def _login(username, password, cookiejar):
"""
Attempt to log in to YouTube.
True is returned if successful or skipped.
@ -181,10 +181,10 @@ def _login(username, password, cookie_jar):
Taken from youtube-dl
"""
login_page = common.fetch_url(_LOGIN_URL, yt_dl_headers, report_text='Downloaded login page', cookie_jar_receive=cookie_jar, use_tor=False).decode('utf-8')
login_page = common.fetch_url(_LOGIN_URL, yt_dl_headers, report_text='Downloaded login page', cookiejar_receive=cookiejar, use_tor=False).decode('utf-8')
'''with open('debug/login_page', 'w', encoding='utf-8') as f:
f.write(login_page)'''
#print(cookie_jar.as_lwp_str())
#print(cookiejar.as_lwp_str())
if login_page is False:
return
@ -207,8 +207,8 @@ def _login(username, password, cookie_jar):
'Google-Accounts-XSRF': 1,
}
headers.update(yt_dl_headers)
result = common.fetch_url(url, headers, report_text=note, data=data, cookie_jar_send=cookie_jar, cookie_jar_receive=cookie_jar, use_tor=False).decode('utf-8')
#print(cookie_jar.as_lwp_str())
result = common.fetch_url(url, headers, report_text=note, data=data, cookiejar_send=cookiejar, cookiejar_receive=cookiejar, use_tor=False).decode('utf-8')
#print(cookiejar.as_lwp_str())
'''with open('debug/' + note, 'w', encoding='utf-8') as f:
f.write(result)'''
result = re.sub(r'^[^\[]*', '', result)
@ -339,7 +339,7 @@ def _login(username, password, cookie_jar):
return False
try:
check_cookie_results = common.fetch_url(check_cookie_url, headers=yt_dl_headers, report_text="Checked cookie", cookie_jar_send=cookie_jar, cookie_jar_receive=cookie_jar, use_tor=False).decode('utf-8')
check_cookie_results = common.fetch_url(check_cookie_url, headers=yt_dl_headers, report_text="Checked cookie", cookiejar_send=cookiejar, cookiejar_receive=cookiejar, use_tor=False).decode('utf-8')
except (urllib.error.URLError, compat_http_client.HTTPException, socket.error) as err:
return False

View File

@ -171,11 +171,11 @@ def decode_content(content, encoding_header):
content = gzip.decompress(content)
return content
def fetch_url(url, headers=(), timeout=15, report_text=None, data=None, cookie_jar_send=None, cookie_jar_receive=None, use_tor=True):
def fetch_url(url, headers=(), timeout=15, report_text=None, data=None, cookiejar_send=None, cookiejar_receive=None, use_tor=True):
'''
When cookie_jar_send is set to a CookieJar object,
When cookiejar_send is set to a CookieJar object,
those cookies will be sent in the request (but cookies in response will not be merged into it)
When cookie_jar_receive is set to a CookieJar object,
When cookiejar_receive is set to a CookieJar object,
cookies received in the response will be merged into the object (nothing will be sent from it)
When both are set to the same object, cookies will be sent from the object,
and response cookies will be merged into it.
@ -194,7 +194,7 @@ def fetch_url(url, headers=(), timeout=15, report_text=None, data=None, cookie_j
req = urllib.request.Request(url, data=data, headers=headers)
cookie_processor = HTTPAsymmetricCookieProcessor(cookiejar_send=cookie_jar_send, cookiejar_receive=cookie_jar_receive)
cookie_processor = HTTPAsymmetricCookieProcessor(cookiejar_send=cookiejar_send, cookiejar_receive=cookiejar_receive)
if use_tor and settings.route_tor:
opener = urllib.request.build_opener(sockshandler.SocksiPyHandler(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9150), cookie_processor)

View File

@ -8,7 +8,7 @@ import traceback
import settings
import os
def _post_comment(text, video_id, session_token, cookie_jar):
def _post_comment(text, video_id, session_token, cookiejar):
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': '*/*',
@ -31,7 +31,7 @@ def _post_comment(text, video_id, session_token, cookie_jar):
data = urllib.parse.urlencode(data_dict).encode()
content = common.fetch_url("https://m.youtube.com/service_ajax?name=createCommentEndpoint", headers=headers, data=data, cookie_jar_send=cookie_jar)
content = common.fetch_url("https://m.youtube.com/service_ajax?name=createCommentEndpoint", headers=headers, data=data, cookiejar_send=cookiejar)
code = json.loads(content)['code']
print("Comment posting code: " + code)
@ -40,7 +40,7 @@ def _post_comment(text, video_id, session_token, cookie_jar):
f.write(content)'''
def _post_comment_reply(text, video_id, parent_comment_id, session_token, cookie_jar):
def _post_comment_reply(text, video_id, parent_comment_id, session_token, cookiejar):
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': '*/*',
@ -62,7 +62,7 @@ def _post_comment_reply(text, video_id, parent_comment_id, session_token, cookie
}
data = urllib.parse.urlencode(data_dict).encode()
content = common.fetch_url("https://m.youtube.com/service_ajax?name=createCommentReplyEndpoint", headers=headers, data=data, cookie_jar_send=cookie_jar)
content = common.fetch_url("https://m.youtube.com/service_ajax?name=createCommentReplyEndpoint", headers=headers, data=data, cookiejar_send=cookiejar)
code = json.loads(content)['code']
print("Comment posting code: " + code)
@ -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:
f.write(content)'''
def delete_comment(video_id, comment_id, author_id, session_token, cookie_jar):
def delete_comment(video_id, comment_id, author_id, session_token, cookiejar):
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': '*/*',
@ -90,12 +90,12 @@ def delete_comment(video_id, comment_id, author_id, session_token, cookie_jar):
}
data = urllib.parse.urlencode(data_dict).encode()
content = common.fetch_url("https://m.youtube.com/service_ajax?name=performCommentActionEndpoint", headers=headers, data=data, cookie_jar_send=cookie_jar)
content = common.fetch_url("https://m.youtube.com/service_ajax?name=performCommentActionEndpoint", headers=headers, data=data, cookiejar_send=cookiejar)
xsrf_token_regex = re.compile(r'''XSRF_TOKEN"\s*:\s*"([\w-]*(?:=|%3D){0,2})"''')
def post_comment(parameters, fields):
username = parameters['username']
cookie_jar = accounts.account_cookie_jar(username)
cookiejar = accounts.account_cookiejar(username)
#parameters = urllib.parse.parse_qs(query_string)
try:
@ -108,7 +108,7 @@ def post_comment(parameters, fields):
# 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}
mobile_page = common.fetch_url('https://m.youtube.com/watch?v=' + video_id, headers, report_text="Retrieved session token for comment", cookie_jar_send=cookie_jar).decode()
mobile_page = common.fetch_url('https://m.youtube.com/watch?v=' + video_id, headers, report_text="Retrieved session token for comment", cookiejar_send=cookiejar).decode()
match = xsrf_token_regex.search(mobile_page)
if match:
token = match.group(1).replace("%3D", "=")