From 650e521cb9ae7bd94bdd52d02f58c6fdf3239e96 Mon Sep 17 00:00:00 2001 From: Cheap Nightbot <151442433+CheapNightbot@users.noreply.github.com> Date: Mon, 9 Dec 2024 14:08:10 +0900 Subject: [PATCH] Update musicyt.py --- yutify/musicyt.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/yutify/musicyt.py b/yutify/musicyt.py index 76c08eb..b36adf6 100644 --- a/yutify/musicyt.py +++ b/yutify/musicyt.py @@ -97,22 +97,33 @@ def _get_song(self, result: dict) -> None: song_url = f"https://music.youtube.com/watch?v={video_id}" lyrics_id = self.ytmusic.get_watch_playlist(video_id) - song_data = self.ytmusic.get_song(video_id) - release_date = song_data.get("microformat").get("microformatDataRenderer").get("uploadDate") - # Get the index of "T" and slice the string after that or only get string before "T". - # For example, "2016-10-18T11:00:03-07:00" will become "2016-10-18" - release_date: str = release_date[:release_date.index("T")] + try: + song_data = self.ytmusic.get_song(video_id) + release_date = ( + song_data.get("microformat") + .get("microformatDataRenderer") + .get("uploadDate") + ) + # Get the index of "T" and slice the string after that or only get string before "T". + # For example, "2016-10-18T11:00:03-07:00" will become "2016-10-18" + release_date: str = release_date[: release_date.index("T")] + except exceptions.YTMusicServerError: + song_data = {} try: lyrics = self.ytmusic.get_lyrics(lyrics_id.get("lyrics")) except exceptions.YTMusicUserError: lyrics = {} + try: album_art = result["thumbnails"][-1]["url"] except KeyError: - album_art = song_data["videoDetails"]["thumbnail"]["thumbnails"][-1][ - "url" - ] + album_art = ( + song_data.get("videoDetails") + .get("thumbnail") + .get("thumbnails")[-1] + .get("url") + ) self.music_info.append( { "album_art": album_art,