lyrics.py

Go to the documentation of this file.
00001 from __future__ import with_statement
00002 
00003 import urllib,sys
00004 
00005 import re
00006 from htmlentitydefs import name2codepoint
00007 
00008 import gtk,gtk.glade
00009 from plai import config
00010 
00011 
00012 _entity_re = re.compile(r'&(?:(#)(\d+)|([^;]+));')
00013 
00014 def _FixHTML(html):
00015         """Turn HTML codepoints etc into single characters.
00016         taken from http://groups.google.com/group/comp.lang.python/browse_frm/thread/9b7bb3f621b4b8e4"""
00017         def _repl_func(match):
00018                 if match.group(1): # Numeric character reference
00019                         return unichr(int(match.group(2)))
00020                 else:
00021                         return unichr(name2codepoint[match.group(3)])
00022                         
00023         return _entity_re.sub(_repl_func,html).decode('ascii','ignore')    
00024 
00025 
00026 def GetLyrics(artist,title):
00027         """Block and return song lyrics retreived from lyrc.com.ar or None."""
00028         site='http://lyrc.com.ar/en/tema1en.php'
00029         args='?artist='+urllib.quote_plus(artist)+'&songname='+urllib.quote_plus(title)
00030 
00031         f=urllib.urlopen(site+args)
00032         pagecontents=f.read()
00033         f.close()
00034 
00035         startmarker='</script></td></tr></table>'
00036 
00037         # The second of these is for the case where there are no thanks given.
00038         endmarkers=["<p><hr size=1 noshade color=white width=100%><font color='#E9AF03'>Thk",
00039                                 """<br><br><a href="#" onClick="javascript:window.open('badsong.php?songname="""]
00040                                 
00041         start=pagecontents.find(startmarker)+len(startmarker)
00042 
00043         for marker in endmarkers:
00044                 end=pagecontents.find(marker)
00045                 if end!=-1: break
00046                 
00047         if start==len(startmarker)-1 or end==-1: 
00048                 return None
00049         else:
00050                 return _FixHTML(pagecontents[start:end].replace('<br />','')).strip()
00051                 
00052         
00053 def LyricsWindow(lyrics,artist,title):
00054         """Pops up a window containing 'lyrics' for 'title' by 'artist'."""
00055                 
00056         def onSave(widget):
00057                 """Sub function, displays file dialog and saves lyrics where chosen."""
00058                 dlg=gtk.FileChooserDialog('Save lyrics where?',None,gtk.FILE_CHOOSER_ACTION_SAVE,(gtk.STOCK_SAVE,gtk.RESPONSE_ACCEPT))
00059                 dlg.set_current_name(artist+' - '+title+'.txt')
00060                 dlg.run()
00061                 fname=dlg.get_filename()
00062                 dlg.hide()
00063                 with open(fname,'w') as f:
00064                         f.write(buf.get_text(buf.get_start_iter(),buf.get_end_iter())+'\n')
00065                         
00066         xml=gtk.glade.XML(config.gladelyricsfname)
00067         win=xml.get_widget('LyricsWindow')
00068         win.set_title('Lyrics for '+artist+' - '+title)
00069         buf=gtk.TextBuffer()
00070         buf.set_text(lyrics if lyrics else 'Sorry - no lyrics found.')
00071         view=xml.get_widget('Lyrics')
00072         view.set_buffer(buf)
00073         xml.get_widget('SaveButton').connect('clicked',onSave)
00074         xml.get_widget('QuitButton').connect('clicked',lambda junk:win.destroy())
00075                 

Generated on Mon Aug 6 21:24:19 2007 for plai by  doxygen 1.5.1