@@ -224,6 +224,70 @@ def fetch(self, artist, title):
224
224
return lyrics .strip (',"' ).replace ('\\ n' , '\n ' )
225
225
226
226
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
+
227
291
class LyricsWiki (SymbolsReplaced ):
228
292
"""Fetch lyrics from LyricsWiki."""
229
293
URL_PATTERN = 'http://lyrics.wikia.com/%s:%s'
@@ -444,12 +508,13 @@ def fetch(self, artist, title):
444
508
445
509
446
510
class LyricsPlugin (plugins .BeetsPlugin ):
447
- SOURCES = ['google' , 'lyricwiki' , 'lyrics.com' , 'musixmatch' ]
511
+ SOURCES = ['google' , 'lyricwiki' , 'lyrics.com' , 'musixmatch' , 'genius' ]
448
512
SOURCE_BACKENDS = {
449
513
'google' : Google ,
450
514
'lyricwiki' : LyricsWiki ,
451
515
'lyrics.com' : LyricsCom ,
452
516
'musixmatch' : MusiXmatch ,
517
+ 'genius' : Genius ,
453
518
}
454
519
455
520
def __init__ (self ):
@@ -459,12 +524,16 @@ def __init__(self):
459
524
'auto' : True ,
460
525
'google_API_key' : None ,
461
526
'google_engine_ID' : u'009217259823014548361:lndtuqkycfu' ,
527
+ 'genius_api_key' :
528
+ "Ryq93pUGm8bM6eUWwD_M3NOFFDAtp2yEE7W"
529
+ "76V-uFL5jks5dNvcGCdarqFjDhP9c" ,
462
530
'fallback' : None ,
463
531
'force' : False ,
464
532
'sources' : self .SOURCES ,
465
533
})
466
534
self .config ['google_API_key' ].redact = True
467
535
self .config ['google_engine_ID' ].redact = True
536
+ self .config ['genius_api_key' ].redact = True
468
537
469
538
available_sources = list (self .SOURCES )
470
539
if not self .config ['google_API_key' ].get () and \
0 commit comments