Ejemplo n.º 1
0
class Lyrics:
    """
        Fetches, cleans and saves lyrics.
    """

    def __init__( self, Addon, prefetch ):
        # set our Addon class
        self.Addon = Addon
        # is this prefetch
        self.prefetch = prefetch
        # info regex
        self.clean_info_regex = re.compile( "\[[a-z]+?:.*\]\s" )
        # lrc regex
        self.clean_lrc_lyrics_regex = re.compile( "\[([0-9]+):([0-9]+(?:\.[0-9]+)?)\](.*)" )
        # Overall timestamp adjustment in milliseconds regex
        self.timestamp_lrc_lyrics_regex = re.compile( "\[offset:([+-]*[0-9]+)\]" )
        # Website downloaded from regex
        self.website_regex = re.compile( "\[we:(.+)\]" )
        # set our scraper object
        self.scraper = Scraper( self.Addon, prefetch=self.prefetch )

    def get_lyrics( self, song ):
        try:
            # if embedded lyrics are found set them
            if ( xbmc.getInfoLabel( "MusicPlayer.Offset(%d).Lyrics" % ( self.prefetch, ) ) ):
                song.lyrics = unicode( xbmc.getInfoLabel( "MusicPlayer.Offset(%d).Lyrics" % ( self.prefetch, ) ), "UTF-8", "replace" )
                song.message = self.Addon.getLocalizedString( 30861 )
            # if cached lyrics are found set them
            else:
                # use httpapi for smb:// paths if xbox, with hack for change in xbmc so older revisions still work
                if ( song.lyrics_path.startswith( "smb://" ) and os.environ.get( "OS", "n/a" ) == "xbox" ):
                    song.lyrics = unicode( base64.standard_b64decode( xbmc.executehttpapi( "FileDownload(%s,bare)" % ( song.lyrics_path, ) ).split("\r\n\r\n")[ -1 ].strip( BOM_UTF8 ) ), "UTF-8" )
                else:
                    song.lyrics = unicode( open( song.lyrics_path, "r" ).read().strip( BOM_UTF8 ), "UTF-8" )
                # set cached message
                song.message = self.Addon.getLocalizedString( 30862 )
        except Exception, e:
            # no lyrics found, so fetch lyrics from internet
            self.scraper.fetch_lyrics( song )
            # if the search was successful save lyrics
            if ( song.status ):
                self.save_lyrics( song )
        # we need to clean lyrics in case they are lrc tagged
        self._clean_lyrics( song )