How to use the pytube.exceptions.PytubeError 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_json_data(self, html):
        """Extract the json out from the html.

        :param str html:
            The raw html of the page.
        """
        # 18 represents the length of "ytplayer.config = ".
        if isinstance(html, str):
            json_start_pattern = 'ytplayer.config = '
        else:
            json_start_pattern = bytes('ytplayer.config = ', 'utf-8')
        pattern_idx = html.find(json_start_pattern)
        # In case video is unable to play
        if(pattern_idx == -1):
            raise PytubeError('Unable to find start pattern.')
        start = pattern_idx + 18
        html = html[start:]

        offset = self._get_json_offset(html)
        if not offset:
            raise PytubeError('Unable to extract json.')
        if isinstance(html, str):
            json_content = json.loads(html[:offset])
        else:
            json_content = json.loads(html[:offset].decode('utf-8'))

        return json_content
github nficano / pytube / pytube / api.py View on Github external
itag = reg_exp.findall(video_url)
        if itag and len(itag) == 1:
            itag = int(itag[0])
            # Given an itag, refer to the YouTube quality profiles to get the
            # properties (media type, resolution, etc.) of the video.
            quality_profile = QUALITY_PROFILES.get(itag)
            if not quality_profile:
                return itag, None
            # Here we just combine the quality profile keys to the
            # corresponding quality profile, referenced by the itag.
            return itag, dict(list(zip(QUALITY_PROFILE_KEYS, quality_profile)))
        if not itag:
            raise PytubeError('Unable to get encoding profile, no itag found.')
        elif len(itag) > 1:
            log.warn('Multiple itags found: %s', itag)
            raise PytubeError(
                'Unable to get encoding profile, multiple itags '
                'found.',
            )
        return False
github nficano / pytube / pytube / api.py View on Github external
The malformed url-encoded video_url.
        """
        reg_exp = re.compile('itag=(\d+)')
        itag = reg_exp.findall(video_url)
        if itag and len(itag) == 1:
            itag = int(itag[0])
            # Given an itag, refer to the YouTube quality profiles to get the
            # properties (media type, resolution, etc.) of the video.
            quality_profile = QUALITY_PROFILES.get(itag)
            if not quality_profile:
                return itag, None
            # Here we just combine the quality profile keys to the
            # corresponding quality profile, referenced by the itag.
            return itag, dict(list(zip(QUALITY_PROFILE_KEYS, quality_profile)))
        if not itag:
            raise PytubeError('Unable to get encoding profile, no itag found.')
        elif len(itag) > 1:
            log.warn('Multiple itags found: %s', itag)
            raise PytubeError(
                'Unable to get encoding profile, multiple itags '
                'found.',
            )
        return False
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.
        json_object = self._get_json_data(html)
github nficano / pytube / pytube / exceptions.py View on Github external
# -*- coding: utf-8 -*-
"""Library specific exception definitions."""
import sys


class PytubeError(Exception):
    """Base pytube exception that all others inherent.

    This is done to not pollute the built-in exceptions, which *could* result
    in unintended errors being unexpectedly and incorrectly handled within
    implementers code.
    """


class ExtractError(PytubeError):
    """Data extraction based exception."""

    def __init__(self, msg, video_id=None):
        """Construct an instance of a :class:`ExtractError `.

        :param str msg:
            User defined error message.
        :param str video_id:
            A YouTube video identifier.
        """
        if video_id is not None:
            msg = '{video_id}: {msg}'.format(video_id=video_id, msg=msg)

        super(ExtractError, self).__init__(msg)

        self.exc_info = sys.exc_info()