-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
80 lines (63 loc) · 2.43 KB
/
app.py
File metadata and controls
80 lines (63 loc) · 2.43 KB
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
import requests
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
from flask import Flask, jsonify, render_template
from flask_session import Session
# Strava App Credentials
CLIENT_ID = "141251"
CLIENT_SECRET = "aaa79d5d3addc6e7c97d9f8bf097af854f78edb2"
# Configure application
app = Flask(__name__)
# Configure session to use filesystem (instead of signed cookies)
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
@app.route("/")
def index():
return render_template("index.html")
@app.route('/map')
def map():
return render_template("map.html")
@app.route('/api/map', methods=['GET'])
def get_activities():
# Strava API URLs
auth_url = "https://www.strava.com/oauth/token"
athlete_url = "https://www.strava.com/api/v3/athlete"
activities_url = "https://www.strava.com/api/v3/athlete/activities"
# Payload for authentication
payload = {
"client_id": CLIENT_ID,
"client_secret": CLIENT_SECRET,
"refresh_token": "b1b2ad07f4c54f8f6bb5e8df3cad0a05edf084c5",
"grant_type": "refresh_token",
}
try:
# Request access token
auth_response = requests.post(auth_url, json=payload)
auth_response.raise_for_status()
auth_data = auth_response.json()
access_token = auth_data.get("access_token")
if not access_token:
return jsonify({"error": "Failed to retrieve access token"}), 400
headers = {"Authorization": f"Bearer {access_token}"}
# Fetch athlete information
athlete_response = requests.get(athlete_url, headers=headers)
athlete_response.raise_for_status()
athlete = athlete_response.json()
# Fetch all activities
all_activities = []
page = 1
while True:
params = {"per_page": 200, "page": page}
activities_response = requests.get(activities_url, headers=headers, params=params)
activities_response.raise_for_status()
activities = activities_response.json()
if not activities:
break # No more activities
all_activities.extend(activities)
page += 1
return jsonify({"athlete": athlete, "activities": all_activities})
except requests.RequestException as e:
return jsonify({"error": f"An error occurred: {str(e)}"}), 500
if __name__ == '__main__':
app.run(debug=True)