pythonyoutubepytube

pytube.exceptions.RegexMatchError: get_transform_object: could not find match for var for={(.*?)};


While downloading a video using the PyTube library using this code:

yt.streams.get_highest_resolution().download("PATH", f"PATH.mp4")

I get an error:

raise RegexMatchError(caller="get_transform_object", pattern=pattern)
pytube.exceptions.RegexMatchError: get_transform_object: could not find match for var for={(.*?)};

I've seen a lot of fixes on Stack Overflow and in the Git repository of PyTube, but they seem to go into different parts of cypher.py. I would like to know how I could alternate get_transform_object class in cypher.py to match the RegEx check.


Solution

  • Here is a quick fix in the meantime as the library makes an update.

    -> In file .venv/lib/python3.10/site-packages/pytube/cipher.py I am using python 3.10 and my virtual environment is called .venv You just have to find the library pytube and go to the file cipher.py and edit its source code for now.

    -> Find the method get_transform_object and replace it as below

    def get_transform_object(js: str, var: str) -> List[str]:
        pattern = r"var %s={(.*?)};" % re.escape(var)
        logger.debug("getting transform object")
        regex = re.compile(pattern, flags=re.DOTALL)
        transform_match = regex.search(js)
        
        if not transform_match:
            # i commented out the line raising the error
            # raise RegexMatchError(caller="get_transform_object", pattern=pattern)
            return []  # Return an empty list if no match is found
    
        return transform_match.group(1).replace("\n", " ").split(", ")