Skip to content

Commit

Permalink
Add lyrics module (#149)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
kalbhor authored and swapagarwal committed Jan 3, 2017
1 parent 59beb1c commit d3e7b7c
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
TIME_ZONE_DB_API_KEY = '<<TIME_ZONE_DB_API_KEY>>'
WORDS_API_KEY = '<<WORDS_API_KEY>>'
YOUTUBE_DATA_API_KEY = '<<YOUTUBE_DATA_API_KEY>>'
MUSIX_API_KEY = '<<MUSIX_API_KEY>>'
NEWS_API_KEY = '<<NEWS_API_KEY>>'
Binary file added modules/.DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions modules/src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
'hello',
'help',
'joke',
'lyrics',
'movie',
'music',
'news',
Expand Down
55 changes: 55 additions & 0 deletions modules/src/lyrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import requests
import config
import os
from bs4 import BeautifulSoup
from templates.generic import *
from templates.text import TextTemplate

MUSIX_KEY = os.environ.get('MUSIX_API_KEY', config.MUSIX_API_KEY)

def process(input, entities):
output = {}
try:
query = entities['lyrics'][0]['value']

payload = {
'apikey': MUSIX_KEY,
'q_track': query,
}

r = requests.get('http://api.musixmatch.com/ws/1.1/track.search',params=payload)
data = r.json()

lyrics_url = data['message']['body']['track_list'][0]['track']['track_share_url']
track_id = data['message']['body']['track_list'][0]['track']['track_id']

payload = {
'apikey': MUSIX_KEY,
'track_id': track_id,
}

r = requests.get('http://api.musixmatch.com/ws/1.1/track.lyrics.get',params=payload)
data = r.json()
lyrics = '\n'.join(data['message']['body']['lyrics']['lyrics_body'].split('\n')[:-1])

title = query
item_url = lyrics_url
subtitle = lyrics

template = GenericTemplate()
template.add_element(title=title, item_url=item_url, subtitle=subtitle, buttons=[])

output['input'] = input
output['output'] = template.get_message()
output['success'] = True

except:
error_message = 'There was some error while retrieving data from genius.com'
error_message += '\n Please ask me somrthing else, like:'
error_message += '\n Lyrics for the song Wish you were here'
output['error_msg'] = TextTemplate(error_message).get_message()
output['success'] = False
return output



7 changes: 7 additions & 0 deletions modules/tests/test_lyrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import modules

def test_lyrics():
assert('lyrics' == modules.process_query("Lyrics for the song 'Wish you were here' ")[0])
assert('lyrics' == modules.process_query("lyrics for 'Go Robot' ")[0])
assert('lyrics' == modules.process_query("lyrics for the song Strawberry Fields Forever ")[0])
assert('lyrics' != modules.process_query("something random")[0])
Binary file added xkcd_cache.sqlite
Binary file not shown.

0 comments on commit d3e7b7c

Please sign in to comment.