def run(self): workdir = os.path.dirname(os.path.realpath(__file__)) # enable logging logging.basicConfig(format='%(asctime)s %(levelname)s:%(message)s', level=logging.INFO, filename=workdir + '/output.log') try: # read config file config = yaml.load(open(workdir + '/config.yml')) client = FlowClient() client.login(config['POLAR_USER'], config['POLAR_PASS']) # get last 10 activities activities = sorted(filter( lambda a: a.type == "EXERCISE" and (a.distance and a.distance > 0), client.activities()), key=lambda a: a.timestamp) activities = activities[-10:] stlib = SportsTrackerLib() stlib.login(config['SPORTSTRACKER_USER'], config['SPORTSTRACKER_PASS']) synced = [] p = re.compile('\[polar:[0-9]+\]', re.IGNORECASE) for workout in stlib.get_workouts(): if ('description' in workout): result = p.findall(workout['description']) if len(result) > 0: synced.append(int(result[0].split(':')[1][:-1])) logging.debug('len(synced) = {}'.format(len(synced))) added = 0 for activity in activities: if (activity.listItemId not in synced): sport_name = activity.sport().upper() sportId = activitiesMapping[ sport_name] if sport_name in activitiesMapping else 1 stlib.add_workout( str(activity.listItemId) + '.gpx', activity.gpx(), sportId, '[polar:{}]'.format(activity.listItemId)) added = added + 1 Notifications.display( config['SUCCESS_TITLE'], config['SUCCESS_SYNCED_MSG'].format(added) if added > 0 else config['SUCCESS_NOT_SYNCED_MSG']) except Exception as e: logging.error("type error: " + str(e)) logging.error(traceback.format_exc()) Notifications.display(config['FAILURE_TITLE'], config['FAILURE_MSG'])
def get_workouts(username, password, weightlifting_duration=[], weightlifting_calories=[], running_duration=[], running_calories=[], running_distance=[], biking_duration=[], biking_calories=[], biking_distance=[], swimming_duration=[], swimming_calories=[], swimming_distance=[]): """ Get remote user and activities from Polar Flow service and group them """ client = FlowClient() client.login(username, password) activity = client.activities() # Get global user information (will need patched FlowClient: # https://github.com/florianpaulhoberg/flow-client/blob/patch-1/flow/client.py ) activity_count = len(activity) userweight = activity[0].weight() # Split activities and group them by kind of sport for single_activity in activity: # Weightlifting = d1ce94078aec226be28f6c602e6803e1-2015-10-20_13_45_19 if "d1ce94078aec226be28f6c602e6803e1-2015-10-20_13_45_19" in single_activity.iconUrl: weightlifting_duration.append(single_activity.duration) weightlifting_calories.append(single_activity.calories) # Treadmile = d039f159dd0b62dc0a1ca72d82af2f0b-2015-10-20_13_46_28 # Running = 808d0882e97375e68844ec6c5417ea33-2015-10-20_13_46_22 if "d039f159dd0b62dc0a1ca72d82af2f0b-2015-10-20_13_46_28" or "808d0882e97375e68844ec6c5417ea33-2015-10-20_13_46_22" in single_activity.iconUrl: running_duration.append(single_activity.duration) running_calories.append(single_activity.calories) running_distance.append(single_activity.distance) # Biking = 561a80f6d7eef7cc328aa07fe992af8e-2015-10-20_13_46_03 if "561a80f6d7eef7cc328aa07fe992af8e-2015-10-20_13_46_03" in single_activity.iconUrl: biking_duration.append(single_activity.duration) biking_calories.append(single_activity.calories) biking_distance.append(single_activity.distance) # Swimming = f4197b0c1a4d65962b9e45226c77d4d5-2015-10-20_13_45_26 if "f4197b0c1a4d65962b9e45226c77d4d5-2015-10-20_13_45_26" in single_activity.iconUrl: swimming_duration.append(single_activity.duration) swimming_calories.append(single_activity.calories) swimming_distance.append(single_activity.distance) return weightlifting_duration, weightlifting_calories, running_duration, running_calories, running_distance, biking_duration, biking_calories, biking_distance, swimming_duration, swimming_calories, swimming_distance, activity_count, userweight
class TestActivity(TestCase): def setUp(self): self.client = FlowClient() self.client.login(self.email, self.password) self.activity = self.client.activities()[0] def test_activity_dir(self): d = dir(self.activity) self.assertIn('session', d) self.assertIn('type', d) self.assertIn('distance', d) def test_activity_getattr(self): self.assertIsNotNone(self.activity.distance) def test_activity_tcx(self): tcx = self.activity.tcx() self.assertIsInstance(tcx, str) self.assertTrue(tcx.startswith("<?xml"))
def test_activities(self): client = FlowClient() client.login(self.email, self.password) activities = client.activities() self.assertNotEqual(activities, [])
import os import config from flow import FlowClient from files import clears import endoData clears() client = FlowClient() with open('client.secret') as cred: f = cred.readlines() userP, passwdP = f[2].strip().split(',') client.login(userP, passwdP) activities = client.activities()[::-1] def sync_data(): lastEndo = endoData.run() count = 0 for work in activities: if (work.datetime[:10] <= lastEndo): break count += 1 return count print("1. Synchronizacja automatyczna") print("2. Podaj ilosc treningow do zsynchronizowania")