pythonapicompiler-errorsnonetypegenius-api

Python: Get songs Lyrics from genius - Error


I am working on getting the songs lyrics from genius using API. I am having an issue with extract titles and lyrics from JSON file once it is saved. please see my code below.

import lyricsgenius as genius
api=genius.Genius('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
artist=api.search_artist('Beyonce') #max_songs=3, sort="title"
aux=artist.save_lyrics(filename='artist.txt',overwrite=True,skip_duplicates=True,verbose=True)
titles=[song['title'] for song in aux['songs']]
lyrics=[song['lyrics'] for song in aux['songs']]

The error I am having is:

TypeError                                 Traceback (most recent call last)
<ipython-input-21-4a24319b20b5> in <module>
----> 1 titles=[song['title'] for song in aux['songs']]
      2 lyrics=[song['lyrics'] for song in aux['songs']]

TypeError: 'NoneType' object is not subscriptable

Your help will be much appreciated. Thank you in advance!

Regards,

viku


Solution

  • The query output is saved to a json (or txt) file, i.e.:

    import json
    import lyricsgenius as genius
    
    api=genius.Genius('xxx')
    artist=api.search_artist('Pink Floyd', max_songs=1) #max_songs=3, sort="title"
    aux=artist.save_lyrics(filename='artist.json',overwrite=True,verbose=True)
    
    with open("artist.json") as f:
        j = json.load(f)
    
    # do something with j...
    

    But you can also use:

    artist = api.search_artist("Andy Shauf", max_songs=3, sort="title")
    print(artist.songs)
    song = api.search_song("To You", artist.name)
    print(song.lyrics)
    

    References:

    1. https://github.com/johnwmillr/LyricsGenius#usage
    2. https://docs.genius.com/