# exaile_xchat by christian herberger (aka kabarakh)
#
# script to display the song currently played in amarok
#
#
# when you changed a setting, please reload the script in xchat (if xchat was running while changing)

__module_name__ = "exaile_xchat" 
__module_version__ = "0.3" 
__module_description__ = "python module for xchat to display facts about songs played" 

import xchat, dbus, string

# set switch to True to display the progress bar
switch = True

# set bar_mode to 1, 2 or 3 to decide which progress bar to use:
#    1: 10% = one unit, length 10, 0-10% = 0, 11-20% = 1, 21-30% = 2, ...
#    2: 10% = one unit, length 10, 0-4% = 0, 5-14% = 1, 15-24% = 2, ...
#    3: 5% = one unit, length 20, 0-5% = 0, 6-10% = 1, 11-15% = 2, ...
bar_mode = 3

exa = ""
bar = ""

def print_version(word, word_eol, userdata):
    xchat.command('me uses Kabarakh\'s exaile_xchat version 0.3, get it at http://kabarakh.pytalhost.de/blog/python-scripte/')
    return xchat.EAT_ALL

def get_currentTime():
        global exa
        length = exa.get_length()
        percent = exa.current_position()
        length = length.split(":")
        length = int(length[0])*60+int(length[1])
        currentTime = length * (percent / 100.0)
        sekunden = int(currentTime % 60)
        minuten = int((currentTime - sekunden) // 60)
        minuten = repr(minuten)
        if sekunden < 10:
            sekunden = "0" + repr(sekunden)
        else:
            sekunden = repr(sekunden)
        currentTime=minuten+":"+sekunden
        return currentTime

def bar1(percent):
    global bar
    i = 10
    while ( i <= 100 ):
        if percent >= i:
            bar = bar + "\002:\002"
        else:
            bar = bar + "-"
        i = i + 10
    bar = " \002progress\002[" + bar + " " + repr(percent) + "%]"

def bar2(percent):
    global bar
    i = 5
    while ( i <= 95 ): 
        if percent >= i:
            bar = bar + "\002:\002"
        else:
            bar = bar + "-"
        i = i + 10
    bar = " \002progress\002[" + bar + " " + repr(percent) + "%]"

def bar3(percent):
    global bar
    i = 5
    while ( i <= 100 ): 
        if percent >= i:
            bar = bar + "\002:\002"
        else:
            bar = bar + "-"
        i = i + 5
    bar = " \002progress\002[" + bar + " " + repr(percent) + "%]"

def exaile_send(word, word_eol, userdata):
    global exa, switch, bar_mode, bar
    try:
        bus = dbus.SessionBus()
        obj = bus.get_object("org.exaile.DBusInterface","/DBusInterfaceObject")
        exa = dbus.Interface(obj,"org.exaile.DBusInterface")
    except:
        xchat.command("me isn't running Exaile...")
        return xchat.EAT_ALL
    position = int(exa.current_position())
    if not position:
        xchat.command("me isn't playing any song...")
        return xchat.EAT_ALL
    else:
        if switch:
            if bar_mode == 1:
                bar1(position)
            elif bar_mode == 2:
                bar2(position)
            else:
                bar3(position)
        xchat.command('me is listening to \002title\002[' + str(exa.get_title().encode("utf-8")) + "] \002artist\002[" + str(exa.get_artist().encode("utf-8")) + "] \002album\002[" + str(exa.get_album().encode("utf-8")) + "] \002duration\002["+ str(get_currentTime().encode("utf-8")) + "/" + str(exa.get_length().encode("utf-8")) + "]" + bar)
    bar = ""
    return xchat.EAT_ALL

xchat.hook_command('exaile', exaile_send)
xchat.hook_command('exaversion', print_version)

xchat.prnt('exaile_xchat v0.3 loaded...')

