27
27
# much time should we wait between retries?
28
28
RETRIES = 10
29
29
RETRY_INTERVAL = 5
30
+ DUPLICATE_PLAY_THRESHOLD = 10.0
30
31
31
32
32
33
mpd_config = config ["mpd" ]
@@ -143,7 +144,9 @@ def __init__(self, lib, log):
143
144
144
145
self .do_rating = mpd_config ["rating" ].get (bool )
145
146
self .rating_mix = mpd_config ["rating_mix" ].get (float )
146
- self .time_threshold = 10.0 # TODO: maybe add config option?
147
+ self .played_ratio_threshold = mpd_config ["played_ratio_threshold" ].get (
148
+ float
149
+ )
147
150
148
151
self .now_playing = None
149
152
self .mpd = MPDClientWrapper (log )
@@ -216,10 +219,8 @@ def handle_song_change(self, song):
216
219
217
220
Returns whether the change was manual (skipped previous song or not)
218
221
"""
219
- diff = abs (song ["remaining" ] - (time .time () - song ["started" ]))
220
-
221
- skipped = diff >= self .time_threshold
222
-
222
+ elapsed = song ["elapsed_at_start" ] + (time .time () - song ["started" ])
223
+ skipped = elapsed / song ["duration" ] < self .played_ratio_threshold
223
224
if skipped :
224
225
self .handle_skipped (song )
225
226
else :
@@ -256,13 +257,10 @@ def on_pause(self, status):
256
257
257
258
def on_play (self , status ):
258
259
path , songid = self .mpd .currentsong ()
259
-
260
260
if not path :
261
261
return
262
262
263
263
played , duration = map (int , status ["time" ].split (":" , 1 ))
264
- remaining = duration - played
265
-
266
264
if self .now_playing :
267
265
if self .now_playing ["path" ] != path :
268
266
self .handle_song_change (self .now_playing )
@@ -273,7 +271,7 @@ def on_play(self, status):
273
271
# after natural song start.
274
272
diff = abs (time .time () - self .now_playing ["started" ])
275
273
276
- if diff <= self . time_threshold :
274
+ if diff <= DUPLICATE_PLAY_THRESHOLD :
277
275
return
278
276
279
277
if self .now_playing ["path" ] == path and played == 0 :
@@ -288,7 +286,8 @@ def on_play(self, status):
288
286
289
287
self .now_playing = {
290
288
"started" : time .time (),
291
- "remaining" : remaining ,
289
+ "elapsed_at_start" : played ,
290
+ "duration" : duration ,
292
291
"path" : path ,
293
292
"id" : songid ,
294
293
"beets_item" : self .get_item (path ),
@@ -337,6 +336,7 @@ def __init__(self):
337
336
"host" : os .environ .get ("MPD_HOST" , "localhost" ),
338
337
"port" : int (os .environ .get ("MPD_PORT" , 6600 )),
339
338
"password" : "" ,
339
+ "played_ratio_threshold" : 0.85 ,
340
340
}
341
341
)
342
342
mpd_config ["password" ].redact = True
0 commit comments