Skip to content

Instantly share code, notes, and snippets.

@featherbear
Created October 27, 2020 18:13
Show Gist options
  • Select an option

  • Save featherbear/2fa6c7113166a00df2477dcb99e6fc30 to your computer and use it in GitHub Desktop.

Select an option

Save featherbear/2fa6c7113166a00df2477dcb99e6fc30 to your computer and use it in GitHub Desktop.

Revisions

  1. featherbear created this gist Oct 27, 2020.
    33 changes: 33 additions & 0 deletions life360.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    life360_username="---"
    life360_password="---"

    import requests

    urlPrefix = "https://api.life360.com/v3/"

    print("Authorising...")
    auth = requests.post(urlPrefix + "oauth2/token.json",headers=dict(Authorization="Basic cFJFcXVnYWJSZXRyZTRFc3RldGhlcnVmcmVQdW1hbUV4dWNyRUh1YzptM2ZydXBSZXRSZXN3ZXJFQ2hBUHJFOTZxYWtFZHI0Vg=="), data=dict(grant_type="password",username=life360_username,password=life360_password))
    assert auth.status_code ==200
    life360_me = auth.json()["user"]["id"]
    auth = auth.json()["access_token"]

    def request(url, data=None):
    req_url = urlPrefix + url
    req_header = dict(Authorization="Bearer %s" % auth)
    req = requests.post(req_url, headers=req_header, data=data) if data else requests.get(req_url, headers=req_header)
    return req.json() if req.status_code==200 else None

    print("Fetching circles...")
    circles = request("circles.json")["circles"]

    getUser = lambda circle=circles[0]["id"], member=life360_me: request("circles/%s/members/%s" % (circle,member))
    parseLocationData = lambda l: dict(lat=l["latitude"],long=l["longitude"],since=l["since"],last=l["timestamp"],name=l["name"],battery=l["battery"])

    print("Retrieving user's location...")
    location = parseLocationData(getUser()["location"])

    def reverseGeocode(lat,long):
    req = requests.get("http://maps.googleapis.com/maps/api/geocode/json?latlng=%s,%s&sensor=false" % (lat,long)).json()
    return req["results"][0]["formatted_address"] if req["status"] == "OK" else None

    print("Location: " + (location["name"] if location["name"] else reverseGeocode(location["lat"], location["long"])))