async def process_song(self, track):
        """Adds the track to the playlist instance and plays it, if it is the first song"""

        host = linkutils.identify_url(track)
        is_playlist = linkutils.identify_playlist(track)

        if is_playlist != linkutils.Playlist_Types.Unknown:

            if len(self.playlist.playque) == 0:
                start = True
            else:
                start = False

            await self.process_playlist(is_playlist, track)

            if is_playlist == linkutils.Playlist_Types.Spotify_Playlist:
                song = Song(linkutils.Origins.Playlist,
                            linkutils.Sites.Spotify, "", "", "", "", "")

            if is_playlist == linkutils.Playlist_Types.YouTube_Playlist:
                song = Song(linkutils.Origins.Playlist,
                            linkutils.Sites.YouTube, "", "", "", "", "")

            if start == True:
                await self.play_song(self.playlist.playque[0])
                print("Playing {}".format(track))
            return song

        if host == linkutils.Sites.Unknown:
            if linkutils.get_url(track) is not None:
                return None

            track = await self.search_youtube(track)

        if host == linkutils.Sites.Spotify:
            title = linkutils.convert_spotify(track)
            track = await self.search_youtube(title)

        try:
            downloader = youtube_dl.YoutubeDL({
                'format': 'bestaudio',
                'title': True
            })
            r = downloader.extract_info(track, download=False)
        except:
            downloader = youtube_dl.YoutubeDL({'title': True})
            r = downloader.extract_info(track, download=False)

        song = Song(linkutils.Origins.Default, host, r.get('url'),
                    r.get('uploader'), r.get('title'), r.get('duration'),
                    r.get('webpage_url'))

        self.playlist.add(song)
        if len(self.playlist.playque) == 1:
            print("Playing {}".format(track))
            await self.play_song(song)

        return song
    async def process_song(self, track):
        """Adds the track to the playlist instance and plays it, if it is the first song"""

        host = linkutils.identify_url(track)
        is_playlist = linkutils.identify_playlist(track)

        if is_playlist != linkutils.Playlist_Types.Unknown:

            await self.process_playlist(is_playlist, track)

            if self.current_song == None:
                await self.play_song(self.playlist.playque[0])
                print("Playing {}".format(track))

            song = Song(linkutils.Origins.Playlist,
                        linkutils.Sites.Unknown)
            return song

        if host == linkutils.Sites.Unknown:
            if linkutils.get_url(track) is not None:
                return None

            track = self.search_youtube(track)

        if host == linkutils.Sites.Spotify:
            title = await linkutils.convert_spotify(track)
            track = self.search_youtube(title)

        if host == linkutils.Sites.YouTube:
            track = track.split("&list=")[0]

        try:
            downloader = youtube_dl.YoutubeDL(
                {'format': 'bestaudio', 'title': True, "cookiefile": config.COOKIE_PATH})
            r = downloader.extract_info(
                track, download=False)
        except:
            downloader = youtube_dl.YoutubeDL(
                {'title': True, "cookiefile": config.COOKIE_PATH})
            r = downloader.extract_info(
                track, download=False)

        if r.get('thumbnails') is not None:
            thumbnail = r.get('thumbnails')[len(
                r.get('thumbnails')) - 1]['url']
        else:
            thumbnail = None

        song = Song(linkutils.Origins.Default, host, base_url=r.get('url'), uploader=r.get('uploader'), title=r.get(
            'title'), duration=r.get('duration'), webpage_url=r.get('webpage_url'), thumbnail=thumbnail)

        self.playlist.add(song)
        if self.current_song == None:
            print("Playing {}".format(track))
            await self.play_song(song)

        return song
Esempio n. 3
0
    async def on_raw_reaction_add(self, reaction):

        serv = self.bot.get_guild(reaction.guild_id)

        sett = utils.guild_to_settings[serv]
        button_name = sett.get('button_emote')

        if button_name == "":
            return

        if reaction.emoji.name == button_name:
            channels = serv.text_channels

            for chan in channels:
                if chan.id == reaction.channel_id:
                    if reaction.member == self.bot.user:
                        return

                    try:
                        if reaction.member.voice.channel == None:
                            return
                    except:
                        message = await chan.fetch_message(reaction.message_id)
                        await message.remove_reaction(reaction.emoji,
                                                      reaction.member)
                        return
                    message = await chan.fetch_message(reaction.message_id)
                    await message.remove_reaction(reaction.emoji,
                                                  reaction.member)

            current_guild = utils.get_guild(self.bot, message)
            audiocontroller = utils.guild_to_audiocontroller[current_guild]

            url = linkutils.get_url(message.content)

            host = linkutils.identify_url(url)

            if host == linkutils.Sites.Spotify:
                await audiocontroller.process_song(url)

            if host == linkutils.Sites.Spotify.Spotify_Playlist:
                await audiocontroller.process_song(url)

            if host == linkutils.Sites.YouTube:
                await audiocontroller.process_song(url)
    async def search_youtube(self, title):
        """Searches youtube for the video title and returns the first results video link"""

        # if title is already a link
        if linkutils.get_url(title) is not None:
            return title

        options = {
            'format': 'bestaudio/best',
            'default_search': 'auto',
            'noplaylist': True
        }

        with youtube_dl.YoutubeDL(options) as ydl:
            r = ydl.extract_info(title, download=False)

        videocode = r['entries'][0]['id']

        return "https://www.youtube.com/watch?v={}".format(videocode)