# 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 re
import sys

from common import c

class ultimateguitar():

    __name__ = 'Ultimate-Guitar.com'
    __version__ = '0.1'
    __author__ = 'Jaap Broekhuizen <jaapz.b@gmail.com> And Jordi Hernandez <guitarboy000@gmail.com>'
    __desc__ = 'API to load Ultimate-Guitar.com tabs'
    
    URL = ""

    def get_data(self,artist,song):
        artist_name = artist.replace(" ","_")
        artist_name = artist_name.lower()
        artist_name = urllib.quote(artist_name.encode('utf-8'))
        song_name = song.replace(" ","_")
        song_name = song_name.lower()
        song_name = urllib.quote(song_name.encode('utf-8'))
        
        first_letter = artist_name[:1]
        #URL = "http://www.guitartabs.cc/tabs/" + first_letter + "/" + artist_name + "/" + song_name +".html"
        #self.URL = "http://www.ultimate-guitar.com/tabs/r/rise_against/any_way_you_want_it_solo_tab.htm"
        self.URL = "http://www.ultimate-guitar.com/tabs/" + first_letter +"/"+artist_name+"/"+song_name+"_tab.htm"
        c.debug('Ultimate-Guitar URL: ' + self.URL)
        web = urllib.urlopen(self.URL)
        web_code = web.read()

        #print web_code
        regexp = re.compile ("<pre>(.*)</pre>",re.DOTALL)
        m = re.search(regexp, web_code)
        

        try:
            tabs = m.group(1)
            tabs = tabs.replace("\n\n\n\n", "")
            tabs = tabs.replace("<span>", "")
            tabs = tabs.replace("</span>", "")
            #print tabs
            return tabs + "\n\n"
        except:
            return None
            
#==================== GET WEBPAGE ============================= #
    def get_webpage(self):
        return self.URL
        
