File size: 4,256 Bytes
97f04a9
 
 
cd0bfbf
97f04a9
8dfb028
 
97f04a9
24c3a19
97f04a9
 
328e22b
82321d6
 
328e22b
97f04a9
 
328e22b
82321d6
328e22b
97f04a9
 
 
f9962fa
82321d6
f9962fa
 
 
82321d6
78b647b
82321d6
ca1059b
78b647b
 
82321d6
78b647b
 
 
f9962fa
328e22b
82321d6
97f04a9
82321d6
 
97f04a9
 
82321d6
328e22b
97f04a9
 
 
328e22b
 
97f04a9
 
 
328e22b
 
97f04a9
24c3a19
 
328e22b
24c3a19
328e22b
24c3a19
82321d6
24c3a19
82321d6
 
24c3a19
 
82321d6
328e22b
24c3a19
 
 
 
 
 
 
 
328e22b
24c3a19
328e22b
24c3a19
82321d6
24c3a19
82321d6
 
24c3a19
 
82321d6
328e22b
24c3a19
 
 
 
8dfb028
328e22b
 
24c3a19
82321d6
24c3a19
 
 
 
8dfb028
328e22b
 
24c3a19
 
 
 
 
 
 
8dfb028
328e22b
 
24c3a19
134b939
 
24c3a19
 
 
 
8dfb028
328e22b
 
24c3a19
 
 
 
 
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
# -*- coding: utf-8 -*-
import random

from unittest import mock

import pytest

from pytube import request
from pytube import Stream


def test_filesize(cipher_signature, mocker):
    mocker.patch.object(request, "get")
    request.get.return_value = {"content-length": "6796391"}
    assert cipher_signature.streams.first().filesize == 6796391


def test_default_filename(cipher_signature):
    expected = "PSY - GANGNAM STYLE(๊ฐ•๋‚จ์Šคํƒ€์ผ) MV.mp4"
    stream = cipher_signature.streams.first()
    assert stream.default_filename == expected


def test_title(cipher_signature):
    expected = "PSY - GANGNAM STYLE(๊ฐ•๋‚จ์Šคํƒ€์ผ) M/V"
    stream = cipher_signature.streams.first()
    assert stream.title == expected

    expected = "PSY - GANGNAM STYLE(๊ฐ•๋‚จ์Šคํƒ€์ผ)"
    stream.player_config_args = {
        "player_response": {"videoDetails": {"title": expected}},
    }
    assert stream.title == expected

    expected = "Unknown YouTube Video Title"
    stream.player_config_args = {}
    assert stream.title == expected


def test_download(cipher_signature, mocker):
    mocker.patch.object(request, "get")
    request.get.side_effect = [
        {"content-length": "16384"},
        {"content-length": "16384"},
        iter([str(random.getrandbits(8 * 1024))]),
    ]
    with mock.patch("pytube.streams.open", mock.mock_open(), create=True):
        stream = cipher_signature.streams.first()
        stream.download()


def test_progressive_streams_return_includes_audio_track(cipher_signature):
    stream = cipher_signature.streams.filter(progressive=True).first()
    assert stream.includes_audio_track


def test_progressive_streams_return_includes_video_track(cipher_signature):
    stream = cipher_signature.streams.filter(progressive=True).first()
    assert stream.includes_video_track


def test_on_progress_hook(cipher_signature, mocker):
    callback_fn = mock.MagicMock()
    cipher_signature.register_on_progress_callback(callback_fn)

    mocker.patch.object(request, "get")
    request.get.side_effect = [
        {"content-length": "16384"},
        {"content-length": "16384"},
        iter([str(random.getrandbits(8 * 1024))]),
    ]
    with mock.patch("pytube.streams.open", mock.mock_open(), create=True):
        stream = cipher_signature.streams.first()
        stream.download()
    assert callback_fn.called
    args, _ = callback_fn.call_args
    assert len(args) == 4
    stream, _, _, _ = args
    assert isinstance(stream, Stream)


def test_on_complete_hook(cipher_signature, mocker):
    callback_fn = mock.MagicMock()
    cipher_signature.register_on_complete_callback(callback_fn)

    mocker.patch.object(request, "get")
    request.get.side_effect = [
        {"content-length": "16384"},
        {"content-length": "16384"},
        iter([str(random.getrandbits(8 * 1024))]),
    ]
    with mock.patch("pytube.streams.open", mock.mock_open(), create=True):
        stream = cipher_signature.streams.first()
        stream.download()
    assert callback_fn.called


@pytest.mark.skip
def test_repr_for_audio_streams(cipher_signature):
    stream = str(cipher_signature.streams.filter(only_audio=True).first())
    expected = (
        '<Stream: itag="140" mime_type="audio/mp4" abr="128kbps" ' 'acodec="mp4a.40.2">'
    )
    assert stream == expected


@pytest.mark.skip
def test_repr_for_video_streams(cipher_signature):
    stream = str(cipher_signature.streams.filter(only_video=True).first())
    expected = (
        '<Stream: itag="137" mime_type="video/mp4" res="1080p" '
        'fps="30fps" vcodec="avc1.640028">'
    )
    assert stream == expected


@pytest.mark.skip
def test_repr_for_progressive_streams(cipher_signature):
    stream = str(cipher_signature.streams.filter(progressive=True).first())
    expected = (
        '<Stream: itag="18" mime_type="video/mp4" res="360p" '
        'fps="30fps" vcodec="avc1.42001E" acodec="mp4a.40.2">'
    )
    assert stream == expected


@pytest.mark.skip
def test_repr_for_adaptive_streams(cipher_signature):
    stream = str(cipher_signature.streams.filter(adaptive=True).first())
    expected = (
        '<Stream: itag="137" mime_type="video/mp4" res="1080p" '
        'fps="30fps" vcodec="avc1.640028">'
    )
    assert stream == expected