Last active
September 30, 2018 17:36
-
-
Save Jacajack/490152f82e066e6dddb757d4a1c6f4e1 to your computer and use it in GitHub Desktop.
Skips Spotify tracks played less than 2h ago
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import gi | |
gi.require_version( "Playerctl", "1.0" ) | |
from gi.repository import Playerctl, GLib | |
import time, sys | |
player = Playerctl.Player() | |
interval = 7200 | |
if len( sys.argv ) > 1: | |
try: | |
interval = float( sys.argv[1] ) | |
except ValueError: | |
print( "Usage: spotify-skipper [min-interval (sec)]" ); | |
sys.exit( 1 ) | |
# The list of tracks already played | |
# track: timestamp | |
history = {} | |
# Decides whether the track should be skipped | |
def should_skip( title ): | |
if title in history: | |
return time.time( ) - history[title] > 10 | |
else: | |
history[title] = time.time( ) | |
return False | |
# Removes tracks played more than 2h ago from history | |
def remove_old( ): | |
for title in history: | |
if time.time( ) - history[title] > interval: | |
history.remove( title ); | |
# Callback on track change | |
def on_track_change( player, e ): | |
remove_old( ); | |
title = player.get_artist( ) + " - " + player.get_title( ); | |
if should_skip( title ): | |
print( "Skipping '{}', because it was last played {:.2f} minutes ago".format( title, ( time.time( ) - history[title] ) / 60 ) ) | |
player.next( ) | |
else: | |
print( "Now playing '{}'".format( title ) ) | |
player.on( 'metadata', on_track_change ) | |
GLib.MainLoop( ).run( ) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment