File size: 4,921 Bytes
49627f0
ccd4117
49627f0
 
 
a92dd5e
49627f0
 
 
 
328e22b
d2c119a
328e22b
 
82321d6
 
 
328e22b
 
 
d68bda8
 
d2c119a
328e22b
d68bda8
 
 
 
 
 
 
 
 
b22e0c8
 
 
 
 
 
 
fc9aec5
b22e0c8
 
 
ccd4117
 
 
328e22b
d68bda8
 
328e22b
ccd4117
 
 
328e22b
ccd4117
d68bda8
328e22b
 
 
 
 
d68bda8
 
328e22b
2f4cd16
 
67e6144
 
 
 
 
 
 
 
 
 
 
 
 
 
2f4cd16
 
 
f293f76
 
2f4cd16
ee0deb6
 
 
2f4cd16
7f436b1
 
d156421
 
 
 
 
f293f76
 
d156421
ee0deb6
 
 
d156421
 
 
e0b8c49
 
1ef61ce
 
 
f293f76
 
1ef61ce
 
 
 
 
 
 
 
 
e0b8c49
 
 
 
 
 
d68bda8
e0b8c49
 
d68bda8
 
f293f76
 
 
 
 
 
 
 
 
 
 
 
 
 
642bb0a
 
 
 
cd26b1c
 
 
642bb0a
cd26b1c
 
 
 
 
 
 
 
 
 
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"""Reusable dependency injected testing components."""
import gzip
import json
import os
import pytest
from unittest import mock

from pytube import YouTube


def load_playback_file(filename):
    """Load a gzip json playback file."""
    cur_fp = os.path.realpath(__file__)
    cur_dir = os.path.dirname(cur_fp)
    fp = os.path.join(cur_dir, "mocks", filename)
    with gzip.open(fp, "rb") as fh:
        content = fh.read().decode("utf-8")
        return json.loads(content)


@mock.patch('pytube.request.urlopen')
def load_and_init_from_playback_file(filename, mock_urlopen):
    """Load a gzip json playback file and create YouTube instance."""
    pb = load_playback_file(filename)

    # Mock the responses to YouTube
    mock_url_open_object = mock.Mock()
    mock_url_open_object.read.side_effect = [
        pb['watch_html'].encode('utf-8'),
        pb['js'].encode('utf-8')
    ]
    mock_urlopen.return_value = mock_url_open_object

    # Pytest caches this result, so we can speed up the tests
    #  by causing the object to fetch all the relevant information
    #  it needs. Previously, this was handled by prefetch_init()
    #  and descramble(), but this functionality has since been
    #  deferred
    v = YouTube(pb["url"])
    v.watch_html
    v._vid_info = pb['vid_info']
    v.js
    v.fmt_streams
    return v


@pytest.fixture
def cipher_signature():
    """Youtube instance initialized with video id 2lAe1cqCOXo."""
    filename = "yt-video-2lAe1cqCOXo-html.json.gz"
    return load_and_init_from_playback_file(filename)


@pytest.fixture
def presigned_video():
    """Youtube instance initialized with video id QRS8MkLhQmM."""
    filename = "yt-video-QRS8MkLhQmM-html.json.gz"
    return load_and_init_from_playback_file(filename)


@pytest.fixture
def age_restricted():
    """Youtube instance initialized with video id irauhITDrsE."""
    filename = "yt-video-irauhITDrsE-html.json.gz"
    return load_playback_file(filename)


@pytest.fixture
def private():
    """Youtube instance initialized with video id m8uHb5jIGN8."""
    filename = "yt-video-m8uHb5jIGN8-html.json.gz"
    return load_playback_file(filename)


@pytest.fixture
def missing_recording():
    """Youtube instance initialized with video id 5YceQ8YqYMc."""
    filename = "yt-video-5YceQ8YqYMc-html.json.gz"
    return load_playback_file(filename)


@pytest.fixture
def playlist_html():
    """Youtube playlist HTML loaded on 2020-01-25 from
    https://www.youtube.com/playlist?list=PLzMcBGfZo4-mP7qA9cagf68V06sko5otr
    """
    file_path = os.path.join(
        os.path.dirname(os.path.realpath(__file__)),
        "mocks",
        "playlist.html.gz",
    )
    with gzip.open(file_path, "rb") as f:
        return f.read().decode("utf-8")


@pytest.fixture
def playlist_long_html():
    """Youtube playlist HTML loaded on 2020-01-25 from
    https://www.youtube.com/playlist?list=PLzMcBGfZo4-mP7qA9cagf68V06sko5otr
    """
    file_path = os.path.join(
        os.path.dirname(os.path.realpath(__file__)),
        "mocks",
        "playlist_long.html.gz",
    )
    with gzip.open(file_path, "rb") as f:
        return f.read().decode("utf-8")


@pytest.fixture
def playlist_submenu_html():
    """Youtube playlist HTML loaded on 2020-01-24 from
    https://www.youtube.com/playlist?list=PLZHQObOWTQDMsr9K-rj53DwVRMYO3t5Yr
    """
    file_path = os.path.join(
        os.path.dirname(os.path.realpath(__file__)),
        "mocks",
        "playlist_submenu.html.gz",
    )
    with gzip.open(file_path, "rb") as f:
        return f.read().decode("utf-8")


@pytest.fixture
def stream_dict():
    """Youtube instance initialized with video id WXxV9g7lsFE."""
    file_path = os.path.join(
        os.path.dirname(os.path.realpath(__file__)),
        "mocks",
        "yt-video-WXxV9g7lsFE-html.json.gz",
    )
    with gzip.open(file_path, "rb") as f:
        content = json.loads(f.read().decode("utf-8"))
        return content['watch_html']


@pytest.fixture
def channel_videos_html():
    """Youtube channel HTML loaded on 2021-05-05 from
    https://www.youtube.com/c/ProgrammingKnowledge/videos
    """
    file_path = os.path.join(
        os.path.dirname(os.path.realpath(__file__)),
        "mocks",
        "channel-videos.html.gz",
    )
    with gzip.open(file_path, 'rb') as f:
        return f.read().decode('utf-8')


@pytest.fixture
def base_js():
    """Youtube base.js files retrieved on 2022-02-04 and 2022-04-15
    from https://www.youtube.com/watch?v=vmzxpUsN0uA and
    https://www.youtube.com/watch?v=Y4-GSFKZmEg respectively
    """
    base_js_files = []
    for file in ["base.js-2022-02-04.gz", "base.js-2022-04-15.gz"]:
        file_path = os.path.join(
            os.path.dirname(os.path.realpath(__file__)),
            "mocks",
            file,
        )
        with gzip.open(file_path, 'rb') as f:
            base_js_files.append(f.read().decode('utf-8'))
    return base_js_files