Add library_artist and library_album
authorMarvin Schenkel <marvinschenkel@gmail.com>
Tue, 5 Jul 2022 20:34:37 +0000 (22:34 +0200)
committerMarvin Schenkel <marvinschenkel@gmail.com>
Tue, 5 Jul 2022 20:34:37 +0000 (22:34 +0200)
examples/ytmusic.py
music_assistant/controllers/music/artists.py
music_assistant/models/enums.py
music_assistant/music_providers/ytmusic.py

index 0884c4c273c80ea73242cd39d1c62013a40aadc8..61b34e21d5de66e391893f0e01248242b529d9d3 100644 (file)
@@ -102,10 +102,16 @@ async def main():
 
     async with MusicAssistant(mass_conf) as mass:
         # get some data
-        ytm = mass.music.get_provider(ProviderType.YTMUSIC)
+        ytm = mass.music.get_provider(ProviderType.YTMUSIC)
         # track = await yt.get_track("pE3ju1qS848")
-        album = await ytm.get_album("MPREb_AYetWMZunqA")
-        print(album)
+        # album = await ytm.get_album("MPREb_AYetWMZunqA")
+        # print(album)
+        # start sync
+        # await mass.music.start_sync(schedule=3)
+        artists = await mass.music.artists.count()
+        print(f"Got {artists} artists in library")
+        albums = await mass.music.albums.count()
+        print(f"Got {albums} albums in library")
         # sd = await yt.get_stream_details(track.item_id)
         # print(sd.data)
 
index a06f69f9a5d59bfda5d42a203e91c2749142f469..a3b031567218782d596cef693cf407a414a96ada 100644 (file)
@@ -155,7 +155,7 @@ class ArtistsController(MediaControllerBase[Artist]):
         self, item: Artist, overwrite_existing: bool = False, db: Optional[Db] = None
     ) -> Artist:
         """Add a new item record to the database."""
-        assert item.provider_ids, "Album is missing provider id(s)"
+        assert item.provider_ids, "Artist is missing provider id(s)"
         async with self.mass.database.get_db(db) as db:
             # always try to grab existing item by musicbrainz_id
             cur_item = None
index f4b80d2c7113521018f6e9f02ba6cce549577ce9..7cea294a8df50125a5686a0ce540e3e3d5ff00f3 100644 (file)
@@ -66,6 +66,7 @@ class AlbumType(Enum):
     ALBUM = "album"
     SINGLE = "single"
     COMPILATION = "compilation"
+    EP = "ep"
     UNKNOWN = "unknown"
 
 
index 10f4f63f6e1e3f5256d63547fb5567a2dc80f4c5..eeaf3f66fcd30b31f193af15a09fa98466503aa9 100644 (file)
@@ -112,6 +112,96 @@ class YTMusic(MusicProvider):
                     print(category)
         return parsed_results
 
+    async def get_library_artists(self) -> AsyncGenerator[Artist, None]:
+        """Retrieve all library artists from Youtube Music."""
+        data = {"browseId": "FEmusic_library_corpus_track_artists"}
+        response = await self._post_data(endpoint="browse", data=data)
+        response_artist = response["contents"]["singleColumnBrowseResultsRenderer"][
+            "tabs"
+        ][0]["tabRenderer"]["content"]["sectionListRenderer"]["contents"][1][
+            "itemSectionRenderer"
+        ][
+            "contents"
+        ][
+            0
+        ][
+            "musicShelfRenderer"
+        ][
+            "contents"
+        ]
+        parsed_artists = ytmusicapi.parsers.library.parse_artists(response_artist)
+        for item in parsed_artists:
+            artist = Artist(
+                item_id=item["browseId"], provider=self.type, name=item["artist"]
+            )
+            artist.metadata.images = [
+                MediaItemImage(ImageType.THUMB, thumb["url"])
+                for thumb in item["thumbnails"]
+            ]
+            artist.add_provider_id(
+                MediaItemProviderId(
+                    item_id=str(item["browseId"]), prov_type=self.type, prov_id=self.id
+                )
+            )
+            yield artist
+
+    async def get_library_albums(self) -> AsyncGenerator[Album, None]:
+        """Retrieve all library albums from Youtube Music."""
+        data = {"browseId": "FEmusic_liked_albums"}
+        response = await self._post_data(endpoint="browse", data=data)
+        response_albums = response["contents"]["singleColumnBrowseResultsRenderer"][
+            "tabs"
+        ][0]["tabRenderer"]["content"]["sectionListRenderer"]["contents"][1][
+            "itemSectionRenderer"
+        ][
+            "contents"
+        ][
+            0
+        ][
+            "gridRenderer"
+        ][
+            "items"
+        ]
+        parsed_albums = ytmusicapi.parsers.library.parse_albums(response_albums)
+        for item in parsed_albums:
+            if item["type"] == "Single":
+                album_type = AlbumType.SINGLE
+            elif item["type"] == "EP":
+                album_type = AlbumType.EP
+            elif item["type"] == "Album":
+                album_type = AlbumType.ALBUM
+            else:
+                album_type = AlbumType.UNKNOWN
+            album = Album(
+                item_id=item["browseId"],
+                name=item["title"],
+                year=item["year"],
+                album_type=album_type,
+                provider=self.type,
+            )
+            artists = []
+            for artist in item["artists"]:
+                album_artist = Artist(
+                    item_id=artist["id"], name=artist["name"], provider=self.type
+                )
+                album_artist.add_provider_id(
+                    MediaItemProviderId(
+                        item_id=str(artist["id"]), prov_type=self.type, prov_id=self.id
+                    )
+                )
+                artists.append(album_artist)
+            album.artists = artists
+            album.metadata.images = [
+                MediaItemImage(ImageType.THUMB, thumb["url"])
+                for thumb in item["thumbnails"]
+            ]
+            album.add_provider_id(
+                MediaItemProviderId(
+                    item_id=str(item["browseId"]), prov_type=self.type, prov_id=self.id
+                )
+            )
+            yield album
+
     async def get_album(self, prov_album_id) -> Album:
         """Get full album details by id."""
         data = {"browseId": prov_album_id}