Skip to content

Commit 000487c

Browse files
committed
Merge pull request #1639 from sadatay/genius_lyrics
Add Genius as a lyric source
2 parents 7c6f7b2 + 3ab9932 commit 000487c

File tree

3 files changed

+73
-3
lines changed

3 files changed

+73
-3
lines changed

beetsplug/lyrics.py

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,70 @@ def fetch(self, artist, title):
224224
return lyrics.strip(',"').replace('\\n', '\n')
225225

226226

227+
class Genius(Backend):
228+
"""Fetch lyrics from Genius via genius-api."""
229+
def __init__(self, config, log):
230+
super(Genius, self).__init__(config, log)
231+
self.api_key = config['genius_api_key'].get(unicode)
232+
self.headers = {'Authorization': "Bearer %s" % self.api_key}
233+
234+
def search_genius(self, artist, title):
235+
query = u"%s %s" % (artist, title)
236+
url = u'https://api.genius.com/search?q=%s' \
237+
% (urllib.quote(query.encode('utf8')))
238+
239+
data = requests.get(
240+
url,
241+
headers=self.headers,
242+
allow_redirects=True
243+
).content
244+
245+
return json.loads(data)
246+
247+
def get_lyrics(self, link):
248+
url = u'http://genius-api.com/api/lyricsInfo'
249+
250+
data = requests.post(
251+
url,
252+
data={'link': link},
253+
headers=self.headers,
254+
allow_redirects=True
255+
).content
256+
257+
return json.loads(data)
258+
259+
def build_lyric_string(self, lyrics):
260+
if 'lyrics' not in lyrics:
261+
return
262+
sections = lyrics['lyrics']['sections']
263+
264+
lyrics_list = []
265+
for section in sections:
266+
lyrics_list.append(section['name'])
267+
lyrics_list.append('\n')
268+
for verse in section['verses']:
269+
if 'content' in verse:
270+
lyrics_list.append(verse['content'])
271+
272+
return ''.join(lyrics_list)
273+
274+
def fetch(self, artist, title):
275+
search_data = self.search_genius(artist, title)
276+
277+
if not search_data['meta']['status'] == 200:
278+
return
279+
else:
280+
records = search_data['response']['hits']
281+
if not records:
282+
return
283+
284+
record_url = records[0]['result']['url']
285+
lyric_data = self.get_lyrics(record_url)
286+
lyrics = self.build_lyric_string(lyric_data)
287+
288+
return lyrics
289+
290+
227291
class LyricsWiki(SymbolsReplaced):
228292
"""Fetch lyrics from LyricsWiki."""
229293
URL_PATTERN = 'http://lyrics.wikia.com/%s:%s'
@@ -444,12 +508,13 @@ def fetch(self, artist, title):
444508

445509

446510
class LyricsPlugin(plugins.BeetsPlugin):
447-
SOURCES = ['google', 'lyricwiki', 'lyrics.com', 'musixmatch']
511+
SOURCES = ['google', 'lyricwiki', 'lyrics.com', 'musixmatch', 'genius']
448512
SOURCE_BACKENDS = {
449513
'google': Google,
450514
'lyricwiki': LyricsWiki,
451515
'lyrics.com': LyricsCom,
452516
'musixmatch': MusiXmatch,
517+
'genius': Genius,
453518
}
454519

455520
def __init__(self):
@@ -459,12 +524,16 @@ def __init__(self):
459524
'auto': True,
460525
'google_API_key': None,
461526
'google_engine_ID': u'009217259823014548361:lndtuqkycfu',
527+
'genius_api_key':
528+
"Ryq93pUGm8bM6eUWwD_M3NOFFDAtp2yEE7W"
529+
"76V-uFL5jks5dNvcGCdarqFjDhP9c",
462530
'fallback': None,
463531
'force': False,
464532
'sources': self.SOURCES,
465533
})
466534
self.config['google_API_key'].redact = True
467535
self.config['google_engine_ID'].redact = True
536+
self.config['genius_api_key'].redact = True
468537

469538
available_sources = list(self.SOURCES)
470539
if not self.config['google_API_key'].get() and \

docs/changelog.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ The new features:
2626
* :doc:`/plugins/zero`: The plugin can now use a "whitelist" policy as an
2727
alternative to the (default) "blacklist" mode. Thanks to :user:`adkow`.
2828
:bug:`1621` :bug:`1641`
29-
29+
* :doc:`/plugins/lyrics`: Genius.com is now a source for lyrics.
3030

3131
Fixes:
3232

docs/plugins/lyrics.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ Lyrics Plugin
33

44
The ``lyrics`` plugin fetches and stores song lyrics from databases on the Web.
55
Namely, the current version of the plugin uses `Lyric Wiki`_, `Lyrics.com`_,
6-
`Musixmatch`_, and, optionally, the Google custom search API.
6+
`Musixmatch`_, `Genius.com`_, and, optionally, the Google custom search API.
77

88
.. _Lyric Wiki: http://lyrics.wikia.com/
99
.. _Lyrics.com: http://www.lyrics.com/
1010
.. _Musixmatch: https://www.musixmatch.com/
11+
.. _Genius.com: http://genius.com/
1112

1213

1314
Fetch Lyrics During Import

0 commit comments

Comments
 (0)