Exemplo n.º 1
0
    async def genreinfo(self, ctx, *, arg):
        async with ctx.typing():

            username = db.get_user(ctx.author.id)

            if username is None:
                return await ctx.send(
                    f"`You need to first set your Last.fm username with the command`\n```>set [your username]```"
                )

            lastfm_username = username[0][1]
            if not lastfm_username:
                return await ctx.send(
                    f"`You need to first set your Last.fm username with the command`\n```>set [your username]```"
                )

            genre_info_params = {
                "tag": arg,
                "api_key": os.getenv("LASTFM_API_KEY"),
                "format": "json",
                "method": "tag.getInfo"
            }

            r = requests.get("http://ws.audioscrobbler.com/2.0/",
                             params=genre_info_params)
            gidata = r.json()
            await asyncio.sleep(0.25)
            try:
                arg = gidata["tag"]["name"]
            except KeyError:
                return await ctx.send(embed=discord.Embed(
                    description=f"This genre doesn't exist.", colour=0x4a5fc3))
            genre_name = arg.lower()
            genre_info = gidata["tag"]["wiki"]["content"]

            genre_info = genre_info.strip()
            sep = "<a"
            genre_info = genre_info.split(sep, 1)[0]
            if len(genre_info) > 800:
                genre_info = genre_info[:800] + "..."

            if genre_info == "":
                embed = discord.Embed(
                    description=f"*No info has been given about this genre.*",
                    timestamp=datetime.now() - timedelta(hours=2),
                    colour=0x4a5fc3)
            else:
                embed = discord.Embed(description=f"{genre_info}",
                                      timestamp=datetime.now() -
                                      timedelta(hours=2),
                                      colour=0x4a5fc3)

            embed.set_author(
                name=f"Genre info for {lastfm_username} about {genre_name}")
            embed.set_footer(
                text=
                f"Requested by {ctx.author.name}#{ctx.author.discriminator}")

        await ctx.send(embed=embed)
Exemplo n.º 2
0
    async def profile(self, ctx, arg=None):

        if arg is None:
            username = db.get_user(ctx.author.id)
        else:
            username = db.get_user(ctx.message.mentions[0].id)

        if username is None:
            embed = discord.Embed(
                description=
                f"You need to first set your Last.fm username with the command\n`>set [your username]`",
                colour=0x4a5fc3)
            return await ctx.send(embed=embed)

        lastfm_username = username[0][1]

        await ctx.send(f"https://www.last.fm/user/{lastfm_username}")
Exemplo n.º 3
0
    async def spotify(self, ctx, *, arg=None):
        async with ctx.typing():
            
            if arg is None:
                username = db.get_user(ctx.author.id)

                if username is None:
                    embed = discord.Embed(description = f"You need to first set your Last.fm username with the command\n`>set [your username]`", colour = 0x4a5fc3)
                    return await ctx.send(embed=embed)

                lastfm_username = username [0][1];
                if not lastfm_username:
                    embed = discord.Embed(description = f"You need to first set your Last.fm username with the command\n`>set [your username]`", colour = 0x4a5fc3)
                    return await ctx.send(embed=embed)

                recent_tracks_params = {
                    "limit": "1",
                    "user": lastfm_username,
                    "api_key": os.getenv("LASTFM_API_KEY"),
                    "format": "json",
                    "method": "user.getRecentTracks"
                }

                r = requests.get("http://ws.audioscrobbler.com/2.0/", params=recent_tracks_params)
                rtdata = r.json()
                rtinfo = rtdata["recenttracks"]["track"][0]
                track = rtinfo["name"]
                artist = rtinfo["artist"]["#text"]

                np = "@attr" in rtinfo and "nowplaying" in rtinfo["@attr"]
                state = f"*Now playing for {lastfm_username}*" if np else f"*Last scrobbled track for {lastfm_username}*"

                sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials())
                sp_track_url = ""
                results = sp.search(f"{track} {artist}", type="track", limit=20)
                items = results["tracks"]["items"]
                lfm_artist_name_lowercase = f"{artist.lower()}"

                for sp_track in items:
                    sp_track_correct = sp_track["artists"][0]
                    sp_artist_name_lowercase = sp_track_correct["name"].lower()
                    if lfm_artist_name_lowercase == sp_artist_name_lowercase:
                        sp_track_url = sp_track["external_urls"]["spotify"]
                        break

            else:
                sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials())
                results = sp.search(arg, type="track", limit=1)
                sp_track_url = results["tracks"]["items"][0]["external_urls"]["spotify"]

                state = f"*Link requested by {ctx.author.name}#{ctx.author.discriminator}*"

        await ctx.send(f"{state}\n{sp_track_url}")
Exemplo n.º 4
0
    async def nowplaying(self, ctx, arg=None):
        async with ctx.typing():

            username = db.get_user(ctx.author.id)
            author = ctx.message.author
            pfp = author.avatar_url

            if username is None:
                embed = discord.Embed(
                    description=
                    f"You need to first set your Last.fm username with the command\n`>set [your username]`",
                    colour=0x4a5fc3)
                return await ctx.send(embed=embed)

            lastfm_username = username[0][1]
            if not lastfm_username:
                embed = discord.Embed(
                    description=
                    f"You need to first set your Last.fm username with the command\n`>set [your username]`",
                    colour=0x4a5fc3)
                return await ctx.send(embed=embed)

            recent_tracks_params = {
                "limit": "1",
                "user": lastfm_username,
                "api_key": os.getenv("LASTFM_API_KEY"),
                "format": "json",
                "method": "user.getRecentTracks"
            }

            r1 = requests.get("http://ws.audioscrobbler.com/2.0/",
                              params=recent_tracks_params)
            rtdata = r1.json()

            try:
                rtinfo = rtdata["recenttracks"]["track"][0]
            except IndexError:
                embed = discord.Embed(
                    description=
                    f"**You haven't listened to anything yet on Last.fm!**",
                    colour=0x4a5fc3)
                return await ctx.send(embed=embed)
            artist = rtinfo["artist"]["#text"]
            track = rtinfo["name"]
            album = rtinfo["album"]["#text"]
            api_album_cover = rtinfo["image"][-1]["#text"]
            no_file_type_album_cover = api_album_cover.rsplit(".", 1)[0]
            higher_res_album_cover = no_file_type_album_cover.replace(
                "300x300", "700x0", 1)
            total_playcount = rtdata["recenttracks"]["@attr"]["total"]
            track_url = rtinfo["url"]

            track_info_params = {
                "track": track,
                "artist": artist,
                "user": lastfm_username,
                "api_key": os.getenv("LASTFM_API_KEY"),
                "format": "json",
                "method": "track.getInfo"
            }

            r2 = requests.get("http://ws.audioscrobbler.com/2.0/",
                              params=track_info_params)
            rtdata = r2.json()

            try:
                loved = rtdata["track"]["userloved"]
                if loved is "1":
                    loved = "❤️ "
                else:
                    loved = ""
            except KeyError:
                loved = ""

            np = "@attr" in rtinfo and "nowplaying" in rtinfo["@attr"]
            state = "Now playing for" if np else "Last scrobbled track for"

            artist_info_params = {
                "artist": artist,
                "user": lastfm_username,
                "api_key": os.getenv("LASTFM_API_KEY"),
                "format": "json",
                "method": "artist.getInfo"
            }

            r3 = requests.get("http://ws.audioscrobbler.com/2.0/",
                              params=artist_info_params)
            aidata = r3.json()
            try:
                artist_playcount = aidata["artist"]["stats"]["userplaycount"]
            except KeyError:
                artist_playcount = "n/a"
            artist_url = aidata["artist"]["url"]
            try:
                artist_tags = [
                    tag["name"] for tag in aidata["artist"]["tags"]["tag"]
                ]
            except TypeError:
                artist_tags = ""
            artist_tags_string = " ∙ ".join(artist_tags)

            album_info_params = {
                "artist": artist,
                "album": album,
                "user": lastfm_username,
                "api_key": os.getenv("LASTFM_API_KEY"),
                "format": "json",
                "method": "album.getInfo"
            }

            r4 = requests.get("http://ws.audioscrobbler.com/2.0/",
                              params=album_info_params)
            abidata = r4.json()
            try:
                album_url = abidata["album"]["url"]
                if abidata["album"]["userplaycount"] == "1":
                    album_scrobbles = str(
                        abidata["album"]["userplaycount"]) + " album play"
                else:
                    album_scrobbles = str(
                        abidata["album"]["userplaycount"]) + " album plays"
            except KeyError:
                album_url = ""
                album_scrobbles = "n/a"

            track_info_params = {
                "track": track,
                "artist": artist,
                "user": lastfm_username,
                "api_key": os.getenv("LASTFM_API_KEY"),
                "format": "json",
                "method": "track.getInfo"
            }

            r5 = requests.get("http://ws.audioscrobbler.com/2.0/",
                              params=track_info_params)
            trackdata = r5.json()
            try:
                if trackdata["track"]["userplaycount"] == "1":
                    track_scrobbles = str(
                        trackdata["track"]["userplaycount"]) + " track play"
                else:
                    track_scrobbles = str(
                        trackdata["track"]["userplaycount"]) + " track plays"
            except KeyError:
                track_scrobbles = "n/a"

        try:
            if artist_playcount == "1":
                artist_scrobbles = f"{artist_playcount} {artist} play"
            else:
                artist_scrobbles = f"{artist_playcount} {artist} plays"
        except KeyError:
            artist_scrobbles = "n/a"

        if album == "":
            embed = discord.Embed(
                url=track_url,
                title=track,
                description=f"By **[{artist}]({artist_url})**",
                colour=0x4a5fc3)
            embed.set_footer(
                text=
                f"{artist_tags_string.lower()}\n{track_scrobbles} ∙ {artist_scrobbles} ∙ {total_playcount} total plays"
            )
        else:
            embed = discord.Embed(
                url=track_url,
                title=track,
                description=
                f"By **[{artist}]({artist_url})** from **[{album}]({album_url})**",
                colour=0x4a5fc3)

            if arg is "a" or arg is "album":
                embed.set_footer(
                    text=
                    f"{artist_tags_string.lower()}\n{album_scrobbles} ∙ {artist_scrobbles} ∙ {total_playcount} total plays"
                )
            else:
                embed.set_footer(
                    text=
                    f"{artist_tags_string.lower()}\n{track_scrobbles} ∙ {artist_scrobbles} ∙ {total_playcount} total plays"
                )

        embed.set_author(name=f"{loved}{state} {lastfm_username}",
                         url=f"https://www.last.fm/user/{lastfm_username}",
                         icon_url=pfp)
        embed.set_thumbnail(url=higher_res_album_cover)

        await ctx.send(embed=embed)
Exemplo n.º 5
0
    async def whoknows(self, ctx, *, arg=None):
        async with ctx.typing():

            if arg is None:
                username = db.get_user(ctx.author.id)

                if username is None:
                  return await ctx.send(f"`You need to first set your Last.fm username with the command`\n```>set [your username]```")

                lastfm_username = username [0][1];
                if not lastfm_username:
                    return await ctx.send(f"`You need to first set your Last.fm username with the command`\n```>set [your username]```")

                recent_tracks_params = {
                    "limit": "1",
                    "user": lastfm_username,
                    "api_key": os.getenv("LASTFM_API_KEY"),
                    "format": "json",
                    "method": "user.getRecentTracks"
                }

                r = requests.get("http://ws.audioscrobbler.com/2.0/", params=recent_tracks_params)
                rtdata = r.json()
                rtinfo = rtdata["recenttracks"]["track"][0]
                artistname = rtinfo["artist"]["#text"]

                artist_info_params = {
                "artist": artistname,
                "api_key": os.getenv("LASTFM_API_KEY"),
                "format": "json",
                "method": "artist.getInfo"
                }
        
                r = requests.get("http://ws.audioscrobbler.com/2.0/", params=artist_info_params)
                aidata = r.json()
                artist_url = aidata["artist"]["url"]
            else:
                artist_info_params = {
                "artist": arg,
                "api_key": os.getenv("LASTFM_API_KEY"),
                "format": "json",
                "method": "artist.getInfo"
                }
        
                r = requests.get("http://ws.audioscrobbler.com/2.0/", params=artist_info_params)
                aidata = r.json()
                artistname = aidata["artist"]["name"]
                artist_url = aidata["artist"]["url"]

            sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials())
            sp_artist_image = ""
            results = sp.search(artistname, type="artist", limit=20)
            items = results["artists"]["items"]
            lfm_artist_name_lowercase = f"{artistname.lower()}"

            for sp_artist in items:
                sp_artist_name_lowercase = sp_artist["name"].lower()
                if lfm_artist_name_lowercase == sp_artist_name_lowercase:
                    try:
                        sp_artist_image = sp_artist["images"][0]["url"]
                    except IndexError:
                        sp_artist_image = None
                    break

            listeners_list = {}
            listens = ""
            lastfm_username_url = ""
            users = db.lfmquery()
            names = itertools.cycle(users)
            for _ in users:
                next_name = next(names)
                artist_info_params = {"artist": artistname, "user": next_name, "api_key": os.getenv("LASTFM_API_KEY"), "format": "json", "method": "artist.getInfo"}
                r = requests.get("http://ws.audioscrobbler.com/2.0/", params=artist_info_params)
                aidata = r.json()
                lastfm_username = f"{next_name}"
                lastfm_username_url = f"https://www.last.fm/user/{lastfm_username}"
                await asyncio.sleep(0.25)
                try:
                    listens = int(aidata["artist"]["stats"]["userplaycount"])
                except (KeyError, TypeError):
                    listens = 0
                if listens == 0:
                    continue
                else:
                    listeners_list[f"[{next_name}]({lastfm_username_url})"] = int(f"{listens}")
            listeners_list_sorted = dict(sorted(listeners_list.items(), key=lambda item: item[1], reverse=True))
            listeners_ranked = ""
            for position, (name, listens) in enumerate(listeners_list_sorted.items(), start=1):
                listeners_ranked += (str(position) + ". " + name + " - **" + str(listens) + "** plays" + "\n")
        
            if listeners_ranked == "":
                listeners_ranked = f"*No one in this server has listened to this artist.*"

            embed = discord.Embed(
            url = artist_url,
            title = f"Who knows {artistname} in {ctx.guild.name}",
            description = f"{listeners_ranked}",
            timestamp = datetime.now() - timedelta(hours=2),
            colour = 0x4a5fc3
            )
        
            if sp_artist_image is not None:
                embed.set_thumbnail(url=f"{sp_artist_image}")
            embed.set_footer(text=f"Requested by {ctx.author.name}#{ctx.author.discriminator}")
        await ctx.send(embed=embed)
Exemplo n.º 6
0
    async def combo(self, ctx, arg=None):
        async with ctx.typing():

            if arg is None:
                username = db.get_user(ctx.author.id)
                author = ctx.message.author
                pfp = author.avatar_url
            else:
                username = db.get_user(ctx.message.mentions[0].id)
                author = ctx.message.mentions[0]
                pfp = author.avatar_url

            if username is None:
                embed = discord.Embed(
                    description=
                    f"You need to first set your Last.fm username with the command\n`>set [your username]`",
                    colour=0x4a5fc3)
                return await ctx.send(embed=embed)

            lastfm_username = username[0][1]
            if not lastfm_username:
                embed = discord.Embed(
                    description=
                    f"You need to first set your Last.fm username with the command\n`>set [your username]`",
                    colour=0x4a5fc3)
                return await ctx.send(embed=embed)

            recent_tracks_params = {
                "limit": "1000",
                "user": lastfm_username,
                "api_key": os.getenv("LASTFM_API_KEY"),
                "format": "json",
                "method": "user.getRecentTracks"
            }

            r = requests.get("http://ws.audioscrobbler.com/2.0/",
                             params=recent_tracks_params)
            rtdata = r.json()
            await asyncio.sleep(0.25)

            try:
                rtinfo = rtdata["recenttracks"]["track"][0]
            except IndexError:
                embed = discord.Embed(
                    description=
                    f"**You haven't listened to anything yet on Last.fm!**",
                    colour=0x4a5fc3)
                return await ctx.send(embed=embed)
            artist_name = rtinfo["artist"]["#text"]
            album_name = rtinfo["album"]["#text"]
            first_track_url = rtinfo["url"]

            np = "@attr" in rtinfo and "nowplaying" in rtinfo["@attr"]

            artist_info_params = {
                "artist": artist_name,
                "user": lastfm_username,
                "api_key": os.getenv("LASTFM_API_KEY"),
                "format": "json",
                "method": "artist.getInfo"
            }

            r2 = requests.get("http://ws.audioscrobbler.com/2.0/",
                              params=artist_info_params)
            aidata = r2.json()
            await asyncio.sleep(0.25)
            first_artist_url = aidata["artist"]["url"]

            album_info_params = {
                "artist": artist_name,
                "album": album_name,
                "user": lastfm_username,
                "api_key": os.getenv("LASTFM_API_KEY"),
                "format": "json",
                "method": "album.getInfo"
            }

            r3 = requests.get("http://ws.audioscrobbler.com/2.0/",
                              params=album_info_params)
            abidata = r3.json()
            await asyncio.sleep(0.25)
            try:
                first_album_url = abidata["album"]["url"]
            except KeyError:
                first_album_url = ""
            tracks = rtdata["recenttracks"]["track"]
            first_artist_obj = tracks[0]
            first_artist_name = first_artist_obj["artist"]["#text"]
            first_artist_album = first_artist_obj["album"]["#text"]
            first_artist_track = first_artist_obj["name"]

            first_artist_combo = 0
            first_album_combo = 0
            first_track_combo = 0

            for track in tracks:
                if (track["artist"]["#text"] == first_artist_name):
                    if 0 <= first_artist_combo < 1000:
                        first_artist_combo += 1
                else:
                    break

            for track in tracks:
                if (track["album"]["#text"] == first_artist_album):
                    if 0 <= first_album_combo < 1000:
                        first_album_combo += 1
                else:
                    break

            for track in tracks:
                if (track["name"] == first_artist_track):
                    if 0 <= first_track_combo < 1000:
                        first_track_combo += 1
                else:
                    break

            second_artist_combo = 0
            second_album_combo = 0
            second_track_combo = 0

            if first_artist_combo == 1000 or first_album_combo == 1000 or first_track_combo == 1000:
                recent_tracks_params = {
                    "limit": "1000",
                    "page": "2",
                    "user": lastfm_username,
                    "api_key": os.getenv("LASTFM_API_KEY"),
                    "format": "json",
                    "method": "user.getRecentTracks"
                }

                r = requests.get("http://ws.audioscrobbler.com/2.0/",
                                 params=recent_tracks_params)
                rtdata = r.json()
                await asyncio.sleep(0.25)

                rtinfo = rtdata["recenttracks"]["track"][0]
                artist_name = rtinfo["artist"]["#text"]
                album_name = rtinfo["album"]["#text"]

                album_info_params = {
                    "artist": artist_name,
                    "album": album_name,
                    "user": lastfm_username,
                    "api_key": os.getenv("LASTFM_API_KEY"),
                    "format": "json",
                    "method": "album.getInfo"
                }

                r3 = requests.get("http://ws.audioscrobbler.com/2.0/",
                                  params=album_info_params)
                abidata = r3.json()
                await asyncio.sleep(0.25)
                tracks = rtdata["recenttracks"]["track"]

                if first_artist_combo == 1000:
                    for track in tracks:
                        if (track["artist"]["#text"] == first_artist_name):
                            if 0 <= second_artist_combo < 1000:
                                second_artist_combo += 1
                        else:
                            break

                if first_album_combo == 1000:
                    for track in tracks:
                        if (track["album"]["#text"] == first_artist_album):
                            if 0 <= second_album_combo < 1000:
                                second_album_combo += 1
                        else:
                            break

                if first_track_combo == 1000:
                    for track in tracks:
                        if (track["name"] == first_artist_track):
                            if 0 <= second_track_combo < 1000:
                                second_track_combo += 1
                        else:
                            break

            third_artist_combo = 0
            third_album_combo = 0
            third_track_combo = 0

            if second_artist_combo == 1000 or second_album_combo == 1000 or second_track_combo == 1000:
                recent_tracks_params = {
                    "limit": "1000",
                    "page": "3",
                    "user": lastfm_username,
                    "api_key": os.getenv("LASTFM_API_KEY"),
                    "format": "json",
                    "method": "user.getRecentTracks"
                }

                r = requests.get("http://ws.audioscrobbler.com/2.0/",
                                 params=recent_tracks_params)
                rtdata = r.json()
                await asyncio.sleep(0.25)

                rtinfo = rtdata["recenttracks"]["track"][0]
                artist_name = rtinfo["artist"]["#text"]
                album_name = rtinfo["album"]["#text"]

                album_info_params = {
                    "artist": artist_name,
                    "album": album_name,
                    "user": lastfm_username,
                    "api_key": os.getenv("LASTFM_API_KEY"),
                    "format": "json",
                    "method": "album.getInfo"
                }

                r3 = requests.get("http://ws.audioscrobbler.com/2.0/",
                                  params=album_info_params)
                abidata = r3.json()
                await asyncio.sleep(0.25)
                tracks = rtdata["recenttracks"]["track"]

                if second_artist_combo == 1000:
                    for track in tracks:
                        if (track["artist"]["#text"] == first_artist_name):
                            if 0 <= third_artist_combo < 1000:
                                third_artist_combo += 1
                        else:
                            break

                if second_album_combo == 1000:
                    for track in tracks:
                        if (track["album"]["#text"] == first_artist_album):
                            if 0 <= third_album_combo < 1000:
                                third_album_combo += 1
                        else:
                            break

                if second_track_combo == 1000:
                    for track in tracks:
                        if (track["name"] == first_artist_track):
                            if 0 <= third_track_combo < 1000:
                                third_track_combo += 1
                        else:
                            break

            fourth_artist_combo = 0
            fourth_album_combo = 0
            fourth_track_combo = 0

            if third_artist_combo == 1000 or third_album_combo == 1000 or third_track_combo == 1000:
                recent_tracks_params = {
                    "limit": "1000",
                    "page": "4",
                    "user": lastfm_username,
                    "api_key": os.getenv("LASTFM_API_KEY"),
                    "format": "json",
                    "method": "user.getRecentTracks"
                }

                r = requests.get("http://ws.audioscrobbler.com/2.0/",
                                 params=recent_tracks_params)
                rtdata = r.json()
                await asyncio.sleep(0.25)

                rtinfo = rtdata["recenttracks"]["track"][0]
                artist_name = rtinfo["artist"]["#text"]
                album_name = rtinfo["album"]["#text"]

                album_info_params = {
                    "artist": artist_name,
                    "album": album_name,
                    "user": lastfm_username,
                    "api_key": os.getenv("LASTFM_API_KEY"),
                    "format": "json",
                    "method": "album.getInfo"
                }

                r3 = requests.get("http://ws.audioscrobbler.com/2.0/",
                                  params=album_info_params)
                abidata = r3.json()
                await asyncio.sleep(0.25)
                tracks = rtdata["recenttracks"]["track"]

                if third_artist_combo == 1000:
                    for track in tracks:
                        if (track["artist"]["#text"] == first_artist_name):
                            if 0 <= fourth_artist_combo < 1000:
                                fourth_artist_combo += 1
                        else:
                            break

                if third_album_combo == 1000:
                    for track in tracks:
                        if (track["album"]["#text"] == first_artist_album):
                            if 0 <= fourth_album_combo < 1000:
                                fourth_album_combo += 1
                        else:
                            break

                if third_track_combo == 1000:
                    for track in tracks:
                        if (track["name"] == first_artist_track):
                            if 0 <= fourth_track_combo < 1000:
                                fourth_track_combo += 1
                        else:
                            break

            fifth_artist_combo = 0
            fifth_album_combo = 0
            fifth_track_combo = 0

            if fourth_artist_combo == 1000 or fourth_album_combo == 1000 or fourth_track_combo == 1000:
                recent_tracks_params = {
                    "limit": "1000",
                    "page": "5",
                    "user": lastfm_username,
                    "api_key": os.getenv("LASTFM_API_KEY"),
                    "format": "json",
                    "method": "user.getRecentTracks"
                }

                r = requests.get("http://ws.audioscrobbler.com/2.0/",
                                 params=recent_tracks_params)
                rtdata = r.json()
                await asyncio.sleep(0.25)

                rtinfo = rtdata["recenttracks"]["track"][0]
                artist_name = rtinfo["artist"]["#text"]
                album_name = rtinfo["album"]["#text"]

                album_info_params = {
                    "artist": artist_name,
                    "album": album_name,
                    "user": lastfm_username,
                    "api_key": os.getenv("LASTFM_API_KEY"),
                    "format": "json",
                    "method": "album.getInfo"
                }

                r3 = requests.get("http://ws.audioscrobbler.com/2.0/",
                                  params=album_info_params)
                abidata = r3.json()
                await asyncio.sleep(0.25)
                tracks = rtdata["recenttracks"]["track"]

                if fourth_artist_combo == 1000:
                    for track in tracks:
                        if (track["artist"]["#text"] == first_artist_name):
                            if 0 <= fifth_artist_combo < 1000:
                                fifth_artist_combo += 1
                        else:
                            break

                if fourth_album_combo == 1000:
                    for track in tracks:
                        if (track["album"]["#text"] == first_artist_album):
                            if 0 <= fifth_album_combo < 1000:
                                fifth_album_combo += 1
                        else:
                            break

                if fourth_track_combo == 1000:
                    for track in tracks:
                        if (track["name"] == first_artist_track):
                            if 0 <= fifth_track_combo < 1000:
                                fifth_track_combo += 1
                        else:
                            break

            artist_combo_text = ""
            album_combo_text = ""
            track_combo_text = ""
            no_combo_text = ""
            add_artist_combos = first_artist_combo + second_artist_combo + third_artist_combo + fourth_artist_combo + fifth_artist_combo
            add_album_combos = first_album_combo + second_album_combo + third_album_combo + fourth_album_combo + fifth_album_combo
            add_track_combos = first_track_combo + second_track_combo + third_track_combo + fourth_track_combo + fifth_track_combo
            artist_combo = add_artist_combos if not np else (
                add_artist_combos - 1)
            album_combo = add_album_combos if not np else (add_album_combos -
                                                           1)
            track_combo = add_track_combos if not np else (add_track_combos -
                                                           1)

            if artist_combo >= 2:
                artist_combo_text = f"**Artist:** {artist_combo} plays in a row - **[{first_artist_name}]({first_artist_url})**\n"
            if album_combo >= 2 and first_album_url != "":
                album_combo_text = f"**Album:** {album_combo} plays in a row - **[{first_artist_album}]({first_album_url})**\n"
            else:
                if album_combo >= 2 and first_album_url == "":
                    album_combo_text = f"**Album:** {album_combo} plays in a row - **{first_artist_album}**\n"
            if track_combo >= 2:
                track_combo_text = f"**Track:** {track_combo} plays in a row - **[{first_artist_track}]({first_track_url})**"
            if artist_combo_text == "" and album_combo_text == "" and track_combo_text == "":
                no_combo_text = f"*No consecutive tracks found.*"

            embed = discord.Embed(
                description=
                f"{no_combo_text}{artist_combo_text}{album_combo_text}{track_combo_text}",
                timestamp=datetime.now() - timedelta(hours=2),
                colour=0x4a5fc3)

            embed.set_author(name=f"Active Combo for {lastfm_username}",
                             icon_url=pfp)
            embed.set_footer(
                text=
                f"Requested by {ctx.author.name}#{ctx.author.discriminator}")

        await ctx.send(embed=embed)
Exemplo n.º 7
0
    async def lyrics(self, ctx, arg=None):
        async with ctx.typing():

            username = db.get_user(ctx.author.id)

            if username is None:
                embed = discord.Embed(
                    description=
                    f"You need to first set your Last.fm username with the command\n**>set [your username]**",
                    colour=0x4a5fc3)
                return await ctx.send(embed=embed)

            lastfm_username = username[0][1]
            if not lastfm_username:
                embed = discord.Embed(
                    description=
                    f"You need to first set your Last.fm username with the command\n**>set [your username]**",
                    colour=0x4a5fc3)
                return await ctx.send(embed=embed)

            recent_tracks_params = {
                "limit": "1",
                "user": lastfm_username,
                "api_key": os.getenv("LASTFM_API_KEY"),
                "format": "json",
                "method": "user.getRecentTracks"
            }

            r = requests.get("http://ws.audioscrobbler.com/2.0/",
                             params=recent_tracks_params)
            rtdata = r.json()

            try:
                rtinfo = rtdata["recenttracks"]["track"][0]
            except IndexError:
                embed = discord.Embed(
                    description=
                    f"**You haven't listened to anything yet on Last.fm!**",
                    colour=0x4a5fc3)
                return await ctx.send(embed=embed)
            artist = rtinfo["artist"]["#text"]
            track = rtinfo["name"]

            genius = lg.Genius(skip_non_songs=True, verbose=False)
            song = genius.search_song(track, artist)
            try:
                lyrics = song.lyrics
            except AttributeError:
                lyrics = "*No lyrics were found.*"
            sep = "EmbedShare"
            lyrics_fix = lyrics.split(sep, 1)[0]
            lyrics1 = lyrics_fix[0:2000]

            if len(lyrics + track + artist) > 2000:
                lyrics2 = lyrics[2000:4000]
                embed2 = discord.Embed(description=lyrics2, colour=0x4a5fc3)
            if len(lyrics) > 4000:
                lyrics3 = lyrics[4000:6000]
                lyrics2 = lyrics[2000:4000]
                embed3 = discord.Embed(description=lyrics3, colour=0x4a5fc3)
            if len(lyrics) > 6000:
                lyrics4 = lyrics[6000:8000]
                lyrics3 = lyrics[4000:6000]
                embed4 = discord.Embed(description=lyrics4, colour=0x4a5fc3)

            embed1 = discord.Embed(title=f"Lyrics for {track} by {artist}",
                                   description=lyrics1,
                                   colour=0x4a5fc3)

            await ctx.send(embed=embed1)
            if len(lyrics + track + artist) > 2000:
                await ctx.send(embed=embed2)
                if len(lyrics) > 4000:
                    await ctx.send(embed=embed3)
                    if len(lyrics) > 6000:
                        await ctx.send(embed=embed4)
Exemplo n.º 8
0
    async def toptags(self, ctx, *, arg=None):
        async with ctx.typing():

            valid_timeframes = ["w", "m", "q", "s", "y", "a", None]
            timeframe = ""
            if arg == "w":
                arg = "7day"
                timeframe = "of the last week"
            elif arg == "m":
                arg = "1month"
                timeframe = "of the last month"
            elif arg == "q":
                arg = "3month"
                timeframe = "of the last quarter"
            elif arg == "s":
                arg = "6month"
                timeframe = "of the last semester"
            elif arg == "y":
                arg = "12month"
                timeframe = "of the last year"
            elif arg == "a":
                arg = "overall"
                timeframe = "overall"
            elif arg is None:
                arg = "7day"
                timeframe = "of the last week"
            elif arg is not valid_timeframes:
                await ctx.send(
                    f"`Invalid timeframe` <a:DubuAngry:773329674679746610>")
                return

            username = db.get_user(ctx.author.id)
            author = ctx.message.author
            pfp = author.avatar_url
            if username is None:
                embed = discord.Embed(
                    description=
                    f"You need to first set your Last.fm username with the command\n`>set [your username]`",
                    colour=0x4a5fc3)
                return await ctx.send(embed=embed)

            lastfm_username = username[0][1]
            if not lastfm_username:
                embed = discord.Embed(
                    description=
                    f"You need to first set your Last.fm username with the command\n`>set [your username]`",
                    colour=0x4a5fc3)
                return await ctx.send(embed=embed)

            top_artists_params = {
                "period": arg,
                "limit": "10",
                "user": lastfm_username,
                "api_key": os.getenv("LASTFM_API_KEY"),
                "format": "json",
                "method": "user.getTopArtists"
            }

            r = requests.get("http://ws.audioscrobbler.com/2.0/",
                             params=top_artists_params)
            tadata = r.json()
            await asyncio.sleep(0.25)
            top_artists_names = [
                name["name"] for name in tadata["topartists"]["artist"]
            ]
            first_top_artist = top_artists_names[0]
            second_top_artist = top_artists_names[1]
            third_top_artist = top_artists_names[2]
            fourth_top_artist = top_artists_names[3]
            fifth_top_artist = top_artists_names[4]
            sixth_top_artist = top_artists_names[5]
            seventh_top_artist = top_artists_names[6]
            eighth_top_artist = top_artists_names[7]
            ninth_top_artist = top_artists_names[8]
            tenth_top_artist = top_artists_names[9]

            artist_info_params = {
                "artist": first_top_artist,
                "user": lastfm_username,
                "api_key": os.getenv("LASTFM_API_KEY"),
                "format": "json",
                "method": "artist.getInfo"
            }

            r = requests.get("http://ws.audioscrobbler.com/2.0/",
                             params=artist_info_params)
            aidata = r.json()
            try:
                first_artist_tags = [
                    tag["name"] for tag in aidata["artist"]["tags"]["tag"]
                ]
            except KeyError:
                first_artist_tags = [""]
            await asyncio.sleep(0.25)

            artist_info_params = {
                "artist": second_top_artist,
                "user": lastfm_username,
                "api_key": os.getenv("LASTFM_API_KEY"),
                "format": "json",
                "method": "artist.getInfo"
            }

            r = requests.get("http://ws.audioscrobbler.com/2.0/",
                             params=artist_info_params)
            aidata = r.json()
            try:
                second_artist_tags = [
                    tag["name"] for tag in aidata["artist"]["tags"]["tag"]
                ]
            except KeyError:
                second_artist_tags = [""]
            await asyncio.sleep(0.25)

            artist_info_params = {
                "artist": third_top_artist,
                "user": lastfm_username,
                "api_key": os.getenv("LASTFM_API_KEY"),
                "format": "json",
                "method": "artist.getInfo"
            }

            r = requests.get("http://ws.audioscrobbler.com/2.0/",
                             params=artist_info_params)
            aidata = r.json()
            try:
                third_artist_tags = [
                    tag["name"] for tag in aidata["artist"]["tags"]["tag"]
                ]
            except KeyError:
                third_artist_tags = [""]
            await asyncio.sleep(0.25)

            artist_info_params = {
                "artist": fourth_top_artist,
                "user": lastfm_username,
                "api_key": os.getenv("LASTFM_API_KEY"),
                "format": "json",
                "method": "artist.getInfo"
            }

            r = requests.get("http://ws.audioscrobbler.com/2.0/",
                             params=artist_info_params)
            aidata = r.json()
            try:
                fourth_artist_tags = [
                    tag["name"] for tag in aidata["artist"]["tags"]["tag"]
                ]
            except KeyError:
                fourth_artist_tags = [""]
            await asyncio.sleep(0.25)

            artist_info_params = {
                "artist": fifth_top_artist,
                "user": lastfm_username,
                "api_key": os.getenv("LASTFM_API_KEY"),
                "format": "json",
                "method": "artist.getInfo"
            }

            r = requests.get("http://ws.audioscrobbler.com/2.0/",
                             params=artist_info_params)
            aidata = r.json()
            try:
                fifth_artist_tags = [
                    tag["name"] for tag in aidata["artist"]["tags"]["tag"]
                ]
            except KeyError:
                fifth_artist_tags = [""]
            await asyncio.sleep(0.25)

            artist_info_params = {
                "artist": sixth_top_artist,
                "user": lastfm_username,
                "api_key": os.getenv("LASTFM_API_KEY"),
                "format": "json",
                "method": "artist.getInfo"
            }

            r = requests.get("http://ws.audioscrobbler.com/2.0/",
                             params=artist_info_params)
            aidata = r.json()
            try:
                sixth_artist_tags = [
                    tag["name"] for tag in aidata["artist"]["tags"]["tag"]
                ]
            except KeyError:
                sixth_artist_tags = [""]
            await asyncio.sleep(0.25)

            artist_info_params = {
                "artist": seventh_top_artist,
                "user": lastfm_username,
                "api_key": os.getenv("LASTFM_API_KEY"),
                "format": "json",
                "method": "artist.getInfo"
            }

            r = requests.get("http://ws.audioscrobbler.com/2.0/",
                             params=artist_info_params)
            aidata = r.json()
            try:
                seventh_artist_tags = [
                    tag["name"] for tag in aidata["artist"]["tags"]["tag"]
                ]
            except KeyError:
                seventh_artist_tags = [""]
            await asyncio.sleep(0.25)

            artist_info_params = {
                "artist": eighth_top_artist,
                "user": lastfm_username,
                "api_key": os.getenv("LASTFM_API_KEY"),
                "format": "json",
                "method": "artist.getInfo"
            }

            r = requests.get("http://ws.audioscrobbler.com/2.0/",
                             params=artist_info_params)
            aidata = r.json()
            try:
                eighth_artist_tags = [
                    tag["name"] for tag in aidata["artist"]["tags"]["tag"]
                ]
            except KeyError:
                eighth_artist_tags = [""]
            await asyncio.sleep(0.25)

            artist_info_params = {
                "artist": ninth_top_artist,
                "user": lastfm_username,
                "api_key": os.getenv("LASTFM_API_KEY"),
                "format": "json",
                "method": "artist.getInfo"
            }

            r = requests.get("http://ws.audioscrobbler.com/2.0/",
                             params=artist_info_params)
            aidata = r.json()
            try:
                ninth_artist_tags = [
                    tag["name"] for tag in aidata["artist"]["tags"]["tag"]
                ]
            except KeyError:
                ninth_artist_tags = [""]
            await asyncio.sleep(0.25)

            artist_info_params = {
                "artist": tenth_top_artist,
                "user": lastfm_username,
                "api_key": os.getenv("LASTFM_API_KEY"),
                "format": "json",
                "method": "artist.getInfo"
            }

            r = requests.get("http://ws.audioscrobbler.com/2.0/",
                             params=artist_info_params)
            aidata = r.json()
            try:
                tenth_artist_tags = [
                    tag["name"] for tag in aidata["artist"]["tags"]["tag"]
                ]
            except KeyError:
                tenth_artist_tags = [""]
            await asyncio.sleep(0.25)

            all_artist_tags = first_artist_tags + second_artist_tags + third_artist_tags + fourth_artist_tags + fifth_artist_tags + sixth_artist_tags + seventh_artist_tags + eighth_artist_tags + ninth_artist_tags + tenth_artist_tags
            random.shuffle(all_artist_tags)
            no_duplicates = set(all_artist_tags)
            all_artist_tags_string = " ∙ ".join(no_duplicates)

            embed = discord.Embed(
                description=f"{all_artist_tags_string.lower()}",
                timestamp=datetime.now() - timedelta(hours=2),
                colour=0x4a5fc3)

            embed.set_author(
                name=f"Top tags {timeframe} for {lastfm_username}",
                icon_url=pfp)
            embed.set_footer(
                text=
                f"Requested by {ctx.author.name}#{ctx.author.discriminator}")

        await ctx.send(embed=embed)
Exemplo n.º 9
0
    async def artistinfo(self, ctx, *, arg=None):
        async with ctx.typing():

            username = db.get_user(ctx.author.id)

            if username is None:
                embed = discord.Embed(
                    description=
                    f"You need to first set your Last.fm username with the command\n`>set [your username]`",
                    colour=0x4a5fc3)
                return await ctx.send(embed=embed)

            lastfm_username = username[0][1]
            if not lastfm_username:
                embed = discord.Embed(
                    description=
                    f"You need to first set your Last.fm username with the command\n`>set [your username]`",
                    colour=0x4a5fc3)
                return await ctx.send(embed=embed)

            if arg is None:
                recent_tracks_params = {
                    "limit": "1",
                    "user": lastfm_username,
                    "api_key": os.getenv("LASTFM_API_KEY"),
                    "format": "json",
                    "method": "user.getRecentTracks"
                }

                r = requests.get("http://ws.audioscrobbler.com/2.0/",
                                 params=recent_tracks_params)
                rtdata = r.json()
                await asyncio.sleep(0.25)
                rtinfo = rtdata["recenttracks"]["track"][0]
                actual_artist = rtinfo["artist"]["#text"]

                artist_info_params = {
                    "artist": actual_artist,
                    "api_key": os.getenv("LASTFM_API_KEY"),
                    "format": "json",
                    "method": "artist.getInfo"
                }

                r = requests.get("http://ws.audioscrobbler.com/2.0/",
                                 params=artist_info_params)
                aidata = r.json()
                artist_url = aidata["artist"]["url"]
                try:
                    artist_info = aidata["artist"]["bio"]["summary"]
                except TypeError:
                    artist_info = ""
                except KeyError:
                    artist_info = ""
                try:
                    artist_tags = [
                        tag["name"] for tag in aidata["artist"]["tags"]["tag"]
                    ]
                except TypeError:
                    artist_tags = ""
                artist_tags_string = " ∙ ".join(artist_tags)

            else:
                artist_info_params = {
                    "artist": arg,
                    "api_key": os.getenv("LASTFM_API_KEY"),
                    "format": "json",
                    "method": "artist.getInfo"
                }

                r = requests.get("http://ws.audioscrobbler.com/2.0/",
                                 params=artist_info_params)
                aidata = r.json()
                await asyncio.sleep(0.25)
                try:
                    actual_artist = aidata["artist"]["name"]
                except KeyError:
                    return await ctx.send(embed=discord.Embed(
                        description=f"This artist doesn't exist.",
                        colour=0x4a5fc3))
                artist_url = aidata["artist"]["url"]
                artist_info = aidata["artist"]["bio"]["summary"]
                try:
                    artist_tags = [
                        tag["name"] for tag in aidata["artist"]["tags"]["tag"]
                    ]
                except TypeError:
                    artist_tags = ""
                artist_tags_string = " ∙ ".join(artist_tags)

            artist_info = artist_info.strip()
            sep = "<a"
            artist_info = artist_info.split(sep, 1)[0]
            if len(artist_info) > 460:
                artist_info = artist_info[:460] + "..."

            sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials())
            sp_artist_image = ""
            results = sp.search(actual_artist, type="artist", limit=20)
            items = results["artists"]["items"]
            lfm_artist_name_lowercase = f"{actual_artist.lower()}"

            for sp_artist in items:
                sp_artist_name_lowercase = sp_artist["name"].lower()
                if lfm_artist_name_lowercase == sp_artist_name_lowercase:
                    try:
                        sp_artist_image = sp_artist["images"][0]["url"]
                    except IndexError:
                        sp_artist_image = None
                    break

            embed = discord.Embed(colour=0x4a5fc3)

            embed.set_author(
                name=f"Artist info for {lastfm_username} about {actual_artist}"
            )
            if sp_artist_image is not None:
                embed.set_thumbnail(url=f"{sp_artist_image}")
            if artist_info != "":
                embed.add_field(name=f"Summary",
                                value=f"{artist_info}",
                                inline=False)
            if artist_info == "":
                embed.add_field(name=f"Summary",
                                value=f"*No summary exists for this artist.*",
                                inline=False)
            if artist_url is not None:
                embed.add_field(name="\u200b",
                                value=f"[Link to the site]({artist_url})")
            if artist_tags is not None:
                embed.set_footer(text=f"{artist_tags_string.lower()}")

        await ctx.send(embed=embed)
Exemplo n.º 10
0
    async def youtube(self, ctx, *, arg=None):
        async with ctx.typing():

            if arg is None:
                username = db.get_user(ctx.author.id)

                if username is None:
                    embed = discord.Embed(description = f"You need to first set your Last.fm username with the command\n`>set [your username]`", colour = 0x4a5fc3)
                    return await ctx.send(embed=embed)

                lastfm_username = username [0][1];
                if not lastfm_username:
                    embed = discord.Embed(description = f"You need to first set your Last.fm username with the command\n`>set [your username]`", colour = 0x4a5fc3)
                    return await ctx.send(embed=embed)

                recent_tracks_params = {
                    "limit": "1",
                    "user": lastfm_username,
                    "api_key": os.getenv("LASTFM_API_KEY"),
                    "format": "json",
                    "method": "user.getRecentTracks"
                }

                r = requests.get("http://ws.audioscrobbler.com/2.0/", params=recent_tracks_params)
                rtdata = r.json()
                await asyncio.sleep(0.25)
                rtinfo = rtdata["recenttracks"]["track"][0]
                artist = rtinfo["artist"]["#text"]
                track = rtinfo["name"]

                np = "@attr" in rtinfo and "nowplaying" in rtinfo["@attr"]
                state = f"*Now playing for {lastfm_username}*" if np else f"*Last scrobbled track for {lastfm_username}*"

                video_info_params = {
                    "part": "snippet",
                    "type": "video",
                    "maxResults": 1,
                    "q": f"{artist} {track}",
                    "key": os.getenv("YOUTUBE_API_KEY"),
                }

                r = requests.get("https://www.googleapis.com/youtube/v3/search", params=video_info_params)
                vidata = r.json()
            else:
                video_info_params = {
                    "part": "snippet",
                    "type": "video",
                    "maxResults": 1,
                    "q": arg,
                    "key": os.getenv("YOUTUBE_API_KEY"),
                }

                r = requests.get("https://www.googleapis.com/youtube/v3/search", params=video_info_params)
                vidata = r.json()
                await asyncio.sleep(0.25)

                state = f"*Link requested by {ctx.author.name}#{ctx.author.discriminator}*"

            video_id = vidata["items"][0]["id"]["videoId"]
            video_url = f"https://youtube.com/watch?v={video_id}"

        await ctx.send(f"{state}\n{video_url}")
Exemplo n.º 11
0
    async def topartists(self, ctx, *, arg=None):
        async with ctx.typing():

            valid_timeframes = ["w", "m", "q", "s", "y", "a", None]
            timeframe = ""
            if arg is "w":
                arg = "7day"
                timeframe = "of the last week"
            elif arg is "m":
                arg = "1month"
                timeframe = "of the last month"
            elif arg is "q":
                arg = "3month"
                timeframe = "of the last quarter"
            elif arg is "s":
                arg = "6month"
                timeframe = "of the last semester"
            elif arg is "y":
                arg = "12month"
                timeframe = "of the last year"
            elif arg is "a":
                arg = "overall"
                timeframe = "overall"
            elif arg is None:
                arg = "7day"
                timeframe = "of the last week"
            elif arg is not valid_timeframes:
                await ctx.send(
                    f"`Invalid timeframe` <a:DubuAngry:773329674679746610>")
                return

            username = db.get_user(ctx.author.id)
            author = ctx.message.author
            pfp = author.avatar_url
            if username is None:
                embed = discord.Embed(
                    description=
                    f"You need to first set your Last.fm username with the command\n`>set [your username]`",
                    colour=0x4a5fc3)
                return await ctx.send(embed=embed)

            lastfm_username = username[0][1]
            if not lastfm_username:
                embed = discord.Embed(
                    description=
                    f"You need to first set your Last.fm username with the command\n`>set [your username]`",
                    colour=0x4a5fc3)
                return await ctx.send(embed=embed)

            top_artists_params = {
                "period": arg,
                "limit": "10",
                "user": lastfm_username,
                "api_key": os.getenv("LASTFM_API_KEY"),
                "format": "json",
                "method": "user.getTopArtists"
            }

            r = requests.get("http://ws.audioscrobbler.com/2.0/",
                             params=top_artists_params)
            tadata = r.json()
            await asyncio.sleep(0.25)
            top_artists_names = [
                name["name"] for name in tadata["topartists"]["artist"]
            ]
            top_artists_string = "\n".join(top_artists_names)

            embed = discord.Embed(description=f"**{top_artists_string}**",
                                  timestamp=datetime.now() -
                                  timedelta(hours=2),
                                  colour=0x4a5fc3)

            embed.set_author(
                name=f"Top artists {timeframe} for {lastfm_username}",
                icon_url=pfp)
            embed.set_footer(
                text=
                f"Requested by {ctx.author.name}#{ctx.author.discriminator}")

        await ctx.send(embed=embed)
Exemplo n.º 12
0
    async def albumcover(self, ctx, *, arg=None):
        async with ctx.typing():

            username = db.get_user(ctx.author.id)

            if username is None:
                embed = discord.Embed(
                    description=
                    f"You need to first set your Last.fm username with the command\n`>set [your username]`",
                    colour=0x4a5fc3)
                return await ctx.send(embed=embed)

            lastfm_username = username[0][1]
            if not lastfm_username:
                embed = discord.Embed(
                    description=
                    f"You need to first set your Last.fm username with the command\n`>set [your username]`",
                    colour=0x4a5fc3)
                return await ctx.send(embed=embed)

            if arg is None:
                recent_tracks_params = {
                    "limit": "1",
                    "user": lastfm_username,
                    "api_key": os.getenv("LASTFM_API_KEY"),
                    "format": "json",
                    "method": "user.getRecentTracks"
                }

                r = requests.get("http://ws.audioscrobbler.com/2.0/",
                                 params=recent_tracks_params)
                rtdata = r.json()
                rtinfo = rtdata["recenttracks"]["track"][0]
                artist = rtinfo["artist"]["#text"]
                album = rtinfo["album"]["#text"]

                album_info_params = {
                    "artist": artist,
                    "album": album,
                    "autocorrect": "1",
                    "api_key": os.getenv("LASTFM_API_KEY"),
                    "format": "json",
                    "method": "album.getInfo"
                }

                r = requests.get("http://ws.audioscrobbler.com/2.0/",
                                 params=album_info_params)
                abidata = r.json()
            else:
                try:
                    artist, album = arg.split("|")
                except:
                    return await ctx.send(embed=discord.Embed(
                        description="Use the format `artist | album`.",
                        colour=0x4a5fc3))
                album_info_params = {
                    "artist": artist.strip(),
                    "album": album.strip(),
                    "autocorrect": "1",
                    "api_key": os.getenv("LASTFM_API_KEY"),
                    "format": "json",
                    "method": "album.getInfo"
                }

                r = requests.get("http://ws.audioscrobbler.com/2.0/",
                                 params=album_info_params)
                abidata = r.json()

            actual_artist = abidata["album"]["artist"]
            actual_album = abidata["album"]["name"]
            api_album_cover = abidata["album"]["image"][-1]["#text"]
            no_file_type_album_cover = api_album_cover.rsplit(".", 1)[0]
            higher_res_album_cover = no_file_type_album_cover.replace(
                "300x300", "700x0", 1)

            try:
                album_url = abidata["album"]["url"]
            except KeyError:
                album_url = ""

            try:
                album_cover = abidata["album"]["image"][-1]["#text"]
            except KeyError:
                embed = discord.Embed(
                    description=
                    f"**{actual_artist} - [{actual_album}]({album_url})**\n\n*No cover exists for this album.*",
                    timestamp=datetime.now() - timedelta(hours=2),
                    colour=0x4a5fc3)
                embed.set_footer(
                    text=
                    f"Requested by {ctx.author.name}#{ctx.author.discriminator}"
                )

            if album_cover == "":
                embed = discord.Embed(
                    description=
                    f"**{actual_artist} - [{actual_album}]({album_url})**\n\n*No cover exists for this album.*",
                    timestamp=datetime.now() - timedelta(hours=2),
                    colour=0x4a5fc3)
                embed.set_footer(
                    text=
                    f"Requested by {ctx.author.name}#{ctx.author.discriminator}"
                )
            else:
                embed = discord.Embed(
                    description=
                    f"**{actual_artist} - [{actual_album}]({album_url})**",
                    timestamp=datetime.now() - timedelta(hours=2),
                    colour=0x4a5fc3)
                embed.set_image(url=f"{higher_res_album_cover}")
            embed.set_footer(
                text=
                f"Requested by {ctx.author.name}#{ctx.author.discriminator}")

        await ctx.send(embed=embed)
Exemplo n.º 13
0
    async def albumcoverspotify(self, ctx, *, arg=None):
        async with ctx.typing():

            username = db.get_user(ctx.author.id)

            if username is None:
                return await ctx.send(
                    f"`You need to first set your Last.fm username with the command`\n```>set [your username]```"
                )

            lastfm_username = username[0][1]
            if not lastfm_username:
                return await ctx.send(
                    f"`You need to first set your Last.fm username with the command`\n```>set [your username]```"
                )

            if arg is None:
                recent_tracks_params = {
                    "limit": "1",
                    "user": lastfm_username,
                    "api_key": os.getenv("LASTFM_API_KEY"),
                    "format": "json",
                    "method": "user.getRecentTracks"
                }

                r = requests.get("http://ws.audioscrobbler.com/2.0/",
                                 params=recent_tracks_params)
                rtdata = r.json()
                rtinfo = rtdata["recenttracks"]["track"][0]
                artist = rtinfo["artist"]["#text"]
                album = rtinfo["album"]["#text"]

                album_info_params = {
                    "artist": artist.strip(),
                    "album": album.strip(),
                    "autocorrect": "1",
                    "api_key": os.getenv("LASTFM_API_KEY"),
                    "format": "json",
                    "method": "album.getInfo"
                }

                r = requests.get("http://ws.audioscrobbler.com/2.0/",
                                 params=album_info_params)
                abidata = r.json()
            else:
                try:
                    artist, album = arg.split("|")
                except:
                    return await ctx.send(embed=discord.Embed(
                        description="Use the format `artist | album`.",
                        colour=0x4a5fc3))

            album_info_params = {
                "artist": artist.strip(),
                "album": album.strip(),
                "autocorrect": "1",
                "api_key": os.getenv("LASTFM_API_KEY"),
                "format": "json",
                "method": "album.getInfo"
            }

            r = requests.get("http://ws.audioscrobbler.com/2.0/",
                             params=album_info_params)
            abidata = r.json()
            actual_artist = abidata["album"]["artist"]
            actual_album = abidata["album"]["name"]

            try:
                album_url = abidata["album"]["url"]
            except KeyError:
                album_url = ""

            sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials())
            sp_album_image = ""
            results = sp.search(f"{actual_artist} {actual_album}",
                                type="track",
                                limit=20)
            items = results["tracks"]["items"]
            lfm_artist_name_lowercase = f"{actual_artist.lower()}"
            lfm_album_name_lowercase = f"{actual_album.lower()}"

            for sp_track in items:
                sp_artist_correct = sp_track["artists"][0]
                sp_album_correct = sp_track["album"]
                sp_artist_name_lowercase = sp_artist_correct["name"].lower()
                sp_album_name_lowercase = sp_album_correct["name"].lower()
                if lfm_artist_name_lowercase == sp_artist_name_lowercase and lfm_album_name_lowercase == sp_album_name_lowercase:
                    try:
                        sp_album_image = sp_track["album"]["images"][0]["url"]
                    except IndexError:
                        sp_album_image = None
                    break

            if sp_album_image is None:
                embed = discord.Embed(
                    description=
                    f"**{actual_artist} - [{actual_album}]({album_url})**\n*No cover exists for this album.*",
                    timestamp=datetime.now() - timedelta(hours=2),
                    colour=0x4a5fc3)
                embed.set_footer(
                    text=
                    f"Requested by {ctx.author.name}#{ctx.author.discriminator}"
                )
            else:
                embed = discord.Embed(
                    description=
                    f"**{actual_artist} - [{actual_album}]({album_url})**",
                    timestamp=datetime.now() - timedelta(hours=2),
                    colour=0x4a5fc3)
                embed.set_image(url=f"{sp_album_image}")
            embed.set_footer(
                text=
                f"Requested by {ctx.author.name}#{ctx.author.discriminator}")

        await ctx.send(embed=embed)
Exemplo n.º 14
0
    async def albuminfo(self, ctx, *, arg=None):
        async with ctx.typing():
            
            username = db.get_user(ctx.author.id)

            if username is None:
                embed = discord.Embed(description = f"You need to first set your Last.fm username with the command\n`>set [your username]`", colour = 0x4a5fc3)
                return await ctx.send(embed=embed)

            lastfm_username = username [0][1];
            if not lastfm_username:
                embed = discord.Embed(description = f"You need to first set your Last.fm username with the command\n`>set [your username]`", colour = 0x4a5fc3)
                return await ctx.send(embed=embed)

            if arg is None:
                recent_tracks_params = {
                    "limit": "1",
                    "user": lastfm_username,
                    "api_key": os.getenv("LASTFM_API_KEY"),
                    "format": "json",
                    "method": "user.getRecentTracks"
                }

                r = requests.get("http://ws.audioscrobbler.com/2.0/", params=recent_tracks_params)
                rtdata = r.json()
                await asyncio.sleep(0.25)
                rtinfo = rtdata["recenttracks"]["track"][0]
                album_cover = rtinfo["image"][-1]["#text"]
                actual_artist = rtinfo["artist"]["#text"]
                actual_album = rtinfo["album"]["#text"]

                album_info_params = {
                    "artist": actual_artist,
                    "album": actual_album,
                    "autocorrect": "1",
                    "api_key": os.getenv("LASTFM_API_KEY"),
                    "format": "json",
                    "method": "album.getInfo"
                }

                r = requests.get("http://ws.audioscrobbler.com/2.0/", params=album_info_params)
                abidata = r.json()
            else:
                artist, album = arg.split("|")
                params = {
                    "artist": artist.strip(),
                    "album": album.strip(),
                    "api_key": os.getenv("LASTFM_API_KEY"),
                    "format": "json",
                    "method": "album.getInfo"
                }

                r = requests.get("http://ws.audioscrobbler.com/2.0/", params=params)
                abidata = r.json()
                await asyncio.sleep(0.25)

            try:
                actual_artist = abidata["album"]["artist"]
            except KeyError:
                return await ctx.send(embed = discord.Embed(description = f"This artist's album doesn't exist.", colour = 0x4a5fc3))
            try:
                album_info = abidata["album"]["wiki"]["content"]
            except KeyError:
                album_info = ""


            actual_album = abidata["album"]["name"]
            album_url = abidata["album"]["url"]
            album_cover = abidata["album"]["image"][-1]["#text"]
            try:
                album_tags = [tag["name"] for tag in abidata["album"]["tags"]["tag"]]
            except TypeError:
                album_tags = ""
            album_tags_string = " ∙ ".join(album_tags)

            album_info = album_info.strip()
            sep = "<a"
            album_info = album_info.split(sep, 1)[0]
            if len(album_info)>800:
                album_info = album_info[:800] + "..."

            embed = discord.Embed(
            colour = 0x4a5fc3
            )

            embed.set_author(name=f"Album info for {lastfm_username} about {actual_artist} - {actual_album}")
            if album_cover is not None:
                embed.set_thumbnail(url=f"{album_cover}")
            if album_info != "":
                embed.add_field(name=f"Summary", value=f"{album_info}", inline=False)
            if album_info == "":
                embed.add_field(name=f"Summary", value=f"*No summary exists for this album.*", inline=False)
            if album_url is not None:
                embed.add_field(name="\u200b", value=f"[Link to the site]({album_url})")
            if album_tags is not None:
                embed.set_footer(text=f"{album_tags_string.lower()}")

        await ctx.send(embed=embed)