dwmbar-p/dwmbar

337 lines
6.1 KiB
Plaintext
Raw Normal View History

2024-02-24 16:17:26 +08:00
#!/usr/bin/python3
import time
import asyncio
import signal
import psutil
import alsaaudio
import requests
2024-02-24 16:24:17 +08:00
from subprocess import run
2024-02-24 16:17:26 +08:00
from datetime import datetime
DD = ';'
DELIM = ' '
2024-05-30 14:02:35 +08:00
COLOR = True
2024-02-24 16:17:26 +08:00
def shutdown():
2024-05-30 14:02:35 +08:00
print("Shutting down...")
loop.stop()
2024-02-24 16:17:26 +08:00
async def dwmbar():
global DD
2024-02-24 16:17:26 +08:00
await asyncio.sleep(1)
while True:
bar = f"{status()}{DD}{extrabar()}"
2024-02-24 16:17:26 +08:00
2024-02-24 16:39:41 +08:00
# print(bar)
2024-02-24 16:17:26 +08:00
run(["xprop", "-root", "-set", "WM_NAME", bar], check=True)
await asyncio.sleep(0.1)
def status():
global DELIM, net, ram, cpu, gpu, wlan, disk, vol, lang, dat, tim
try:
bar = [
net,
ram,
cpu,
gpu,
disk,
vol,
lang,
dat,
tim
]
return DELIM.join(bar)
except:
return ''
def extrabar():
global DELIM, currates, weather, COLOR
try:
bar = [
weather,
2024-10-27 23:46:50 +08:00
# currates
]
return DELIM.join(bar)
except:
return ''
async def currates():
global DELIM, currates, COLOR
2024-02-24 16:17:26 +08:00
COLOR1='#AAAAAA'
COLOR2='#444444'
while True:
curs = []
curs.append(['', f"${float(requests.get('https://api.binance.com/api/v3/ticker/24hr?symbol=BTCUSDT').json()['lastPrice']):.2f}"])
curs.append(['', f"${float(requests.get('https://api.binance.com/api/v3/ticker/24hr?symbol=LTCUSDT').json()['lastPrice']):.2f}"])
curs.append(['Ð', f"${float(requests.get('https://api.binance.com/api/v3/ticker/24hr?symbol=DOGEUSDT').json()['lastPrice']):.2f}"])
2024-02-24 16:39:41 +08:00
if COLOR:
row = list(map(lambda x: f'^b{COLOR1}^^c{COLOR2}^ {x[0]} ^c{COLOR1}^^b{COLOR2}^ {x[1]} ^d^', curs))
else:
row = list(map(lambda x: f'[{x[0]} | {x[1]}]', curs))
2024-02-24 16:17:26 +08:00
currates = DELIM.join(row)
2024-02-24 16:17:26 +08:00
await asyncio.sleep(60 * 10)
async def weather():
global weather, COLOR
2024-02-24 16:17:26 +08:00
COLOR1='#AAAAAA'
COLOR2='#444444'
CITY='Tyumen'
while True:
i = requests.get(f"https://wttr.in/{CITY}?format=%c").text.strip() #.encode('utf-8')[0:3].decode('utf-8')
t = requests.get(f"https://wttr.in/{CITY}?format=%t").text.strip()
w = requests.get(f"https://wttr.in/{CITY}?format=j2").json()['current_condition'][0]['windspeedKmph'].strip()
2024-02-24 16:39:41 +08:00
if COLOR:
weather = f"^b{COLOR2}^^c{COLOR1}^ {w} km/h ^b{COLOR1}^^c{COLOR2}^ {i} ^c{COLOR1}^^b{COLOR2}^ {t} ^d^"
2024-02-24 16:39:41 +08:00
else:
weather = f"[{w} km/h | {i} | {t}]"
2024-05-31 23:35:45 +08:00
2024-02-24 16:17:26 +08:00
await asyncio.sleep(60 * 30)
async def net():
2024-02-24 16:39:41 +08:00
global net, COLOR
2024-02-24 16:17:26 +08:00
COLOR1='#FF9B00'
COLOR2='#A16200'
osent = psutil.net_io_counters().bytes_sent
orecv = psutil.net_io_counters().bytes_recv
while True:
sent = psutil.net_io_counters().bytes_sent
recv = psutil.net_io_counters().bytes_recv
inb = f'{(recv - orecv) / 1024:0.0f} KB/s'
outb = f'{(sent - osent) / 1024:0.0f} KB/s'
orecv = recv
osent = sent
2024-02-24 16:39:41 +08:00
if COLOR:
net = f'^c{COLOR1}^^b{COLOR2}^ {inb} ^b{COLOR1}^^c{COLOR2}^  ^c{COLOR1}^^b{COLOR2}^ {outb} ^d^'
else:
net = f'[{inb} |  | {outb}]'
2024-02-24 16:17:26 +08:00
await asyncio.sleep(1)
async def ram():
2024-02-24 16:39:41 +08:00
global ram, COLOR
2024-02-24 16:17:26 +08:00
COLOR1='#C700FF'
COLOR2='#590073'
while True:
ramt = psutil.virtual_memory().total / 1024 / 1024 / 1024
ramu = psutil.virtual_memory().used / 1024 / 1024 / 1024
2024-02-24 16:39:41 +08:00
if COLOR:
ram = f'^b{COLOR1}^^c{COLOR2}^  ^c{COLOR1}^^b{COLOR2}^ {ramu:0.1f}/{ramt:0.1f}G ^d^'
else:
ram = f'[ | {ramu:0.1f}/{ramt:0.1f}G]'
2024-02-24 16:17:26 +08:00
await asyncio.sleep(1)
async def cpu():
2024-02-24 16:39:41 +08:00
global cpu, COLOR
2024-02-24 16:17:26 +08:00
2024-10-27 23:46:50 +08:00
COLOR1='#FF00B2'
COLOR2='#990057'
2024-02-24 16:17:26 +08:00
while True:
load = psutil.cpu_percent()
2024-02-24 16:39:41 +08:00
if COLOR:
cpu = f'^b{COLOR1}^^c{COLOR2}^  ^c{COLOR1}^^b{COLOR2}^ {load:0.0f}% ^d^'
else:
cpu = f'[ | {load:0.0f}%]'
2024-02-24 16:17:26 +08:00
await asyncio.sleep(1)
async def gpu():
2024-02-24 16:39:41 +08:00
global gpu, COLOR
2024-02-24 16:17:26 +08:00
2024-10-27 23:46:50 +08:00
COLOR1='#FF00B2'
COLOR2='#990057'
2024-02-24 16:17:26 +08:00
while True:
2024-05-30 14:02:35 +08:00
load = open('/sys/class/drm/card1/device/gpu_busy_percent').read().strip()
2024-02-24 16:17:26 +08:00
2024-02-24 16:39:41 +08:00
if COLOR:
gpu = f'^b{COLOR1}^^c{COLOR2}^  ^c{COLOR1}^^b{COLOR2}^ {load}% ^d^'
else:
gpu = f'[ | {load}%]'
2024-02-24 16:17:26 +08:00
await asyncio.sleep(1)
async def wlan():
2024-02-24 16:39:41 +08:00
global cpu, COLOR
2024-02-24 16:17:26 +08:00
COLOR1='#009CFF'
COLOR2='#005083'
while True:
c = psutil.cpu_percent()
i = ''
2024-02-24 16:39:41 +08:00
if COLOR:
cpu = f'^b{COLOR1}^^c{COLOR2}^  ^c{COLOR1}^^b{COLOR2}^ {c:0.0f}% ^d^'
else:
cpu = f'[ | {c:0.0f}%]'
2024-02-24 16:17:26 +08:00
await asyncio.sleep(1)
async def disk():
2024-02-24 16:39:41 +08:00
global disk, COLOR
2024-02-24 16:17:26 +08:00
COLOR1='#00FF90'
COLOR2='#00A45D'
while True:
f = psutil.disk_usage('/').free / 1024 / 1024 / 1024
t = psutil.disk_usage('/').total / 1024 / 1024 / 1024
2024-02-24 16:39:41 +08:00
if COLOR:
disk = f'^b{COLOR1}^^c{COLOR2}^  ^c{COLOR1}^^b{COLOR2}^ {f:0.0f}/{t:0.0f}G ^d^'
else:
disk = f'[ | {f:0.0f}/{t:0.0f}G]'
2024-02-24 16:17:26 +08:00
await asyncio.sleep(3)
async def vol():
2024-02-24 16:39:41 +08:00
global vol, COLOR
2024-02-24 16:17:26 +08:00
COLOR1='#FF0000'
COLOR2='#700000'
while True:
v = alsaaudio.Mixer().getvolume()[0]
if v > 67:
i = ''
elif v > 34:
i = ''
elif v > 1:
i = ''
else:
i = ''
if alsaaudio.Mixer().getmute()[0]:
i = ''
2024-02-24 16:39:41 +08:00
if COLOR:
vol = f'^b{COLOR1}^^c{COLOR2}^ {i} ^c{COLOR1}^^b{COLOR2}^ {v}% ^d^'
else:
vol = f'[{i} | {v}%]'
2024-02-24 16:17:26 +08:00
await asyncio.sleep(1)
async def lang():
2024-02-24 16:39:41 +08:00
global lang, COLOR
2024-02-24 16:17:26 +08:00
COLOR1='#FF5800'
COLOR2='#973400'
while True:
l = run(["xkblayout-state", "print", "%s"], check=True, capture_output=True, text=True).stdout
2024-02-24 16:39:41 +08:00
if COLOR:
lang = f'^b{COLOR1}^^c{COLOR2}^  ^c{COLOR1}^^b{COLOR2}^ {l} ^d^'
else:
lang = f'[ | {l}]'
2024-02-24 16:17:26 +08:00
await asyncio.sleep(0.1)
async def tim():
2024-02-24 16:39:41 +08:00
global tim, COLOR
2024-02-24 16:17:26 +08:00
COLOR1='#00E9FF'
COLOR2='#008592'
while True:
t = datetime.now().strftime("%H:%M:%S")
2024-02-24 16:39:41 +08:00
if COLOR:
tim = f'^b{COLOR1}^^c{COLOR2}^  ^c{COLOR1}^^b{COLOR2}^ {t} ^d^'
else:
tim = f'[ | {t}]'
2024-02-24 16:17:26 +08:00
await asyncio.sleep(1)
async def dat():
2024-02-24 16:39:41 +08:00
global dat, COLOR
2024-02-24 16:17:26 +08:00
COLOR1='#17FF00'
COLOR2='#0EA100'
while True:
d = datetime.now().strftime("%d.%m.%y")
2024-02-24 16:39:41 +08:00
if COLOR:
dat = f'^b{COLOR1}^^c{COLOR2}^  ^c{COLOR1}^^b{COLOR2}^ {d} ^d^'
else:
dat = f'[ | {d}]'
2024-02-24 16:17:26 +08:00
await asyncio.sleep(1)
2024-05-30 14:02:35 +08:00
async def main():
await asyncio.gather(
2024-02-24 16:17:26 +08:00
dwmbar(),
net(),
ram(),
cpu(),
gpu(),
disk(),
vol(),
lang(),
dat(),
tim(),
2024-10-27 23:46:50 +08:00
# currates(),
weather()
2024-05-30 14:02:35 +08:00
)
if __name__ == '__main__':
signal.signal(signal.SIGINT, lambda n, f: shutdown())
loop = asyncio.get_event_loop()
main_task = asyncio.ensure_future(main())
2024-02-24 16:17:26 +08:00
try:
loop.run_until_complete(main_task)
2024-05-30 14:02:35 +08:00
except KeyboardInterrupt:
print("KeyboardInterrupt: Stopping the event loop...")
finally:
loop.close()