All pastes #288231 Raw Edit

Anonymous

public text v1 · immutable
#288231 ·published 2006-12-22 03:20 UTC
rendered paste body
###############################################################################
# amarok.py 2.1                                                               #
# (c) 2004 Noel M. Forbes                                                     #
###############################################################################

###############################################################################
# ChangeLog                                                                   #
#                                                                             #
# 2.1   - Added support for environment variables and user directories in     #
#         AmarokControl._dcop_directory.                                      #
# 2.0   - Nearly complete rewrite to utilize dcop.py and xcc.py               #
# 1.1   - Minor bug fixes.                                                    #
# 1.0   - Initial release.                                                    #
###############################################################################

import xchat
import string
import re
import sys
import os.path

class AmarokControl:

    ###########################################################################
    # SECTION [USER CUSTOMIZABLE OPTIONS]                                     #
    ###########################################################################

    # The command that is typed to call this script.
    _command = "amarok"

    # How the result should be sent.
    #  0 - Send as an action (/me).
    #  1 - Send as a channel or private message (/msg).
    #  2 - Send as a notice (/notice).
    _message_type = 0

    # The format of the result. Any variable is prefixed with a %.
    #  t - The title of the current song.
    #  a - Who the song was written by.
    #  m - The album that the song was in.
    #  d - The year that the song was published.
    #  c - The current track time.
    #  l - The total track time.
    #  x - The current track time in seconds.
    #  X - The total track time in seconds.
    #  g - The genre of the of the song.
    #  b - The bitrate that the song is being played at.
    #
    #  B - Bold or unbold text.
    #  U - Underline text.
    #  C - Colorize or uncolorize text (mIRC color).
    _format = "is listening to %a - %t [%c/%l]"

    # The directory that contains your dcop.py and xcc.py files.
    _script_directory = "~/.xchat2/script-tools"

    ###########################################################################
    # END SECTION                                                             #
    ###########################################################################

    ###########################################################################
    # SECTION [X-CHAT INFO]                                                   #
    ###########################################################################

    __version__ = "2.1"
    __module_name__ = "Now playing for amaroK"
    __module_version__ = __version__
    __module_description__ = "Customizable now playing script for amaroK"

    ###########################################################################
    # END SECTION                                                             #
    ###########################################################################

    ###########################################################################
    # SECTION [MAIN CODE]                                                     #
    ###########################################################################

    def __init__(self):
        sys.path.extend([os.path.expandvars(os.path.expanduser(self._script_directory))])
        from dcop import DCOPControl, DCOPFormat
        from xcc import XChatControl

        self.xcc = XChatControl()
        self.dcc = DCOPControl(self.xcc)

        self.dcc.check()

        self.df = DCOPFormat(self.dcc)
        self.df.setValue('t', self.df.EXECUTE, 'amarok default title')
        self.df.setValue('a', self.df.EXECUTE, 'amarok default artist')
        self.df.setValue('m', self.df.EXECUTE, 'amarok default album')
        self.df.setValue('d', self.df.EXECUTE, 'amarok default year')
        self.df.setValue('c', self.df.EXECUTE, 'amarok default currentTime')
        self.df.setValue('l', self.df.EXECUTE, 'amarok default totalTime')
        self.df.setValue('x', self.df.EXECUTE, 'amarok default trackCurrentTime')
        self.df.setValue('X', self.df.EXECUTE, 'amarok default trackTotalTime')
        self.df.setValue('g', self.df.EXECUTE, 'amarok default genre')
        self.df.setValue('b', self.df.EXECUTE, 'amarok default bitrate')
        self.df.setValue('B', self.df.LITERAL, self.xcc.CHARS['bold'])
        self.df.setValue('U', self.df.LITERAL, self.xcc.CHARS['underline'])
        self.df.setValue('C', self.df.LITERAL, self.xcc.CHARS['color'])

        self._print('amaroK now playing %s loaded.' % (self.__version__))

    def __deinit__(self, userdata):
        self._print('amaroK now playing %s unloaded.' % (self.__version__))
        return xchat.EAT_ALL

    def bind(self):
        xchat.hook_command(string.upper(self._command), self.display,
                           help='Usage: %s, prints information about the song playing in amaroK' % (string.upper(self._command)))
        xchat.hook_unload(self.__deinit__)

    def display(self, argv, argvExpanded, userdata):
        if len(argv) != 1:
            self._notice('no special parameters are necessary (you passed "%s"); continuing anyway.' % (argvExpanded[1]))

        if not self.dcc.getProgramStarted('amarok'):
            self._warning('amaroK doesn\'t appear to be started, or it isn\'t registered with dcop.')
            return xchat.EAT_ALL

        if self.dcc.getResult('amarok', 'default', 'isPlaying')[1] == 'false':
            self._warning('amaroK isn\'t playing anything right now.')
            return xchat.EAT_ALL

        result = re.sub(r'%([a-zA-Z]){1}', self.df.parse, self._format)
        self.df.clear()

        if self._message_type == 0:
            xchat.command('me %s' % (result))
        elif self._message_type == 1:
            xchat.command('msg %s %s' % (self.xcc.getChannel(), result))
        elif self._message_type == 2:
            xchat.command('notice %s %s' % (self.xcc.getChannel(), result))

        return xchat.EAT_ALL

    def _notice(self, text):
        self.xcc._print('(amaroK) [NOTICE] %s' % (text))

    def _warning(self, text):
        self.xcc._print('(amaroK) [%sWARNING%s] %s' % (self.xcc.CHARS['bold'], self.xcc.CHARS['bold'], text))

    def _fatal(self, text):
        self.xcc._print('(amaroK) [%sFATAL%s] %s%s%s' % (self.xcc.CHARS['bold'], self.xcc.CHARS['bold'], self.xcc.CHARS['bold'], text, self.xcc.CHARS['bold']))

    def _print(self, text):
        self.xcc._print('(amaroK) %s' % (text))

    ###########################################################################
    # END SECTION [MAIN CODE]                                                 #
    ###########################################################################

###############################################################################
# SECTION [INIT CODE]                                                         #
###############################################################################

def AmarokSilentExceptionHandler(exceptType, exceptValue, exceptObject):
    pass

sys.excepthook = AmarokSilentExceptionHandler

ac = AmarokControl()
ac.bind()

__module_name__ = ac.__module_name__
__module_version__ = ac.__module_version__
__module_description__ = ac.__module_description__

###############################################################################
# END SECTION [INIT CODE]                                                     #
###############################################################################