Exemplo n.º 1
0
    def get_lyrics(self, lyrics):
        utilities.log(debug, "%s: searching lyrics for %s - %s - %s" % (__title__, lyrics.artist, lyrics.album, lyrics.title))

        artist = utilities.deAccent(lyrics.artist)
        title = utilities.deAccent(lyrics.title)
        try: # below is borowed from XBMC Lyrics
            url = "http://www.lyricsmode.com/lyrics/%s/%s/%s.html" % (artist.lower()[:1], artist.lower().replace("&","and").replace(" ","_"), title.lower().replace("&","and").replace(" ","_"))
            lyrics_found = False
            while True:
                utilities.log(debug, "%s: search url: %s" % (__title__, url))
                song_search = urllib.urlopen(url).read()
                if song_search.find("<p id=\"lyrics_text\" class=\"ui-annotatable\">") >= 0:
                    break

                if lyrics_found:
                    # if we're here, we found the lyrics page but it didn't
                    # contains the lyrics part (licensing issue or some bug)
                    return False

                # Let's try to use the research box if we didn't yet
                if not 'search' in url:
                    url = "http://www.lyricsmode.com/search.php?what=songs&s=" + urllib.quote_plus(title.lower())
                else:
                    # the search gave more than on result, let's try to find our song
                    url = ""
                    start = song_search.find('<!--output-->')
                    end = song_search.find('<!--/output-->', start)
                    results = self.search_results_regex.findall(song_search, start, end)

                    for result in results:
                        if result[0].lower() in artist.lower():
                            url = "http://www.lyricsmode.com" + result[1]
                            lyrics_found = True
                            break

                    if not url:
                        # Is there a next page of results ?
                        match = self.next_results_regex.search(song_search[end:])
                        if match:
                            url = "http://www.lyricsmode.com/search.php" + match.group(1)
                        else:
                            return False

            lyr = song_search.split("<p id=\"lyrics_text\" class=\"ui-annotatable\">")[1].split('</p><p id=\"lyrics_text_selected\">')[0]
            lyr = self.clean_br_regex.sub( "\n", lyr ).strip()
            lyr = self.clean_lyrics_regex.sub( "", lyr ).strip()
            lyr = self.normalize_lyrics_regex.sub( lambda m: unichr( int( m.group( 1 ) ) ), lyr.decode("ISO-8859-1") )
            lir = []
            for line in lyr.splitlines():
                line.strip()
                if line.find("Lyrics from:") < 0:
                    lir.append(line)
            lyr = u"\n".join( lir )
            if lyr.startswith('These lyrics are missing'):
                return False
            lyrics.lyrics = lyr
            return True
        except:
            utilities.log(True, "%s: %s::%s (%d) [%s]" % (
                   __title__, self.__class__.__name__,
                   sys.exc_info()[ 2 ].tb_frame.f_code.co_name,
                   sys.exc_info()[ 2 ].tb_lineno,
                   sys.exc_info()[ 1 ]
                   ))
            return False
Exemplo n.º 2
0
    def get_lyrics(self, lyrics):
        utilities.log(
            debug, "%s: searching lyrics for %s - %s - %s" %
            (__title__, lyrics.artist, lyrics.album, lyrics.title))

        artist = utilities.deAccent(lyrics.artist)
        title = utilities.deAccent(lyrics.title)
        try:  # below is borowed from XBMC Lyrics
            url = "http://www.lyricsmode.com/lyrics/%s/%s/%s.html" % (
                artist.lower()[:1], artist.lower().replace("&", "and").replace(
                    " ", "_"), title.lower().replace("&", "and").replace(
                        " ", "_"))
            lyrics_found = False
            while True:
                utilities.log(debug, "%s: search url: %s" % (__title__, url))
                song_search = urllib.urlopen(url).read()
                if song_search.find(
                        "<p id=\"lyrics_text\" class=\"ui-annotatable\">"
                ) >= 0:
                    break

                if lyrics_found:
                    # if we're here, we found the lyrics page but it didn't
                    # contains the lyrics part (licensing issue or some bug)
                    return False

                # Let's try to use the research box if we didn't yet
                if not 'search' in url:
                    url = "http://www.lyricsmode.com/search.php?what=songs&s=" + urllib.quote_plus(
                        title.lower())
                else:
                    # the search gave more than on result, let's try to find our song
                    url = ""
                    start = song_search.find('<!--output-->')
                    end = song_search.find('<!--/output-->', start)
                    results = self.search_results_regex.findall(
                        song_search, start, end)

                    for result in results:
                        if result[0].lower() in artist.lower():
                            url = "http://www.lyricsmode.com" + result[1]
                            lyrics_found = True
                            break

                    if not url:
                        # Is there a next page of results ?
                        match = self.next_results_regex.search(
                            song_search[end:])
                        if match:
                            url = "http://www.lyricsmode.com/search.php" + match.group(
                                1)
                        else:
                            return False

            lyr = song_search.split(
                "<p id=\"lyrics_text\" class=\"ui-annotatable\">")[1].split(
                    '</p><p id=\"lyrics_text_selected\">')[0]
            lyr = self.clean_br_regex.sub("\n", lyr).strip()
            lyr = self.clean_lyrics_regex.sub("", lyr).strip()
            lyr = self.normalize_lyrics_regex.sub(
                lambda m: unichr(int(m.group(1))), lyr.decode("ISO-8859-1"))
            lir = []
            for line in lyr.splitlines():
                line.strip()
                if line.find("Lyrics from:") < 0:
                    lir.append(line)
            lyr = u"\n".join(lir)
            if lyr.startswith('These lyrics are missing'):
                return False
            lyrics.lyrics = lyr
            return True
        except:
            utilities.log(
                True, "%s: %s::%s (%d) [%s]" %
                (__title__, self.__class__.__name__,
                 sys.exc_info()[2].tb_frame.f_code.co_name,
                 sys.exc_info()[2].tb_lineno, sys.exc_info()[1]))
            return False