# -*- coding: utf-8 -*-
import encodings.utf_8
import urllib
import urllib2
from xml.dom import minidom
from grabber import LyricProviderBase

class LyricsPlugin(LyricProviderBase):
	def GetName(self):
		return "Lyrics Plugin"

	def GetVersion(self):
			return "1"

	def GetURL(self):
		return "http://www.lyricsplugin.com"

	def GetAuthor(self):
		return "Hitchhiker427 + Qudeid"

	def Query(self, handles, status, abort):
		result = []

		for handle in handles:
			status.Advance()

			if abort.Aborting():
				return result

			artist = handle.Format("[%artist%]")
			title = handle.Format("[%title%]")

			try:
				req = urllib2.Request("http://www.lyricsplugin.com/winamp03/plugin/content.php?artist=%s&title=%s" % (urllib.quote(artist), urllib.quote(title)))
				req.add_header('Referer', "http://www.lyricsplugin.com/winamp03/plugin/?artist=%s&title=%s" % (urllib.quote(artist), urllib.quote(title)))
				string = urllib2.urlopen(req).read()
				start = string.find("<div id=\"lyrics\">") + 18
				end = string.find("</div>", start)
				lyric = string[start:end].replace("<br />","")
				result.append(lyric)
			except Exception, e:
				traceback.print_exc(file=sys.stdout)
				result.append('')
				continue

		return result

if __name__ == "__main__":
	LyricProviderInstance = LyricsPlugin()