Skip to content
This repository has been archived by the owner on Dec 12, 2017. It is now read-only.

Commit

Permalink
Better download error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
desbma committed Dec 12, 2015
1 parent c839542 commit ddba62b
Showing 1 changed file with 34 additions and 29 deletions.
63 changes: 34 additions & 29 deletions canalplus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,35 +91,40 @@ def download(self, dir):
logging.getLogger().info("File already exists, skipping download")
return

with mkstemp_ctx.mkstemp(suffix=".ts") as video_filepath_tmp:
# download
if sys.stdout.isatty() and logging.getLogger().isEnabledFor(logging.INFO):
progress = progress_display.ProgressBar()
else:
progress = None
if self.stream_url.endswith(".m3u8"):
# fetch m3u8 playlist
m3u8_data = self.fetchText(self.stream_url)
# parse it
ts_urls = tuple(filter(lambda x: not x.startswith("#"),
m3u8_data.splitlines()))
# download ts files
self.download_ts(ts_urls, video_filepath_tmp, progress)

else:
# direct stream download
logging.getLogger().info("Downloading video to '%s'..." % (video_filepath_ts))
self.download_ts((self.stream_url,), video_filepath_tmp, progress)

if progress is not None:
progress.updateProgress(100)
progress.display()
progress.end()

# try to remux to mp4
remuxed = self.remuxToMp4(video_filepath_tmp, video_filepath_mp4)
if not remuxed:
shutil.move(video_filepath_tmp, video_filepath_ts)
try:
with mkstemp_ctx.mkstemp(suffix=".ts") as video_filepath_tmp:
# download
if sys.stdout.isatty() and logging.getLogger().isEnabledFor(logging.INFO):
progress = progress_display.ProgressBar()
else:
progress = None
if self.stream_url.endswith(".m3u8"):
# fetch m3u8 playlist
m3u8_data = self.fetchText(self.stream_url)
# parse it
ts_urls = tuple(filter(lambda x: not x.startswith("#"),
m3u8_data.splitlines()))
# download ts files
self.download_ts(ts_urls, video_filepath_tmp, progress)

else:
# direct stream download
logging.getLogger().info("Downloading video to '%s'..." % (video_filepath_ts))
self.download_ts((self.stream_url,), video_filepath_tmp, progress)

if progress is not None:
progress.updateProgress(100)
progress.display()
progress.end()

# try to remux to mp4
remuxed = self.remuxToMp4(video_filepath_tmp, video_filepath_mp4)
if not remuxed:
shutil.move(video_filepath_tmp, video_filepath_ts)

except Exception as e:
logging.getLogger().error("Download failed: %s %s", e.__class__.__qualname__, e)
exit(1)

def download_ts(self, urls, filepath, progress):
""" Download one or several MPEG-TS videos to a file. """
Expand Down

0 comments on commit ddba62b

Please sign in to comment.