Created
November 22, 2024 03:29
-
-
Save korakot/8625f75bef67c84ba6eb41ad65a2ebee to your computer and use it in GitHub Desktop.
pytube patch - nov 2024
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# From https://github.com/pytube/pytube/issues/2062 | |
import ssl | |
from pytube import YouTube | |
from pytube import request | |
from pytube import extract | |
from pytube.innertube import _default_clients | |
from pytube.exceptions import RegexMatchError | |
_default_clients["ANDROID"]["context"]["client"]["clientVersion"] = "19.08.35" | |
_default_clients["IOS"]["context"]["client"]["clientVersion"] = "19.08.35" | |
_default_clients["ANDROID_EMBED"]["context"]["client"]["clientVersion"] = "19.08.35" | |
_default_clients["IOS_EMBED"]["context"]["client"]["clientVersion"] = "19.08.35" | |
_default_clients["IOS_MUSIC"]["context"]["client"]["clientVersion"] = "6.41" | |
_default_clients["ANDROID_MUSIC"] = _default_clients["ANDROID"] | |
import pytube, re | |
def patched_get_throttling_function_name(js: str) -> str: | |
function_patterns = [ | |
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]\)', | |
] | |
for pattern in function_patterns: | |
regex = re.compile(pattern) | |
function_match = regex.search(js) | |
if function_match: | |
if len(function_match.groups()) == 1: | |
return function_match.group(1) | |
idx = function_match.group(2) | |
if idx: | |
idx = idx.strip("[]") | |
array = re.search( | |
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" | |
) | |
ssl._create_default_https_context = ssl._create_unverified_context | |
pytube.cipher.get_throttling_function_name = patched_get_throttling_function_name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment