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

Add lyrics module #26

Open
wants to merge 2 commits into
base: master
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
13 changes: 13 additions & 0 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,17 @@ async def search_image(ctx, search_arg):
print(e)
await ctx.send("Sorry, something went wrong.")


@bot.command(pass_context=True, name='lyrics')
async def get_lyrics(ctx, *, message):
try:
output = lyrics.process(message)
r = str(output['output']).replace('{', '').replace('}', '')
r = r.replace('\'text\':', '').replace('\'', '')
r = r.replace('\\n', '\n').replace('"', '')
await ctx.send(r)
except Exception as e:
print(e)
await ctx.send("Sorry, something went wrong.")

bot.run(TOKEN)
56 changes: 56 additions & 0 deletions modules/lyrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import os

import requests

import config
from templates.text import TextTemplate

MUSIXMATCH_API_KEY = os.environ.get('MUSIXMATCH_API_KEY', config.MUSIXMATCH_API_KEY)


def process(message):
output = {}
try:
# Search in title
r = requests.get('http://api.musixmatch.com/ws/1.1/track.search', params={
'apikey': MUSIXMATCH_API_KEY,
'q_track': message,
's_track_rating': 'desc',
'f_has_lyrics': '1'
})
data = r.json()
# Search inside lyrics if no results found in title search
if int(data['message']['header']['available']) == 0:
r = requests.get('http://api.musixmatch.com/ws/1.1/track.search', params={
'apikey': MUSIXMATCH_API_KEY,
'q_lyrics': message,
's_track_rating': 'desc',
'f_has_lyrics': '1'
})
data = r.json()
track = data['message']['body']['track_list'][0]['track']
track_id = track['track_id']
track_name = track['track_name']
artist_name = track['artist_name']
lyrics_url = track['track_share_url']

r = requests.get('http://api.musixmatch.com/ws/1.1/track.lyrics.get', params={
'apikey': MUSIXMATCH_API_KEY,
'track_id': track_id
})
data = r.json()
lyrics = data['message']['body']['lyrics']['lyrics_body']
string1 = ('Here are the lyrics of ' + track_name + ' by ' + artist_name + ':\n' + lyrics
+ '\n- Powered by MusiXmatch')
string2 = '\nFull Lyrics: ' + lyrics_url
output['output'] = string1 + string2
output['success'] = True
except Except:
error_message = 'I couldn\'t find any lyrics matching your query.'
error_message += '\nPlease ask me something else, like:'
error_message += '\n - paradise lyrics'
error_message += '\n - lyrics of the song hall of fame'
error_message += '\n - What are the lyrics to see you again?'
output['error_msg'] = TextTemplate(error_message).get_message()
output['success'] = False
return output