nficano commited on
Commit
cb3337b
·
2 Parent(s): 07b5aa8 454d2fa

Merge branch 'sonicaj-playlist_exception'

Browse files

* sonicaj-playlist_exception:
lint
fixed bug that caused on_complete to fail
Suppress exception while downloaiding Playlist

Files changed (3) hide show
  1. .gitignore +2 -0
  2. pytube/contrib/playlist.py +31 -23
  3. pytube/streams.py +1 -1
.gitignore CHANGED
@@ -41,5 +41,7 @@ _templates
41
  _autosummary
42
  .pytest_cache*
43
 
 
 
44
  #Pycharm stuff
45
  .idea/*
 
41
  _autosummary
42
  .pytest_cache*
43
 
44
+ # IDE Files
45
+ .idea/
46
  #Pycharm stuff
47
  .idea/*
pytube/contrib/playlist.py CHANGED
@@ -18,16 +18,17 @@ class Playlist(object):
18
  playlist
19
  """
20
 
21
- def __init__(self, url):
22
  self.playlist_url = url
23
  self.video_urls = []
 
24
 
25
  def construct_playlist_url(self):
26
- """There are two kinds of playlist urls in YouTube. One that
27
- contains watch?v= in URL, another one contains the "playlist?list="
28
- portion. It is preferable to work with the later one.
29
 
30
- :return: playlist url -> string
31
  """
32
 
33
  if 'watch?v=' in self.playlist_url:
@@ -41,8 +42,6 @@ class Playlist(object):
41
  def _load_more_url(self, req):
42
  """Given an html page or a fragment thereof, looks for
43
  and returns the "load more" url if found.
44
-
45
- :return: string
46
  """
47
  try:
48
  load_more_url = 'https://www.youtube.com' + re.search(
@@ -57,8 +56,6 @@ class Playlist(object):
57
  """Parse the video links from the page source, extracts and
58
  returns the /watch?v= part from video link href
59
  It's an alternative for BeautifulSoup
60
-
61
- :return: list
62
  """
63
 
64
  url = self.construct_playlist_url()
@@ -120,7 +117,9 @@ class Playlist(object):
120
  return (str(i).zfill(digits) for i in range(start, stop, step))
121
 
122
  def download_all(
123
- self, download_path=None, prefix_number=True,
 
 
124
  reverse_numbering=False,
125
  ):
126
  """Download all the videos in the the playlist. Initially, download
@@ -151,17 +150,26 @@ class Playlist(object):
151
  prefix_gen = self._path_num_prefix_generator(reverse_numbering)
152
 
153
  for link in self.video_urls:
154
- yt = YouTube(link)
155
- # TODO: this should not be hardcoded to a single user's preference
156
- dl_stream = yt.streams.filter(
157
- progressive=True, subtype='mp4',
158
- ).order_by('resolution').desc().first()
159
-
160
- logger.debug('download path: %s', download_path)
161
- if prefix_number:
162
- prefix = next(prefix_gen)
163
- logger.debug('file prefix is: %s', prefix)
164
- dl_stream.download(download_path, filename_prefix=prefix)
165
  else:
166
- dl_stream.download(download_path)
167
- logger.debug('download complete')
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  playlist
19
  """
20
 
21
+ def __init__(self, url, suppress_exception=False):
22
  self.playlist_url = url
23
  self.video_urls = []
24
+ self.suppress_exception = suppress_exception
25
 
26
  def construct_playlist_url(self):
27
+ """There are two kinds of playlist urls in YouTube. One that contains
28
+ watch?v= in URL, another one contains the "playlist?list=" portion. It
29
+ is preferable to work with the later one.
30
 
31
+ :return: playlist url
32
  """
33
 
34
  if 'watch?v=' in self.playlist_url:
 
42
  def _load_more_url(self, req):
43
  """Given an html page or a fragment thereof, looks for
44
  and returns the "load more" url if found.
 
 
45
  """
46
  try:
47
  load_more_url = 'https://www.youtube.com' + re.search(
 
56
  """Parse the video links from the page source, extracts and
57
  returns the /watch?v= part from video link href
58
  It's an alternative for BeautifulSoup
 
 
59
  """
60
 
61
  url = self.construct_playlist_url()
 
117
  return (str(i).zfill(digits) for i in range(start, stop, step))
118
 
119
  def download_all(
120
+ self,
121
+ download_path=None,
122
+ prefix_number=True,
123
  reverse_numbering=False,
124
  ):
125
  """Download all the videos in the the playlist. Initially, download
 
150
  prefix_gen = self._path_num_prefix_generator(reverse_numbering)
151
 
152
  for link in self.video_urls:
153
+ try:
154
+ yt = YouTube(link)
155
+ except Exception as e:
156
+ logger.debug(e)
157
+ if not self.suppress_exception:
158
+ raise e
159
+ else:
160
+ logger.debug('Exception suppressed')
 
 
 
161
  else:
162
+ # TODO: this should not be hardcoded to a single user's
163
+ # preference
164
+ dl_stream = yt.streams.filter(
165
+ progressive=True, subtype='mp4',
166
+ ).order_by('resolution').desc().first()
167
+
168
+ logger.debug('download path: %s', download_path)
169
+ if prefix_number:
170
+ prefix = next(prefix_gen)
171
+ logger.debug('file prefix is: %s', prefix)
172
+ dl_stream.download(download_path, filename_prefix=prefix)
173
+ else:
174
+ dl_stream.download(download_path)
175
+ logger.debug('download complete')
pytube/streams.py CHANGED
@@ -226,7 +226,7 @@ class Stream(object):
226
  bytes_remaining -= len(chunk)
227
  # send to the on_progress callback.
228
  self.on_progress(chunk, fh, bytes_remaining)
229
- self.on_complete(fh)
230
  return fp
231
 
232
  def stream_to_buffer(self):
 
226
  bytes_remaining -= len(chunk)
227
  # send to the on_progress callback.
228
  self.on_progress(chunk, fh, bytes_remaining)
229
+ self.on_complete(fh)
230
  return fp
231
 
232
  def stream_to_buffer(self):