File size: 11,623 Bytes
5a6acf1
0cf8d94
811e7aa
5e51daa
 
dab9a64
fa7d47d
af1eeee
 
272841b
 
 
 
5e51daa
cbc4903
ee0deb6
 
af1eeee
ee0deb6
dc3bd9b
 
fa7d47d
1319b15
 
272841b
 
 
 
dc3bd9b
 
 
 
fa7d47d
1319b15
 
d2eed62
 
62b6573
d2eed62
dc3bd9b
d2eed62
272841b
 
d2eed62
2f4cd16
 
fa7d47d
af1eeee
 
 
cbc4903
 
272841b
d67515c
cbc4903
d67515c
 
fa7d47d
1319b15
2f4cd16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa7d47d
cf1c020
 
fa7d47d
 
bfdd549
 
 
fa7d47d
bfdd549
fa7d47d
 
 
 
 
 
 
bfdd549
272841b
 
bfdd549
 
 
 
 
 
 
 
 
 
272841b
 
bfdd549
fa7d47d
bfdd549
 
fa7d47d
9b83536
 
 
 
 
 
 
eab63b5
fa7d47d
cf1c020
0bdab6b
cf1c020
 
 
 
fa7d47d
b2b9291
 
fa7d47d
c95bd40
 
 
 
 
 
 
 
 
fa7d47d
c95bd40
 
fa7d47d
b2b9291
 
 
 
 
 
62b6573
 
fa7d47d
62b6573
 
 
 
eea2f04
 
62b6573
 
 
fa7d47d
d156421
 
fa7d47d
 
 
d156421
 
 
fa7d47d
 
 
d156421
 
 
 
fa7d47d
 
272841b
 
 
 
 
 
 
 
 
 
516b351
 
fa7d47d
 
 
516b351
 
fa7d47d
 
 
272841b
 
 
 
516b351
 
 
272841b
fa7d47d
 
acce645
 
fa7d47d
acce645
 
 
 
272841b
 
 
 
acce645
 
 
b7ec4ba
acce645
 
 
272841b
fa7d47d
ee0deb6
 
 
acce645
 
 
272841b
 
 
 
acce645
 
272841b
 
 
1ef61ce
 
 
fa7d47d
af1eeee
1ef61ce
 
 
 
 
 
 
 
 
 
 
af1eeee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import datetime
from unittest import mock

from pytube import Playlist


@mock.patch("pytube.request.get")
def test_title(request_get, playlist_long_html):
    request_get.return_value = playlist_long_html
    url = (
        "https://www.fakeurl.com/playlist?list=PLS1QulWo1RIaJECMeUT4LFwJ"
        "-ghgoSH6n"
    )
    pl = Playlist(url)
    pl_title = pl.title
    assert (
        pl_title
        == "Python Tutorial for Beginners (For Absolute Beginners)"
    )


@mock.patch("pytube.request.get")
def test_init_with_playlist_url(request_get):
    request_get.return_value = ""
    url = (
        "https://www.youtube.com/playlist?list=PLS1QulWo1RIaJECMeUT4LFwJ"
        "-ghgoSH6n"
    )
    playlist = Playlist(url)
    assert playlist.playlist_url == url


@mock.patch("pytube.request.get")
def test_init_with_watch_url(request_get):
    request_get.return_value = ""
    url = (
        "https://www.youtube.com/watch?v=1KeYzjILqDo&"
        "list=PLS1QulWo1RIaJECMeUT4LFwJ-ghgoSH6n&index=2&t=661s"
    )
    playlist = Playlist(url)
    assert (
        playlist.playlist_url == "https://www.youtube.com/playlist?list"
        "=PLS1QulWo1RIaJECMeUT4LFwJ-ghgoSH6n"
    )


@mock.patch("pytube.request.get")
def test_last_updated(request_get, playlist_long_html):
    expected = datetime.date(2020, 10, 8)
    request_get.return_value = playlist_long_html
    playlist = Playlist(
        "https://www.youtube.com/playlist?list"
        "=PLS1QulWo1RIaJECMeUT4LFwJ-ghgoSH6n"
    )
    assert playlist.last_updated == expected


@mock.patch("pytube.request.get")
def test_video_urls(request_get, playlist_html):
    url = "https://www.fakeurl.com/playlist?list=whatever"
    request_get.return_value = playlist_html
    playlist = Playlist(url)
    assert playlist.video_urls == [
        "https://www.youtube.com/watch?v=ujTCoH21GlA",
        "https://www.youtube.com/watch?v=45ryDIPHdGg",
        "https://www.youtube.com/watch?v=1BYu65vLKdA",
        "https://www.youtube.com/watch?v=3AQ_74xrch8",
        "https://www.youtube.com/watch?v=ddqQUz9mZaM",
        "https://www.youtube.com/watch?v=vwLT6bZrHEE",
        "https://www.youtube.com/watch?v=TQKI0KE-JYY",
        "https://www.youtube.com/watch?v=dNBvQ38MlT8",
        "https://www.youtube.com/watch?v=JHxyrMgOUWI",
        "https://www.youtube.com/watch?v=l2I8NycJMCY",
        "https://www.youtube.com/watch?v=g1Zbuk1gAfk",
        "https://www.youtube.com/watch?v=zixd-si9Q-o",
    ]
    request_get.assert_called()


@mock.patch("pytube.request.get")
def test_html(request_get, playlist_html):
    url = "https://www.fakeurl.com/playlist?list=whatever"
    request_get.return_value = playlist_html
    playlist = Playlist(url)
    assert playlist.html == playlist_html
    request_get.assert_called()


@mock.patch("pytube.request.get")
def test_repr(request_get, playlist_html):
    url = "https://www.fakeurl.com/playlist?list=whatever"
    request_get.return_value = playlist_html
    playlist = Playlist(url)
    assert (
        repr(playlist) == "["
        "'https://www.youtube.com/watch?v=ujTCoH21GlA', "
        "'https://www.youtube.com/watch?v=45ryDIPHdGg', "
        "'https://www.youtube.com/watch?v=1BYu65vLKdA', "
        "'https://www.youtube.com/watch?v=3AQ_74xrch8', "
        "'https://www.youtube.com/watch?v=ddqQUz9mZaM', "
        "'https://www.youtube.com/watch?v=vwLT6bZrHEE', "
        "'https://www.youtube.com/watch?v=TQKI0KE-JYY', "
        "'https://www.youtube.com/watch?v=dNBvQ38MlT8', "
        "'https://www.youtube.com/watch?v=JHxyrMgOUWI', "
        "'https://www.youtube.com/watch?v=l2I8NycJMCY', "
        "'https://www.youtube.com/watch?v=g1Zbuk1gAfk', "
        "'https://www.youtube.com/watch?v=zixd-si9Q-o'"
        "]"
    )
    request_get.assert_called()


@mock.patch("pytube.request.get")
def test_sequence(request_get, playlist_html):
    url = "https://www.fakeurl.com/playlist?list=whatever"
    request_get.return_value = playlist_html
    playlist = Playlist(url)
    assert playlist[0] == "https://www.youtube.com/watch?v=ujTCoH21GlA"
    assert len(playlist) == 12


@mock.patch("pytube.request.get")
@mock.patch("pytube.cli.YouTube.__init__", return_value=None)
def test_videos(youtube, request_get, playlist_html):
    url = "https://www.fakeurl.com/playlist?list=whatever"
    request_get.return_value = playlist_html
    playlist = Playlist(url)
    assert len(list(playlist.videos)) == 12
    request_get.assert_called()


@mock.patch("pytube.request.get")
@mock.patch("pytube.cli.YouTube.__init__", return_value=None)
def test_load_more(youtube, request_get, playlist_html):
    url = "https://www.fakeurl.com/playlist?list=whatever"
    request_get.side_effect = [
        playlist_html,
        '{"content_html":"", "load_more_widget_html":""}',
    ]
    playlist = Playlist(url)
    assert len(list(playlist.videos)) == 12
    request_get.assert_called()


@mock.patch("pytube.request.get")
@mock.patch("pytube.contrib.playlist.install_proxy", return_value=None)
def test_proxy(install_proxy, request_get):
    url = "https://www.fakeurl.com/playlist?list=whatever"
    request_get.return_value = ""
    Playlist(url, proxies={"http": "things"})
    install_proxy.assert_called_with({"http": "things"})


@mock.patch("pytube.request.get")
def test_trimmed(request_get, playlist_html):
    url = "https://www.fakeurl.com/playlist?list=whatever"
    request_get.return_value = playlist_html
    playlist = Playlist(url)
    trimmed = list(playlist.trimmed("1BYu65vLKdA"))
    assert trimmed == [
        "https://www.youtube.com/watch?v=ujTCoH21GlA",
        "https://www.youtube.com/watch?v=45ryDIPHdGg",
    ]
    assert request_get.call_count == 1


@mock.patch("pytube.request.get")
@mock.patch("pytube.request.post")
def test_playlist_failed_pagination(request_post, request_get, playlist_long_html):
    url = "https://www.fakeurl.com/playlist?list=whatever"
    request_get.side_effect = [
        playlist_long_html,
    ]
    request_post.side_effect = [
        "{}"
    ]
    playlist = Playlist(url)
    video_urls = playlist.video_urls
    assert len(video_urls) == 100
    assert request_get.call_count == 1
    assert request_post.call_count == 1
    # TODO: Cannot get this test to work probably
    # request_get.assert_called_with(
    #    "https://www.youtube.com/browse_ajax?ctoken" # noqa
    #    "=4qmFsgIsEhpWTFVVYS12aW9HaGUyYnRCY1puZWFQb25LQRoOZWdaUVZEcERSMUUlM0Q" # noqa
    #    "%3D&continuation" # noqa
    #    "=4qmFsgIsEhpWTFVVYS12aW9HaGUyYnRCY1puZWFQb25LQRoOZWdaUVZEcERSMUUlM0Q" # noqa
    #    "%3D", # noqa
    #    {"extra_headers": {'X-YouTube-Client-Name': '1',
    #                       'X-YouTube-Client-Version': '2.20200720.00.02'}}
    # ) # noqa


@mock.patch("pytube.request.get")
@mock.patch("pytube.request.post")
def test_playlist_pagination(request_post, request_get, playlist_html, playlist_long_html):
    url = "https://www.fakeurl.com/playlist?list=whatever"
    request_get.side_effect = [
        playlist_long_html
    ]
    request_post.side_effect = [
        '{"content_html":"<a '
        'href=\\"/watch?v=BcWz41-4cDk&amp;feature=plpp_video&amp;ved'
        '=CCYQxjQYACITCO33n5-pn-cCFUG3xAodLogN2yj6LA\\">}", '
        '"load_more_widget_html":""}',
        "{}",
    ]
    playlist = Playlist(url)
    assert len(playlist.video_urls) == 100
    assert request_get.call_count == 1
    assert request_post.call_count == 1


@mock.patch("pytube.request.get")
def test_trimmed_pagination(request_get, playlist_html, playlist_long_html):
    url = "https://www.fakeurl.com/playlist?list=whatever"
    request_get.side_effect = [
        playlist_long_html,
        '{"content_html":"<a '
        'href=\\"/watch?v=BcWz41-4cDk&amp;feature=plpp_video&amp;ved'
        '=CCYQxjQYACITCO33n5-pn-cCFUG3xAodLogN2yj6LA\\">}", '
        '"load_more_widget_html":""}',
        "{}",
    ]
    playlist = Playlist(url)
    assert len(list(playlist.trimmed("GTpl5yq3bvk"))) == 3
    assert request_get.call_count == 1


# TODO: Test case not clear to me
@mock.patch("pytube.request.get")
def test_trimmed_pagination_not_found(
    request_get, playlist_html, playlist_long_html
):
    url = "https://www.fakeurl.com/playlist?list=whatever"
    request_get.side_effect = [
        playlist_long_html,
        '{"content_html":"<a '
        'href=\\"/watch?v=BcWz41-4cDk&amp;feature=plpp_video&amp;ved'
        '=CCYQxjQYACITCO33n5-pn-cCFUG3xAodLogN2yj6LA\\">}", '
        '"load_more_widget_html":""}',
        "{}",
    ]
    playlist = Playlist(url) # noqa
    # assert len(list(playlist.trimmed("wont-be-found"))) == 101 # noqa
    assert True


# test case for playlist with submenus
@mock.patch("pytube.request.get")
def test_playlist_submenu(request_get, playlist_submenu_html):
    url = "https://www.fakeurl.com/playlist?list=whatever"
    request_get.side_effect = [
        playlist_submenu_html,
        '{"content_html":"<a '
        'href=\\"/watch?v=BcWz41-4cDk&amp;feature=plpp_video&amp;ved'
        '=CCYQxjQYACITCO33n5-pn-cCFUG3xAodLogN2yj6LA\\">}", '
        '"load_more_widget_html":""}',
        "{}",
    ]
    playlist = Playlist(url)
    assert len(playlist.video_urls) == 12


@mock.patch("pytube.request.get")
def test_playlist_length(request_get, playlist_long_html):
    url = 'https://www.example.com/playlist?list=whatever'
    request_get.return_value = playlist_long_html
    p = Playlist(url)
    assert p.length == 217


@mock.patch("pytube.request.get")
def test_playlist_description(request_get, playlist_long_html):
    url = 'https://www.example.com/playlist?list=whatever'
    request_get.return_value = playlist_long_html
    p = Playlist(url)
    assert p.description == (
        'Python Object Oriented - Learning Python in '
        "simple and easy steps ,python,xml,script,install, A beginner's "
        'tutorial containing complete knowledge of Python Syntax Object '
        'Oriented Language, Methods, Tuples,Learn,Python,Tutorial,Interactive,'
        'Free, Tools/Utilities,Getting the most popular pages from your Apache'
        ' logfile,Make your life easier with Virtualenvwrapper,This site now '
        'runs on Django,PythonForBeginners.com has a new owner,How to use '
        'Pillow, a fork of PIL,How to use the Python Imaging Library,Python '
        'Websites and Tutorials,How to use Envoy,Using Feedparser in Python,'
        'Subprocess and Shell Commands in Python, Exceptions Handling, '
        'Sockets, GUI, Extentions, XML Programming'
    )


@mock.patch("pytube.request.get")
def test_playlist_views(request_get, playlist_long_html):
    url = 'https://www.example.com/playlist?list=whatever'
    request_get.return_value = playlist_long_html
    p = Playlist(url)
    assert p.views == 4617130


@mock.patch("pytube.request.get")
def test_playlist_owner(request_get, playlist_long_html):
    url = 'https://www.example.com/playlist?list=whatever'
    request_get.return_value = playlist_long_html
    p = Playlist(url)
    assert p.owner == 'ProgrammingKnowledge'


@mock.patch("pytube.request.get")
def test_playlist_owner_id(request_get, playlist_long_html):
    url = 'https://www.example.com/playlist?list=whatever'
    request_get.return_value = playlist_long_html
    p = Playlist(url)
    assert p.owner_id == 'UCs6nmQViDpUw0nuIx9c_WvA'


@mock.patch("pytube.request.get")
def test_playlist_owner_url(request_get, playlist_long_html):
    url = 'https://www.example.com/playlist?list=whatever'
    request_get.return_value = playlist_long_html
    p = Playlist(url)
    assert p.owner_url == 'https://www.youtube.com/channel/UCs6nmQViDpUw0nuIx9c_WvA'