-
Notifications
You must be signed in to change notification settings - Fork 1
/
spotify_recorder.py
141 lines (109 loc) · 4.81 KB
/
spotify_recorder.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
#!/usr/bin/env python3
import logging
from queue import Queue
import dbus
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop
from gi.repository import GLib
import song_recorder
class RecordingHandler: # {{{
def __init__(self, session_bus): # {{{
self.logger = logging.getLogger('SpotifyRecorder')
self.current_song = {'trackId': 'None'}
self.thread_queue = Queue()
bus = session_bus.get_object(
'org.mpris.MediaPlayer2.spotify',
'/org/mpris/MediaPlayer2')
bus.connect_to_signal(
'PropertiesChanged',
self.properties_changed) # }}}
def properties_changed(self, # {{{
interface_name,
changed_properties,
invalid_properties):
self.logger.debug(
'Properties have changed: {}, {}, {}'
.format(interface_name,
changed_properties,
invalid_properties))
if 'Metadata' not in changed_properties:
self.logger.warning('Metadata not found in changed properties')
return
try:
if (self.current_song['trackId'] ==
changed_properties['Metadata']
['mpris:trackid'].encode('utf-8')):
return
self.current_song['trackId'] = (changed_properties['Metadata']
['mpris:trackid'].encode('utf-8'))
self.current_song['artist'] = (changed_properties['Metadata']
['xesam:artist'][0].encode('utf-8'))
self.current_song['album'] = (changed_properties['Metadata']
['xesam:album'].encode('utf-8'))
self.current_song['title'] = (changed_properties['Metadata']
['xesam:title'].encode('utf-8'))
self.current_song['albumArt'] = (changed_properties['Metadata']
['mpris:artUrl'].encode('utf-8'))
self.current_song['trackNumber'] = (changed_properties['Metadata']
['xesam:trackNumber'])
self.current_song['playback_status'] = changed_properties[
'PlaybackStatus']
except KeyError:
self.logger.warning('There was an error getting the Metadata')
self.current_song_changed() # }}}
def current_song_changed(self): # {{{
self.logger.debug('Current song has changed')
while not self.thread_queue.empty():
thread = self.thread_queue.get()
thread.shutdown_flag.set()
self.logger.info('Starting song recorder...')
song_recorder_thread = song_recorder.SongRecorder(self.current_song)
song_recorder_thread.start()
self.thread_queue.put(song_recorder_thread)
def shutdown(self): # {{{
while not self.thread_queue.empty():
thread = self.thread_queue.get()
thread.shutdown_flag.set() # }}}}}}
class SpotifyRecorder: # {{{
def __init__(self): # {{{
self.logger = logging.getLogger('SpotifyRecorder')
self.logger.debug('Setting up DBus...')
bus_loop = DBusGMainLoop(set_as_default=True)
self.session_bus = dbus.SessionBus(mainloop=bus_loop)
bus = self.session_bus.get_object(
'org.freedesktop.DBus',
'/org/freedesktop/DBus')
bus.connect_to_signal(
'NameOwnerChanged',
self.spotify_started,
arg0='org.mpris.MediaPlayer2.spotify')
self.logger.info('Waiting for spotify to be started...')
try:
self.loop = GLib.MainLoop()
GLib.threads_init()
self.loop.run()
finally:
self.logger.info(
'Spotify got shut down, stopping recorder...') # }}}
def spotify_started(self, name, before, after): # {{{
self.logger.info('Found spotify on dbus!')
if name != 'org.mpris.MediaPlayer2.spotify':
return
if after:
self.handler = RecordingHandler(self.session_bus)
else:
self.logger.info('Lost spotify on dbus, shutting down')
# FIXME
if self.handler:
self.handler.shutdown()
self.loop.quit()
self.logger.info('Finished shutting down') # }}}}}}
if __name__ == '__main__': # {{{
LOGGER = logging.getLogger('SpotifyRecorder')
LOGGER.setLevel(logging.DEBUG)
CH = logging.StreamHandler()
CH.setLevel(logging.DEBUG)
FORMATTER = logging.Formatter('%(asctime)s: [%(levelname)5s] %(message)s')
CH.setFormatter(FORMATTER)
LOGGER.addHandler(CH)
SpotifyRecorder() # }}}