-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspotify-skip.py
More file actions
98 lines (85 loc) · 2.94 KB
/
spotify-skip.py
File metadata and controls
98 lines (85 loc) · 2.94 KB
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
import subprocess
import time
last_track_name = ''
skipped_push = False
def get_current_track():
applescript = '''
tell application "Spotify"
if it is running then
if player state is playing then
set trackName to name of current track
set trackArtist to artist of current track
set trackAlbum to album of current track
set trackDuration to duration of current track
return trackName & "|" & trackArtist & "|" & trackAlbum & "|" & trackDuration
else
return "NOT_PLAYING"
end if
else
return "SPOTIFY_NOT_RUNNING"
end if
end tell
'''
process = subprocess.Popen(
['osascript', '-e', applescript],
stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
output, error = process.communicate()
if process.returncode == 0:
return output.decode().strip()
else:
return None
def skip(seconds):
applescript = f'''
tell application "Spotify"
set player position to (player position + {seconds})
end tell
'''
subprocess.Popen(['osascript', '-e', applescript],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
def skip_track(track_name):
print(f"Skipping track: {track_name}")
applescript = 'tell application "Spotify" to next track'
process = subprocess.Popen(
['osascript', '-e', applescript],
stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
process.communicate()
def poll_spotify(poll_interval=2):
global last_track_name, skipped_push
while True:
track_info = get_current_track()
if track_info == "SPOTIFY_NOT_RUNNING":
print("Spotify is not running.")
continue
elif track_info == "NOT_PLAYING":
continue
elif track_info is None:
print("Error retrieving track information.")
continue
parts = track_info.split('|')
if len(parts) != 4:
print("Unexpected track format", track_info)
continue
track_name, track_artist, track_album, track_duration = parts
duration_seconds = int(track_duration)
# Reset the push-skip flag if the track has changed.
if last_track_name != track_name:
last_track_name = track_name
skipped_push = False
if "(Messages" in track_name:
skip_track(track_name)
elif "Intro (" in track_name:
skip_track(track_name)
elif "(Push The" in track_name and not skipped_push:
print(f"Skipping 30 seconds for track: {track_name}")
skip(30)
skipped_push = True
elif duration_seconds < 90:
skip_track(track_name)
time.sleep(poll_interval)
if __name__ == '__main__':
try:
poll_spotify()
except KeyboardInterrupt:
print("\nExiting...")