File size: 3,950 Bytes
f7e59f8
67e6144
 
 
 
ee0deb6
67e6144
ee0deb6
 
67e6144
af9a117
f7e59f8
 
e707f93
f7e59f8
25de36d
e7518ec
272841b
e707f93
 
 
 
 
 
 
 
a97679f
 
 
f5188a1
 
 
a97679f
af9a117
a97679f
af9a117
 
67e6144
 
af9a117
f5188a1
 
 
67e6144
af9a117
67e6144
af9a117
 
67e6144
 
 
f5188a1
 
 
67e6144
1e91539
67e6144
1e91539
 
67e6144
 
af9a117
f5188a1
 
 
af9a117
 
 
 
 
 
 
67e6144
 
 
 
 
 
 
 
 
1e91539
67e6144
 
 
 
 
 
 
 
 
 
 
 
af9a117
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# -*- coding: utf-8 -*-
import pytest
from unittest import mock

from pytube import YouTube
from pytube.exceptions import LiveStreamError
from pytube.exceptions import RecordingUnavailable
from pytube.exceptions import RegexMatchError
from pytube.exceptions import VideoUnavailable
from pytube.exceptions import VideoPrivate
from pytube.exceptions import VideoRegionBlocked


def test_video_unavailable():
    try:
        raise VideoUnavailable(video_id="YLnZklYFe7E")
    except VideoUnavailable as e:
        assert e.video_id == "YLnZklYFe7E"  # noqa: PT017
        assert str(e) == "YLnZklYFe7E is unavailable"


def test_regex_match_error():
    try:
        raise RegexMatchError(caller="hello", pattern="*")
    except RegexMatchError as e:
        assert str(e) == "hello: could not find match for *"


def test_live_stream_error():
    # Ensure this can be caught as generic VideoUnavailable exception
    with pytest.raises(VideoUnavailable):
        raise LiveStreamError(video_id='YLnZklYFe7E')
    try:
        raise LiveStreamError(video_id='YLnZklYFe7E')
    except LiveStreamError as e:
        assert e.video_id == 'YLnZklYFe7E'  # noqa: PT017
        assert str(e) == 'YLnZklYFe7E is streaming live and cannot be loaded'


def test_recording_unavailable_error():
    # Ensure this can be caught as generic VideoUnavailable exception
    with pytest.raises(VideoUnavailable):
        raise RecordingUnavailable(video_id='5YceQ8YqYMc')
    try:
        raise RecordingUnavailable(video_id='5YceQ8YqYMc')
    except RecordingUnavailable as e:
        assert e.video_id == '5YceQ8YqYMc'  # noqa: PT017
        assert str(e) == '5YceQ8YqYMc does not have a live stream recording available'


def test_private_error():
    # Ensure this can be caught as generic VideoUnavailable exception
    with pytest.raises(VideoUnavailable):
        raise VideoPrivate('m8uHb5jIGN8')
    try:
        raise VideoPrivate('m8uHb5jIGN8')
    except VideoPrivate as e:
        assert e.video_id == 'm8uHb5jIGN8'  # noqa: PT017
        assert str(e) == 'm8uHb5jIGN8 is a private video'


def test_region_locked_error():
    # Ensure this can be caught as generic VideoUnavailable exception
    with pytest.raises(VideoUnavailable):
        raise VideoRegionBlocked('hZpzr8TbF08')
    try:
        raise VideoRegionBlocked('hZpzr8TbF08')
    except VideoRegionBlocked as e:
        assert e.video_id == 'hZpzr8TbF08'  # noqa: PT017
        assert str(e) == 'hZpzr8TbF08 is not available in your region'


def test_raises_video_private(private):
    with mock.patch('pytube.request.urlopen') as mock_url_open:
        # Mock the responses to YouTube
        mock_url_open_object = mock.Mock()
        mock_url_open_object.read.side_effect = [
            private['watch_html'].encode('utf-8'),
        ]
        mock_url_open.return_value = mock_url_open_object
        with pytest.raises(VideoPrivate):
            YouTube('https://youtube.com/watch?v=m8uHb5jIGN8')


def test_raises_recording_unavailable(missing_recording):
    with mock.patch('pytube.request.urlopen') as mock_url_open:
        # Mock the responses to YouTube
        mock_url_open_object = mock.Mock()
        mock_url_open_object.read.side_effect = [
            missing_recording['watch_html'].encode('utf-8'),
        ]
        mock_url_open.return_value = mock_url_open_object
        with pytest.raises(RecordingUnavailable):
            YouTube('https://youtube.com/watch?v=5YceQ8YqYMc')


def test_raises_video_region_blocked(region_blocked):
    with mock.patch('pytube.request.urlopen') as mock_url_open:
        # Mock the responses to YouTube
        mock_url_open_object = mock.Mock()
        mock_url_open_object.read.side_effect = [
            region_blocked['watch_html'].encode('utf-8')
        ]
        mock_url_open.return_value = mock_url_open_object
        with pytest.raises(VideoRegionBlocked):
            YouTube('https://youtube.com/watch?v=hZpzr8TbF08')