forked from harperreed/life360-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
life360.py
60 lines (45 loc) · 1.88 KB
/
life360.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
import requests
import json
class life360:
# base_url = "https://api.life360.com/v3/"
base_url = "https://api-cloudfront.life360.com/v3/"
token_url = "oauth2/token.json"
circles_url = "circles.json"
circle_url = "circles/"
user_agent = "com.life360.android.safetymapd"
def __init__(self, authorization_token=None, username=None, password=None):
self.authorization_token = authorization_token
self.username = username
self.password = password
def make_request(self, url, params=None, method='GET', authheader=None):
headers = {'Accept': 'application/json', "user-agent": self.user_agent}
if authheader:
headers.update({'Authorization': authheader, 'cache-control': "no-cache",})
if method == 'GET':
r = requests.get(url, headers=headers)
elif method == 'POST':
r = requests.post(url, data=params, headers=headers)
return r.json()
def authenticate(self):
url = self.base_url + self.token_url
params = {
"grant_type":"password",
"username":self.username,
"password":self.password,
}
r = self.make_request(url=url, params=params, method='POST', authheader="Basic " + self.authorization_token)
try:
self.access_token = r['access_token']
return True
except:
return False
def get_circles(self):
url = self.base_url + self.circles_url
authheader="bearer " + self.access_token
r = self.make_request(url=url, method='GET', authheader=authheader)
return r['circles']
def get_circle(self, circle_id):
url = self.base_url + self.circle_url + circle_id
authheader="bearer " + self.access_token
r = self.make_request(url=url, method='GET', authheader=authheader)
return r