From 61a5da2bfe6ea4fcdb263db46d68ece265065f19 Mon Sep 17 00:00:00 2001 From: Lars Noack Date: Tue, 12 Sep 2023 10:47:25 +0200 Subject: [PATCH] implemented fetching of album list --- src/actual_donwload.py | 4 +- src/music_kraken/objects/song.py | 4 ++ .../pages/youtube_music/youtube_music.py | 49 ++++++++++++++++--- 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/src/actual_donwload.py b/src/actual_donwload.py index 3b2074a..78ecefd 100644 --- a/src/actual_donwload.py +++ b/src/actual_donwload.py @@ -3,7 +3,7 @@ import music_kraken if __name__ == "__main__": normally_download = [ - "s: #a Ghost Bath", + "s: #a Favorite #r Anarcho", "1", "d: 1, 5" ] @@ -29,7 +29,7 @@ if __name__ == "__main__": ] youtube_music_test = [ - "s: psychonaut 4", + "s: #a Favorite #r Anarcho", "0" ] diff --git a/src/music_kraken/objects/song.py b/src/music_kraken/objects/song.py index ee66dc7..3f46d4b 100644 --- a/src/music_kraken/objects/song.py +++ b/src/music_kraken/objects/song.py @@ -113,6 +113,10 @@ class Song(MainObject): if object_type is Song: return + if object_type is Lyrics: + self.lyrics_collection.extend(object_list) + return + if object_type is Artist: self.main_artist_collection.extend(object_list) return diff --git a/src/music_kraken/pages/youtube_music/youtube_music.py b/src/music_kraken/pages/youtube_music/youtube_music.py index f766674..ef6f14d 100644 --- a/src/music_kraken/pages/youtube_music/youtube_music.py +++ b/src/music_kraken/pages/youtube_music/youtube_music.py @@ -1,5 +1,5 @@ from typing import Dict, List, Optional, Set, Type -from urllib.parse import urlparse, urlunparse, quote +from urllib.parse import urlparse, urlunparse, quote, parse_qs import logging import random import json @@ -273,7 +273,6 @@ class YoutubeMusic(SuperYouTube): url = urlparse(source.url) browse_id = url.path.replace("/channel/", "") - # POST https://music.youtube.com/youtubei/v1/browse?key=AIzaSyC9XL3ZjWddXya6X74dJoCTL-WEYFDNX30&prettyPrint=false r = self.connection.post( url=get_youtube_url(path="/youtubei/v1/browse", query=f"key={self.credentials.api_key}&prettyPrint=false"), json={ @@ -307,10 +306,48 @@ class YoutubeMusic(SuperYouTube): return artist + def fetch_album(self, source: Source, stop_at_level: int = 1) -> Album: + album = Album() + + parsed_url = urlparse(source.url) + list_id_list = parse_qs(parsed_url.query)['list'] + if len(list_id_list) <= 0: + return album + browse_id = list_id_list[0] + + r = self.connection.post( + url=get_youtube_url(path="/youtubei/v1/browse", query=f"key={self.credentials.api_key}&prettyPrint=false"), + json={ + "browseId": browse_id, + "context": {**self.credentials.context, "adSignalsInfo":{"params":[]}} + } + ) + if r is None: + return album + + if DEBUG: + dump_to_file(f"{browse_id}.json", r.text, is_json=True, exit_after_dump=False) + + renderer_list = r.json().get("contents", {}).get("singleColumnBrowseResultsRenderer", {}).get("tabs", [{}])[0].get("tabRenderer", {}).get("content", {}).get("sectionListRenderer", {}).get("contents", []) + + if DEBUG: + for i, content in enumerate(renderer_list): + dump_to_file(f"{i}-album-renderer.json", json.dumps(content), is_json=True, exit_after_dump=False) + + results = [] + + """ + cant use fixed indices, because if something has no entries, the list dissappears + instead I have to try parse everything, and just reject community playlists and profiles. + """ + + for renderer in renderer_list: + results.extend(parse_renderer(renderer)) + + album.add_list_of_other_objects(results) + + return album + def fetch_song(self, source: Source, stop_at_level: int = 1) -> Song: print(source) return Song() - - def fetch_album(self, source: Source, stop_at_level: int = 1) -> Album: - return Album() -