why am I unable to display the USLT lyrics - python

I am using mutagen to try to find lyrics on my media. when i run the following
import mutagen.mp3
mp3 = MP3(mp3file)
print mp3.pprint()
I can see that the frame USLT exists and it contains:
USLT=[unrepresentable data]
I do not understand why the data is not representable. I have inserted the tag into the mp3 file as follows:
tags = ID3(mp3file)
tags[u"USLT::'eng'"] = (USLT(encoding=3, lang=u'eng', desc=u'desc', text="this is a test"))
tags.save()
I dont really understand why I need to declare the tag as u"USLT::'eng'"] rather than using "USLT" on its own but I can confirm this works because I can see the tag appear in mp3tag (software used to modify mp3 tags)
so the tag exists, with lyrics. I can see this on both mp3.pprint() and in mp3tag yet I am not able to view it with the following code:
ulyrics = mp3["USLT"]
print ulyrics
I have tried changing the "USLT" to u"USLT::'eng'" but I get no difference.
I regularly see the error message:
File "filepath\mutagen_util.py", line 206, in getitem
return self.__dict[key]
KeyError: 'USLT'
but I cannot tell if this is an error in mutagen or my code (seeing as I can see results of all other tags I require)

At this moment, what worked for me is this:
from mutagen.id3 import ID3
mp3file = "... path to mp3 file ..."
tags = ID3(mp3file)
ulyrics = tags.getall('USLT')[0]
# change the lyrics text
ulyrics.text = " An arbitrary new lyrics text..."
tags.setall('USLT', [ulyrics])
# change the lyrics object completely
ulyrics = USLT(encoding=3, lang=u'eng', desc=u'desc', text="this is a test")
tags.setall('USLT', [ulyrics])
It's important to note that it's not necessary to use the key "USLT::'eng'", since the lang is included in the USLT object.

Related

Python adding hyperlink to Outlook task through win32com

I would like to create an hyperlink in the body of a task created through win32com.
This is my code so far:
outlook = win32com.client.Dispatch("Outlook.Application")
outlook_task_item = 3
recipient = "my_email#site.com"
task = outlook.CreateItem(outlook_task_item)
task.Subject = "hello world"
task.Body = "please update the file here"
task.DueDate = dt.datetime.today()
task.ReminderTime = dt.datetime.today()
task.ReminderSet = True
task.Save()
I have tried to set the property task.HTMLBody but I get the error:
AttributeError: Property 'CreateItem.HTMLBody' can not be set.
I have also tried
task.Body = "Here is the <a href='http://www.python.org'>link</a> I need"
but I am not getting a proper hyperlink.
However if I create a task front end in Outlook, I am able to add hyperlinks.
You can also try:
task.HTMLBody = "Here is the <a href='http://www.python.org'>link</a> I need"
this will overwrite data in 'task.Body' to the HTML format provides in 'task.HTMLBody'
so whichever (Body or HTMLBody) is last will be taken as the Body of the mail.
Tasks do not support HTML. Instead, you have to provide RTF.
You can investigate -- but not set -- the RTF of a given task through task.RTFBody (and task.RTFBody.obj to get a convenient view of it). To use RTF in the body of a task, simply use the task.Body property; setting this to a byte array containing RTF will automatically use that RTF in the body. Concretely, to get the body you want, you could let
task.Body = rb'{\rtf1{Here is the }{\field{\*\fldinst { HYPERLINK "https://www.python.org" }}{\fldrslt {link}}}{ I need}}'

Retrieve XML parent and child attributes using Python and lxml

I'm trying to process an XML file using XPATH in Python / lxml.
I can pull out the values at a particular level of the tree using this code:
file_name = input('Enter the file name, including .xml extension: ') # User inputs file name
print('Parsing ' + file_name)
from lxml import etree
parser = etree.XMLParser()
tree = etree.parse(file_name, parser)
r = tree.xpath('/dataimport/programmelist/programme')
print (len(r))
with open(file_name+'.log', 'w', encoding='utf-8') as f:
for r in tree.xpath('/dataimport/programmelist/programme'):
progid = (r.get("id"))
print (progid)
It returns a list of values as expected. I also want to return the value of a 'child' (where it exists), but I can't work out how (I can only get it to work as a separate list, but I need to maintain the link between them).
Note: I will be writing the values out to a log file, but since I haven't been successful in getting everything out that I want, I haven't added the 'write out' code yet.
This is the structure of the XML:
<dataimport dtdversion="1.1">
<programmelist>
<programme id="eid-273168">
<imageref idref="img-1844575"/>
How can I get Python to return the id + idref?
The previous examples I have worked with had namespaces, but this file doesn't.
Since xpath() method returns tree, you can use xpath again to get idref list you want:
for r in tree.xpath('/dataimport/programmelist/programme')
progid = r.get("id")
ref_list = r.xpath('imageref/#idref')
print progid, ref_lis

get a list of comma separated values in python and use that list to perform a command as many times as necessary with each value

I'm sure this is something easy to do for someone with programming skills (unlike me). I am playing around with the Google Sites API. Basically, I want to be able to batch-create a bunch of pages, instead of having to do them one by one using the slow web form, which is a pain.
I have installed all the necessary files, read the documentation, and successfully ran this sample file. As a matter of fact, this sample file already has the python code for creating a page in Google Sites:
elif choice == 4:
print "\nFetching content feed of '%s'...\n" % self.client.site
feed = self.client.GetContentFeed()
try:
selection = self.GetChoiceSelection(
feed, 'Select a parent to upload to (or hit ENTER for none): ')
except ValueError:
selection = None
page_title = raw_input('Enter a page title: ')
parent = None
if selection is not None:
parent = feed.entry[selection - 1]
new_entry = self.client.CreatePage(
'webpage', page_title, '<b>Your html content</b>',
parent=parent)
if new_entry.GetAlternateLink():
print 'Created. View it at: %s' % new_entry.GetAlternateLink().href
I understand the creation of a page revolves around page_title and new_entry and CreatePage. However, instead of creating one page at a time, I want to create many.
I've done some research, and I gather I need something like
page_titles = input("Enter a list of page titles separated by commas: ").split(",")
to gather a list of page titles (like page1, page2, page3, etc. -- I plan to use a text editor or spreadsheet to generate a long list of comma separated names).
Now I am trying to figure out how to get that string and "feed" it to new_entry so that it creates a separate page for each value in the string. I can't figure out how to do that. Can anyone help, please?
In case it helps, this is what the Google API needs to create a page:
entry = client.CreatePage('webpage', 'New WebPage Title', html='<b>HTML content</b>')
print 'Created. View it at: %s' % entry.GetAlternateLink().href
Thanks.
Whenever you want to "use that list to perform a command as many times as necessary with each value", that's a for loop. (It may be an implicit for loop, e.g., in a map call or a list comprehension, but it's still a loop.)
So, after this:
page_titles = raw_input("Enter a list of page titles separated by commas: ").split(",")
You do this:
for page_title in page_titles:
# All the stuff that has to be done for each single title goes here.
# I'm not entirely clear on what you're doing, but I think that's this part:
parent = None
if selection is not None:
parent = feed.entry[selection - 1]
new_entry = self.client.CreatePage(
'webpage', page_title, '<b>Your html content</b>',
parent=parent)
if new_entry.GetAlternateLink():
print 'Created. View it at: %s' % new_entry.GetAlternateLink().href
And that's usually all there is to it.
You can use a for loop to loop over a dict object and create multiple pages. Here is a little snippet to get you started.
import gdata.sites.client
client = gdata.sites.client.SitesClient(
source=SOURCE_APP_NAME, site=site_name, domain=site_domain)
pages = {"page_1":'<b>Your html content</b>',
"page_2":'<b>Your other html content</b>'}
for title, content in pages.items():
feed = client.GetContentFeed()
parent = None
if selection is not None:
parent = feed.entry[selection - 1]
client.CreatePage('webpage', page_title, content, parent=parent)
as for feeding your script from an external source I would recommend something like a csv file
##if you have a csv file called pages.csv with the following lines
##page_1,<b>Your html content</b>
##page_2,<b>Your other html content</b>
import csv
with open("pages.csv", 'r') as f:
reader = csv.reader(f)
for row in reader:
title, content = row
client.CreatePage('webpage', title, content, parent=parent)

Mutagen pprint() and save() errors (python 2.7.5)

I'm running mutagen 1.21 with python 2.7.5 on windows 8, and I get the errors below when I try to use pprint() and save(). If anyone could figure out what the problem is, I would also appreciate a sample code of how to set the title, artist, album artist, album, date and track number of an mp3 file. Also, is it possible to not remove the already existing tags that I don't wish to change? If not, I need a way to get the genre of an mp3 file before it is removed, and then set it again along with the other tags.
code
from mutagen.mp3 import MP3
p = "E:\\Musik\\Aeon\\2005 Bleeding the False\\01 Cenobites - Copy.mp3"
audio = MP3(p)
audio["title"] = "An example"
audio.pprint()
audio.save()
traceback
Traceback (most recent call last):
File "id3tag.py", line 5, in <module>
audio.pprint()
File "C:\Python27\lib\site-packages\mutagen\__init__.py", line 138, in pprint
try: tags = self.tags.pprint()
File "C:\Python27\lib\site-packages\mutagen\id3.py", line 190, in pprint
frames = list(map(Frame.pprint, self.values()))
TypeError: unbound method pprint() must be called with Frame instance as first a
rgument (got str instance instead)
code
from mutagen.mp3 import MP3
p = "E:\\Musik\\Aeon\\2005 Bleeding the False\\01 Cenobites - Copy.mp3"
audio = MP3(p)
audio["title"] = "An example"
audio.save()
traceback
Traceback (most recent call last):
File "id3tag.py", line 5, in <module>
audio.save()
File "C:\Python27\lib\site-packages\mutagen\__init__.py", line 132, in save
return self.tags.save(filename, **kwargs)
File "C:\Python27\lib\site-packages\mutagen\id3.py", line 370, in save
framedata = [self.__save_frame(frame) for (key, frame) in frames]
File "C:\Python27\lib\site-packages\mutagen\id3.py", line 461, in __save_frame
framedata = frame._writeData()
AttributeError: 'str' object has no attribute '_writeData'
Edit: (Regarding Michael0x2a's answer) The chart you linked is exactly what I was looking for, but it only worked halfway. Title, track, artist and album all worked fine. For all fields under the mp3 file's properties>details except these four and "year", previous values were cleared by save(), and new ones couldn't be added - lines such as the one you posted,
audio.add(id3.TPE2(encoding=3, text=u"An example"))
did nothing. In particular the "genre" and "album artist" fields don't work. As for "year" which has both the codes TYER and TDAT, it wouldn't change at all unless the field was empty first, and then only by TYER. "ORIGYEAR" with the code TORY does nothing.
The "genre" field isn't actually completely broken - if you change it with python code (audio.add(etc)), or manually go into properties>details beforehand, save() will clear non-built-in genres such as "Technical Death Metal" or "mt3jr39kf390", while it works with built-in genres such as "Classic Rock" or "Country", while certain integers such as 1 or 2 turn into those built-in genres.
Here's the code I used:
from mutagen.id3 import ID3, TIT2, TPE2, TALB, TPE1, TYER, TDAT, TRCK, TCON, TORY, TPUB
p = "E:\\Musik\\Aeon\\2005 Bleeding the False\\01 Cenobites - Copy.mp3"
audio = ID3(p)
audio.add(TIT2(encoding=3, text=u"t")) #TITLE
audio.add(TRCK(encoding=3, text=u"1")) #TRACK
audio.add(TPE1(encoding=3, text=u"a")) #ARTIST
audio.add(TALB(encoding=3, text=u"al")) #ALBUM
audio.add(TYER(encoding=3, text=u"2000")) #YEAR
audio.add(TDAT(encoding=3, text=u"2001")) #YEAR
audio.add(TORY(encoding=3, text=u"2002")) #ORIGYEAR
audio.add(TPE2(encoding=3, text=u"aa")) #ALBUMARTIST
audio.add(TCON(encoding=3, text=u"g")) #GENRE
audio.save()
Edit: I continued the question at Some mutagen tags don't work
The MP3 class is a type of ID3 class, which as the tutorial says, is highly structured and cannot be simply written to because of metadata it needs to keep track of. I suspect that's the reason for the errors -- when you're trying to save or print the audio object, the code is expecting each key to point to some object, but instead sees a simple string.
Instead, you need to specify that you want to use the EasyMP3 class so you can edit the keys like you want. The EasyMP3 class will automatically convert the strings into the appropriate objects for you. Example from the docs:
from mutagen.mp3 import EasyMP3 as MP3
audio = MP3("example.mp3")
audio["title"] = "An example"
audio.pprint()
However, you can only edit certain keys. To get a list of valid keys that are editable, use the following:
from mutagen.easyid3 import EasyID3
print '\n'.join(EasyID3.valid_keys.keys())
Edit:
Disclaimer: None of this is tested, and might need tweaking or debugging.
To edit tags not included within EasyMP3, you could reference this chart and use the 4-letter tag under the ID3v2.3 column.
For example, to set the album artist, the chart states that the 4-letter code is TPE2. You could then try:
import mutagen.id3 as id3
import mutagen.mp3 as mp3
audio = mp3.MP3("example.mp3")
audio.add(id3.TPE2(encoding=3, text=u"An example"))
audio.save()
However, I'm not 100% sure what the encoding parameter does, nor could I find any documentation about it, so your mileage may vary.

Python: Why will this string print but not write to a file?

I am new to Python and working on a utility that changes an XML file into an HTML. The XML comes from a call to request = urllib2.Request(url), where I generate the custom url earlier in the code, and then set response = urllib2.urlopen(request) and, finally, xml_response = response.read(). This works okay, as far as I can tell.
My trouble is with parsing the response. For starters, here is a partial example of the XML structure I get back:
I tried adapting the slideshow example in the minidom tutorial here to parse my XML (which is ebay search results, by the way): http://docs.python.org/2/library/xml.dom.minidom.html
My code so far looks like this, with try blocks as an attempt to diagnose issues:
doc = minidom.parseString(xml_response)
#Extract relevant information and prepare it for HTML formatting.
try:
handleDocument(doc)
except:
print "Failed to handle document!"
def getText(nodelist): #taken straight from slideshow example
rc = []
for node in nodelist:
if node.nodeType == node.TEXT_NODE:
print "A TEXT NODE!"
rc.append(node.data)
return ''.join(rc) #this is a string, right?
def handleDocument(doc):
outputFile = open("EbaySearchResults.html", "w")
outputFile.write("<html>\n")
outputFile.write("<body>\n")
try:
items = doc.getElementsByTagName("item")
except:
"Failed to get elements by tag name."
handleItems(items)
outputFile.write("</html>\n")
outputFile.write("</body>\n")
def handleItems(items):
for item in items:
title = item.getElementsByTagName("title")[0] #there should be only one title
print "<h2>%s</h2>" % getText(title.childNodes) #this works fine!
try: #none of these things work!
outputFile.write("<h2>%s</h2>" % getText(title.childNodes))
#outputFile.write("<h2>" + getText(title.childNodes) + "</h2>")
#str = getText(title.childNodes)
#outputFIle.write(string(str))
#outputFile.write(getText(title.childNodes))
except:
print "FAIL"
I do not understand why the correct title text does print to the console but throws an exception and does not work for the output file. Writing plain strings like this works fine: outputFile.write("<html>\n") What is going on with my string construction? As far as I can tell, the getText method I am using from the minidom example returns a string--which is just the sort of thing you can write to a file..?
If I print the actual stack trace...
...
except:
print "Exception when trying to write to file:"
print '-'*60
traceback.print_exc(file=sys.stdout)
print '-'*60
traceback.print_tb(sys.last_traceback)
...
...I will instantly see the problem:
------------------------------------------------------------
Traceback (most recent call last):
File "tohtml.py", line 85, in handleItems
outputFile.write(getText(title.childNodes))
NameError: global name 'outputFile' is not defined
------------------------------------------------------------
Looks like something has gone out of scope!
Fellow beginners, take note.

Categories

Resources