# 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 string
from common import c


class lyricsmirkforce():
    __name__ = 'lyrics.mirkforce'
    __version__ = '0.1.1'
    __author__ = 'Alexandr Grigorcea <cahr.gr@gmail.com>'
    __desc__ = 'API to load lyrics.mirkforce.net lyrics'
    
    URL = ""

    def get_data( self , artist, song ):
        artist_name = self.prepare(artist, True)
        song_name = self.prepare(song)
           
        self.URL = "http://lyrics.mirkforce.net/" + artist_name + "/" + song_name + ".txt"
        c.debug('lyrics.mirkforce URL: ' + self.URL)
        try:
            web = urllib2.urlopen(self.URL)
            return web.read()
        except:
            return None
        
    
    #====================== PREPARE ================================#
    def prepare(self, str, artist=False):
        s = str.decode('utf-8')
        s = s.lower()
        if artist:
            s = re.sub('^[Tt][Hh][Ee]\s*', '', s)
        s = re.sub('[\[\(]\s*(live|bonus|feat|ft).*?[\]\)]', '', s)
        s = s.encode('utf-8')
        s = s.translate(string.maketrans('',''), string.punctuation + ' -')
        s = urllib.quote(s)
        return s
        
        
    #==================== GET WEBPAGE ============================= #
    def get_webpage(self):
        return self.URL
