| | 89 | ## MPD |
| | 90 | try: |
| | 91 | import mpd |
| | 92 | self.mpdclient = mpd.MPDClient() |
| | 93 | self.mpd_host = 'localhost' |
| | 94 | self.mpd_port = 6600 |
| | 95 | if os.environ.has_key('MPD_HOST'): |
| | 96 | self.mpd_host = os.environ['MPD_HOST'] |
| | 97 | if os.environ.has_key('MPD_PORT'): |
| | 98 | self.mpd_port = os.environ['MPD_PORT'] |
| | 99 | self.mpdclient.connect(self.mpd_host, self.mpd_port) |
| | 100 | if self.mpdclient.status()['state'] in ['pause', 'stop']: |
| | 101 | self.mpd_paused = 0 |
| | 102 | else: |
| | 103 | self.mpd_paused = 1 |
| | 104 | self.mpd_paused_before = self.mpd_paused |
| | 105 | self.mpd_exists = True |
| | 106 | self.mpd_is_here = True |
| | 107 | self.mpd_current_song = None |
| | 108 | gobject.timeout_add_seconds(10, self._mpd_check_status) |
| | 109 | gobject.timeout_add_seconds(1, self._mpd_check_track_status) |
| | 110 | except: |
| | 111 | self.mpd_exists = False |
| | 112 | self.mpd_is_here = False |
| | 113 | |
| | 212 | def _mpd_check_status(self): |
| | 213 | if self.mpd_exists is True: |
| | 214 | try: |
| | 215 | self.mpdclient.status() |
| | 216 | self.mpd_is_here = True |
| | 217 | except: |
| | 218 | try: |
| | 219 | self.mpdclient.disconnect() |
| | 220 | self.mpdclient.connect(self.mpd_host, self.mpd_port) |
| | 221 | self.mpd_is_here = True |
| | 222 | except: |
| | 223 | self.mpd_is_here = False |
| | 224 | return True |
| | 225 | |
| | 226 | def _mpd_check_track_status(self): |
| | 227 | if not self.mpd_is_here: |
| | 228 | return |
| | 229 | |
| | 230 | try: |
| | 231 | self.mpd_song = self.mpdclient.currentsong() |
| | 232 | info = MusicTrackInfo() |
| | 233 | if self.mpdclient.status()['state'] in ['pause', 'stop']: |
| | 234 | self.mpd_paused = 0 |
| | 235 | else: |
| | 236 | self.mpd_paused = 1 |
| | 237 | if self.mpd_song.has_key('title'): |
| | 238 | info.title = self.mpd_song['title'] |
| | 239 | else: |
| | 240 | info.title = '' |
| | 241 | if self.mpd_song.has_key('album'): |
| | 242 | info.album = self.mpd_song['album'] |
| | 243 | else: |
| | 244 | info.album = '' |
| | 245 | if self.mpd_song.has_key('artist'): |
| | 246 | info.artist = self.mpd_song['artist'] |
| | 247 | else: |
| | 248 | info.artist = '' |
| | 249 | if self.mpd_song.has_key('time'): |
| | 250 | info.duration = self.mpd_song['time'] |
| | 251 | else: |
| | 252 | info.duration = '' |
| | 253 | if self.mpd_song.has_key('track'): |
| | 254 | info.track_number = self.mpd_song['track'] |
| | 255 | else: |
| | 256 | info.track_number = '' |
| | 257 | |
| | 258 | info.paused = self.mpd_paused |
| | 259 | except: |
| | 260 | info = None |
| | 261 | self.mpd_is_here = False |
| | 262 | |
| | 263 | if self.mpd_current_song != self.mpd_song: |
| | 264 | self.emit('music-track-changed', info) |
| | 265 | self.mpd_current_song = self.mpd_song |
| | 266 | if (self.mpd_paused == 1 and self.mpd_paused_before == 0) or \ |
| | 267 | (self.mpd_paused == 0 and self.mpd_paused_before == 1): |
| | 268 | self.emit('music-track-changed', info) |
| | 269 | self.mpd_paused_before = self.mpd_paused |
| | 270 | return True |
| | 271 | |