Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add better metadata and album art functionality to 'now playing' #6

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
aiohttp==3.7.4.post0
async-timeout==3.0.1
attrs==21.2.0
certifi==2021.5.30
cffi==1.14.6
chardet==4.0.0
charset-normalizer==2.0.4
discord.py==1.7.3
idna==3.2
multidict==5.1.0
pycparser==2.20
PyNaCl==1.4.0
requests==2.26.0
six==1.16.0
typing-extensions==3.10.0.0
urllib3==1.26.6
yarl==1.6.3
youtube-dl==2021.6.6
youtube-title-parse==1.0.0
64 changes: 63 additions & 1 deletion yamb/cogs/music.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import asyncio
import os
import requests

import discord
import youtube_dl

from discord.ext import commands
from youtube_title_parse import get_artist_title

# Suppress noise about console usage from errors
youtube_dl.utils.bug_reports_message = lambda: ''
Expand Down Expand Up @@ -38,6 +41,10 @@ def __init__(self, source, *, data, volume=0.5):

self.title = data.get('title')
self.url = data.get('url')
self.channel = data.get('channel')
self.thumb = data.get('thumbnail_url')

self.artist_title = get_artist_title(self.title)

@classmethod
async def from_url(cls, url, *, loop=None, stream=False):
Expand All @@ -56,6 +63,42 @@ class Music(commands.Cog):
def __init__(self, bot):
self.bot = bot

def metadata(self, title):
"""Get metadata from last.fm"""
api_key = os.environ.get('LASTFM_API_KEY')

url = ["http://ws.audioscrobbler.com/2.0/?method=track.search"]
url.append("track=" + title)
url.append("api_key=" + api_key)
url.append("format=json")

response = requests.get('&'.join(url))

if response:
meta = response.json()
try:
title = meta['results']['trackmatches']['track'][0]['name']
artist = meta['results']['trackmatches']['track'][0]['artist']
except KeyError:
title, artist = None, None

# get album art
url = ["http://ws.audioscrobbler.com/2.0/?method=track.getInfo"]
url.append("track=" + title)
url.append("artist=" + artist)
url.append("api_key=" + api_key)
url.append("format=json")

response = requests.get('&'.join(url))
if response:
meta = response.json()
try:
album_art = meta['track']['album']['image'][2]['#text']
except KeyError:
album_art = None

return title, artist, album_art

@commands.command()
async def play(self, ctx, *args):
"""Plays music from query"""
Expand All @@ -65,7 +108,7 @@ async def play(self, ctx, *args):
player = await YTDLSource.from_url(url, loop=self.bot.loop, stream=True)
ctx.voice_client.play(player, after=lambda e: print('Player error: %s' % e) if e else None)

await ctx.send('Now playing: {}'.format(player.title))
await self.np(ctx)

@commands.command()
async def volume(self, ctx, volume: int):
Expand All @@ -83,6 +126,25 @@ async def stop(self, ctx):

await ctx.voice_client.disconnect()

@commands.command()
async def np(self, ctx):
"""Display the currently playing track"""

if ctx.voice_client is None:
return await ctx.send("Not connected to a voice channel.")

meta = self.metadata(ctx.voice_client.source.artist_title[1] or ctx.voice_client.source.title)
title = meta[0] or ctx.voice_client.source.artist_title[1] or ctx.voice_client.source.title
artist = meta[1] or ctx.voice_client.source.artist_title[0] or ctx.voice_client.source.channel
album_art = meta[2] or ctx.voice_client.source.thumb

embed = discord.Embed(title="Now playing")
embed.set_image(url=album_art)
embed.add_field(name="Title", value=title)
embed.add_field(name="Artist", value=artist)

await ctx.send(embed=embed)

@play.before_invoke
async def ensure_voice(self, ctx):
if ctx.voice_client is None:
Expand Down