4
4
import dbus
5
5
import asyncio
6
6
import os
7
- import sys
7
+ import signal
8
8
import time
9
9
import json
10
10
import threading
11
11
from colorama import Fore , Back , Style # Colors in terminal
12
12
13
- loop = asyncio .get_event_loop ()
14
-
15
13
DBusGMainLoop (set_as_default = True )
16
14
17
15
@@ -60,8 +58,6 @@ def create(self, artist):
60
58
return self .artists [artist ]
61
59
62
60
def write (self ):
63
- # Cleanup
64
- print (self .artists )
65
61
# fixed_artists = {i: self.artists[i] for i in self.artists if self.artists[i] != self.default}
66
62
with open (self .file , "w" ) as f :
67
63
json .dump (self .artists , f , indent = 4 )
@@ -89,9 +85,10 @@ def do_nothing(*args, **kwargs):
89
85
90
86
91
87
def skip ():
88
+ print (Fore .RED + 'Skipped!' + Style .RESET_ALL , end = ' ' )
92
89
get_info = dbus .SessionBus ()
93
90
spotify_bus = get_info .get_object ("org.mpris.MediaPlayer2.spotify" , "/org/mpris/MediaPlayer2" )
94
- # status = spotify_bus.Get('org.mpris.MediaPlayer2.Player', 'PlaybackStatus', dbus_interface='org.freedesktop.DBus.Properties')
91
+ status = spotify_bus .Get ('org.mpris.MediaPlayer2.Player' , 'PlaybackStatus' , dbus_interface = 'org.freedesktop.DBus.Properties' )
95
92
spotify_bus .Next (dbus_interface = 'org.mpris.MediaPlayer2.Player' , reply_handler = do_nothing , error_handler = do_nothing )
96
93
97
94
@@ -135,67 +132,82 @@ def song_print(song):
135
132
skip ()
136
133
137
134
138
- def command_handler ( command ):
135
+ def toggle ( ):
139
136
config = Config ()
137
+
138
+ if config .autoskip :
139
+ print (Fore .RED + 'Autoskip disabled' + Style .RESET_ALL , end = ' ' )
140
+ else :
141
+ print (Fore .GREEN + 'Autoskip enabled' + Style .RESET_ALL , end = ' ' )
142
+ config .autoskip = not config .autoskip
143
+ config .write ()
144
+
145
+
146
+ def bls ():
140
147
song = Song ()
141
148
song_config = SongConfig ()
142
149
143
- def toggle ():
144
- if config .autoskip :
145
- print (Fore .RED + 'Autoskip disabled' + Style .RESET_ALL , end = ' ' )
146
- else :
147
- print (Fore .GREEN + 'Autoskip enabled' + Style .RESET_ALL , end = ' ' )
148
- config .autoskip = not config .autoskip
149
- config .write ()
150
-
151
- def bls ():
152
- current_song = song_config .create (song .artist )
153
- if song .title in current_song ["blacklisted_songs" ]:
154
- current_song ["blacklisted_songs" ].remove (song .title )
155
- text = f'{ Fore .RED } Removed{ Style .RESET_ALL } "{ song .title } " from blacklisted songs'
156
- else :
157
- current_song ["blacklisted_songs" ].append (song .title )
158
- text = f'{ Fore .GREEN } Added{ Style .RESET_ALL } "{ song .title } " to blacklisted songs'
159
- skip ()
150
+ current_song = song_config .create (song .artist )
151
+ if song .title in current_song ["blacklisted_songs" ]:
152
+ current_song ["blacklisted_songs" ].remove (song .title )
153
+ text = f'{ Fore .RED } Removed{ Style .RESET_ALL } "{ song .title } " from blacklisted songs'
154
+ else :
155
+ current_song ["blacklisted_songs" ].append (song .title )
156
+ text = f'{ Fore .GREEN } Added{ Style .RESET_ALL } "{ song .title } " to blacklisted songs'
157
+ skip ()
160
158
161
- print (text , end = ' ' )
162
- song_config .write ()
159
+ print (text , end = ' ' )
160
+ song_config .write ()
163
161
164
- def bla ():
165
- current_song = song_config .create (song .artist )
166
- if current_song ["blacklisted" ]:
167
- text = f'{ Fore .RED } Removed{ Style .RESET_ALL } "{ song .artist } " from blacklisted artists'
168
- else :
169
- text = f'{ Fore .GREEN } Added{ Style .RESET_ALL } "{ song .title } " to blacklisted artists'
170
- skip ()
171
-
172
- current_song ["blacklisted" ] = not current_song ["blacklisted" ]
173
- print (text , end = ' ' )
174
- song_config .write ()
175
-
176
- def wls ():
177
- current_song = song_config .create (song .artist )
178
- if song .title in current_song ["whitelisted_songs" ]:
179
- current_song ["whitelisted_songs" ].remove (song .title )
180
- text = f'{ Fore .RED } Removed{ Style .RESET_ALL } "{ song .title } " from whitelisted songs'
181
- else :
182
- current_song ["whitelisted_songs" ].append (song .title )
183
- text = f'{ Fore .GREEN } Added{ Style .RESET_ALL } "{ song .title } " to whitelisted songs'
184
162
185
- print (text , end = ' ' )
186
- song_config .write ()
163
+ def bla ():
164
+ song = Song ()
165
+ song_config = SongConfig ()
166
+
167
+ current_song = song_config .create (song .artist )
168
+ if current_song ["blacklisted" ]:
169
+ text = f'{ Fore .RED } Removed{ Style .RESET_ALL } "{ song .artist } " from blacklisted artists'
170
+ else :
171
+ text = f'{ Fore .GREEN } Added{ Style .RESET_ALL } "{ song .title } " to blacklisted artists'
172
+ skip ()
187
173
188
- def wla ():
189
- current_song = song_config .create (song .artist )
190
- if current_song ["whitelisted" ]:
191
- text = f'{ Fore .RED } Removed{ Style .RESET_ALL } "{ song .artist } " from whitelisted artists'
192
- else :
193
- text = f'{ Fore .GREEN } Added{ Style .RESET_ALL } "{ song .title } " to whitelisted artists'
174
+ current_song ["blacklisted" ] = not current_song ["blacklisted" ]
175
+ print (text , end = ' ' )
176
+ song_config .write ()
194
177
195
- current_song ["whitelisted" ] = not current_song ["whitelisted" ]
196
- print (text , end = ' ' )
197
- song_config .write ()
198
178
179
+ def wls ():
180
+ song = Song ()
181
+ song_config = SongConfig ()
182
+
183
+ current_song = song_config .create (song .artist )
184
+ if song .title in current_song ["whitelisted_songs" ]:
185
+ current_song ["whitelisted_songs" ].remove (song .title )
186
+ text = f'{ Fore .RED } Removed{ Style .RESET_ALL } "{ song .title } " from whitelisted songs'
187
+ else :
188
+ current_song ["whitelisted_songs" ].append (song .title )
189
+ text = f'{ Fore .GREEN } Added{ Style .RESET_ALL } "{ song .title } " to whitelisted songs'
190
+
191
+ print (text , end = ' ' )
192
+ song_config .write ()
193
+
194
+
195
+ def wla ():
196
+ song = Song ()
197
+ song_config = SongConfig ()
198
+
199
+ current_song = song_config .create (song .artist )
200
+ if current_song ["whitelisted" ]:
201
+ text = f'{ Fore .RED } Removed{ Style .RESET_ALL } "{ song .artist } " from whitelisted artists'
202
+ else :
203
+ text = f'{ Fore .GREEN } Added{ Style .RESET_ALL } "{ song .title } " to whitelisted artists'
204
+
205
+ current_song ["whitelisted" ] = not current_song ["whitelisted" ]
206
+ print (text , end = ' ' )
207
+ song_config .write ()
208
+
209
+
210
+ def command_handler (command ):
199
211
commands = {
200
212
't' : toggle ,
201
213
'toggle' : toggle ,
@@ -212,13 +224,20 @@ def wla():
212
224
213
225
214
226
class InputThread (threading .Thread ):
227
+ def signal_handler (sig , frame ):
228
+ # print('\nExiting')
229
+ os ._exit (1 )
230
+
231
+ # Ctrl + C
232
+ signal .signal (signal .SIGINT , signal_handler )
233
+
215
234
def run (self ):
216
235
while True :
217
236
command = input ()
218
237
command_handler (command .split (' ' ))
219
238
220
239
221
- async def main ():
240
+ async def main (loop ):
222
241
# Bad but necessary to prevent rerunning on the same song.
223
242
global past_song
224
243
config = Config ()
@@ -260,12 +279,21 @@ def on_properties_changed(interface_name, changed_properties, invalidated_proper
260
279
score = variant .value ["xesam:autoRating" ].value
261
280
song = Song (title , artist , score )
262
281
# Comparing objects doesn't work so it compares the dict.
263
- if song .__dict__ != past_song .__dict__ :
282
+ # != (" ", " ", " ") to prevent skipping when spotify is used on another device and
283
+ # metadata cannot be extrected.
284
+ if song .__dict__ != past_song .__dict__ and (title , artist , score ) != (" " , " " , " " ):
264
285
past_song = song
265
286
song_print (song )
266
287
267
288
properties .on_properties_changed (on_properties_changed )
268
289
await loop .create_future ()
290
+ input_thread .join ()
291
+
292
+
293
+ def run ():
294
+ loop = asyncio .get_event_loop ()
295
+ loop .run_until_complete (main (loop ))
296
+
269
297
270
- loop . run_until_complete ( main ())
271
- input_thread . join ()
298
+ if __name__ == "__main__" :
299
+ run ()
0 commit comments