Skip to content

Commit

Permalink
Fix crash on finished download
Browse files Browse the repository at this point in the history
* The on_download_ended event might trigger a metadata request after the
  cache has been invalidated, but the new json returned might be missing
a lot of keys now that the stream has ended.
* Instead of overwriting the description with a blank one, keep a copy
  in cache to avoid throwing errors on missing key in json when the hook
triggers a metadata request, which may throw.
  • Loading branch information
glubsy committed Feb 15, 2022
1 parent db61b1e commit 822f741
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions livestream_saver/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ def __init__(
self.done = False
self.error = None
self.mpd = None
self._description = ""

self.output_dir = output_dir
if not self.output_dir.exists():
Expand Down Expand Up @@ -592,8 +593,12 @@ def description(self) -> Optional[str]:
:rtype: str
"""
return self.player_response.get("videoDetails", {}).get("shortDescription")

desc = self.player_response.get("videoDetails", {}).get("shortDescription")
if desc is not None:
# Keep at least the last description in cache just in case we end
# up overwriting it with nothing.
self._description = desc
return desc

# # NOT USED
# def populate_info(self):
Expand Down Expand Up @@ -1401,17 +1406,23 @@ def write_to_file(self, fsrc, fdst, length=0):

def get_metadata_dict(self) -> Dict:
# TODO add more data, refresh those that got stale
thumbnails = self.player_response.get("videoDetails", {})\
.get("thumbnail", {})

thumbnails = {}
try:
thumbnails = self.player_response.get("videoDetails", {})\
.get("thumbnail", {})
except Exception as e:
# This might occur if we invalidated the cache but the stream is not
# live anymore, and "streamingData" key is missing from the json
self.logger.warning(f"Error getting thumbnail metadata value: {e}")

return {
"url": self.url,
"videoId": self.video_id,
"cookie_path": self.session.cookie_path,
"logger": self.logger,
"output_dir": self.output_dir,
"title": self.title,
"description": self.description,
"description": self._description,
"author": self.author,
"isLive": Status.LIVE | Status.VIEWED_LIVE in self.status,
# We'll expect to get an array of thumbnails here
Expand Down

0 comments on commit 822f741

Please sign in to comment.