How to use the pytube.compat.urlopen function in pytube

To help you get started, we’ve selected a few pytube examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github nficano / pytube / pytube / api.py View on Github external
def get_video_data(self):
        """Gets the page and extracts out the video data."""
        # Reset the filename incase it was previously set.
        self.title = None
        response = urlopen(self.url)
        if not response:
            raise PytubeError('Unable to open url: {0}'.format(self.url))

        html = response.read()
        if isinstance(html, str):
            restriction_pattern = 'og:restrictions:age'
        else:
            restriction_pattern = bytes('og:restrictions:age', 'utf-8')

        if restriction_pattern in html:
            raise AgeRestricted(
                'Age restricted video. Unable to download '
                'without being signed in.',
            )

        # Extract out the json data from the html response body.
github nficano / pytube / pytube / api.py View on Github external
def _get_cipher(self, signature, url):
        """Gets the signature using the cipher.

        :param str signature:
            The url signature.
        :param str url:
            The url of the javascript file.
        """
        reg_exp = re.compile(r'"signature",\s?([a-zA-Z0-9$]+)\(')
        # Cache the js since `_get_cipher()` will be called for each video.
        if not self._js_cache:
            response = urlopen(url)
            if not response:
                raise PytubeError('Unable to open url: {0}'.format(self.url))
            self._js_cache = response.read().decode()
        try:
            matches = reg_exp.search(self._js_cache)
            if matches:
                # Return the first matching group.
                func = next(g for g in matches.groups() if g is not None)
            # Load js into JS Python interpreter.
            jsi = JSInterpreter(self._js_cache)
            initial_function = jsi.extract_function(func)
            return initial_function([signature])
        except Exception as e:
            raise CipherError("Couldn't cipher the signature. Maybe YouTube "
                              'has changed the cipher algorithm. Notify this '
                              'issue on GitHub: {0}'.format(e))
github nficano / pytube / pytube / request.py View on Github external
):
    """Send an http GET request.

    :param str url:
        The URL to perform the GET request for.
    :param bool headers:
        Only return the http headers.
    :param bool streaming:
        Returns the response body in chunks via a generator.
    :param int chunk_size:
        The size in bytes of each chunk.
    """

    # https://github.com/nficano/pytube/pull/465
    req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
    response = urlopen(req)

    if streaming:
        return stream_response(response, chunk_size)
    elif headers:
        # https://github.com/nficano/pytube/issues/160
        return {k.lower(): v for k, v in response.info().items()}
    return (
        response
        .read()
        .decode('utf-8')
    )