hbmartin commited on
Commit
96801e7
·
1 Parent(s): 2099c93

print cli message if file already downloaded

Browse files
Files changed (3) hide show
  1. pytube/cli.py +5 -0
  2. pytube/streams.py +21 -15
  3. tests/test_cli.py +1 -0
pytube/cli.py CHANGED
@@ -223,6 +223,11 @@ def _download(
223
  ) -> None:
224
  filesize_megabytes = stream.filesize // 1048576
225
  print(f"{stream.default_filename} | {filesize_megabytes} MB")
 
 
 
 
 
226
  stream.download(output_path=target, filename=filename)
227
  sys.stdout.write("\n")
228
 
 
223
  ) -> None:
224
  filesize_megabytes = stream.filesize // 1048576
225
  print(f"{stream.default_filename} | {filesize_megabytes} MB")
226
+ file_path = stream.get_file_path(filename=filename, output_path=target)
227
+ if stream.exists_at_path(file_path):
228
+ print(f"Already downloaded at:\n{file_path}")
229
+ return
230
+
231
  stream.download(output_path=target, filename=filename)
232
  sys.stdout.write("\n")
233
 
pytube/streams.py CHANGED
@@ -215,22 +215,11 @@ class Stream:
215
  :rtype: str
216
 
217
  """
218
- if filename:
219
- filename = f"{safe_filename(filename)}.{self.subtype}"
220
- else:
221
- filename = self.default_filename
222
-
223
- if filename_prefix:
224
- filename = f"{safe_filename(filename_prefix)}{filename}"
225
-
226
- file_path = os.path.join(target_directory(output_path), filename)
227
 
228
- if (
229
- skip_existing
230
- and os.path.isfile(file_path)
231
- and os.path.getsize(file_path) == self.filesize
232
- ):
233
- # likely the same file, so skip it
234
  logger.debug("file %s already exists, skipping", file_path)
235
  self.on_complete(file_path)
236
  return file_path
@@ -249,6 +238,23 @@ class Stream:
249
  self.on_complete(file_path)
250
  return file_path
251
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
252
  def stream_to_buffer(self) -> io.BytesIO:
253
  """Write the media stream to buffer
254
 
 
215
  :rtype: str
216
 
217
  """
218
+ file_path = self.get_file_path(
219
+ filename=filename, output_path=output_path, filename_prefix=filename_prefix
220
+ )
 
 
 
 
 
 
221
 
222
+ if skip_existing and self.exists_at_path(file_path):
 
 
 
 
 
223
  logger.debug("file %s already exists, skipping", file_path)
224
  self.on_complete(file_path)
225
  return file_path
 
238
  self.on_complete(file_path)
239
  return file_path
240
 
241
+ def get_file_path(
242
+ self,
243
+ filename: Optional[str],
244
+ output_path: Optional[str],
245
+ filename_prefix: Optional[str] = None,
246
+ ) -> str:
247
+ if filename:
248
+ filename = f"{safe_filename(filename)}.{self.subtype}"
249
+ else:
250
+ filename = self.default_filename
251
+ if filename_prefix:
252
+ filename = f"{safe_filename(filename_prefix)}{filename}"
253
+ return os.path.join(target_directory(output_path), filename)
254
+
255
+ def exists_at_path(self, file_path: str) -> bool:
256
+ return os.path.isfile(file_path) and os.path.getsize(file_path) == self.filesize
257
+
258
  def stream_to_buffer(self) -> io.BytesIO:
259
  """Write the media stream to buffer
260
 
tests/test_cli.py CHANGED
@@ -28,6 +28,7 @@ def test_download_when_itag_not_found(youtube, display_streams):
28
  @mock.patch("pytube.Stream")
29
  def test_download_when_itag_is_found(youtube, stream):
30
  stream.itag = 123
 
31
  youtube.streams = StreamQuery([stream])
32
  with patch.object(
33
  youtube.streams, "get_by_itag", wraps=youtube.streams.get_by_itag
 
28
  @mock.patch("pytube.Stream")
29
  def test_download_when_itag_is_found(youtube, stream):
30
  stream.itag = 123
31
+ stream.exists_at_path.return_value = False
32
  youtube.streams = StreamQuery([stream])
33
  with patch.object(
34
  youtube.streams, "get_by_itag", wraps=youtube.streams.get_by_itag