Skip to content

Commit

Permalink
Fix unmaintained pytube 15 issue (see pytube/pytube#1954 (comment))
Browse files Browse the repository at this point in the history
  • Loading branch information
Jierr committed Sep 18, 2024
1 parent 0963d79 commit 0fa607d
Showing 1 changed file with 46 additions and 3 deletions.
49 changes: 46 additions & 3 deletions ytdownload.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,55 @@
import os
import os, re
from time import sleep
from pytube import YouTube, Playlist
from pytube import YouTube, Playlist, cipher
from moviepy.editor import *
import argparse
from multiprocessing import Process, Queue, cpu_count, freeze_support


def 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*' 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 re.RegexMatchError(caller="get_throttling_function_name", pattern="multiple")


cipher.get_throttling_function_name = get_throttling_function_name

processes = {}
max_proccesses = cpu_count()
max_proccesses = max(cpu_count() / 2, 1)
queue = Queue()


Expand Down

0 comments on commit 0fa607d

Please sign in to comment.