hbmartin commited on
Commit
d156421
·
1 Parent(s): 0e082c5

added pagination test

Browse files
pytube/contrib/playlist.py CHANGED
@@ -90,7 +90,7 @@ class Playlist:
90
  try:
91
  html = load_more["content_html"]
92
  except KeyError:
93
- logger.debug("Could not find next pagination url")
94
  return
95
  videos_urls = self._extract_videos(html)
96
  if until_watch_id:
@@ -138,6 +138,9 @@ class Playlist:
138
 
139
  @property
140
  def videos(self) -> Iterable[YouTube]:
 
 
 
141
  for url in self.video_urls:
142
  yield YouTube(url)
143
 
 
90
  try:
91
  html = load_more["content_html"]
92
  except KeyError:
93
+ logger.debug("Could not find content_html")
94
  return
95
  videos_urls = self._extract_videos(html)
96
  if until_watch_id:
 
138
 
139
  @property
140
  def videos(self) -> Iterable[YouTube]:
141
+ """Iterable of YouTube objects representing videos in this playlist
142
+ :rtype: Iterable[YouTube]
143
+ """
144
  for url in self.video_urls:
145
  yield YouTube(url)
146
 
tests/conftest.py CHANGED
@@ -61,3 +61,14 @@ def playlist_html():
61
  )
62
  with gzip.open(file_path, "rb") as f:
63
  return f.read().decode("utf-8")
 
 
 
 
 
 
 
 
 
 
 
 
61
  )
62
  with gzip.open(file_path, "rb") as f:
63
  return f.read().decode("utf-8")
64
+
65
+
66
+ @pytest.fixture
67
+ def playlist_long_html():
68
+ """Youtube playlist HTML loaded on 2020-01-25 from
69
+ https://www.youtube.com/playlist?list=PLzMcBGfZo4-mP7qA9cagf68V06sko5otr"""
70
+ file_path = os.path.join(
71
+ os.path.dirname(os.path.realpath(__file__)), "mocks", "playlist_long.html.gz"
72
+ )
73
+ with gzip.open(file_path, "rb") as f:
74
+ return f.read().decode("utf-8")
tests/contrib/test_playlist.py CHANGED
@@ -127,3 +127,20 @@ def test_trimmed(request_get, playlist_html):
127
  "https://www.youtube.com/watch?v=ujTCoH21GlA",
128
  "https://www.youtube.com/watch?v=45ryDIPHdGg",
129
  ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
  "https://www.youtube.com/watch?v=ujTCoH21GlA",
128
  "https://www.youtube.com/watch?v=45ryDIPHdGg",
129
  ]
130
+
131
+
132
+ @mock.patch("pytube.contrib.playlist.request.get")
133
+ def test_playlist_pagination(request_get, playlist_long_html):
134
+ url = "https://www.fakeurl.com/playlist?list=whatever"
135
+ request_get.side_effect = [
136
+ playlist_long_html,
137
+ "{}",
138
+ ]
139
+ playlist = Playlist(url)
140
+ video_urls = playlist.video_urls
141
+ assert len(video_urls) == 100
142
+ assert request_get.call_count == 2
143
+ request_get.assert_called_with(
144
+ "https://www.youtube.com/browse_ajax?action_continuation=1&continuation"
145
+ "=4qmFsgIsEhpWTFVVYS12aW9HaGUyYnRCY1puZWFQb25LQRoOZWdaUVZEcERSMUUlM0Q%253D"
146
+ )