-
Notifications
You must be signed in to change notification settings - Fork 0
/
icloud.py
146 lines (125 loc) · 5.09 KB
/
icloud.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
"""
Requires the following python module:
* pyicloud
Parameters:
* nic.exclude: Comma-separated list of interface prefixes to exclude (defaults to "lo,virbr,docker,vboxnet,veth,br")
* nic.include: Comma-separated list of interfaces to include
* nic.states: Comma-separated list of states to show (prefix with "^" to invert - i.e. ^down -> show all devices that are not in state down)
* nic.format: Format string (defaults to "{intf} {state} {ip} {ssid}")
"""
import arrow
from pyicloud import PyiCloudService
import configparser
import os
import time
import threading
import bumblebee.input
import bumblebee.output
import bumblebee.engine
ICLOUD_LOGINS_FOLDER = "iCloudLogins/"
REFRESH_TIMER = 60 # Seconds
DISPLAY_PROPERTY= 'modelDisplayName'
currentPath = os.path.realpath(__file__)
currentPath = currentPath[:currentPath.rfind('/')+1]
# Get config file
config = configparser.ConfigParser()
config.read(currentPath + "icloud.ini")
# Read credentials from config file
email = config['iCloud']['email']
password = config['iCloud']['password']
devicesInfo = []
refreshing = False
def Log(message):
current_time = arrow.utcnow().format('YYYY-MM-DD HH:mm:ss')
line=current_time+" | " +message+'\n'
f = open(currentPath+"log.txt", "a")
f.write(line)
f.close()
def UpdateBattery():
Log("Thread started")
global devicesInfo, refreshing
api = PyiCloudService(email, password, currentPath+ICLOUD_LOGINS_FOLDER)
while(True):
refreshing = True
Log("Refreshing")
Log(str(api))
devicesInfo = []
devices = api.devices
for device in devices:
info={}
status = device.status(['deviceClass','batteryStatus',DISPLAY_PROPERTY]) # https://github.com/picklepete/pyicloud/issues/220
info['name'] = str(status[DISPLAY_PROPERTY])
info['battery'] = int(round(status['batteryLevel']*100))
info['class']=str(device ['deviceClass'])
if(device['batteryStatus']=="Charging"):
info['charging']=True
else:
info['charging']=False
if(info['battery'] > 0): # Cause iMacs/offline devices have 0% battery in status
devicesInfo.append(info)
Log(str(devicesInfo))
refreshing=False
time.sleep(REFRESH_TIMER)
thread = threading.Thread(target=UpdateBattery)
thread.start()
class Module(bumblebee.engine.Module):
def __init__(self, engine, config):
widgets = []
super(Module, self).__init__(engine, config, widgets)
# bumblebee.output.Widget(full_text=self.icloud)
self._update_widgets(widgets)
self._exclude = tuple(filter(len, self.parameter("exclude", "imac").split(",")))
Log("Init module")
def _update_widgets(self, widgets):
if not refreshing: # Cause maybe i erase devices array while bar updates
# Removes devices to exclude
devicesInfoToAdd = [d for d in devicesInfo if not d['name'].startswith(self._exclude)]
for widget in widgets:
widget.set("visited", False)
for device in devicesInfoToAdd:
# Check if widget was already created
widget = self.widget(device['name'])
# If not, I create the widget for the new device with the name of the device, to help me find it later
if not widget:
widget = bumblebee.output.Widget(name=device['name'])
widgets.append(widget)
# I set properties to the widget
widget.full_text(device['name'] + ": " +
str(device['battery'])+'%')
widget.set("visited", True)
widget.set("charging", device['charging'])
widget.set("class", device['class'])
widget.set("battery", device['battery'])
# The widget I have not touched now are widget of devices that may be offline now then I remove them
for widget in widgets:
if widget.get("visited") is False:
widgets.remove(widget)
def update(self, widgets):
global thread
if(not thread.is_alive()):
Log("Thread was not running: starting...")
thread = threading.Thread(target=UpdateBattery)
thread.start()
self._update_widgets(widgets)
def state(self, widget):
states = []
if widget.get("charging"):
states.append("charging")
else:
state="discharging-"
if(widget.get("battery")<=10):
states.append("critical")
state=state+'10'
elif(widget.get("battery")<=25):
state=state+'25'
states.append("warning")
elif(widget.get("battery")<=50):
state=state+'50'
elif(widget.get("battery")<=80):
state=state+'80'
else:
state=state+'100'
states.append(state)
states.append(widget.get("class"))
return states
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4