-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
65 lines (51 loc) · 2.01 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from flow import FlowClient
from notifications import Notifications
from sportstracker import SportsTrackerLib
import logging
import os
import re
import traceback
import yaml
# mapping between polar and sportstracker
activitiesMapping = {
'RUNNING': 1,
'CYCLING': 2
}
class App:
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'])
if __name__ == "__main__":
app = App()
app.run()