hbmartin commited on
Commit
8d06533
·
1 Parent(s): 3fe716b

add resolution parameter to playlist.download_all with helper function in StreamQuery

Browse files
Files changed (2) hide show
  1. pytube/contrib/playlist.py +6 -10
  2. pytube/query.py +28 -1
pytube/contrib/playlist.py CHANGED
@@ -120,13 +120,12 @@ class Playlist:
120
  download_path: Optional[str] = None,
121
  prefix_number: bool = True,
122
  reverse_numbering: bool = False,
 
123
  ) -> None:
124
  """Download all the videos in the the playlist. Initially, download
125
  resolution is 720p (or highest available), later more option
126
  should be added to download resolution of choice
127
 
128
- TODO(nficano): Add option to download resolution of user's choice
129
-
130
  :param download_path:
131
  (optional) Output path for the playlist If one is not
132
  specified, defaults to the current working directory.
@@ -140,6 +139,9 @@ class Playlist:
140
  (optional) Lets you number playlists in reverse, since some
141
  playlists are ordered newest -> oldest.
142
  :type reverse_numbering: bool
 
 
 
143
  """
144
 
145
  self.populate_video_urls()
@@ -156,14 +158,8 @@ class Playlist:
156
  if not self.suppress_exception:
157
  raise e
158
  else:
159
- # TODO: this should not be hardcoded to a single user's
160
- # preference
161
- dl_stream = (
162
- yt.streams.filter(progressive=True, subtype="mp4",)
163
- .order_by("resolution")
164
- .desc()
165
- .first()
166
- )
167
 
168
  logger.debug("download path: %s", download_path)
169
  if prefix_number:
 
120
  download_path: Optional[str] = None,
121
  prefix_number: bool = True,
122
  reverse_numbering: bool = False,
123
+ resolution:str = "720p"
124
  ) -> None:
125
  """Download all the videos in the the playlist. Initially, download
126
  resolution is 720p (or highest available), later more option
127
  should be added to download resolution of choice
128
 
 
 
129
  :param download_path:
130
  (optional) Output path for the playlist If one is not
131
  specified, defaults to the current working directory.
 
139
  (optional) Lets you number playlists in reverse, since some
140
  playlists are ordered newest -> oldest.
141
  :type reverse_numbering: bool
142
+ :param resolution:
143
+ Video resolution i.e. "720p", "480p", "360p", "240p", "144p"
144
+ :type resolution: str
145
  """
146
 
147
  self.populate_video_urls()
 
158
  if not self.suppress_exception:
159
  raise e
160
  else:
161
+ dl_stream = yt.streams.get_by_resolution(resolution=resolution) or yt.streams.get_lowest_resolution()
162
+ assert dl_stream is not None
 
 
 
 
 
 
163
 
164
  logger.debug("download path: %s", download_path)
165
  if prefix_number:
pytube/query.py CHANGED
@@ -221,7 +221,7 @@ class StreamQuery:
221
  """
222
  return self
223
 
224
- def get_by_itag(self, itag) -> Optional[Stream]:
225
  """Get the corresponding :class:`Stream <Stream>` for a given itag.
226
 
227
  :param int itag:
@@ -234,6 +234,33 @@ class StreamQuery:
234
  """
235
  return self.itag_index.get(int(itag))
236
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
237
  def first(self) -> Optional[Stream]:
238
  """Get the first :class:`Stream <Stream>` in the results.
239
 
 
221
  """
222
  return self
223
 
224
+ def get_by_itag(self, itag:int) -> Optional[Stream]:
225
  """Get the corresponding :class:`Stream <Stream>` for a given itag.
226
 
227
  :param int itag:
 
234
  """
235
  return self.itag_index.get(int(itag))
236
 
237
+ def get_by_resolution(self, resolution:str) -> Optional[Stream]:
238
+ """Get the corresponding :class:`Stream <Stream>` for a given resolution.
239
+ Stream must be a progressive mp4.
240
+
241
+ :param str resolution:
242
+ Video resolution i.e. "720p", "480p", "360p", "240p", "144p"
243
+ :rtype: :class:`Stream <Stream>` or None
244
+ :returns:
245
+ The :class:`Stream <Stream>` matching the given itag or None if
246
+ not found.
247
+
248
+ """
249
+ return self.filter(
250
+ progressive=True, subtype='mp4', resolution=resolution
251
+ ).first()
252
+
253
+ def get_lowest_resolution(self) -> Optional[Stream]:
254
+ """Get lowest resolution stream that is a progressive mp4.
255
+
256
+ :rtype: :class:`Stream <Stream>` or None
257
+ :returns:
258
+ The :class:`Stream <Stream>` matching the given itag or None if
259
+ not found.
260
+
261
+ """
262
+ return self.filter(progressive=True, subtype='mp4').order_by('resolution').desc().last()
263
+
264
  def first(self) -> Optional[Stream]:
265
  """Get the first :class:`Stream <Stream>` in the results.
266