annotate contrib/xchat-audacious.py @ 2772:3c2786bbcf36 trunk

[svn] Fixes for xchat-audacious.py: - Added checks for DBusException (bad D-Bus or no Audacious) - Fixed an off-by-one error that rendered /sendtrack's check for a username useless - Removed some duplication
author deitarion
date Tue, 15 May 2007 23:53:07 -0700
parents c5dec731eff6
children cd9311fed796
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2740
a67712c75069 [svn] - add an xchat script which wraps our DBus API using dbus-python.
nenolod
parents:
diff changeset
1 #
a67712c75069 [svn] - add an xchat script which wraps our DBus API using dbus-python.
nenolod
parents:
diff changeset
2 # X-Chat Audacious for Audacious 1.4 and later
a67712c75069 [svn] - add an xchat script which wraps our DBus API using dbus-python.
nenolod
parents:
diff changeset
3 # This uses the native Audacious D-Bus interface.
a67712c75069 [svn] - add an xchat script which wraps our DBus API using dbus-python.
nenolod
parents:
diff changeset
4 #
a67712c75069 [svn] - add an xchat script which wraps our DBus API using dbus-python.
nenolod
parents:
diff changeset
5 # To consider later:
a67712c75069 [svn] - add an xchat script which wraps our DBus API using dbus-python.
nenolod
parents:
diff changeset
6 # - support org.freedesktop.MediaPlayer (MPRIS)?
a67712c75069 [svn] - add an xchat script which wraps our DBus API using dbus-python.
nenolod
parents:
diff changeset
7 #
2752
1ca3b314301d [svn] - add /SENDTRACK which can DCC the current track to a user
nenolod
parents: 2751
diff changeset
8 # This script is in the public domain.
2772
3c2786bbcf36 [svn] Fixes for xchat-audacious.py:
deitarion
parents: 2764
diff changeset
9 # $Id: xchat-audacious.py 4570 2007-05-16 06:53:07Z deitarion $
2752
1ca3b314301d [svn] - add /SENDTRACK which can DCC the current track to a user
nenolod
parents: 2751
diff changeset
10 #
2740
a67712c75069 [svn] - add an xchat script which wraps our DBus API using dbus-python.
nenolod
parents:
diff changeset
11
a67712c75069 [svn] - add an xchat script which wraps our DBus API using dbus-python.
nenolod
parents:
diff changeset
12 __module_name__ = "xchat-audacious"
a67712c75069 [svn] - add an xchat script which wraps our DBus API using dbus-python.
nenolod
parents:
diff changeset
13 __module_version__ = "1.0"
a67712c75069 [svn] - add an xchat script which wraps our DBus API using dbus-python.
nenolod
parents:
diff changeset
14 __module_description__ = "Get NP information from Audacious"
a67712c75069 [svn] - add an xchat script which wraps our DBus API using dbus-python.
nenolod
parents:
diff changeset
15
2772
3c2786bbcf36 [svn] Fixes for xchat-audacious.py:
deitarion
parents: 2764
diff changeset
16 from dbus import Bus, DBusException, Interface
2740
a67712c75069 [svn] - add an xchat script which wraps our DBus API using dbus-python.
nenolod
parents:
diff changeset
17 import xchat
a67712c75069 [svn] - add an xchat script which wraps our DBus API using dbus-python.
nenolod
parents:
diff changeset
18
a67712c75069 [svn] - add an xchat script which wraps our DBus API using dbus-python.
nenolod
parents:
diff changeset
19 # connect to DBus
a67712c75069 [svn] - add an xchat script which wraps our DBus API using dbus-python.
nenolod
parents:
diff changeset
20 bus = Bus(Bus.TYPE_SESSION)
a67712c75069 [svn] - add an xchat script which wraps our DBus API using dbus-python.
nenolod
parents:
diff changeset
21
2772
3c2786bbcf36 [svn] Fixes for xchat-audacious.py:
deitarion
parents: 2764
diff changeset
22 def get_aud():
3c2786bbcf36 [svn] Fixes for xchat-audacious.py:
deitarion
parents: 2764
diff changeset
23 try:
3c2786bbcf36 [svn] Fixes for xchat-audacious.py:
deitarion
parents: 2764
diff changeset
24 return bus.get_object('org.atheme.audacious', '/org/atheme/audacious')
3c2786bbcf36 [svn] Fixes for xchat-audacious.py:
deitarion
parents: 2764
diff changeset
25 except DBusException:
3c2786bbcf36 [svn] Fixes for xchat-audacious.py:
deitarion
parents: 2764
diff changeset
26 print "\x02Either Audacious is not running or you have something wrong with your D-Bus setup."
3c2786bbcf36 [svn] Fixes for xchat-audacious.py:
deitarion
parents: 2764
diff changeset
27 return None
3c2786bbcf36 [svn] Fixes for xchat-audacious.py:
deitarion
parents: 2764
diff changeset
28
2740
a67712c75069 [svn] - add an xchat script which wraps our DBus API using dbus-python.
nenolod
parents:
diff changeset
29 def command_np(word, word_eol, userdata):
2772
3c2786bbcf36 [svn] Fixes for xchat-audacious.py:
deitarion
parents: 2764
diff changeset
30 aud = get_aud()
3c2786bbcf36 [svn] Fixes for xchat-audacious.py:
deitarion
parents: 2764
diff changeset
31 if not aud:
3c2786bbcf36 [svn] Fixes for xchat-audacious.py:
deitarion
parents: 2764
diff changeset
32 return None
2740
a67712c75069 [svn] - add an xchat script which wraps our DBus API using dbus-python.
nenolod
parents:
diff changeset
33
a67712c75069 [svn] - add an xchat script which wraps our DBus API using dbus-python.
nenolod
parents:
diff changeset
34 # this seems to be best, probably isn't!
a67712c75069 [svn] - add an xchat script which wraps our DBus API using dbus-python.
nenolod
parents:
diff changeset
35 length = "stream"
a67712c75069 [svn] - add an xchat script which wraps our DBus API using dbus-python.
nenolod
parents:
diff changeset
36 if aud.SongLength(aud.Position()) > 0:
a67712c75069 [svn] - add an xchat script which wraps our DBus API using dbus-python.
nenolod
parents:
diff changeset
37 length = "%d:%02d" % (aud.SongLength(aud.Position()) / 60,
a67712c75069 [svn] - add an xchat script which wraps our DBus API using dbus-python.
nenolod
parents:
diff changeset
38 aud.SongLength(aud.Position()) % 60)
a67712c75069 [svn] - add an xchat script which wraps our DBus API using dbus-python.
nenolod
parents:
diff changeset
39
a67712c75069 [svn] - add an xchat script which wraps our DBus API using dbus-python.
nenolod
parents:
diff changeset
40 xchat.command("SAY [%s | %d:%02d/%s]" % (
2759
0c9bb15a4321 [svn] - encode songtitle in utf8 before passing to XChat.
nenolod
parents: 2758
diff changeset
41 aud.SongTitle(aud.Position()).encode("utf8"),
2740
a67712c75069 [svn] - add an xchat script which wraps our DBus API using dbus-python.
nenolod
parents:
diff changeset
42 aud.Time() / 1000 / 60, aud.Time() / 1000 % 60,
a67712c75069 [svn] - add an xchat script which wraps our DBus API using dbus-python.
nenolod
parents:
diff changeset
43 length))
a67712c75069 [svn] - add an xchat script which wraps our DBus API using dbus-python.
nenolod
parents:
diff changeset
44
a67712c75069 [svn] - add an xchat script which wraps our DBus API using dbus-python.
nenolod
parents:
diff changeset
45 return xchat.EAT_ALL
a67712c75069 [svn] - add an xchat script which wraps our DBus API using dbus-python.
nenolod
parents:
diff changeset
46
2772
3c2786bbcf36 [svn] Fixes for xchat-audacious.py:
deitarion
parents: 2764
diff changeset
47 def makeVoidCommand(cmd):
3c2786bbcf36 [svn] Fixes for xchat-audacious.py:
deitarion
parents: 2764
diff changeset
48 def callback(word, word_eol, userdata):
3c2786bbcf36 [svn] Fixes for xchat-audacious.py:
deitarion
parents: 2764
diff changeset
49 aud = get_aud()
3c2786bbcf36 [svn] Fixes for xchat-audacious.py:
deitarion
parents: 2764
diff changeset
50 if not aud:
3c2786bbcf36 [svn] Fixes for xchat-audacious.py:
deitarion
parents: 2764
diff changeset
51 return None
3c2786bbcf36 [svn] Fixes for xchat-audacious.py:
deitarion
parents: 2764
diff changeset
52 getattr(aud, cmd)()
3c2786bbcf36 [svn] Fixes for xchat-audacious.py:
deitarion
parents: 2764
diff changeset
53 return xchat.EAT_ALL
3c2786bbcf36 [svn] Fixes for xchat-audacious.py:
deitarion
parents: 2764
diff changeset
54 return callback
2749
96e7c0385973 [svn] - add stop, pause, next, prev, play functions
nenolod
parents: 2740
diff changeset
55
2772
3c2786bbcf36 [svn] Fixes for xchat-audacious.py:
deitarion
parents: 2764
diff changeset
56 command_next = makeVoidCommand('Advance')
3c2786bbcf36 [svn] Fixes for xchat-audacious.py:
deitarion
parents: 2764
diff changeset
57 command_prev = makeVoidCommand('Reverse')
3c2786bbcf36 [svn] Fixes for xchat-audacious.py:
deitarion
parents: 2764
diff changeset
58 command_pause = makeVoidCommand('Pause')
3c2786bbcf36 [svn] Fixes for xchat-audacious.py:
deitarion
parents: 2764
diff changeset
59 command_stop = makeVoidCommand('Stop')
3c2786bbcf36 [svn] Fixes for xchat-audacious.py:
deitarion
parents: 2764
diff changeset
60 command_play = makeVoidCommand('Play')
2749
96e7c0385973 [svn] - add stop, pause, next, prev, play functions
nenolod
parents: 2740
diff changeset
61
2752
1ca3b314301d [svn] - add /SENDTRACK which can DCC the current track to a user
nenolod
parents: 2751
diff changeset
62 def command_send(word, word_eol, userdata):
2772
3c2786bbcf36 [svn] Fixes for xchat-audacious.py:
deitarion
parents: 2764
diff changeset
63 if len(word) < 2:
2752
1ca3b314301d [svn] - add /SENDTRACK which can DCC the current track to a user
nenolod
parents: 2751
diff changeset
64 print "You must provide a user to send the track to."
1ca3b314301d [svn] - add /SENDTRACK which can DCC the current track to a user
nenolod
parents: 2751
diff changeset
65 return xchat.EAT_ALL
1ca3b314301d [svn] - add /SENDTRACK which can DCC the current track to a user
nenolod
parents: 2751
diff changeset
66
2772
3c2786bbcf36 [svn] Fixes for xchat-audacious.py:
deitarion
parents: 2764
diff changeset
67 aud = get_aud()
3c2786bbcf36 [svn] Fixes for xchat-audacious.py:
deitarion
parents: 2764
diff changeset
68 if not aud:
3c2786bbcf36 [svn] Fixes for xchat-audacious.py:
deitarion
parents: 2764
diff changeset
69 return None
3c2786bbcf36 [svn] Fixes for xchat-audacious.py:
deitarion
parents: 2764
diff changeset
70
2764
c5dec731eff6 [svn] - handle UTF-8 filenames
nenolod
parents: 2759
diff changeset
71 xchat.command('DCC SEND %s "%s"' % (word[1], aud.SongFilename(aud.Position()).encode("utf8")))
2752
1ca3b314301d [svn] - add /SENDTRACK which can DCC the current track to a user
nenolod
parents: 2751
diff changeset
72 return xchat.EAT_ALL
1ca3b314301d [svn] - add /SENDTRACK which can DCC the current track to a user
nenolod
parents: 2751
diff changeset
73
2740
a67712c75069 [svn] - add an xchat script which wraps our DBus API using dbus-python.
nenolod
parents:
diff changeset
74 xchat.hook_command("NP", command_np, help="Displays current playing song.")
2749
96e7c0385973 [svn] - add stop, pause, next, prev, play functions
nenolod
parents: 2740
diff changeset
75 xchat.hook_command("NEXT", command_next, help="Advances in Audacious' playlist.")
96e7c0385973 [svn] - add stop, pause, next, prev, play functions
nenolod
parents: 2740
diff changeset
76 xchat.hook_command("PREV", command_prev, help="Goes backwards in Audacious' playlist.")
2750
4df1d5552a4d [svn] - map to right handlers
nenolod
parents: 2749
diff changeset
77 xchat.hook_command("PAUSE", command_pause, help="Toggles paused status.")
4df1d5552a4d [svn] - map to right handlers
nenolod
parents: 2749
diff changeset
78 xchat.hook_command("STOP", command_stop, help="Stops playback.")
4df1d5552a4d [svn] - map to right handlers
nenolod
parents: 2749
diff changeset
79 xchat.hook_command("PLAY", command_play, help="Begins playback.")
2752
1ca3b314301d [svn] - add /SENDTRACK which can DCC the current track to a user
nenolod
parents: 2751
diff changeset
80 xchat.hook_command("SENDTRACK", command_send, help="Sends the currently playing track to a user.")
2749
96e7c0385973 [svn] - add stop, pause, next, prev, play functions
nenolod
parents: 2740
diff changeset
81
2772
3c2786bbcf36 [svn] Fixes for xchat-audacious.py:
deitarion
parents: 2764
diff changeset
82 print "xchat-audacious $Id: xchat-audacious.py 4570 2007-05-16 06:53:07Z deitarion $ loaded"