Skip to content

Instantly share code, notes, and snippets.

@Leinnan
Last active June 13, 2016 20:19
Show Gist options
  • Save Leinnan/796fc1a0c04f9f5cd3a929ba445e42f0 to your computer and use it in GitHub Desktop.
Save Leinnan/796fc1a0c04f9f5cd3a929ba445e42f0 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
import psutil
import datetime
import os
output = " "
##########################
# MPD
##########################
mpd_title = os.popen("mpc | head -1").read()
mpd_title = mpd_title.rstrip()
mpd_status = os.popen("mpc | head -2 | tail -1").read()
mpd_status = mpd_status.rstrip()
if "playing" in mpd_status:
output += mpd_title
output += "\n"
elif "paused" in mpd_status:
output += mpd_title + " [paused]"
output += "\n"
##########################
# Battery
##########################
def get_battery_state(prop):
"""
Return the first line from the file located at battery_path/prop as a
string.
"""
with open(prop, 'r') as f:
return f.readline().strip()
def get_battery_directory():
for folder in os.listdir("/sys/class/power_supply/"):
if "BAT" in folder:
return(os.path.join("/sys/class/power_supply",folder))
return 0
if get_battery_directory() != 0:
battery_directory = get_battery_directory()
energy_now = int(get_battery_state(os.path.join(battery_directory,'charge_now')))
energy_full = int(get_battery_state(os.path.join(battery_directory,'charge_full')))
status = get_battery_state(os.path.join(battery_directory,'status')).replace('\n','')
charge = (energy_now / energy_full) * 100
charge = int(charge)
if charge > 100:
charge = 100
output += "BAT " + str(charge) + "%\n"
##########################
# RAM
##########################
memory=psutil.virtual_memory().percent
output += "RAM " + str(memory) + "%\n"
cpu_usage=psutil.cpu_percent(interval=1)
##########################
# CPU
##########################
output += "CPU " + str(cpu_usage) + "%\n"
##########################
# HDD
##########################
free_space = os.popen("df -Ph / | tail -1 | awk '{print $4}'").read()
free_space = free_space.rstrip()
if free_space[-1] == "G":
free_space = free_space[:-1]
free_space += " GB"
output += "HDD " + str(free_space) + "\n"
##########################
# DATE
##########################
def getMonthInPolish(month_nr):
return {
1: "stycznia",
2: "lutego",
3: "marca",
4: "kwietnia",
5: "maja",
6: "czerwca",
7: "lipca",
8: "sierpnia",
9: "września",
10: "października",
11: "listopada",
12: "grudnia",
}[month_nr]
now = datetime.datetime.now()
current_date = now.strftime("%H:%M, %d ")
current_date += getMonthInPolish(now.month)
output += current_date + "\n"
# podmiana znaków nowej linii na ręczne tabulacje
# wszystkich poza ostatnim, ten usuwam
output = output.replace("\n"," • ",output.count("\n")-1)
output = output.replace("\n","")
output = "<txt>" + output + "</txt>"
print(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment