hbmartin commited on
Commit
0386ae1
·
1 Parent(s): 25bdc94

target directory for caption download

Browse files
pytube/captions.py CHANGED
@@ -8,7 +8,7 @@ from typing import Dict, Optional
8
  from pytube import request
9
  from html import unescape
10
 
11
- from pytube.helpers import safe_filename
12
 
13
 
14
  class Caption:
@@ -107,8 +107,6 @@ class Caption:
107
  :rtype: str
108
 
109
  """
110
- output_path = output_path or os.getcwd()
111
-
112
  if title.endswith(".srt") or title.endswith(".xml"):
113
  filename = ".".join(title.split(".")[:-1])
114
  else:
@@ -128,7 +126,7 @@ class Caption:
128
  else:
129
  filename += ".xml"
130
 
131
- file_path = os.path.join(output_path, filename)
132
 
133
  with open(file_path, "w", encoding="utf-8") as file_handle:
134
  if srt:
 
8
  from pytube import request
9
  from html import unescape
10
 
11
+ from pytube.helpers import safe_filename, target_directory
12
 
13
 
14
  class Caption:
 
107
  :rtype: str
108
 
109
  """
 
 
110
  if title.endswith(".srt") or title.endswith(".xml"):
111
  filename = ".".join(title.split(".")[:-1])
112
  else:
 
126
  else:
127
  filename += ".xml"
128
 
129
+ file_path = os.path.join(target_directory(output_path), filename)
130
 
131
  with open(file_path, "w", encoding="utf-8") as file_handle:
132
  if srt:
pytube/cli.py CHANGED
@@ -63,7 +63,6 @@ def _perform_args_on_youtube(youtube: YouTube, args: argparse.Namespace) -> None
63
  download_by_resolution(
64
  youtube=youtube, resolution=args.resolution, target=args.target
65
  )
66
- # TODO: default to download_highest_resultion if no actions specified
67
 
68
 
69
  def _parse_args(
@@ -299,7 +298,7 @@ def download_caption(
299
 
300
  caption = youtube.captions.get_by_language_code(lang_code=lang_code)
301
  if caption:
302
- downloaded_path = caption.download(title=youtube.title)
303
  print("Saved caption file to: {}".format(downloaded_path))
304
  else:
305
  print("Unable to find caption with code: {}".format(lang_code))
 
63
  download_by_resolution(
64
  youtube=youtube, resolution=args.resolution, target=args.target
65
  )
 
66
 
67
 
68
  def _parse_args(
 
298
 
299
  caption = youtube.captions.get_by_language_code(lang_code=lang_code)
300
  if caption:
301
+ downloaded_path = caption.download(title=youtube.title, output_path=target)
302
  print("Saved caption file to: {}".format(downloaded_path))
303
  else:
304
  print("Unable to find caption with code: {}".format(lang_code))
pytube/contrib/playlist.py CHANGED
@@ -163,10 +163,10 @@ class Playlist:
163
  prefix_gen = self._path_num_prefix_generator(reverse_numbering)
164
 
165
  for link in self.video_urls:
166
- yt = YouTube(link)
167
  dl_stream = (
168
- yt.streams.get_by_resolution(resolution=resolution)
169
- or yt.streams.get_lowest_resolution()
170
  )
171
  assert dl_stream is not None
172
 
 
163
  prefix_gen = self._path_num_prefix_generator(reverse_numbering)
164
 
165
  for link in self.video_urls:
166
+ youtube = YouTube(link)
167
  dl_stream = (
168
+ youtube.streams.get_by_resolution(resolution=resolution)
169
+ or youtube.streams.get_lowest_resolution()
170
  )
171
  assert dl_stream is not None
172
 
pytube/helpers.py CHANGED
@@ -2,6 +2,7 @@
2
  """Various helper functions implemented by pytube."""
3
  import functools
4
  import logging
 
5
  import pprint
6
  import re
7
  import warnings
@@ -135,3 +136,13 @@ def deprecated(reason: str) -> Callable:
135
  return new_func1
136
 
137
  return decorator
 
 
 
 
 
 
 
 
 
 
 
2
  """Various helper functions implemented by pytube."""
3
  import functools
4
  import logging
5
+ import os
6
  import pprint
7
  import re
8
  import warnings
 
136
  return new_func1
137
 
138
  return decorator
139
+
140
+
141
+ def target_directory(output_path: str) -> str:
142
+ if output_path:
143
+ if not os.path.isabs(output_path):
144
+ output_path = os.path.join(os.getcwd(), output_path)
145
+ else:
146
+ output_path = os.getcwd()
147
+ os.makedirs(output_path, exist_ok=True)
148
+ return output_path
pytube/query.py CHANGED
@@ -284,7 +284,7 @@ class StreamQuery:
284
  """
285
  return self.filter(progressive=True).order_by("resolution").asc().last()
286
 
287
- def get_audio_only(self, codec:str = "mp4") -> Optional[Stream]:
288
  """Get highest bitrate audio stream for given codec (defaults to mp4)
289
 
290
  :param str codec:
@@ -295,7 +295,12 @@ class StreamQuery:
295
  not found.
296
 
297
  """
298
- return self.filter(only_audio=True, codex=codec).order_by("resolution").asc().last()
 
 
 
 
 
299
 
300
  def first(self) -> Optional[Stream]:
301
  """Get the first :class:`Stream <Stream>` in the results.
 
284
  """
285
  return self.filter(progressive=True).order_by("resolution").asc().last()
286
 
287
+ def get_audio_only(self, codec: str = "mp4") -> Optional[Stream]:
288
  """Get highest bitrate audio stream for given codec (defaults to mp4)
289
 
290
  :param str codec:
 
295
  not found.
296
 
297
  """
298
+ return (
299
+ self.filter(only_audio=True, codec=codec)
300
+ .order_by("resolution")
301
+ .asc()
302
+ .last()
303
+ )
304
 
305
  def first(self) -> Optional[Stream]:
306
  """Get the first :class:`Stream <Stream>` in the results.
pytube/streams.py CHANGED
@@ -16,7 +16,7 @@ from typing import Dict, Tuple, Optional, List
16
 
17
  from pytube import extract
18
  from pytube import request
19
- from pytube.helpers import safe_filename
20
  from pytube.itags import get_format_profile
21
  from pytube.monostate import Monostate
22
 
@@ -227,13 +227,6 @@ class Stream:
227
  :rtype: str
228
 
229
  """
230
- if output_path:
231
- if not os.path.isabs(output_path):
232
- output_path = os.path.join(os.getcwd(), output_path)
233
- else:
234
- output_path = os.getcwd()
235
- os.makedirs(output_path, exist_ok=True)
236
-
237
  if filename:
238
  filename = "{filename}.{s.subtype}".format(
239
  filename=safe_filename(filename), s=self
@@ -246,7 +239,7 @@ class Stream:
246
  prefix=safe_filename(filename_prefix), filename=filename,
247
  )
248
 
249
- file_path = os.path.join(output_path, filename)
250
 
251
  if (
252
  skip_existing
 
16
 
17
  from pytube import extract
18
  from pytube import request
19
+ from pytube.helpers import safe_filename, target_directory
20
  from pytube.itags import get_format_profile
21
  from pytube.monostate import Monostate
22
 
 
227
  :rtype: str
228
 
229
  """
 
 
 
 
 
 
 
230
  if filename:
231
  filename = "{filename}.{s.subtype}".format(
232
  filename=safe_filename(filename), s=self
 
239
  prefix=safe_filename(filename_prefix), filename=filename,
240
  )
241
 
242
+ file_path = os.path.join(target_directory(output_path), filename)
243
 
244
  if (
245
  skip_existing