Skip to content

Instantly share code, notes, and snippets.

@ejain
Created January 31, 2017 22:00
Show Gist options
  • Save ejain/0b2fed983394742e33eee0416c4e2fce to your computer and use it in GitHub Desktop.
Save ejain/0b2fed983394742e33eee0416c4e2fce to your computer and use it in GitHub Desktop.
Retrieves sleep presence records from Emfit QS, and merges the records into a single spreadsheet.
import io, re, requests, sys, time, zipfile
def get_device(token):
r = get("/api/v1/user/get", token)
device = r.json()["user"]["devices"]
print("Device: " + device, file = sys.stderr)
return device
def list_presences(token, device):
r = get("/v4/presence/{0}/latest".format(device), token)
presences = [ presence["id"] for presence in r.json()["navigation_data"] ]
print("Presences: {0}".format(len(presences)), file = sys.stderr)
return presences
def download_presence(token, id):
time.sleep(1)
r = get("/api/v1/presence/{0}/download".format(id), token, True)
z = zipfile.ZipFile(io.BytesIO(r.content))
f = next(name for name in z.namelist() if re.match("device-[^-]+-presence-[\d\-\.]+\.csv", name))
print("Presence: {0} -> {1}".format(id, f), file = sys.stderr)
data = [ line.decode().replace("\n", "") for line in z.open(f, "rU") ]
z.close()
return data
def get(path, token, stream = False):
r = requests.get("https://api-qs.emfit.com" + path, params = { "remember_token" : token }, stream = stream)
r.raise_for_status()
return r
def merge(records):
assert len(records) > 1
merged = records[0]
for record in records[1:]:
merged.append(record[1])
return merged
token = "xxx"
device = get_device(token)
presences = list_presences(token, device)
records = [ download_presence(token, presence) for presence in presences ]
print("\n".join(merge(records)))
@gfryhof
Copy link

gfryhof commented May 17, 2017

I am not very experienced in coding/Python...can you tell me:

  1. Where I am supposed to edit the code with user-specific information (e.g. my email address, password, device ID [is this a 4-digit #?])?

  2. Do I need to be connected to the network where the device is located, or can I access this data remotely from another network?

  3. How do I obtain a value for 'token'?

When I try to run the code using Python 3.6, I get this error:

$ python3.6 emfitqs.py
Traceback (most recent call last):
  File "emfitqs.py", line 38, in <module>
    device = get_device(token)
  File "emfitqs.py", line 5, in get_device
    device = r.json()["user"]["devices"]
KeyError: 'user'

Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment