forked from jrlucier/eero_tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheero_tracker_instantiate.py
154 lines (124 loc) · 4.91 KB
/
eero_tracker_instantiate.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
import re
import json
import requests
from argparse import ArgumentParser
from abc import abstractproperty
__version__ = "0.0.1"
__all__ = ['ClientException', 'Eero', 'SessionStorage', '__version__']
class Eero(object):
def __init__(self, session):
# type(SessionStorage) -> ()
self.session = session
self.client = Client()
@property
def _cookie_dict(self):
if self.needs_login():
return dict()
else:
return dict(s=self.session.cookie)
def needs_login(self):
return self.session.cookie is None
def login(self, identifier):
# type(string) -> string
params = dict(login=identifier)
data = self.client.post('login', params=params)
return data['user_token']
def login_verify(self, verification_code, user_token):
params = dict(code=verification_code)
response = self.client.post('login/verify', params=params,
cookies=dict(s=user_token))
self.session.cookie = user_token
return response
def refreshed(self, func):
try:
return func()
except ClientException as exception:
if (exception.status == 401
and exception.error_message == 'error.session.refresh'):
self.login_refresh()
return func()
else:
raise
def login_refresh(self):
response = self.client.post('login/refresh', cookies=self._cookie_dict)
self.session.cookie = response['user_token']
def account(self):
return self.refreshed(lambda: self.client.get(
'account',
cookies=self._cookie_dict))
def id_from_url(self, id_or_url):
match = re.search('^[0-9]+$', id_or_url)
if match:
return match.group(0)
match = re.search(r'\/([0-9]+)$', id_or_url)
if match:
return match.group(1)
def devices(self, network_id):
return self.refreshed(lambda: self.client.get(
'networks/{}/devices'.format(
self.id_from_url(network_id)),
cookies=self._cookie_dict))
class SessionStorage(object):
@abstractproperty
def cookie(self):
pass
class ClientException(Exception):
def __init__(self, status, error_message):
super(ClientException, self).__init__()
self.status = status
self.error_message = error_message
class Client(object):
API_ENDPOINT = 'https://api-user.e2ro.com/2.2/{}'
def _parse_response(self, response):
data = json.loads(response.text)
if data['meta']['code'] is not 200 and data['meta']['code'] is not 201:
raise ClientException(data['meta']['code'],
data['meta'].get('error', ""))
return data.get('data', "")
def post(self, action, **kwargs):
response = requests.post(self.API_ENDPOINT.format(action), **kwargs)
return self._parse_response(response)
def get(self, action, **kwargs):
response = requests.get(self.API_ENDPOINT.format(action), **kwargs)
return self._parse_response(response)
class CookieStore(SessionStorage):
def __init__(self, cookie_file):
from os import path
self.cookie_file = path.abspath(cookie_file)
try:
with open(self.cookie_file, 'r') as f:
self.__cookie = f.read()
except IOError:
self.__cookie = None
@property
def cookie(self):
return self.__cookie
@cookie.setter
def cookie(self, cookie):
self.__cookie = cookie
with open(self.cookie_file, 'w+') as f:
f.write(self.__cookie)
session = CookieStore('eero.session')
eero = Eero(session)
if __name__ == '__main__':
if eero.needs_login():
parser = ArgumentParser()
parser.add_argument("-l", help="Your eero login (phone number)")
args = parser.parse_args()
if args.l:
phone_number = args.l
else:
phone_number = raw_input('Your eero login (phone number): ')
user_token = eero.login(phone_number)
verification_code = raw_input('Verification key from SMS: ')
eero.login_verify(verification_code, user_token)
print('Login successful. eero.session created, you can now use the device_tracker.')
else:
print('eero.session already created dumping all devices')
account = eero.account()
for network in account['networks']['data']:
devices = eero.devices(network['url'])
json_obj = json.loads(json.dumps(devices, indent=4))
for device in json_obj:
if device['wireless'] and device['connected']:
print("{}, {}, {}".format(device['nickname'], device['hostname'], device['mac']))