From 0fc6ecb70c3d2f2a2545fa3b91ae7eba2c50813a Mon Sep 17 00:00:00 2001 From: Sander Roosendaal Date: Fri, 2 Apr 2021 10:52:30 +0200 Subject: [PATCH] gets a valid DF out of NK --- rowers/nkstuff.py | 24 +++++++++++++++--------- rowers/tests/mocks.py | 5 ++++- rowers/tests/test_imports.py | 2 +- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/rowers/nkstuff.py b/rowers/nkstuff.py index bdbbb84d..e2c5ad0f 100644 --- a/rowers/nkstuff.py +++ b/rowers/nkstuff.py @@ -182,16 +182,17 @@ def get_nk_workout_list(user,fake=False): # #def get_workout(user,nkid): -def getdict(x): +def getdict(x, seatIndex=1): + seatStrokes = pd.DataFrame(x) try: - return x[0] + seatStrokes = seatStrokes.set_index('seatIndex') + return dict(seatStrokes.loc[seatIndex]) except KeyError: pass return {} def get_workout(user,nkid): - print('aap') r = Rower.objects.get(user=user) if (r.nktoken == '') or (r.nktoken is None): s = "Token doesn't exist. Need to authorize" @@ -219,15 +220,20 @@ def get_workout(user,nkid): # error handling pass - data = response.json()[str(nkid)] + jsonData = response.json() + #print(jsonData) + strokeData = jsonData[str(nkid)] + + df = pd.DataFrame.from_dict(strokeData) + oarlockData = df['oarlockStrokes'] + oarlockData = oarlockData.apply(lambda x:getdict(x, seatIndex=1)) + df2 = pd.DataFrame.from_records(oarlockData.values) + df2 = df2.rename(columns={"timestamp":"ts"}) - df = pd.DataFrame.from_dict(data) - ol = df['oarlockStrokes'] - ol = ol.apply(lambda x:getdict(x)) - df2 = pd.DataFrame.from_records(ol.values) - df2 = df2.rename(colunms={"timestamp":"ts"}) df = pd.concat([df,df2],axis=1) + print(df.info()) + # get workout data return 0,0 diff --git a/rowers/tests/mocks.py b/rowers/tests/mocks.py index 102f19f3..37dba495 100644 --- a/rowers/tests/mocks.py +++ b/rowers/tests/mocks.py @@ -758,7 +758,7 @@ def mocked_requests(*args, **kwargs): nkworkoutlistregex = '.*?nkrowlink\.com\/api\/v1\/sessions' nkworkoutlisttester = re.compile(nkworkoutlistregex) - nkstrokesregex = '.*?nkrowlink\.com\/api\/v1\/strokes\?sessionIds=\d' + nkstrokesregex = '.*?nkrowlink\.com\/api\/v1\/sessions\/strokes' nkstrokestester = re.compile(nkstrokesregex) stravaathleteregex = '.*?strava\.com\/api\/v3\/athlete$' @@ -927,6 +927,7 @@ def mocked_requests(*args, **kwargs): if nktester.match(args[0]): + if 'token' in args[0]: json_data = { 'access_token': 'TA3n1vrNjuQJWw0TdCDHnjSmrjIPULhTlejMIWqq', @@ -935,8 +936,10 @@ def mocked_requests(*args, **kwargs): } return MockResponse(json_data,200) if nkstrokestester.match(args[0]): + return MockResponse(nkstrokedata,200) if nkworkoutlisttester.match(args[0]): + return MockResponse(nkworkoutlist,200) diff --git a/rowers/tests/test_imports.py b/rowers/tests/test_imports.py index 9da148b9..afe715e8 100644 --- a/rowers/tests/test_imports.py +++ b/rowers/tests/test_imports.py @@ -431,7 +431,7 @@ class NKObjects(DjangoTestCase): mocked_getsmallrowdata_db): result = rowers.nkstuff.rower_nk_token_refresh(self.u) - response = self.c.get('/rowers/workout/nkimport/12',follow=True) + response = self.c.get('/rowers/workout/nkimport/469',follow=True) self.assertRedirects(response, expected_url='/rowers/workout/'+encoded2+'/edit/',