-
Notifications
You must be signed in to change notification settings - Fork 0
/
i3bar.py
executable file
·193 lines (173 loc) · 5.25 KB
/
i3bar.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env python
import time
import json
from datetime import datetime
import socket
import psutil
import dbus
import GPUtil
from functools import reduce
from operator import add,truediv
main_color="#2E3440"
# in seconds
update_rate = 10
def separator(c1, c2):
obj = {
"full_text": "",
"separator": "false",
"separator_block_width": 0,
"border": main_color,
"border_left": 0,
"border_right": 0,
"border_top": 0,
"border_bottom": 0,
"color": c1,
"background": c2
}
return json.dumps(obj)
def getSpotifyInfo():
try:
sessionBus = dbus.SessionBus()
spotify_object = sessionBus.get_object('org.mpris.MediaPlayer2.spotify', '/org/mpris/MediaPlayer2')
spotify_props = dbus.Interface(spotify_object,'org.freedesktop.DBus.Properties')
spotify_meta = spotify_props.Get('org.mpris.MediaPlayer2.Player', 'Metadata')
return "{} - {}".format(spotify_meta['xesam:artist'][0], spotify_meta['xesam:title'])
except Exception as e:
# spotify is closed
if isinstance(e, dbus.exceptions.DBusException):
return ''
else:
print(e)
return ''
def getNvidiaGPUTemp():
try:
nvidia = GPUtil.getGPUs()[0]
return f" {nvidia.temperature}°C "
except Exception as e:
print(e)
return ''
def getCPUAvgTemp():
try:
temps = psutil.sensors_temperatures()
cpu = temps['coretemp']
avgTemp = getAvgTemp(cpu)
return f" {avgTemp}°C "
except Exception as e:
print(e)
return ''
def getAvgTemp(temp_array):
return int(truediv(reduce(add,list(map(lambda v: v.current, temp_array))),len(temp_array)))
def getAMDTemp():
try:
temps = psutil.sensors_temperatures()
gpu = temps['amdgpu']
avgTemp = getAvgTemp(gpu)
return f" {avgTemp}°C "
except Exception as e:
print(e)
return ''
class Item:
def __init__(self, name, full_text, color, background, enabled):
self.name = name
self.full_text = full_text
self.color = color
self.background = background
self.enabled = enabled
def print(self, prev_color):
obj = {
"full_text": self.full_text,
"name": self.name,
"color": self.color,
"background": self.background,
"separator": "false",
"separator_block_width": 0,
"border": main_color,
"border_left": 0,
"border_right": 0,
"border_top": 0,
"border_bottom": 0,
}
return ",".join([separator(self.background, prev_color), json.dumps(obj)])
def items_array():
spotifyInfo = getSpotifyInfo()
nvidiaTemp = getNvidiaGPUTemp()
amdTemp = getAMDTemp()
cpuAvgTemp = getCPUAvgTemp()
return list(filter(lambda i: i.enabled, [
Item(
name="id_spotify_info",
full_text=" {} ".format(spotifyInfo),
color="#EEEEEE",
background="#666666",
enabled=len(spotifyInfo) > 0
),
Item(
name="id_ip_local",
full_text=" {} ".format(socket.gethostbyname(socket.gethostname())),
color="#333333",
background="#C49D58",
enabled=True
),
Item(
name="id_nvidia_temp",
full_text=nvidiaTemp,
color="#98A9B4",
background="#123D57",
enabled=len(nvidiaTemp) > 0
),
Item(
name="id_amd_gpu_temp",
full_text=amdTemp,
color="#F6F6F1",
background="#ECC259",
enabled=len(amdTemp) > 0
),
Item(
name="id_disk_usage",
full_text=" {}% ".format(psutil.disk_usage('/').percent),
color="#EEEEEE",
background="#3949AB",
enabled=True
),
Item(
name="id_memory",
full_text=" {}% ".format(psutil.virtual_memory()[2]),
color="#DDDDDD",
background="#B87238",
enabled=True
),
Item(
name="id_cpu_usage",
full_text=" {}% ".format(psutil.cpu_percent(interval=1)),
color="#FFFFFF",
background="#A7282E",
enabled=True
),
Item(
name="id_cpu_avg_temp",
full_text=cpuAvgTemp,
color="#96B7D4",
background="#F3F5F8",
enabled=len(cpuAvgTemp) > 0
),
Item(
name="id_date",
full_text=" {} ".format(datetime.now().strftime("%H:%M %a %d-%B")),
color="#333333",
background="#80B3B1",
enabled=True
)
]))
if __name__ == '__main__':
print('{ "version": 1 }')
print("[")
print("[]")
while True:
items = []
items_arr = items_array()
for i, item in enumerate(items_arr):
prev_color = main_color if i == 0 else items_arr[i-1].background
items.append(item.print(prev_color))
items.append(separator(main_color, items_arr[-1].background))
print(",[{}]".format(",".join(items)))
time.sleep(update_rate)