juhee commited on
Commit
b7ec4ba
·
unverified ·
1 Parent(s): 26a50cc

Revise Playlist Extraction to YouTubes architecture changes (#805)

Browse files

* Revise playlist extraction

* Regenerated playlist_long mock

* Adjusted unit test to account for changes

pytube/contrib/playlist.py CHANGED
@@ -179,21 +179,24 @@ class Playlist(Sequence):
179
  ][
180
  "playlistVideoListRenderer"
181
  ]
 
182
  except (KeyError, IndexError, TypeError):
183
  try:
184
  # this is the json tree structure, if the json was directly sent
185
  # by the server in a continuation response
186
- important_content = initial_data[1]["response"][
187
- "continuationContents"
188
- ]["playlistVideoListContinuation"]
 
189
  except (KeyError, IndexError, TypeError) as p:
190
  print(p)
191
  return [], None
192
- videos = important_content["contents"]
193
  try:
194
- continuation = important_content["continuations"][0][
195
- "nextContinuationData"
196
- ]["continuation"]
 
197
  except (KeyError, IndexError):
198
  # if there is an error, no continuation is available
199
  continuation = None
 
179
  ][
180
  "playlistVideoListRenderer"
181
  ]
182
+ videos = important_content["contents"]
183
  except (KeyError, IndexError, TypeError):
184
  try:
185
  # this is the json tree structure, if the json was directly sent
186
  # by the server in a continuation response
187
+ important_content = initial_data[1]['response']['onResponseReceivedActions'][
188
+ 0
189
+ ]['appendContinuationItemsAction']['continuationItems']
190
+ videos = important_content
191
  except (KeyError, IndexError, TypeError) as p:
192
  print(p)
193
  return [], None
194
+
195
  try:
196
+ continuation = videos[-1]['continuationItemRenderer'][
197
+ 'continuationEndpoint'
198
+ ]['continuationCommand']['token']
199
+ videos = videos[:-1]
200
  except (KeyError, IndexError):
201
  # if there is an error, no continuation is available
202
  continuation = None
tests/contrib/test_playlist.py CHANGED
@@ -224,7 +224,7 @@ def test_trimmed_pagination(request_get, playlist_html, playlist_long_html):
224
  "{}",
225
  ]
226
  playlist = Playlist(url)
227
- assert len(list(playlist.trimmed("Gj6fv4pvUwM"))) == 3
228
  assert request_get.call_count == 1
229
 
230
 
 
224
  "{}",
225
  ]
226
  playlist = Playlist(url)
227
+ assert len(list(playlist.trimmed("GTpl5yq3bvk"))) == 3
228
  assert request_get.call_count == 1
229
 
230
 
tests/mocks/playlist_long.html.gz CHANGED
Binary files a/tests/mocks/playlist_long.html.gz and b/tests/mocks/playlist_long.html.gz differ