-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrava_db_sync_mongodb_atlas.py
180 lines (157 loc) · 7.55 KB
/
strava_db_sync_mongodb_atlas.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import os
import requests
from datetime import datetime
import polyline
import time
from pymongo import MongoClient
import logging
from logging.handlers import RotatingFileHandler
# -----------------------------------------------------------------------------
# 1. Configuration
# -----------------------------------------------------------------------------
DATABASE_URL = os.environ.get('DATABASE_URL', '')
STRAVA_CLIENT_ID = os.environ.get('STRAVA_CLIENT_ID', '')
STRAVA_CLIENT_SECRET = os.environ.get('STRAVA_CLIENT_SECRET', '')
STRAVA_REFRESH_TOKEN = os.environ.get('STRAVA_REFRESH_TOKEN', '')
STRAVA_TOKEN_URL = "https://www.strava.com/oauth/token"
STRAVA_ACCESS_TOKEN = None # This will be updated after refreshing
SYNC_INTERVAL = int(os.environ.get('SYNC_INTERVAL', 6)) * 3600 # Default to 6 hours (4x per day)
ACTIVITIES_PER_PAGE = int(os.environ.get('ACTIVITIES_PER_PAGE', 200)) # Default to 50 activities per page
# -----------------------------------------------------------------------------
# 2. Logging Setup
# -----------------------------------------------------------------------------
def setup_logging():
os.makedirs('/logs', exist_ok=True)
log_filename = f"/logs/sync_log_{datetime.now().strftime('%Y%m%d_%H%M%S')}.log"
handler = RotatingFileHandler(log_filename, maxBytes=10*1024*1024, backupCount=5)
handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(message)s')
handler.setFormatter(formatter)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
console_handler.setFormatter(formatter)
logging.getLogger().setLevel(logging.INFO)
logging.getLogger().addHandler(handler)
logging.getLogger().addHandler(console_handler)
# -----------------------------------------------------------------------------
# 3. MongoDB Setup
# -----------------------------------------------------------------------------
def connect_to_db():
try:
client = MongoClient(DATABASE_URL)
db = client.strava_db
return db
except Exception as e:
logging.error(f"Error connecting to MongoDB: {e}")
exit()
def initialize_db():
db = connect_to_db()
db.activities.create_index("id", unique=True) # Ensure unique activity IDs
db.activities.create_index([("route", "2dsphere")]) # Geospatial index for route
logging.info("Database initialized.")
# -----------------------------------------------------------------------------
# 4. Refresh Access Token
# -----------------------------------------------------------------------------
def refresh_access_token():
global STRAVA_ACCESS_TOKEN
logging.info("Refreshing access token...")
payload = {
"client_id": STRAVA_CLIENT_ID,
"client_secret": STRAVA_CLIENT_SECRET,
"refresh_token": STRAVA_REFRESH_TOKEN,
"grant_type": "refresh_token",
}
response = requests.post(STRAVA_TOKEN_URL, data=payload)
if response.status_code == 200:
token_data = response.json()
STRAVA_ACCESS_TOKEN = token_data["access_token"]
logging.info("Access token refreshed successfully.")
else:
logging.error(f"Error refreshing token: {response.status_code}, {response.text}")
exit()
# -----------------------------------------------------------------------------
# 5. Fetch Activities from Strava
# -----------------------------------------------------------------------------
def fetch_activities(page=1, per_page=50):
url = f"https://www.strava.com/api/v3/athlete/activities?page={page}&per_page={per_page}"
headers = {"Authorization": f"Bearer {STRAVA_ACCESS_TOKEN}"}
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.json()
else:
logging.error(f"Error fetching activities: {response.status_code}, {response.text}")
return []
# -----------------------------------------------------------------------------
# 6. Save Activities to MongoDB
# -----------------------------------------------------------------------------
def save_activities_to_db(activities):
db = connect_to_db()
collection = db.activities
for activity in activities:
try:
# Extract polyline and decode it into GeoJSON LineString
map_data = activity.get("map", {})
polyline_data = map_data.get("summary_polyline")
geojson_route = None
if polyline_data:
decoded_polyline = polyline.decode(polyline_data)
# Swap latitude and longitude to ensure [longitude, latitude] order
geojson_route = {
"type": "LineString",
"coordinates": [[coord[1], coord[0]] for coord in decoded_polyline]
}
# Insert activity into the database
activity_document = {
"id": activity["id"],
"name": activity["name"],
"type": activity["type"],
"distance": activity.get("distance"),
"moving_time": activity.get("moving_time"),
"elapsed_time": activity.get("elapsed_time"),
"total_elevation_gain": activity.get("total_elevation_gain"),
"sport_type": activity.get("sport_type"),
"start_date": datetime.strptime(activity["start_date"], "%Y-%m-%dT%H:%M:%SZ"),
"start_date_local": datetime.strptime(activity["start_date_local"], "%Y-%m-%dT%H:%M:%S%z"),
"timezone": activity.get("timezone"),
"map": map_data, # Store map as JSON
"route": geojson_route, # Store route in GeoJSON format
"gear": activity.get("gear"), # Store gear as JSON
"average_speed": activity.get("average_speed"),
"max_speed": activity.get("max_speed"),
"average_cadence": activity.get("average_cadence"),
"average_heartrate": activity.get("average_heartrate"),
"max_heartrate": activity.get("max_heartrate"),
"calories": activity.get("calories"),
"raw_data": activity, # Store raw activity data as JSON
}
collection.update_one({"id": activity["id"]}, {"$set": activity_document}, upsert=True)
logging.info(f"Saved activity {activity['id']} - {activity['name']} to the database.")
except Exception as e:
logging.error(f"Error saving activity {activity['id']}: {e}")
# -----------------------------------------------------------------------------
# 7. Sync Activities
# -----------------------------------------------------------------------------
def sync_activities():
logging.info(f"Starting sync at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}...")
# Refresh the access token
refresh_access_token()
page = 1
while True:
logging.info(f"Fetching page {page}...")
activities = fetch_activities(page, ACTIVITIES_PER_PAGE)
if not activities:
logging.info("No more activities to fetch. Sync complete.")
break
save_activities_to_db(activities)
page += 1
logging.info(f"Sync complete at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}.")
# -----------------------------------------------------------------------------
# 8. Main Function: Sync Every Interval
# -----------------------------------------------------------------------------
if __name__ == "__main__":
setup_logging()
initialize_db()
while True:
sync_activities()
logging.info(f"Waiting {SYNC_INTERVAL // 3600} hours for the next sync...")
time.sleep(SYNC_INTERVAL)