All pastes #1374145 Raw Edit

[phear]crippler

public python v1 · immutable
#1374145 ·published 2009-03-27 17:32 UTC
rendered paste body
__module_name__ = "AmarokNP For XChat"__module_version__ = "1.5"__module_description__ = "Displays the current song playing in amarok. Has a nice gui for configuring how/what is output"import xchatimport commandsdef amarok_is_playing():  ''' Checks if amarok is playing and registered with dcop      If amarok isn't playing or something went wrong, this will return false '''  is_playing = commands.getoutput("dcop amarok player status")  if is_playing == 'call failed' or is_playing == '0':    return False  else:    return Truedef get_artist():  ''' Will get the artist's name from amarok. '''  if amarok_is_playing():    try:      artist = commands.getoutput("dcop amarok player artist")    except Exception, e:      xchat.prnt( "Problem getting the artist from dcop...%s" % e )      return False    return artistdef get_album():  ''' Gets the album name from amarok '''  if amarok_is_playing():    try:      album = commands.getoutput("dcop amarok player album")    except Exception, e:      xchat.prnt( "Problem getting the album from dcop...%s" % e )      return False    return albumdef get_title():  ''' Gets the title name from amarok '''  if amarok_is_playing():    try:      title = commands.getoutput("dcop amarok player title")    except Exception, e:      xchat.prnt( "Problem getting the title from dcop...%s" % e )      return False    return titledef get_my_nick():  ''' Gets your first prefered nickname and assume that is the nickname you're using. '''  try:    my_nick = xchat.get_prefs("irc_nick1")  except Exception, e:    xchat.prnt( "Problem with getting your nickname...%s" % e )    return False  return my_nickdef get_track_playcount():  ''' Gets the play count from amarok '''  if amarok_is_playing():    try:      playCount = commands.getoutput("dcop amarok player trackPlayCounter")    except Exception, e:      xchat.prnt( "Problem getting the playcount from dcop...%s" % e )      return False    return int(playCount)def get_current_playtime():  ''' Gets the current play time of the song.      Since the time returned is in seconds, we need to convert it. '''  if amarok_is_playing():    try:      playTime = commands.getoutput("dcop amarok player trackCurrentTime")      playTime = convertSecondstoMinutes(playTime)    except Exception, e:      xchat.prnt( "Problem getting the play time from dcop...%s" % e )      return False    return playTimedef convertSecondstoMinutes(s):  ''' I found a function on a mailing list (http://mail.python.org/pipermail/python-list/2003-January/181442.html)      It accepts a seconds arguement and returns minutes '''  minutes, s = divmod(s, 60)  return str(minutes) + ' minutes ' + str(s) + ' seconds'def now_playing(word, word_eol, userdata):  ''' The "main" function. Prints out the information. '''  if amarok_is_playing():    playing =playString()    if conf["echo"]=="0":        xchat.command ( "me %s" % playing )    else:        print playing  return xchat.EAT_ALLdef now_playingc():  ''' The "main" function. Prints out the information. '''  if conf["echo"]=="0":      for b in xchat.get_list("channels"):          a =b.channel          if conf.has_key(a) and conf["broadcast"]=="1":              if conf[a]=="1":                  playing = playString()                  xchat.command("msg "+a+" %s" % playing)                  return xchat.EAT_NONE  else:      print playString()  return xchat.EAT_ALLdef anp_param(word, word_eol, userdata):    ''' Set conf[word[1]] = word[2]. Used to update the conf dictionary'''    conf[word[1]] = word[2]    writeConfig()    return xchat.EAT_ALL    def writeConfig():    ''' Writes all the conf array to a config file. Really simple '''    confFile = open(xchat.get_info("xchatdir") + "/amarok_np.conf","w")    for param in conf:        confFile.write(param+":"+conf[param]+"!")    confFile.close()    return 0def loadConfig():    ''' Loads the dictionary from the config file '''    try:        confFile = open(xchat.get_info("xchatdir") + "/amarok_np.conf","r")    except IOError:        conf = {"global_win":"1","allow_msg":"1","auto_show":"0","artist":"1","title":"1",        "album":"0","play_time":"0","play_count":"1","play_string":"%a%A` - `%t%B` (`%b%B`)`%C`. Played %c times.`","broadcast":"0",        "echo":"0"}        return conf    r = confFile.read()    n = 0    y = 0    conf = {}    while n >= 0:        x = r.find(":",n)        e = r.find("!",n)        name = r[n:x]        value = r[x+1:e]        conf[name] = value        if e == -1:            break        n = e + 1    confFile.close()    del conf['']    return confdef part_remove(word,word_wol,userdata):    if conf.has_key(word[2]) and word[0].find(":" + xchat.get_info("nick") + "!") >=0:        cc = word[2]        print "Running MENU DEL \"Amarok/"+cc+"\""        xchat.command("MENU DEL \"Amarok/"+cc+"\"")    return xchat.EAT_NONE        def add_join(word,word_eol, userdata):    x = word[0]    if x[x.find(":")+1:x.find("!")] == xchat.get_info("nick"):        cc = word[2]           if conf.has_key(cc):            xchat.command("MENU -t"+conf[cc]+" ADD \"Amarok/"+cc+"\" \"anp_param "+cc+" 1\" \"anp_param "+cc+" 0\"")        else:            xchat.command("MENU -t0 ADD \"Amarok/"+cc+"\" \"anp_param "+cc+" 1\" \"anp_param "+cc+" 0\"")            conf[cc] = "0"    return xchat.EAT_NONEdef unload(userdata):    xchat.unhook(time)    xchat.command("MENU DEL Amarok")def anp_dump(word, word_eol, userdata):    keys = conf.keys()    keys.sort()    for a in keys:        print a + "."*(19-len(a)) +conf[a]    return xchat.EAT_ALLdef chkEcho(word=None,word_eol=None,userdata=None):    global nowPlaying    if nowPlaying!=get_title():        nowPlaying=get_title()        now_playingc()    return xchat.EAT_ALLdef anpStr(word,word_eol,userdata):    try:        if word_eol[1]!="":            conf["play_string"]=word_eol[1]            writeConfig()            return xchat.EAT_ALL    except IndexError:        print "USAGE: "+word[0]+" <string>\nSets <string> to be echoed with /nowplaying. Replacement is done as follows:\n%a - artist\n%b - album\n%c - play count\n%t - title\n----------------\n%A`` will only show text between the `'s if artist is defined, %B for album etc.."    return xchat.EAT_ALLdef playString():    playing = conf["play_string"]    a = get_artist()    b = get_album()    c = get_track_playcount()    t = get_title()    if a and conf["artist"]=="1":        playing=playing.replace("%a",a)        while playing.find("%A`")>=0:                    x=playing.find("%A`")            playing=playing.replace("%A`","",1)            playing=playing[:playing.find("`",x)] + playing[playing.find("`",x)+1:]    else:        playing=playing.replace("%a","")        while playing.find("%A`")>=0:                    x=playing.find("%A`")            playing=playing.replace("%A`","",1)            playing=playing[:x]+playing[playing.find("`",x)+1:]    if b and conf["album"]=="1":        playing=playing.replace("%b",b)        while playing.find("%B`")>=0:            x=playing.find("%B`")            playing=playing.replace("%B`","",1)            playing=playing[:playing.find("`",x)] + playing[playing.find("`",x)+1:]    else:        playing=playing.replace("%b","")        while playing.find("%B`")>=0:            x=playing.find("%B`")            playing=playing.replace("%B`","",1)            playing=playing[:x]+playing[playing.find("`",x)+1:]    if c and conf["play_count"]=="1":        playing=playing.replace("%c",str(c))        while playing.find("%C`")>=0:            x=playing.find("%C`")            playing=playing.replace("%C`","",1)            playing=playing[:playing.find("`",x)] + playing[playing.find("`",x)+1:]    else:        playing=playing.replace("%c","")        while playing.find("%C`")>=0:            x=playing.find("%C`")            playing=playing.replace("%C`","",1)            playing=playing[:x]+playing[playing.find("`",x)+1:]    if t and conf["title"]=="1":        playing=playing.replace("%t",t)        while playing.find("%T`")>=0:            x=playing.find("%T`")            playing=playing.replace("%T`","",1)            playing=playing[:playing.find("`",x)] + playing[playing.find("`",x)+1:]    else:        playing=playing.replace("%t","")        while playing.find("%T`")>=0:            x=playing.find("%T`")            playing=playing.replace("%T`","",1)            playing=playing[:x]+playing[playing.find("`",x)+1:]                    return playingdef anpReset(word,word_eol,userdata):    commands.getoutput("rm "+xchat.get_info("xchatdir")+"/amarok_np.conf")    #xchat.command("py reload amarok_np.py")    return xchat.EAT_ALLdef msgReply(word,word_eol,userdata):    if word[1] == "!np" and conf["allow_msg"]=="1":        xchat.command ( "me %s" % playString() )    return xchat.EAT_NONE        nowPlaying = get_title()conf = loadConfig()lastTitle = get_title()xchat.hook_unload(unload)time = xchat.hook_timer(1000,chkEcho)xchat.hook_command('nowplaying', now_playing)xchat.hook_command('anp_param', anp_param)xchat.hook_command('anp_dump',anp_dump)xchat.hook_command('anp_setstring',anpStr)xchat.hook_command('anp_reset',anpReset)xchat.hook_print('Channel Message',msgReply)xchat.hook_server('JOIN',add_join)xchat.hook_server('PART',part_remove)xchat.prnt(__module_name__ + ' version ' + __module_version__ + ' loaded. Use /nowplaying to display the song you\'re playing.')xchat.command("MENU -p-1 ADD Amarok")xchat.command("MENU ADD \"Amarok/Global\"")xchat.command("MENU -t" + conf['title'] + " ADD \"Amarok/Global/Display Title\" \"anp_param title 1\" \"anp_param title 0\"")xchat.command("MENU -t" + conf['artist'] + " ADD \"Amarok/Global/Display Artist\" \"anp_param artist 1\" \"anp_param artist 0\"")xchat.command("MENU -t" + conf['album'] + " ADD \"Amarok/Global/Display Album\" \"anp_param album 1\" \"anp_param album 0\"")xchat.command("MENU -t" + conf['play_count'] + " ADD \"Amarok/Global/Display Play Count\" \"anp_param play_count 1\" \"anp_param play_count 0\"")xchat.command("MENU -t" + conf['broadcast'] + " ADD \"Amarok/Global/Announce To Checked Channels\" \"anp_param broadcast 1\" \"anp_param broadcast 0\"")xchat.command("MENU -t" + conf['echo'] + " ADD \"Amarok/Global/Echo Instead Of Say\" \"anp_param echo 1\" \"anp_param echo 0\"")xchat.command("MENU -t" + conf['allow_msg'] + " ADD \"Amarok/Global/Reply To !np\" \"anp_param allow_msg 1\" \"anp_param allow_msg 0\"")chans = xchat.get_list("channels")for c in chans:    if c.type == 2 and conf.has_key(c.channel):        cc = c.channel        xchat.command("MENU -t"+conf[cc]+" ADD \"Amarok/"+cc+"\" \"anp_param "+cc+" 1\" \"anp_param "+cc+" 0\"")    else:        if c.type == 2:            cc = c.channel            xchat.command("MENU -t0 ADD \"Amarok/"+cc+"\" \"anp_param "+cc+" 1\" \"anp_param "+cc+" 0\"")xchat.command("anp_time")