# Copyright (C) 2009-2010 Gloobus Developers
#
#This program is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public License
#as published by the Free Software Foundation; either version 2
#of the License, or (at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

import urllib
import urllib2
import re
#import lyrics
from xml.dom import minidom

from common import c

class lyricsfly():
    __name__ = 'lyricsfly'
    __version__ = '0.1.1'
    __author__ = 'Jaap Broekhuizen <jaapz.b@gmail.com>, Jordi Hernandez <guitarboy000@gmail.com>'
    __desc__ = 'API to load LyricsFly.com lyrics'
    
    URL = ""
    key = '1c0736f65ac693cbd-temporary.API.access'
    #we need a pemanent api key

    def get_data(self,artist,song):
        artist_name = artist.replace(" ","_")
        artist_name = artist_name.replace("\'","")
        artist_name = urllib.quote(artist_name.encode('utf-8'))
        song_name = song.replace(" ","_")
        song_name = song_name.replace("\'","")
        song_name = urllib.quote(song_name.encode('utf-8'))

        self.URL = "http://lyricsfly.com/api/api.php?i="+self.key+"&a="+artist_name+"&t="+song_name
        c.debug('LyricsFly URL: ' + self.URL)
        web = urllib2.urlopen(self.URL) 
        try: 
            xml = minidom.parseString(web.read())
            lyric = xml.getElementsByTagName("tx")[0].childNodes[0].nodeValue.replace("[br]","")
        except:
            lyric = None

        #print lyric
        return lyric

    #==================== GET WEBPAGE ============================= #
    def get_webpage(self):
        return 'http://lyricsfly.com/'
