Skip to content

Instantly share code, notes, and snippets.

@valpackett
Created November 20, 2010 17:57
Show Gist options
  • Save valpackett/708012 to your computer and use it in GitHub Desktop.
Save valpackett/708012 to your computer and use it in GitHub Desktop.
Fetching full lyrics from LyricWiki
(*
Getting Lyrics
by myfreeweb
thanks to pepelsbey for "Year to Album Sort", based on it
*)
tell application "iTunes"
set sel to selection
if sel is not {} then
set ofi to fixed indexing
set fixed indexing to true
repeat with thisTrack in sel
try
tell thisTrack to set lyrics to my {do shell script "getlyrics.py '" & artist & "' '" & name & "'"}
on error m
log m
end try
end repeat
set fixed indexing to ofi
else
display dialog return & "Select tracks" buttons {"Cancel"} default button 1 with icon 0 giving up after 15
return
end if
end tell
#!/usr/bin/env python
import sys
import urllib2
from BeautifulSoup import BeautifulSoup, BeautifulStoneSoup, Comment
from soupselect import select # http://code.google.com/p/soupselect/
def get_lyrics(artist, track):
"""Fetches lyric page from LyricsWiki, parses it,
returns the lyrics.
"""
try:
src = urllib2.urlopen('http://lyrics.wikia.com/%s:%s' % (
artist.replace(' ', '_'), track.replace(' ', '_')))
parsed = BeautifulSoup(src)
comments = parsed.findAll(text=lambda text:isinstance(text, Comment))
ringtones = parsed.findAll(attrs={'class': 'rtMatcher'})
[node.extract() for node in comments + ringtones]
box = select(parsed, '.lyricbox')[0]
text = unicode(box.decodeContents()).replace('<br />', '\n')
text = BeautifulStoneSoup(text, convertEntities=BeautifulStoneSoup.HTML_ENTITIES)
except:
text = ''
return text
if __name__ == '__main__':
if len(sys.argv) != 3:
print "Wrong args. Syntax: getlyrics.py \"artist\" \"track\""
else:
print get_lyrics(sys.argv[1], sys.argv[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment