8000 Update cipher and intertube by mahdjourOussama · Pull Request #1 · mahdjourOussama/pytube · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Update cipher and intertube #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 50 additions & 53 deletions pytube/cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
signature and decoding it.

"""

import logging
import re
from itertools import chain
Expand All @@ -28,17 +29,17 @@ class Cipher:
def __init__(self, js: str):
self.transform_plan: List[str] = get_transform_plan(js)
# var_regex = re.compile(r"^\w+\W")
var_regex = re.compile(r"^[\w\$_]+\W") # fix the regex issue that was causing the error in downloading the video
var_regex = re.compile(
r"^[\w\$_]+\W"
) # fix the regex issue that was causing the error in downloading the video
var_match = var_regex.search(self.transform_plan[0])
if not var_match:
raise RegexMatchError(
caller="__init__", pattern=var_regex.pattern
)
raise RegexMatchError(caller="__init__", pattern=var_regex.pattern)
var = var_match.group(0)[:-1]
self.transform_map = get_transform_map(js, var)
self.js_func_patterns = [
r"\w+\.(\w+)\(\w,(\d+)\)",
r"\w+\[(\"\w+\")\]\(\w,(\d+)\)"
r"\w+\[(\"\w+\")\]\(\w,(\d+)\)",
]

self.throttling_plan = get_throttling_plan(js)
Expand All @@ -53,15 +54,15 @@ def calculate_n(self, initial_n: list):

# First, update all instances of 'b' with the list(initial_n)
for i in range(len(self.throttling_array)):
if self.throttling_array[i] == 'b':
if self.throttling_array[i] == "b":
10000 self.throttling_array[i] = initial_n

for step in self.throttling_plan:
curr_func = self.throttling_array[int(step[0])]
if not callable(curr_func):
logger.debug(f'{curr_func} is not callable.')
logger.debug(f'Throttling array:\n{self.throttling_array}\n')
raise ExtractError(f'{curr_func} is not callable.')
logger.debug(f"{curr_func} is not callable.")
logger.debug(f"Throttling array:\n{self.throttling_array}\n")
raise ExtractError(f"{curr_func} is not callable.")

first_arg = self.throttling_array[int(step[1])]

Expand All @@ -71,7 +72,7 @@ def calculate_n(self, initial_n: list):
second_arg = self.throttling_array[int(step[2])]
curr_func(first_arg, second_arg)

self.calculated_n = ''.join(initial_n)
self.calculated_n = "".join(initial_n)
return self.calculated_n

def get_signature(self, ciphered_signature: str) -> str:
Expand Down Expand Up @@ -131,9 +132,7 @@ def parse_function(self, js_func: str) -> Tuple[str, int]:
fn_name, fn_arg = parse_match.groups()
return fn_name, int(fn_arg)

raise RegexMatchError(
caller="parse_function", pattern="js_func_patterns"
)
raise RegexMatchError(caller="parse_function", pattern="js_func_patterns")


def get_initial_function_name(js: str) -> str:
Expand Down Expand Up @@ -167,9 +166,7 @@ def get_initial_function_name(js: str) -> str:
logger.debug("finished regex search, matched: %s", pattern)
return function_match.group(1)

raise RegexMatchError(
caller="get_initial_function_name", pattern="multiple"
)
raise RegexMatchError(caller="get_initial_function_name", pattern="multiple")


def get_transform_plan(js: str) -> List[str]:
Expand Down Expand Up @@ -270,10 +267,11 @@ def get_throttling_function_name(js: str) -> str:
# a.C && (b = a.get("n")) && (b = Bpa[0](b), a.set("n", b),
# Bpa.length || iha("")) }};
# In the above case, `iha` is the relevant function name
r'a\.[a-zA-Z]\s*&&\s*\([a-z]\s*=\s*a\.get\("n"\)\)\s*&&\s*'
r'\([a-z]\s*=\s*([a-zA-Z0-9$]+)(\[\d+\])?\([a-z]\)',
r'a\.[a-zA-Z]\s*&&\s*\([a-z]\s*=\s*a\.get\("n"\)\)\s*&&.*?\|\|\s*([a-z]+)',
r"\([a-z]\s*=\s*([a-zA-Z0-9$]+)(\[\d+\])?\([a-z]\)",
r"\([a-z]\s*=\s*([a-zA-Z0-9$]+)(\[\d+\])\([a-z]\)",
]
logger.debug('Finding throttling function name')
logger.debug("Finding throttling function name")
for pattern in function_patterns:
regex = re.compile(pattern)
function_match = regex.search(js)
Expand All @@ -285,18 +283,17 @@ def get_throttling_function_name(js: str) -> str:
if idx:
idx = idx.strip("[]")
array = re.search(
r'var {nfunc}\s*=\s*(\[.+?\]);'.format(
nfunc=re.escape(function_match.group(1))),
js
r"var {nfunc}\s*=\s*(\[.+?\])".format(
nfunc=re.escape(function_match.group(1))
),
js,
)
if array:
array = array.group(1).strip("[]").split(",")
array = [x.strip() for x in array]
return array[int(idx)]

raise RegexMatchError(
caller="get_throttling_function_name", pattern="multiple"
)
raise RegexMatchError(caller="get_throttling_function_name", pattern="multiple")


def get_throttling_function_code(js: str) -> str:
Expand All @@ -317,7 +314,7 @@ def get_throttling_function_code(js: str) -> str:
match = regex.search(js)

# Extract the code within curly braces for the function itself, and merge any split lines
code_lines_list = find_object_from_startpoint(js, match.span()[1]).split('\n')
code_lines_list = find_object_from_startpoint(js, match.span()[1]).split("\n")
joined_lines = "".join(code_lines_list)

# Prepend function definition (e.g. `Dea=function(a)`)
Expand Down Expand Up @@ -350,7 +347,7 @@ def get_throttling_function_array(js: str) -> List[Any]:
# Not an integer value.
pass

if el == 'null':
if el == "null":
converted_array.append(None)
continue

Expand All @@ -359,17 +356,29 @@ def get_throttling_function_array(js: str) -> List[Any]:
converted_array.append(el[1:-1])
continue

if el.startswith('function'):
if el.startswith("function"):
mapper = (
(r"{for\(\w=\(\w%\w\.length\+\w\.length\)%\w\.length;\w--;\)\w\.unshift\(\w.pop\(\)\)}", throttling_unshift), # noqa:E501
(
r"{for\(\w=\(\w%\w\.length\+\w\.length\)%\w\.length;\w--;\)\w\.unshift\(\w.pop\(\)\)}",
throttling_unshift,
), # noqa:E501
(r"{\w\.reverse\(\)}", throttling_reverse),
(r"{\w\.push\(\w\)}", throttling_push),
(r";var\s\w=\w\[0\];\w\[0\]=\w\[\w\];\w\[\w\]=\w}", throttling_swap),
(r"case\s\d+", throttling_cipher_function),
(r"\w\.splice\(0,1,\w\.splice\(\w,1,\w\[0\]\)\[0\]\)", throttling_nested_splice), # noqa:E501
(
r"\w\.splice\(0,1,\w\.splice\(\w,1,\w\[0\]\)\[0\]\)",
throttling_nested_splice,
), # noqa:E501
(r";\w\.splice\(\w,1\)}", js_splice),
(r"\w\.splice\(-\w\)\.reverse\(\)\.forEach\(function\(\w\){\w\.unshift\(\w\)}\)", throttling_prepend), # noqa:E501
(r"for\(var \w=\w\.length;\w;\)\w\.push\(\w\.splice\(--\w,1\)\[0\]\)}", throttling_reverse), # noqa:E501
(
r"\w\.splice\(-\w\)\.reverse\(\)\.forEach\(function\(\w\){\w\.unshift\(\w\)}\)",
throttling_prepend,
), # noqa:E501
(
r"for\(var \w=\w\.length;\w;\)\w\.push\(\w\.splice\(--\w,1\)\[0\]\)}",
throttling_reverse,
), # noqa:E501
)

found = False
Expand Down Expand Up @@ -417,10 +426,10 @@ def get_throttling_plan(js: str):
matches = step_regex.findall(transform_plan_raw)
transform_steps = []
for match in matches:
if match[4] != '':
transform_steps.append((match[0],match[1],match[4]))
if match[4] != "":
transform_steps.append((match[0], match[1], match[4]))
else:
transform_steps.append((match[0],match[1]))
transform_steps.append((match[0], match[1]))

return transform_steps

Expand Down Expand Up @@ -537,7 +546,7 @@ def throttling_cipher_function(d: list, e: str):
e.split("")
)
"""
h = list('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_')
h = list("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_")
f = 96
# by naming it "this" we can more closely reflect the js
this = list(e)
Expand All @@ -548,9 +557,7 @@ def throttling_cipher_function(d: list, e: str):

for m, l in enumerate(copied_list):
bracket_val = (h.index(l) - h.index(this[m]) + m - 32 + f) % len(h)
this.append(
h[bracket_val]
)
this.append(h[bracket_val])
d[m] = h[bracket_val]
f -= 1

Expand All @@ -577,18 +584,8 @@ def throttling_nested_splice(d: list, e: int):
case that was not considered.
"""
e = throttling_mod_func(d, e)
inner_splice = js_splice(
d,
e,
1,
d[0]
)
js_splice(
d,
0,
1,
inner_splice[0]
)
inner_splice = js_splice(d, e, 1, d[0])
js_splice(d, 0, 1, inner_splice[0])


def throttling_prepend(d: list, e: int):
Expand Down Expand Up @@ -659,10 +656,10 @@ def js_splice(arr: list, start: int, delete_count=None, *items):
if not delete_count or delete_count >= len(arr) - start:
delete_count = len(arr) - start # noqa: N806

deleted_elements = arr[start:start + delete_count]
deleted_elements = arr[start : start + delete_count]

# Splice appropriately.
new_arr = arr[:start] + list(items) + arr[start + delete_count:]
new_arr = arr[:start] + list(items) + arr[start + delete_count :]

# Replace contents of input array
arr.clear()
Expand Down
Loading
0