Skip to content

Commit d3e7b7c

Browse files
kalbhorswapagarwal
authored andcommitted
Add lyrics module (#149)
* Added support for searching lyrics from genius.com * Removed .DS_Store * Minor changes * Changed quotes, fixed tests, removed extra lines * Removed DS_Store, extra line from __init__ * Added MusixMatch Search
1 parent 59beb1c commit d3e7b7c

File tree

7 files changed

+64
-0
lines changed

7 files changed

+64
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

config.py

+1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@
1616
TIME_ZONE_DB_API_KEY = '<<TIME_ZONE_DB_API_KEY>>'
1717
WORDS_API_KEY = '<<WORDS_API_KEY>>'
1818
YOUTUBE_DATA_API_KEY = '<<YOUTUBE_DATA_API_KEY>>'
19+
MUSIX_API_KEY = '<<MUSIX_API_KEY>>'
1920
NEWS_API_KEY = '<<NEWS_API_KEY>>'

modules/.DS_Store

10 KB
Binary file not shown.

modules/src/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
'hello',
1212
'help',
1313
'joke',
14+
'lyrics',
1415
'movie',
1516
'music',
1617
'news',

modules/src/lyrics.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import requests
2+
import config
3+
import os
4+
from bs4 import BeautifulSoup
5+
from templates.generic import *
6+
from templates.text import TextTemplate
7+
8+
MUSIX_KEY = os.environ.get('MUSIX_API_KEY', config.MUSIX_API_KEY)
9+
10+
def process(input, entities):
11+
output = {}
12+
try:
13+
query = entities['lyrics'][0]['value']
14+
15+
payload = {
16+
'apikey': MUSIX_KEY,
17+
'q_track': query,
18+
}
19+
20+
r = requests.get('http://api.musixmatch.com/ws/1.1/track.search',params=payload)
21+
data = r.json()
22+
23+
lyrics_url = data['message']['body']['track_list'][0]['track']['track_share_url']
24+
track_id = data['message']['body']['track_list'][0]['track']['track_id']
25+
26+
payload = {
27+
'apikey': MUSIX_KEY,
28+
'track_id': track_id,
29+
}
30+
31+
r = requests.get('http://api.musixmatch.com/ws/1.1/track.lyrics.get',params=payload)
32+
data = r.json()
33+
lyrics = '\n'.join(data['message']['body']['lyrics']['lyrics_body'].split('\n')[:-1])
34+
35+
title = query
36+
item_url = lyrics_url
37+
subtitle = lyrics
38+
39+
template = GenericTemplate()
40+
template.add_element(title=title, item_url=item_url, subtitle=subtitle, buttons=[])
41+
42+
output['input'] = input
43+
output['output'] = template.get_message()
44+
output['success'] = True
45+
46+
except:
47+
error_message = 'There was some error while retrieving data from genius.com'
48+
error_message += '\n Please ask me somrthing else, like:'
49+
error_message += '\n Lyrics for the song Wish you were here'
50+
output['error_msg'] = TextTemplate(error_message).get_message()
51+
output['success'] = False
52+
return output
53+
54+
55+

modules/tests/test_lyrics.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import modules
2+
3+
def test_lyrics():
4+
assert('lyrics' == modules.process_query("Lyrics for the song 'Wish you were here' ")[0])
5+
assert('lyrics' == modules.process_query("lyrics for 'Go Robot' ")[0])
6+
assert('lyrics' == modules.process_query("lyrics for the song Strawberry Fields Forever ")[0])
7+
assert('lyrics' != modules.process_query("something random")[0])

xkcd_cache.sqlite

36 KB
Binary file not shown.

0 commit comments

Comments
 (0)