Created
January 31, 2017 22:00
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am not very experienced in coding/Python...can you tell me:
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 #?])?
Do I need to be connected to the network where the device is located, or can I access this data remotely from another network?
How do I obtain a value for 'token'?
When I try to run the code using Python 3.6, I get this error:
Thank you so much!