-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathspotify-tray.py
executable file
·143 lines (112 loc) · 4.5 KB
/
spotify-tray.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
142
143
#! /usr/bin/env python3
import sys
import ast
import signal
import subprocess
from PyQt4 import QtGui, QtCore
def playerctl(command):
return ["playerctl", "-p", "spotify", command]
def xdotool(command, id):
return ["xdotool", command, str(id)]
class SpotifyWrapper(QtGui.QSystemTrayIcon, subprocess.Popen):
def __init__(self, spotify_id, spotify_process, parent=None):
self.app = QtGui.QApplication(sys.argv)
self.spotify_id = spotify_id
self.spotify_process = spotify_process
# TODO: Find reliable way of keeping track of spotify visibility status
self.spotify_hidden = False
# TODO: Change to spotify icon
# icon = QtGui.QIcon(style.standardPixmap(QtGui.QStyle.SP_FileIcon))
icon = QtGui.QIcon("/usr/share/pixmaps/spotify-client.png")
QtGui.QSystemTrayIcon.__init__(self, icon, parent)
self.menu = QtGui.QMenu(parent)
QtCore.QObject.connect(
self.menu,
QtCore.SIGNAL('aboutToShow()'),
self.populate_menu
)
QtCore.QObject.connect(
self,
QtCore.SIGNAL('activated(QSystemTrayIcon::ActivationReason)'),
self.icon_click
)
self.setContextMenu(self.menu)
def icon_click(self, reason):
if reason == QtGui.QSystemTrayIcon.Trigger:
self.toggle_spotify_visibility()
def populate_menu(self):
self.menu.clear()
playing_now = self.track_info()
track_info = ', '.join(playing_now['artist']) + \
" - " + playing_now['title'] + \
"\nfrom: " + playing_now['album']
self.menu.addAction(
track_info,
lambda: self.toggle_spotify_visibility()
)
self.menu.addSeparator()
self.menu.addAction('Show Spotify', lambda: self.show_spotify())
self.menu.addAction('Hide Spotify', lambda: self.hide_spotify())
self.menu.addSeparator()
play_pause_text = 'Pause' if self.isPlaying() else 'Play'
self.menu.addAction(play_pause_text, lambda: self.play_pause())
self.menu.addAction('Next', lambda: self.next_track())
self.menu.addAction('Previous', lambda: self.prev_track())
self.menu.addSeparator()
self.menu.addAction("Exit", lambda: self.exit_pressed())
def toggle_spotify_visibility(self):
if self.spotify_hidden:
self.show_spotify()
else:
self.hide_spotify()
def hide_spotify(self):
subprocess.call(xdotool("windowunmap", self.spotify_id))
self.spotify_hidden = True
def show_spotify(self):
subprocess.call(xdotool("windowmap", self.spotify_id))
subprocess.call(xdotool("windowactivate", self.spotify_id))
self.spotify_hidden = False
def play_pause(self):
subprocess.call(playerctl("play-pause"))
def next_track(self):
subprocess.call(playerctl("next"))
def prev_track(self):
subprocess.call(playerctl("previous"))
def exit_pressed(self):
subprocess.call(xdotool("windowclose", self.spotify_id))
QtGui.QApplication.quit()
def isPlaying(self):
val = subprocess.check_output(playerctl("status")).decode("utf-8")
if val == "Playing\n":
return True
else:
return False
def track_info(self):
val = subprocess.check_output(playerctl("metadata")).decode("utf-8")
val = val.replace("<", "").replace(">", "").replace("uint64 ", "")
val = val.replace("xesam:", "").replace("mpris:", "")
# Evaluating literally because of single/double quotes problem
# eg. {'something' : "this 'string', for an example"}
data = ast.literal_eval(val)
data['playing'] = self.isPlaying()
# data = json.loads(val)
return data
def main():
signal.signal(signal.SIGINT, signal.SIG_DFL)
find_spotify_command = [
"xdotool", "search",
"--onlyvisible",
"--limit", "1",
"--class", "spotify"
]
find_failed = subprocess.call(find_spotify_command)
if find_failed == 1:
spotify_process = subprocess.Popen(["spotify"] + sys.argv[1:])
while find_failed == 1:
find_failed = subprocess.call(find_spotify_command)
spotify_id = int(subprocess.check_output(find_spotify_command))
spotify = SpotifyWrapper(spotify_id, spotify_process)
spotify.show()
sys.exit(spotify.app.exec_())
if __name__ == '__main__':
main()