File size: 5,544 Bytes
ccd4117
 
 
 
 
 
8734cba
3047241
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ccd4117
 
328e22b
d2c119a
eab63b5
ccd4117
 
 
3047241
328e22b
d2c119a
da19fc7
d2c119a
 
3047241
8b82476
 
ccd4117
 
328e22b
d2c119a
 
 
c16d7a9
ccd4117
 
328e22b
d2c119a
 
 
c16d7a9
ccd4117
 
328e22b
d2c119a
 
 
ccd4117
eab63b5
ccd4117
3047241
ccd4117
 
328e22b
d2c119a
 
 
de6c1b0
ccd4117
3047241
eab63b5
ccd4117
3047241
 
ccd4117
3047241
de6c1b0
3047241
 
 
0649fcc
de6c1b0
3047241
0649fcc
ccd4117
328e22b
d2c119a
 
 
de6c1b0
ccd4117
3047241
eab63b5
ccd4117
3047241
ccd4117
 
3047241
de6c1b0
3047241
eab63b5
de6c1b0
3047241
 
 
 
eab63b5
3047241
de6c1b0
ccd4117
328e22b
d2c119a
 
 
17564d0
ccd4117
 
328e22b
 
dc3bd9b
 
 
 
 
 
 
 
9a04477
 
9172cb2
 
 
 
9a04477
eab63b5
9a04477
 
b2b9291
 
 
 
 
 
 
 
eab63b5
 
 
 
 
f12f4af
 
c227094
c16d7a9
c227094
 
c16d7a9
c227094
bfdd549
 
 
 
 
 
 
 
 
 
 
 
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
168
169
170
171
172
173
174
# -*- coding: utf-8 -*-
"""Unit tests for the :class:`StreamQuery <StreamQuery>` class."""
import pytest


@pytest.mark.parametrize(
    ("test_input", "expected"),
    [
        ({"progressive": True}, [18]),
        ({"resolution": "720p"}, [136, 247]),
        ({"res": "720p"}, [136, 247]),
        ({"fps": 30, "resolution": "480p"}, [135, 244]),
        ({"mime_type": "audio/mp4"}, [140]),
        ({"type": "audio"}, [140, 249, 250, 251]),
        ({"subtype": "3gpp"}, []),
        ({"abr": "128kbps"}, [140]),
        ({"bitrate": "128kbps"}, [140]),
        ({"audio_codec": "opus"}, [249, 250, 251]),
        ({"video_codec": "vp9"}, [248, 247, 244, 243, 242, 278]),
        ({"only_audio": True}, [140, 249, 250, 251]),
        ({"only_video": True, "video_codec": "avc1.4d4015"}, [133]),
        ({"adaptive": True, "resolution": "1080p"}, [137, 248]),
        ({"custom_filter_functions": [lambda s: s.itag == 18]}, [18]),
    ],
)
def test_filters(test_input, expected, cipher_signature):
    """Ensure filters produce the expected results."""
    result = [s.itag for s in cipher_signature.streams.filter(**test_input)]
    assert result == expected


@pytest.mark.parametrize("test_input", ["first", "last"])
def test_empty(test_input, cipher_signature):
    """Ensure :meth:`~pytube.StreamQuery.last` and
    :meth:`~pytube.StreamQuery.first` return None if the resultset is
    empty.
    """
    query = cipher_signature.streams.filter(video_codec="vp20")
    fn = getattr(query, test_input)
    assert fn() is None


def test_get_last(cipher_signature):
    """Ensure :meth:`~pytube.StreamQuery.last` returns the expected
    :class:`Stream <Stream>`.
    """
    assert cipher_signature.streams[-1].itag == 251


def test_get_first(cipher_signature):
    """Ensure :meth:`~pytube.StreamQuery.first` returns the expected
    :class:`Stream <Stream>`.
    """
    assert cipher_signature.streams[0].itag == 18


def test_order_by(cipher_signature):
    """Ensure :meth:`~pytube.StreamQuery.order_by` sorts the list of
    :class:`Stream <Stream>` instances in the expected order.
    """
    itags = [
        s.itag for s in cipher_signature.streams.filter(type="audio").order_by("itag")
    ]
    assert itags == [140, 249, 250, 251]


def test_order_by_descending(cipher_signature):
    """Ensure :meth:`~pytube.StreamQuery.desc` sorts the list of
    :class:`Stream <Stream>` instances in the reverse order.
    """
    # numerical values
    itags = [
        s.itag
        for s in cipher_signature.streams.filter(type="audio").order_by("itag").desc()
    ]
    assert itags == [251, 250, 249, 140]


def test_order_by_non_numerical(cipher_signature):
    mime_types = [
        s.mime_type
        for s in cipher_signature.streams.filter(res="360p")
        .order_by("mime_type")
        .desc()
    ]
    assert mime_types == ["video/webm", "video/mp4", "video/mp4"]


def test_order_by_ascending(cipher_signature):
    """Ensure :meth:`~pytube.StreamQuery.desc` sorts the list of
    :class:`Stream <Stream>` instances in ascending order.
    """
    # numerical values
    itags = [
        s.itag
        for s in cipher_signature.streams.filter(type="audio").order_by("itag").asc()
    ]
    assert itags == [140, 249, 250, 251]


def test_order_by_non_numerical_ascending(cipher_signature):
    mime_types = [
        s.mime_type
        for s in cipher_signature.streams.filter(res="360p").order_by("mime_type").asc()
    ]
    assert mime_types == ["video/mp4", "video/mp4", "video/webm"]


def test_order_by_with_none_values(cipher_signature):
    abrs = [s.abr for s in cipher_signature.streams.order_by("abr").asc()]
    assert abrs == ["50kbps", "70kbps", "96kbps", "128kbps", "160kbps"]


def test_get_by_itag(cipher_signature):
    """Ensure :meth:`~pytube.StreamQuery.get_by_itag` returns the expected
    :class:`Stream <Stream>`.
    """
    assert cipher_signature.streams.get_by_itag(18).itag == 18


def test_get_by_non_existent_itag(cipher_signature):
    assert not cipher_signature.streams.get_by_itag(22983)


def test_get_by_resolution(cipher_signature):
    assert cipher_signature.streams.get_by_resolution("360p").itag == 18


def test_get_lowest_resolution(cipher_signature):
    assert cipher_signature.streams.get_lowest_resolution().itag == 18


def test_get_highest_resolution(cipher_signature):
    assert cipher_signature.streams.get_highest_resolution().itag == 18


def test_filter_is_dash(cipher_signature):
    streams = cipher_signature.streams.filter(is_dash=False)
    itags = [s.itag for s in streams]
    assert itags == [18, 398, 397, 396, 395, 394]


def test_get_audio_only(cipher_signature):
    assert cipher_signature.streams.get_audio_only().itag == 140


def test_get_audio_only_with_subtype(cipher_signature):
    assert cipher_signature.streams.get_audio_only(subtype="webm").itag == 251


def test_sequence(cipher_signature):
    assert len(cipher_signature.streams) == 22
    assert cipher_signature.streams[0] is not None


def test_otf(cipher_signature):
    non_otf = cipher_signature.streams.otf()
    assert len(non_otf) == 22

    otf = cipher_signature.streams.otf(True)
    assert len(otf) == 0


def test_repr(cipher_signature):
    assert repr(
        cipher_signature.streams.filter(
            progressive=True, subtype="mp4", resolution="360p"
        )
    ) == (
        '[<Stream: itag="18" mime_type="video/mp4" '
        'res="360p" fps="30fps" vcodec="avc1.42001E" '
        'acodec="mp4a.40.2" progressive="True" type="video">]'
    )