5

I am trying to create a program in Python which automatically retrieves lyrics for a particular folder of MP3s. [I get the lyrics from azlyrics.com ]

So far, I have succeeded in doing everything except for actually embedding the lyrics into the "lyrics" tag.

You answered a question regarding reading the lyrics from it's tag over here.

I was wondering if you could help me with setting the lyrics. Here's my code.

import urllib2 # For downloading webpage
import time # For pausing
import eyed3 # For MP3s
import re # For replacing characters
import os # For reading folders


path = raw_input('Please enter folder of music') # TODO Must make GUI PATH SELECTION


files = os.listdir(path) 
for x in files:
    # Must make the program stop for a while to minimize server load
    time.sleep(3)
    # Opening MP3
    mp3 = eyed3.load(path + '/' + x)
    # Setting Values
    artist = mp3.tag.artist.lower()
    raw_song = str(mp3.tag.title).lower()
    song = re.sub('[^0-9a-zA-Z]+', '', raw_song) #Stripping songs of anything other than alpha-numeric characters
    # Generating A-Z Lyrics URL
    url = "http://www.azlyrics.com/lyrics/" + artist + "/" + song + ".html"
    # Getting Source and extracting lyrics
    text = urllib2.urlopen(url).read()
    where_start = text.find('<!-- start of lyrics -->')
    start = where_start + 26
    where_end = text.find('<!-- end of lyrics -->')
    end = where_end - 2
    lyrics = unicode(text[start:end].replace('<br />', ''), "UTF8")
    # Setting Lyrics to the ID3 "lyrics" tag
    mp3.tag.lyrics = lyrics ### RUNNING INTO PROBLEMS HERE
    mp3.tag.save()

I am running into the following error after the 2nd-last line gets executed:-

Traceback (most recent call last):
File "<pyshell#62>", line 31, in <module>
mp3.tag.lyrics = lyrics
AttributeError: can't set attribute

I would also like you to know that I am a 15 year old who has been learning Python for about a year now. I searched everywhere and tried everything but I guess I need some help now.

Thanks in advance for all your help!

Community
  • 1
  • 1
Prateek Alat
  • 216
  • 2
  • 8
  • Looks like lyrics is expected as a ["specific" formatted string.](http://eyed3.nicfit.net/_modules/eyed3/id3/frames.html#LyricsFrame) and when browsing the source a saw warning messages like this:'"Fixing invalid lyrics language code...' Maybe changing your set lyrics code to: mp3.tag.lyrics = 'eng:MyDescription:'+lyrics will do the trick. – Robert Caspary Sep 20 '13 at 12:36
  • I get this error:- Traceback (most recent call last): File "/home/prateek/Desktop/Auto Lyrics Getter.py", line 31, in mp3.tag.lyrics = 'eng:MyDescription:'+lyrics AttributeError: can't set attribute – Prateek Alat Sep 20 '13 at 12:47
  • @PrateekAlat Do you have right answer for this? I was looking for this answer. – Gagan T K Jan 11 '20 at 11:57

2 Answers2

2

I don't pretend to understand why this is the way it is, but check out how lyrics are set in the handy example file:

from eyed3.id3 import Tag

t = Tag()
t.lyrics.set(u"""la la la""")

I believe this has to do with lyrics being placed into frames, but others may have to chime in with corrections on that. Note that this will fail unless you pass it unicode.

mlissner
  • 17,359
  • 18
  • 106
  • 169
2

chcp 65001

eyeD3 --encoding utf8 --add-lyrics "001-001.txt" 001-001.mp3