File size: 9,108 Bytes
ca1059b 5a6acf1 0cf8d94 2f4cd16 811e7aa 5e51daa dab9a64 17e4141 0cf8d94 91a4ea9 272841b 5e51daa ee0deb6 dc3bd9b 1319b15 272841b dc3bd9b 1319b15 d2eed62 62b6573 d2eed62 dc3bd9b d2eed62 272841b d2eed62 2f4cd16 5a6acf1 272841b 5a6acf1 d67515c 272841b d67515c 2f4cd16 1319b15 2f4cd16 cf1c020 bfdd549 272841b bfdd549 272841b bfdd549 9b83536 eab63b5 cf1c020 0bdab6b cf1c020 b2b9291 c95bd40 b2b9291 62b6573 eea2f04 30b06f6 eea2f04 62b6573 d156421 516b351 d156421 272841b 516b351 272841b 516b351 272841b 516b351 acce645 272841b acce645 272841b acce645 272841b acce645 ee0deb6 acce645 272841b acce645 272841b |
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 |
# -*- coding: utf-8 -*-
import datetime
from unittest import mock
from unittest.mock import MagicMock
from pytube import Playlist
@mock.patch("pytube.contrib.playlist.request.get")
def test_title(request_get):
request_get.return_value = (
"<title>(149) Python Tutorial for Beginners "
"(For Absolute Beginners) - YouTube</title>"
)
url = (
"https://www.fakeurl.com/playlist?list=PLS1QulWo1RIaJECMeUT4LFwJ"
"-ghgoSH6n"
)
pl = Playlist(url)
pl_title = pl.title()
assert (
pl_title
== "(149) Python Tutorial for Beginners (For Absolute Beginners)"
)
@mock.patch("pytube.contrib.playlist.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.contrib.playlist.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.contrib.playlist.request.get")
def test_last_update(request_get, playlist_html):
expected = datetime.date(2020, 3, 11)
request_get.return_value = playlist_html
playlist = Playlist("url")
assert playlist.last_update == expected
@mock.patch("pytube.contrib.playlist.request.get")
def test_init_with_watch_id(request_get):
request_get.return_value = ""
playlist = Playlist("PLS1QulWo1RIaJECMeUT4LFwJ-ghgoSH6n")
assert (
playlist.playlist_url == "https://www.youtube.com/playlist?list"
"=PLS1QulWo1RIaJECMeUT4LFwJ-ghgoSH6n"
)
@mock.patch("pytube.contrib.playlist.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)
playlist._find_load_more_url = MagicMock(return_value=None)
request_get.assert_called()
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",
]
@mock.patch("pytube.contrib.playlist.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)
playlist._find_load_more_url = MagicMock(return_value=None)
request_get.assert_called()
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'"
"]"
)
@mock.patch("pytube.contrib.playlist.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)
playlist._find_load_more_url = MagicMock(return_value=None)
assert playlist[0] == "https://www.youtube.com/watch?v=ujTCoH21GlA"
assert len(playlist) == 12
@mock.patch("pytube.contrib.playlist.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)
playlist._find_load_more_url = MagicMock(return_value=None)
request_get.assert_called()
assert len(list(playlist.videos)) == 12
@mock.patch("pytube.contrib.playlist.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)
playlist._find_load_more_url = MagicMock(side_effect=["dummy", None])
request_get.assert_called()
assert len(list(playlist.videos)) == 12
@mock.patch("pytube.contrib.playlist.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.contrib.playlist.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)
playlist._find_load_more_url = MagicMock(return_value=None)
assert request_get.call_count == 1
trimmed = list(playlist.trimmed("1BYu65vLKdA"))
assert trimmed == [
"https://www.youtube.com/watch?v=ujTCoH21GlA",
"https://www.youtube.com/watch?v=45ryDIPHdGg",
]
@mock.patch("pytube.contrib.playlist.request.get")
def test_playlist_failed_pagination(request_get, playlist_long_html):
url = "https://www.fakeurl.com/playlist?list=whatever"
request_get.side_effect = [
playlist_long_html,
"{}",
]
playlist = Playlist(url)
video_urls = playlist.video_urls
assert len(video_urls) == 100
assert request_get.call_count == 2
# 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.contrib.playlist.request.get")
def test_playlist_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&feature=plpp_video&ved'
'=CCYQxjQYACITCO33n5-pn-cCFUG3xAodLogN2yj6LA\\">}", '
'"load_more_widget_html":""}',
"{}",
]
playlist = Playlist(url)
assert len(playlist.video_urls) == 100
assert request_get.call_count == 2
@mock.patch("pytube.contrib.playlist.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&feature=plpp_video&ved'
'=CCYQxjQYACITCO33n5-pn-cCFUG3xAodLogN2yj6LA\\">}", '
'"load_more_widget_html":""}',
"{}",
]
playlist = Playlist(url)
assert len(list(playlist.trimmed("Gj6fv4pvUwM"))) == 3
assert request_get.call_count == 1
# TODO: Test case not clear to me
@mock.patch("pytube.contrib.playlist.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&feature=plpp_video&ved'
'=CCYQxjQYACITCO33n5-pn-cCFUG3xAodLogN2yj6LA\\">}", '
'"load_more_widget_html":""}',
"{}",
]
playlist = Playlist(url) # noqa
# assert len(list(playlist.trimmed("wont-be-found"))) == 101 # noqa
assert True
|