Skip to content

Commit

Permalink
temporary fix for pytube decipher algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
KoljaWindeler committed Aug 10, 2024
1 parent 2169e60 commit 1e043c0
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions custom_components/ytube_music_player/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from urllib.request import urlopen, Request
from urllib.parse import unquote
import requests
from typing import Any, Callable, Dict, List, Optional, Tuple

import voluptuous as vol
from homeassistant.components.media_player import BrowseError
Expand Down Expand Up @@ -58,6 +59,70 @@ def patched__init__(self, js: str):

pytube.cipher.Cipher.__init__ = patched__init__
################### Temp FIX remove me! ###############################
#https://github.com/pytube/pytube/issues/2005
def patched__get_transform_map__(js: str, var: str) -> Dict:
transform_object = pytube.cipher.get_transform_object(js, var)
mapper = {}
for obj in transform_object:
# AJ:function(a){a.reverse()} => AJ, function(a){a.reverse()}
name, function = obj.split(":", 1)
if function == '"jspb"':
continue
fn = pytube.cipher.map_functions(function)
mapper[name] = fn
return mapper

pytube.cipher.get_transform_map = patched__get_transform_map__


def patched_get_throttling_function_name(js: str) -> str:
"""Extract the name of the function that computes the throttling parameter.
:param str js:
The contents of the base.js asset file.
:rtype: str
:returns:
The name of the function used to compute the throttling parameter.
"""
function_patterns = [
# https://github.com/ytdl-org/youtube-dl/issues/29326#issuecomment-865985377
# https://github.com/yt-dlp/yt-dlp/commit/48416bc4a8f1d5ff07d5977659cb8ece7640dcd8
# var Bpa = [iha];
# ...
# 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*([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')
for pattern in function_patterns:
regex = re.compile(pattern)
function_match = regex.search(js)
if function_match:
logger.debug("finished regex search, matched: %s", pattern)
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"
)

pytube.cipher.get_throttling_function_name = patched_get_throttling_function_name
################### Temp FIX remove me! ###############################
################### Temp FIX remove me! ###############################

_LOGGER = logging.getLogger(__name__)
Expand Down

0 comments on commit 1e043c0

Please sign in to comment.