-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_run.py
executable file
·129 lines (110 loc) · 4.84 KB
/
update_run.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python
from datetime import datetime
import os
import time
from stravalib.client import Client
from stravalib.model import Activity
from db import ChallengeSqlDB
from model2 import Run, User
CLIENT_ID = os.environ["STRAVA_CLIENT_ID"]
CLIENT_SECRET = os.environ["STRAVA_CLIENT_SECRET"]
SERVICE_TYPE = os.environ["SERVICE_TYPE"]
MYSQL_HOST = os.environ["MYSQL_HOST"]
MYSQL_USERNAME = os.environ["MYSQL_USERNAME"]
MYSQL_PASSWORD = os.environ["MYSQL_PASSWORD"]
MYSQL_DB_NAME = os.environ["MYSQL_DB_NAME"]
ACT_START_DATE = os.getenv("ACT_START_DATE", '2019-10-01T17:00:00Z')
ACT_END_DATE = os.getenv("ACT_END_DATE", '2020-01-12T17:00:00Z')
CHALLENGE_START_DATE = datetime.strptime(ACT_START_DATE, "%Y-%m-%dT%H:%M:%SZ")
CHALLENGE_END_DATE = datetime.strptime(ACT_END_DATE, "%Y-%m-%dT%H:%M:%SZ")
def adjust_run_promo(run):
naive_start_date = run.start_date.replace(tzinfo=None)
# Time in Thailand time zone
date_2019_11_23 = datetime.strptime('2019-11-22T17:00:00Z', "%Y-%m-%dT%H:%M:%SZ")
date_2019_11_24 = datetime.strptime('2019-11-24T17:00:00Z', "%Y-%m-%dT%H:%M:%SZ")
if naive_start_date >= date_2019_11_23 and naive_start_date < date_2019_11_24:
run.promo_comment = "2019-11-23 to 2019-11-24"
run.promo_multiplier = 2.0
print('Adjust promo:', run.strava_id, run.promo_comment, run.promo_multiplier)
def main():
ChallengeSqlDB.init(MYSQL_HOST, MYSQL_USERNAME,
MYSQL_PASSWORD, MYSQL_DB_NAME)
print("Challenge start date:", CHALLENGE_START_DATE)
print("Challenge end date:", CHALLENGE_END_DATE)
# Read all runners that have intania from DB
print("Get all runners from db")
if SERVICE_TYPE.lower() == 'ranger' :
print("Service type: %s" % ('RANGER'))
users = ChallengeSqlDB.get_all_ranger_users()
else:
print("Service type: %s" % ('INTANIA'))
users = ChallengeSqlDB.get_all_intania_users()
n_user = len(users)
print("Total runners: %d" % (n_user))
# For each runners get their activities
runs = []
for idx, user in enumerate(users):
# if user.clubs is None or not user.clubs:
# print("Skip runner with None intania: id=%s displayname='%s %s'" %
# (user.strava_id, user.first_name, user.last_name))
# continue
if not user.credentials:
print("Skip runner with empty credentials: id=%s displayname='%s %s'" %
(user.strava_id, user.first_name, user.last_name))
continue
refresh_token = None
for cred in user.credentials:
if cred.strava_client == CLIENT_ID:
refresh_token = cred.strava_refresh
if refresh_token is None:
print("Skip runner with empty credentials for client_id=%s : id=%s displayname='%s %s'" %
(CLIENT_ID, user.strava_id, user.first_name, user.last_name))
continue
print('Found refresh_token for the user ...')
try:
client = Client()
# Get new access token
refresh_response = client.refresh_access_token(
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
refresh_token=refresh_token)
# Set up user's access token and ready to fetch Strava data
client.access_token = refresh_response['access_token']
except Exception as e:
continue
if user.clubs:
intania = user.clubs[0].intania
else:
intania = 0
if user.registration and user.registration.foundation:
ranger_team = user.registration.foundation.name
else:
ranger_team = None
time.sleep(0.25)
activities = client.get_activities(
after=CHALLENGE_START_DATE, before=CHALLENGE_END_DATE)
print("Get activities: idx=%d/%d id=%s displayname='%s %s' intania='%s' ranger='%s'" %
(idx + 1, n_user, user.strava_id, user.first_name, user.last_name, intania, ranger_team))
n_run = 0
try:
for act in activities:
if act.type not in [Activity.RUN, Activity.WALK]:
continue
n_run += 1
run = Run.from_activity(act, user.id)
# Adjust promo multiplier
adjust_run_promo(run)
# Try to save activity to DB
try:
if ChallengeSqlDB.get_run_by_strava_id(run.strava_id) is None:
# New run activity
ChallengeSqlDB.insert_run(run)
except Exception as e:
ChallengeSqlDB.insert_run(run)
runs.append(run)
except Exception as e:
print(e)
print('Got run acitivities: %d' % (n_run))
print("Total run activities: %d" % (len(runs)))
if __name__ == '__main__':
main()