All pastes #618793 Raw Edit

Someone

public text v1 · immutable
·published 2007-07-14 05:48 UTC
rendered paste body
# Copyright (C) 2007 Jason Smith
#
# 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 1, 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., 675 Mass Ave, Cambridge, MA 02139, USA.

import gtk, pygtk, plugins, urllib
from xl import library, xlmisc, common

PLUGIN_NAME = "Information Panel"
PLUGIN_AUTHORS = ['Jason Smith <jassmith@gmail.com>']
PLUGIN_VERSION = '0.0.5'
PLUGIN_DESCRIPTION = r"""Will one day display information about the currently playing song"""

PLUGIN_ENABLED = False
PLUGIN_ICON = None

#setup infobox namespace

INFOBOX = None
CONNS = plugins.SignalContainer()

class infobox:
    def __init__(self):
        """
            Set up variables and create window
        """
        self.WINDOW = None
        self.window_visible = 0
        self.window_ready = 0
        self.create_window()
    
    def create_window(self):
        self.WINDOW = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.WINDOW.set_title("Information Panel")
        self.WINDOW.set_border_width(0)
        self.WINDOW.set_default_size(450, 800)
        
        #what to do when window is destroyed
        self.WINDOW.connect("delete_event", self.destroy_window)
        
        self.EXTERNAL_BOX = gtk.VBox(homogeneous=False, spacing=0)
        self.WINDOW.add(self.EXTERNAL_BOX)
        
        self.TITLE_LABEL = gtk.Label("Title - Artist")
        self.TITLE_LABEL.set_use_markup(True)
        self.EXTERNAL_BOX.pack_start(self.TITLE_LABEL, False, True, 0)
        
        self.ALBUM_LABEL = gtk.Label("Album")
        self.EXTERNAL_BOX.pack_start(self.ALBUM_LABEL, False, True, 0)
        
        self.LYRICS_WRAP = gtk.ScrolledWindow()
        self.LYRICS_WRAP.set_border_width(10)
        
        self.LYRICS = gtk.TextView()
        self.LYRICS.set_editable(False)
        
        
        #get us a buffer
        self.LYR_BUFFER = self.LYRICS.get_buffer()
        
        self.LYRICS_WRAP.add(self.LYRICS)
        self.EXTERNAL_BOX.pack_start(self.LYRICS_WRAP, True, True, 0)
        
        #make everything show
        self.WINDOW.show_all()
        
        self.window_visible = 1
        self.window_ready = 1
    
    def fetch_lyrics(self, artist, song):
        params = {'artist': APP.player.current.artist.encode('latin1'), 'songname': APP.player.current.title.encode('latin1')}
    
        search = "http://lyricwiki.org/api.php?artist=%s&song=%s&fmt=text" % (
            urllib.quote_plus(params['artist']), 
            urllib.quote_plus(params['songname']))
        data = urllib.urlopen(search).read()
        return data
    
    def destroy_window(self, widget=None, event=None, data=None):
        self.WINDOW.destroy()
        self.window_visible = 0
    
    @common.threaded
    def update(self, widget=None, args=None):
        if not self.window_visible:
            self.create_window()
            
        self.artist = APP.player.current.artist
        self.song = APP.player.current.title
        self.album = APP.player.current.album
        #format the string
        self.TitleText = "<b>" + self.song + " - " + self.artist + "</b>"
        self.TITLE_LABEL.set_markup(self.TitleText)
        self.ALBUM_LABEL.set_text(self.album)
        
        self.LYR_BUFFER.set_text("Loading Lyrics...")
        self.LYR_BUFFER.set_text(self.fetch_lyrics(self.artist, self.song))
    

def initialize():
    """
        Called when the plugin is enabled
    """
    global INFOBOX
    
    #create INFOBOX object from infobox() class
    INFOBOX = infobox()
    
    #setup callback on play track
    CONNS.connect(APP.player, 'play-track', INFOBOX.update)
    return True
    
def destroy():
    """
        Called when the plugin is disabled
    """
    INFOBOX.destroy_window()
    CONNS.disconnect_all()
    
def configure():
    """
        Called when the user clicks Configure in the Plugin Manager
    """
    dialog = gtk.MessageDialog(APP.window, gtk.DIALOG_DESTROY_WITH_PARENT,
        gtk.MESSAGE_INFO, gtk.BUTTONS_OK, "This plugin has no options... get a job")
    dialog.run()
    dialog.destroy()