music_track_listener.py 8.51 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# -*- coding: utf-8 -*-
##	musictracklistener.py
##
## Copyright (C) 2006 Gustavo Carneiro <gjcarneiro@gmail.com>
## Copyright (C) 2006 Nikos Kouremenos <kourem@gmail.com>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published
## by the Free Software Foundation; version 2 only.
##
## 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.
##
import gobject
17 18 19
if __name__ == '__main__':
	# install _() func before importing dbus_support
	from common import i18n
nkour's avatar
nkour committed
20

21
from common import dbus_support
22 23 24
if dbus_support.supported:
	import dbus
	import dbus.glib
25 26

class MusicTrackInfo(object):
27 28
	__slots__ = ['title', 'album', 'artist', 'duration', 'track_number',
		'paused']
29 30 31


class MusicTrackListener(gobject.GObject):
32 33 34
	__gsignals__ = {
		'music-track-changed': (gobject.SIGNAL_RUN_LAST, None, (object,)),
	}
35 36 37 38 39 40 41 42 43 44

	_instance = None
	@classmethod
	def get(cls):
		if cls._instance is None:
			cls._instance = cls()
		return cls._instance
	
	def __init__(self):
		super(MusicTrackListener, self).__init__()
45 46
		self._last_playing_music = None
		
47
		bus = dbus.SessionBus()
48 49

		## Muine
50 51
		bus.add_signal_receiver(self._muine_music_track_change_cb, 'SongChanged',
			'org.gnome.Muine.Player')
52 53 54 55 56 57
		bus.add_signal_receiver(self._player_name_owner_changed,
			'NameOwnerChanged', 'org.freedesktop.DBus', arg0='org.gnome.Muine')
		bus.add_signal_receiver(self._player_playing_changed_cb, 'StateChanged',
			'org.gnome.Muine.Player')

		## Rhythmbox
58 59
		bus.add_signal_receiver(self._rhythmbox_music_track_change_cb,
			'playingUriChanged', 'org.gnome.Rhythmbox.Player')
60 61 62 63
		bus.add_signal_receiver(self._player_name_owner_changed,
			'NameOwnerChanged', 'org.freedesktop.DBus', arg0='org.gnome.Rhythmbox')
		bus.add_signal_receiver(self._player_playing_changed_cb,
			'playingChanged', 'org.gnome.Rhythmbox.Player')
64 65
		bus.add_signal_receiver(self._player_playing_song_property_changed_cb,
			'playingSongPropertyChanged', 'org.gnome.Rhythmbox.Player')
66 67 68
			
		## Banshee
		banshee_bus = dbus.SessionBus()
69 70
		dubus = banshee_bus.get_object('org.freedesktop.DBus',
			'/org/freedesktop/dbus')
71
		self.dubus_methods = dbus.Interface(dubus, 'org.freedesktop.DBus')
72 73 74 75
		self.current_banshee_title = ''
		self.banshee_paused_before = False
		self.banshee_is_here = False
		gobject.timeout_add(10000, self._check_if_banshee_bus)
76 77
		if self.dubus_methods.NameHasOwner('org.gnome.Banshee'):
			self._get_banshee_bus()
78
			self.banshee_is_here = True
79 80 81
		# Otherwise, it opens Banshee!
		self.banshee_props ={}
		gobject.timeout_add(1000, self._banshee_check_track_status)
82

83 84 85 86 87 88 89
	def _check_if_banshee_bus(self):
		if self.dubus_methods.NameHasOwner('org.gnome.Banshee'):
			self._get_banshee_bus()
			self.banshee_is_here = True
		else:
			self.banshee_is_here = False
		return True
90 91 92 93 94

	def _get_banshee_bus(self):
		bus = dbus.SessionBus()
		banshee = bus.get_object('org.gnome.Banshee', '/org/gnome/Banshee/Player')
		self.banshee_methods = dbus.Interface(banshee, 'org.gnome.Banshee.Core')
95

96 97 98 99
	def do_music_track_changed(self, info):
		if info is not None:
			self._last_playing_music = info

100 101 102 103 104 105 106 107 108
	def _player_name_owner_changed(self, name, old, new):
		if not new:
			self.emit('music-track-changed', None)

	def _player_playing_changed_cb(self, playing):
		if playing:
			self.emit('music-track-changed', self._last_playing_music)
		else:
			self.emit('music-track-changed', None)
109

110 111 112 113
	def _player_playing_song_property_changed_cb(self, a, b, c, d):
		if b == 'rb:stream-song-title':
			self.emit('music-track-changed', self._last_playing_music)

114
	def _muine_properties_extract(self, song_string):
115 116
		d = dict((x.strip() for x in  s1.split(':', 1)) for s1 in \
			song_string.split('\n'))
117 118 119 120 121 122
		info = MusicTrackInfo()
		info.title = d['title']
		info.album = d['album']
		info.artist = d['artist']
		info.duration = int(d['duration'])
		info.track_number = int(d['track_number'])
123 124 125 126
		return info

	def _muine_music_track_change_cb(self, arg):
		info = self._muine_properties_extract(arg)
127 128
		self.emit('music-track-changed', info)

129
	def _rhythmbox_properties_extract(self, props):
130 131 132 133 134 135
		info = MusicTrackInfo()
		info.title = props['title']
		info.album = props['album']
		info.artist = props['artist']
		info.duration = int(props['duration'])
		info.track_number = int(props['track-number'])
136 137 138
		return info
	
	def _rhythmbox_music_track_change_cb(self, uri):
139 140
		if not uri:
			return
141
		bus = dbus.SessionBus()
142 143
		rbshellobj = bus.get_object('org.gnome.Rhythmbox',
			'/org/gnome/Rhythmbox/Shell')
144 145 146
		rbshell = dbus.Interface(rbshellobj, 'org.gnome.Rhythmbox.Shell')
		props = rbshell.getSongProperties(uri)
		info = self._rhythmbox_properties_extract(props)
147 148
		self.emit('music-track-changed', info)

149 150 151 152 153
	def _banshee_check_track_status(self):
		if self.dubus_methods.NameHasOwner('org.gnome.Banshee') and \
		not hasattr(self, 'banshee_methods'):
			self._get_banshee_bus()

154 155 156 157 158 159 160
		if self.dubus_methods.NameHasOwner('org.gnome.Banshee') and self.banshee_is_here:
			try:
				self.banshee_props['title'] = self.banshee_methods.GetPlayingTitle()
				self.banshee_props['album'] = self.banshee_methods.GetPlayingAlbum()
				self.banshee_props['artist'] = self.banshee_methods.\
					GetPlayingArtist()
				self.banshee_props['duration'] = \
161
				self.banshee_methods.GetPlayingDuration()
162 163 164 165 166 167 168 169 170 171
				self.banshee_props['paused'] = self.banshee_methods.\
					GetPlayingStatus()
				info = self._banshee_properties_extract(self.banshee_props)
			except dbus.DBusException, err:
				info = None

				for key in self.banshee_props.keys():
					self.banshee_props[key] = ''
				self.banshee_is_here = False

172 173
			if self.current_banshee_title != self.banshee_props['title']:
				self.emit('music-track-changed', info)
174 175 176 177 178 179 180 181 182
				self.banshee_paused_before = False
			if self.banshee_props['paused'] == 0 and self.banshee_paused_before ==\
			False:
				self.emit('music-track-changed', info)
				self.banshee_paused_before = True
			else: 
				if self.banshee_paused_before and self.banshee_props['paused'] == 1:
					self.emit('music-track-changed', info)
					self.banshee_paused_before = False
183 184 185 186 187 188 189 190 191 192 193 194 195
			self.current_banshee_title = self.banshee_props['title']
		return 1

	def _banshee_music_track_change_cb(self, arg):
		info = self._banshee_properties_extract(arg)
		self.emit('music-track-changed', info)

	def _banshee_properties_extract(self, props):
		info = MusicTrackInfo()
		info.title = props['title']
		info.album = props['album']
		info.artist = props['artist']
		info.duration = int(props['duration'])
196
		info.paused = props['paused']
197 198
		return info

199 200 201 202 203 204 205
	def get_playing_track(self):
		'''Return a MusicTrackInfo for the currently playing
		song, or None if no song is playing'''

		bus = dbus.SessionBus()

		## Check Muine playing track
206 207 208 209 210 211 212 213
		test = False
		if hasattr(bus, 'name_has_owner'):
			if bus.name_has_owner('org.gnome.Muine'):
				test = True
		elif dbus.dbus_bindings.bus_name_has_owner(bus.get_connection(),
		'org.gnome.Muine'):
			test = True
		if test:
214 215 216 217 218
			obj = bus.get_object('org.gnome.Muine', '/org/gnome/Muine/Player')
			player = dbus.Interface(obj, 'org.gnome.Muine.Player')
			if player.GetPlaying():
				song_string = player.GetCurrentSong()
				song = self._muine_properties_extract(song_string)
219
				self._last_playing_music = song
220 221 222
				return song

		## Check Rhythmbox playing song
223 224 225 226 227 228 229 230
		test = False
		if hasattr(bus, 'name_has_owner'):
			if bus.name_has_owner('org.gnome.Rhythmbox'):
				test = True
		elif dbus.dbus_bindings.bus_name_has_owner(bus.get_connection(),
		'org.gnome.Rhythmbox'):
			test = True
		if test:
231 232
			rbshellobj = bus.get_object('org.gnome.Rhythmbox',
				'/org/gnome/Rhythmbox/Shell')
233
			player = dbus.Interface(
234 235
				bus.get_object('org.gnome.Rhythmbox',
				'/org/gnome/Rhythmbox/Player'), 'org.gnome.Rhythmbox.Player')
236 237
			rbshell = dbus.Interface(rbshellobj, 'org.gnome.Rhythmbox.Shell')
			uri = player.getPlayingUri()
238 239
			if not uri:
				return None
240 241
			props = rbshell.getSongProperties(uri)
			info = self._rhythmbox_properties_extract(props)
242
			self._last_playing_music = info
243 244 245 246
			return info

		return None

247 248 249
# here we test :)
if __name__ == '__main__':
	def music_track_change_cb(listener, music_track_info):
250 251 252 253
		if music_track_info is None:
			print "Stop!"
		else:
			print music_track_info.title
254 255 256 257 258 259 260
	listener = MusicTrackListener.get()
	listener.connect('music-track-changed', music_track_change_cb)
	track = listener.get_playing_track()
	if track is None:
		print 'Now not playing anything'
	else:
		print 'Now playing: "%s" by %s' % (track.title, track.artist)
261
	gobject.MainLoop().run()