import requests
import json
import pandas as pd
import datetime

webservice = "https://api.crewmeister.com/api/v2/"

user_name = "bedner@global-cube.de"
user_pass = "7pmicg1w"
crew_id = "26515"

r = requests.post(webservice + "user/authentication", { 'userIdentifier': user_name, 'password': user_pass })
#print(r.json())
payload = r.json()['payload']
cookies = { 'cmAuthenticationUserToken': payload['token'], 'cmAuthenticationUserId': str(payload['id']) }

r = requests.post(webservice + "crew/authentication", { 'crewId': crew_id }, cookies=cookies)
#print(r.json())
payload = r.json()['payload']
cookies['cmAuthenticationCrewToken'] = payload['token']
cookies['cmAuthenticationCrewId'] = payload['id']

#r = requests.get(webservice + "context", cookies=cookies)
#print(r.json())

r = requests.get(webservice + f"crew/{crew_id}/member", cookies=cookies)
users = pd.DataFrame(r.json()['payload'])
print(users.query("activeTimeAccount==1"))

timestamp = datetime.datetime.now().isoformat()
print(timestamp)
r = requests.get(webservice + f"crew/{crew_id}/time-tracking/stamps", { 'startTime': timestamp, 'endTime': timestamp },  cookies=cookies)
stamps =pd.DataFrame(r.json()['payload'])

online = pd.merge(stamps, users, on='userId').query("timeAccount==1").groupby('email')
print(online['timestamp'].max())
