-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnow_playing.py
executable file
·175 lines (140 loc) · 4.77 KB
/
now_playing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/python
#
# now_playing.py - sets pidgin's tune status to the current song
#
# This was written a long time ago (on 12/31/2008 if the file's mtime
# is to be believed. Rhythmbox's DBus API has changed (to be MPRIS
# based) and I don't use Quodlibet anymore so I can't speak to its
# workingness. This only sets the tune attribute in pidgin, so it has
# no effect on protocols that don't support one. It was tested to work
# with MSN and GTalk (but it won't work with other XMPP protocols).
#
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# 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., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA
#
#
# TODO: support MPRIS players (see lrc.pl)
#
import dbus
import sys
from dbus.mainloop.glib import DBusGMainLoop
import gobject
DBusGMainLoop(set_as_default=True)
bus = dbus.SessionBus()
purple = bus.get_object('im.pidgin.purple.PurpleService',
'/im/pidgin/purple/PurpleObject')
purple = dbus.Interface(purple, 'im.pidgin.purple.PurpleInterface')
album = None
artist = None
title = None
def playing_now(new_album, new_artist, new_title):
global album, artist, title
album = new_album
artist = new_artist
title = new_title
print("Now playing [%s] [%s] [%s]" % (album, artist, title))
def playing_resume(*args):
purple.PurpleUtilSetCurrentSong(title, artist, album)
def playing_pause(*args):
purple.PurpleUtilSetCurrentSong('', '', '')
player = None
######################################################################
# Rhythmbox support
######################################################################
def _rhythmbox_play(uri):
if uri is None or uri == '':
return
shell = bus.get_object(using['service'], '/org/gnome/Rhythmbox/Shell')
song = shell.getSongProperties(uri)
playing_now(song['album'], song['artist'], song['title'])
def _rhythmbox_playing_changed(playing):
if playing:
playing_resume()
else:
playing_pause()
def _rhythmbox_init():
uri = player.getPlayingUri()
if uri is not None:
_rhythmbox_play(uri)
if player.getPlaying():
playing_resume()
rhythmbox = {
'service' : 'org.gnome.Rhythmbox',
'object' : '/org/gnome/Rhythmbox/Player',
'signals' : {
'playingUriChanged' : _rhythmbox_play,
'playingChanged' : _rhythmbox_playing_changed,
},
'init' : _rhythmbox_init
}
######################################################################
# Quodlibet support
######################################################################
def _quodlibet_play(song):
playing_now(song['album'], song['artist'], song['title'])
if player.IsPlaying():
playing_resume()
def _quodlibet_init():
song = player.CurrentSong()
if song is not None:
_quodlibet_play(song)
quodlibet = {
'service' : 'net.sacredchao.QuodLibet',
'object' : '/net/sacredchao/QuodLibet',
'signals' : {
'SongStarted' : _quodlibet_play,
'SongEnded' : playing_pause,
'Paused' : playing_pause,
'Unpaused' : playing_resume,
},
'init' : _quodlibet_init
}
######################################################################
# Connect to players
######################################################################
players = ( rhythmbox, quodlibet )
bus = dbus.SessionBus()
bus_obj = bus.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus');
using = None
for p in players:
if bus_obj.NameHasOwner(p['service']):
using = p
break
if using is None:
sys.stderr.write("No running player detected\n")
sys.exit(1)
def connect_to_player():
global player
player = bus.get_object(using['service'], using['object'])
for (s, v) in using['signals'].iteritems():
player.connect_to_signal(s, v, utf8_strings=True)
print("Connected to %s" % (using['service']))
init = using['init']
if callable(init):
init()
# found a player, register to be notified for when it goes down
def _dbus_name_owner_changed(name, old, new):
if name == using['service']:
if new != '' and new != old:
connect_to_player()
else:
playing_pause()
bus_obj.connect_to_signal('NameOwnerChanged', _dbus_name_owner_changed)
connect_to_player()
try:
gobject.MainLoop().run()
except:
pass
playing_pause()