1

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

Viku
  • 29
  • 2
  • 10
  • 1
    can you put sample of aux ? – KcH Mar 03 '20 at 12:31
  • 1
    `artist.save_lyrics()` doesn't return anything. According to the sourcecode found [here](https://github.com/johnwmillr/LyricsGenius/blob/a65c0fb7b2a2c7f35fe004d390c2ea2253265c0f/lyricsgenius/artist.py#L135), it just saves it into a json-file. Edit: And that's why `aux` is `None`. – Hampus Larsson Mar 03 '20 at 12:34
  • 2
    @Codennewbie and Hampus, you are right. aux is none thus it is giving error. so should i then import json and read data from the saved file? – Viku Mar 03 '20 at 12:43

1 Answers1

5

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/
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268