Пример #1
0
    async def soundcloud_search(self, song: Song) -> Song:
        """
        Search SoundCloud
        :param song:
        :return:
        """
        term = getattr(song, "title", getattr(song, "term", None))
        if not term:
            raise NoResultsFound(Errors.no_results_found)
        node: Node = self.node_controller.get_best_node(guild_id=song.guild_id)

        response = await node.client.request("soundcloud_search",
                                             term,
                                             response=True,
                                             timeout=10)

        if not response.successful:
            self.log.warning(f"[SC-TERM] {term} {response.text}")
            raise SongExtractionException()

        song_dict: dict = json.loads(response.text)
        song_dict["term"] = term

        song: Song = Song.from_dict(song_dict)
        return song
Пример #2
0
    async def youtube_url(self, url, guild_id: int) -> Song:
        """
        Extract information from YouTube by url
        @param url:
        @param guild_id:
        @return:
        """
        url = VariableStore.youtube_url_to_id(url)
        node: Node = self.node_controller.get_best_node(guild_id)

        response = await node.client.request("youtube_video",
                                             url,
                                             response=True,
                                             timeout=10)

        if not response.successful:
            LOG.warning(f"[YT-URL] {url} {response.text}")
            raise SongExtractionException()

        song_dict: dict = json.loads(response.text)

        if song_dict == {}:
            raise NoResultsFound(Errors.no_results_found)
        song: Song = Song.from_dict(song_dict)
        return song
Пример #3
0
    async def youtube_term(self, song: (Song, str)):
        if isinstance(song, Song):
            if song.term:
                term = song.term
            elif song.title:
                term = song.title
            else:
                return Error(True, Errors.no_results_found)
        else:
            term = song

        node = self.node_controller.get_best_node(guild_id=song.guild_id)
        url = await self.http_post(
            self.term_url.format(node.ip, node.port), term
        )

        if isinstance(url, Error):
            return url

        url = VariableStore.youtube_url_to_id(url)
        sd = await self.http_post(
            url=self.url_url.format(node.ip, node.port), data=url
        )
        if isinstance(sd, Error):
            return sd
        song_dict: dict = json.loads(sd)
        song_dict["term"] = term

        song: Song = Song.from_dict(song_dict)
        return song
Пример #4
0
 async def tts(self, ctx: commands.Context, *, text: str):
     quoted = quote(text)
     if await self.parent.player.join_check(ctx):
         if await self.parent.player.join_channel(ctx):
             song: Song = Song.from_dict(
                 {
                     "guild_id": ctx.guild.id,
                     "stream": f"{self.tts_base_url}{quoted}",
                 }
             )
             self.tts_queue.put_nowait(song)
             if not self.guilds[ctx.guild.id].voice_client.is_playing():
                 await self.next_tts(ctx)
Пример #5
0
    async def youtube_url(self, url, guild_id: int):
        url = VariableStore.youtube_url_to_id(url)
        node = self.node_controller.get_best_node(guild_id)
        sd = await self.http_post(
            url=self.url_url.format(node.ip, node.port), data=url
        )

        if isinstance(sd, Error):
            return sd

        song_dict: dict = json.loads(sd)

        if song_dict == {}:
            return Error(Errors.default)
        song: Song = Song.from_dict(song_dict)
        return song
Пример #6
0
    async def youtube_term(self, song: (Song, str), service: str) -> Song:
        """
        Extract information from YouTube by Term
        @param song:
        @param service: 
        @return:
        """
        term = getattr(song, "title", getattr(song, "term", None))
        if not term:
            raise NoResultsFound(Errors.no_results_found)

        node: Node = self.node_controller.get_best_node(guild_id=song.guild_id)

        response = await node.client.request(
            "youtube_search",
            json.dumps({
                "service": service,
                "term": term
            }),
            response=True,
            timeout=10,
        )

        if not response.successful:
            raise NoResultsFound(response.text)
        url = response.text

        url = VariableStore.youtube_url_to_id(url)

        response = await node.client.request("youtube_video",
                                             url,
                                             response=True,
                                             timeout=10)

        if not response.successful:
            LOG.warning(f"[YT-TERM] {url} {response.text}")
            raise SongExtractionException()

        song_dict: dict = json.loads(response.text)
        song_dict["term"] = term

        song: Song = Song.from_dict(song_dict)
        return song
Пример #7
0
 def test_song_from_dict(self):
     self.assertEqual(
         str(Song.from_dict({"title": "hello", "duration": 10})),
         str(Song(title="hello", duration=10)),
     )