mirror of
https://github.com/yt-dlp/yt-dlp.git
synced 2024-10-05 00:52:57 +00:00
Some streamlining
This commit is contained in:
parent
b619e119e6
commit
28e4722b00
|
@ -237,12 +237,25 @@ def _real_extract(self, url):
|
||||||
|
|
||||||
|
|
||||||
class NoRedirect(urllib.request.HTTPRedirectHandler):
|
class NoRedirect(urllib.request.HTTPRedirectHandler):
|
||||||
|
|
||||||
def http_error_302(self, req, fp, code, msg, headers):
|
def http_error_302(self, req, fp, code, msg, headers):
|
||||||
result = urllib.error.HTTPError(req.get_full_url(), code, msg, headers, fp)
|
result = urllib.error.HTTPError(req.get_full_url(), code, msg, headers, fp)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
http_error_301 = http_error_303 = http_error_307 = http_error_302
|
http_error_301 = http_error_303 = http_error_307 = http_error_302
|
||||||
|
|
||||||
|
|
||||||
|
class CookiePot(CookieJar):
|
||||||
|
|
||||||
|
def __getitem__(self, name):
|
||||||
|
for cookie in self:
|
||||||
|
if cookie.name == name:
|
||||||
|
return cookie.value
|
||||||
|
return None
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return '\n'.join(f'{cookie.name}={cookie.value}' for cookie in self)
|
||||||
|
|
||||||
|
|
||||||
class VrtNUIE(VRTBaseIE):
|
class VrtNUIE(VRTBaseIE):
|
||||||
IE_DESC = 'VRT MAX'
|
IE_DESC = 'VRT MAX'
|
||||||
|
@ -308,11 +321,19 @@ def _create_cookie_header(self, cookies):
|
||||||
def _perform_login(self, username, password):
|
def _perform_login(self, username, password):
|
||||||
|
|
||||||
|
|
||||||
cookie_jar = CookieJar()
|
cookies = CookiePot()
|
||||||
|
|
||||||
|
# TODO:
|
||||||
|
# 1. Does the _request_webpage() respect this opener too?
|
||||||
|
# 2. If so:
|
||||||
|
# a. modify (and rename) the class NoRedirect to store the 'Location:' header
|
||||||
|
# b. Steps 1.a & 1.b become one call to _request_webpage()
|
||||||
|
#
|
||||||
|
# https://stackoverflow.com/questions/47002795/how-to-trace-or-to-check-history-of-redirected-urls-with-python-only-urllib-libr
|
||||||
|
|
||||||
# Disable automatic redirection to be able to
|
# Disable automatic redirection to be able to
|
||||||
# grab necessary info in intermediate step
|
# grab necessary info in intermediate step
|
||||||
opener= urllib.request.build_opener(NoRedirect,urllib.request.HTTPCookieProcessor(cookie_jar))
|
opener= urllib.request.build_opener(NoRedirect,urllib.request.HTTPCookieProcessor(cookies))
|
||||||
urllib.request.install_opener(opener)
|
urllib.request.install_opener(opener)
|
||||||
|
|
||||||
# 1.a Visit 'login' URL. Get 'authorize' location and 'oidcstate' cookie
|
# 1.a Visit 'login' URL. Get 'authorize' location and 'oidcstate' cookie
|
||||||
|
@ -323,20 +344,20 @@ def _perform_login(self, username, password):
|
||||||
print('login')
|
print('login')
|
||||||
print(res.status)
|
print(res.status)
|
||||||
print(res.headers.get_all('Location')[0])
|
print(res.headers.get_all('Location')[0])
|
||||||
# for cookie in cookie_jar:
|
# for cookie in cookies:
|
||||||
# print(f'{cookie.name}={cookie.value}')
|
# print(f'{cookie.name}={cookie.value}')
|
||||||
print("===================================")
|
print("===================================")
|
||||||
|
|
||||||
|
|
||||||
# 1.b Follow redirection: visit 'authorize' URL. Get OIDCXSRF & SESSION cookies
|
# 1.b Follow redirection: visit 'authorize' URL. Get OIDCXSRF & SESSION cookies
|
||||||
res = urllib.request.urlopen(auth_url, None)
|
res = urllib.request.urlopen(auth_url, None)
|
||||||
cookies_dict = {cookie.name: cookie.value for cookie in cookie_jar}
|
# cookies = {cookie.name: cookie.value for cookie in cookies}
|
||||||
cookies_header = f'OIDCXSRF={cookies_dict["OIDCXSRF"]}; SESSION={cookies_dict["SESSION"]}'
|
cookies_header = f'OIDCXSRF={cookies["OIDCXSRF"]}; SESSION={cookies["SESSION"]}'
|
||||||
|
|
||||||
print("===================================")
|
print("===================================")
|
||||||
print('authorize')
|
print('authorize')
|
||||||
print(res.status)
|
print(res.status)
|
||||||
print(cookies_dict)
|
print(cookies)
|
||||||
print("===================================")
|
print("===================================")
|
||||||
|
|
||||||
# sys.exit(0)
|
# sys.exit(0)
|
||||||
|
@ -344,7 +365,7 @@ def _perform_login(self, username, password):
|
||||||
# 2. Perform login
|
# 2. Perform login
|
||||||
headers = {
|
headers = {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Oidcxsrf': cookies_dict["OIDCXSRF"],
|
'Oidcxsrf': cookies["OIDCXSRF"],
|
||||||
'Cookie': cookies_header
|
'Cookie': cookies_header
|
||||||
}
|
}
|
||||||
post_data = { "loginID": f"{username}", "password": f"{password}", "clientId": "vrtnu-site" }
|
post_data = { "loginID": f"{username}", "password": f"{password}", "clientId": "vrtnu-site" }
|
||||||
|
@ -357,6 +378,8 @@ def _perform_login(self, username, password):
|
||||||
|
|
||||||
# sys.exit(0)
|
# sys.exit(0)
|
||||||
|
|
||||||
|
# TODO: re-enable auto redir here and do step 3 in one urlopen() call?
|
||||||
|
|
||||||
# 3.a Visit 'authorize' again
|
# 3.a Visit 'authorize' again
|
||||||
headers = {
|
headers = {
|
||||||
'Host': 'login.vrt.be',
|
'Host': 'login.vrt.be',
|
||||||
|
@ -370,13 +393,13 @@ def _perform_login(self, username, password):
|
||||||
print('authorize')
|
print('authorize')
|
||||||
print(res.status)
|
print(res.status)
|
||||||
print(res.headers.get_all('Location')[0])
|
print(res.headers.get_all('Location')[0])
|
||||||
# print(cookies_dict)
|
# print(cookies)
|
||||||
# print(json.dumps(tokens))
|
# print(json.dumps(tokens))
|
||||||
print("===================================")
|
print("===================================")
|
||||||
|
|
||||||
# 3.b Visit 'callback'
|
# 3.b Visit 'callback'
|
||||||
headers = {
|
headers = {
|
||||||
'Cookie': f'oidcstate={cookies_dict["oidcstate"]}'
|
'Cookie': f'oidcstate={cookies["oidcstate"]}'
|
||||||
}
|
}
|
||||||
request = urllib.request.Request(callback_url, headers=headers)
|
request = urllib.request.Request(callback_url, headers=headers)
|
||||||
res = urllib.request.urlopen(request, None)
|
res = urllib.request.urlopen(request, None)
|
||||||
|
@ -385,8 +408,11 @@ def _perform_login(self, username, password):
|
||||||
print('callback')
|
print('callback')
|
||||||
print(res.status)
|
print(res.status)
|
||||||
print(res.headers)
|
print(res.headers)
|
||||||
# print(cookies_dict)
|
print(cookies)
|
||||||
# print(json.dumps(tokens))
|
# print(json.dumps(tokens))
|
||||||
|
|
||||||
|
# for cookie in cookies:
|
||||||
|
# print(f'{cookie.name}={cookie.value}')
|
||||||
print("===================================")
|
print("===================================")
|
||||||
|
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
Loading…
Reference in a new issue