-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
47 lines (40 loc) · 2.04 KB
/
main.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
import psutil,requests,time
from discord import Webhook, RequestsWebhookAdapter
# function returning time in hh:mm:ss
def convertTime(seconds):
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
return "%d:%02d:%02d" % (hours, minutes, seconds)
# returns a tuple
def calculate():
battery = psutil.sensors_battery()
try:
if battery.percent < 10 and battery.power_plugged == True:
webhook = Webhook.from_url("YOUR WEBHOOK HERE", adapter=RequestsWebhookAdapter())
timetosend = convertTime(battery.secsleft)
powerstatustosend = battery.power_plugged
percenttosend = battery.percent
percentstring = ("Battery percentage : " + str(percenttosend))
statusstring = "Power plugged in : " + str(powerstatustosend)
timestring = "Battery left : " + str(timetosend)
print(percentstring)
print(statusstring)
print(timestring)
allinonestring = percentstring , " " , statusstring , " " , timestring, " " , "BATTERY LOW"
webhook.send(allinonestring)
print("Sent Notification!")
else:
timetosend = convertTime(battery.secsleft)
powerstatustosend = battery.power_plugged
percenttosend = battery.percent
percentstring = ("Battery percentage : " + str(percenttosend))
statusstring = "Power plugged in : " + str(powerstatustosend)
timestring = "Battery left : " + str(timetosend)
print(percentstring,statusstring,timestring)
print("Did Not Send!\n =========================================================================== \n")
except Exception as e:
print(e)
while True:
calculate()
print("CHECKING AGAIN IN 5 MINS")
time.sleep(300)