diff --git a/ipc/methods.py b/ipc/methods.py index ed97f4c..536e789 100644 --- a/ipc/methods.py +++ b/ipc/methods.py @@ -245,7 +245,11 @@ async def getTracks(bot: commands.Bot, data: Dict) -> Dict: payload["tracks"] = [ track.track_id for track in (tracks.tracks if isinstance(tracks, Playlist) else tracks ) ] return payload - + +async def searchAndPlay(player: Player, member: Member, data: Dict) -> None: + payload = await getTracks(player.bot, data) + await addTracks(player, member, payload) + async def shuffleTrack(player: Player, member: Member, data: Dict) -> None: if not player.is_privileged(member): if member in player.shuffle_votes: @@ -699,6 +703,49 @@ async def updateSettings(bot: commands.Bot, data: Dict) -> None: await func.update_settings(guild.id, {"$set": data}) +async def getFeaturedPlaylists(bot: commands.Bot, data: Dict) -> Dict: + locale = data.get("locale", "sv_SE") + limit = data.get("limit", 20) + offset = data.get("offset", 0) + + request_url = f"https://api.spotify.com/v1/browse/featured-playlists?locale={locale}&limit={max(1, min(limit, 50))}&offset={max(0, offset)}" + + node = NodePool.get_node() + result = await node.spotify_client.get_request(request_url) + + return { + "op": "getFeaturedPlaylists", + "userId": data.get("userId"), + "callback": data.get("callback"), + "playlists": [ + { + "id": item.get("id"), + "title": item.get("name"), + "description": item.get("description"), + "imageUrl": item.get("images", [{}])[0].get("url"), + "href": item.get("external_urls", {}).get("spotify") + } + for item in result.get("playlists", {}).get("items", []) + ] + } + +async def getCategoryPlaylists(bot: commands.Bot, data: Dict) -> Dict: + node = NodePool.get_node() + + return { + "op": "getCategoryPlaylists", + "userId": data.get("userId"), + "callback": data.get("callback"), + "playlists": [ + { + "id": category.id, + "title": category.name, + "imageUrl": category.icon, + } + for category in await node.spotify_client.get_categories() + ] + } + METHODS: Dict[str, Union[SystemMethod, PlayerMethod]] = { "initBot": SystemMethod(initBot, credit=0), "initUser": SystemMethod(initUser, credit=2), @@ -725,6 +772,9 @@ METHODS: Dict[str, Union[SystemMethod, PlayerMethod]] = { "updatePosition": PlayerMethod(updatePosition), "toggleAutoplay": PlayerMethod(toggleAutoplay), "updateFilter": PlayerMethod(updateFilter), + "searchAndPlay": PlayerMethod(searchAndPlay, credit=5, auto_connect=True), + "getFeaturedPlaylists": SystemMethod(getFeaturedPlaylists, credit=5), + "getCategoryPlaylists": SystemMethod(getCategoryPlaylists, credit=2) } async def process_methods(ipc_client, bot: commands.Bot, data: Dict) -> None: