2020-05-10 22:46:12 +00:00
|
|
|
from typing import *
|
2020-03-28 18:38:14 +00:00
|
|
|
import aiohttp
|
|
|
|
import urllib.parse
|
2020-05-10 22:46:12 +00:00
|
|
|
import royalnet.commands as rc
|
|
|
|
|
|
|
|
from .play import PlayCommand
|
2020-03-28 18:38:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FunkwhaleplaylistCommand(PlayCommand):
|
|
|
|
name: str = "funkwhaleplaylist"
|
|
|
|
|
|
|
|
aliases = ["fwp", "fwplaylist", "funkwhalep"]
|
|
|
|
|
|
|
|
description: str = "Cerca una playlist su RoyalWhale e aggiungila alla coda della chat vocale."
|
|
|
|
|
|
|
|
syntax = "{ricerca}"
|
|
|
|
|
|
|
|
def get_embed_color(self):
|
|
|
|
return 0x009FE3
|
|
|
|
|
|
|
|
async def get_urls(self, args):
|
|
|
|
search = urllib.parse.quote(args.joined(require_at_least=1))
|
|
|
|
async with aiohttp.ClientSession() as session:
|
|
|
|
async with session.get(self.config["Funkwhale"]["instance_url"] +
|
|
|
|
f"/api/v1/playlists/?q={search}&ordering=-creation_date&playable=true") as response:
|
2020-05-28 14:51:48 +00:00
|
|
|
if response.status >= 400:
|
|
|
|
raise rc.ExternalError(f"Request returned {response.status}")
|
2020-03-28 18:38:14 +00:00
|
|
|
j = await response.json()
|
|
|
|
if len(j["results"]) < 1:
|
2020-05-10 22:46:12 +00:00
|
|
|
raise rc.UserError("Nessuna playlist trovata con il nome richiesto.")
|
2020-03-28 18:38:14 +00:00
|
|
|
playlist = j["results"][0]
|
|
|
|
playlist_id = playlist["id"]
|
|
|
|
async with session.get(self.config["Funkwhale"]["instance_url"] +
|
|
|
|
f"/api/v1/playlists/{playlist_id}/tracks") as response:
|
2020-05-28 14:51:48 +00:00
|
|
|
if response.status >= 400:
|
|
|
|
raise rc.ExternalError(f"Request returned {response.status}")
|
2020-03-28 18:38:14 +00:00
|
|
|
j = await response.json()
|
|
|
|
return list(map(lambda t: f'{self.config["Funkwhale"]["instance_url"]}{t["track"]["listen_url"]}', j["results"]))
|