Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,47 @@
|
|
| 1 |
from fastapi import FastAPI, Query
|
| 2 |
import requests
|
| 3 |
import re
|
|
|
|
| 4 |
|
| 5 |
# Initialize FastAPI app
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
-
# Strava API credentials (
|
| 9 |
CLIENT_ID = "39765"
|
| 10 |
CLIENT_SECRET = "88afc8eccac348c38659eadb099a712d083b0313"
|
| 11 |
-
REFRESH_TOKEN = "
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# Function to get a valid Strava access token
|
| 15 |
def get_access_token():
|
|
@@ -21,14 +53,23 @@ def get_access_token():
|
|
| 21 |
"grant_type": "refresh_token",
|
| 22 |
}
|
| 23 |
response = requests.post(url, data=payload)
|
| 24 |
-
|
| 25 |
if response.status_code != 200:
|
| 26 |
return None # Token request failed
|
| 27 |
|
| 28 |
token_data = response.json()
|
| 29 |
return token_data.get("access_token", None)
|
| 30 |
|
| 31 |
-
# Function to
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
def extract_activity_id(activity_input):
|
| 33 |
# Case 1: If input is a raw activity ID (only numbers)
|
| 34 |
if activity_input.isdigit():
|
|
@@ -36,11 +77,9 @@ def extract_activity_id(activity_input):
|
|
| 36 |
|
| 37 |
# Case 2: If input is a deep link (e.g., strava.app.link/XYZ)
|
| 38 |
if "strava.app.link" in activity_input:
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
activity_input =
|
| 42 |
-
except requests.RequestException:
|
| 43 |
-
return None # Failed to expand deep link
|
| 44 |
|
| 45 |
# Case 3: Extract Activity ID from direct Strava URL
|
| 46 |
match = re.search(r"/activities/(\d+)", activity_input)
|
|
@@ -61,7 +100,11 @@ def fetch_activity_data(activity_input):
|
|
| 61 |
response = requests.get(url, headers=headers)
|
| 62 |
|
| 63 |
if response.status_code != 200:
|
| 64 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
return response.json()
|
| 67 |
|
|
@@ -70,7 +113,6 @@ def fetch_activity_data(activity_input):
|
|
| 70 |
def analyze_activity(activity_input: str = Query(..., description="Strava Activity URL, Deep Link, or ID")):
|
| 71 |
return fetch_activity_data(activity_input)
|
| 72 |
|
| 73 |
-
# Run
|
| 74 |
if __name__ == "__main__":
|
| 75 |
-
import uvicorn
|
| 76 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
from fastapi import FastAPI, Query
|
| 2 |
import requests
|
| 3 |
import re
|
| 4 |
+
import uvicorn
|
| 5 |
|
| 6 |
# Initialize FastAPI app
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
+
# Strava API credentials (Replace with actual values)
|
| 10 |
CLIENT_ID = "39765"
|
| 11 |
CLIENT_SECRET = "88afc8eccac348c38659eadb099a712d083b0313"
|
| 12 |
+
REFRESH_TOKEN = "your_current_refresh_token" # This will be updated dynamically
|
| 13 |
|
| 14 |
+
# Function to exchange authorization code for access token
|
| 15 |
+
def exchange_code_for_token(auth_code):
|
| 16 |
+
url = "https://www.strava.com/api/v3/oauth/token"
|
| 17 |
+
payload = {
|
| 18 |
+
"client_id": CLIENT_ID,
|
| 19 |
+
"client_secret": CLIENT_SECRET,
|
| 20 |
+
"code": auth_code,
|
| 21 |
+
"grant_type": "authorization_code",
|
| 22 |
+
}
|
| 23 |
+
response = requests.post(url, data=payload)
|
| 24 |
+
|
| 25 |
+
if response.status_code != 200:
|
| 26 |
+
return {"error": "Failed to exchange code for token", "details": response.json()}
|
| 27 |
+
|
| 28 |
+
token_data = response.json()
|
| 29 |
+
|
| 30 |
+
# Update refresh token globally
|
| 31 |
+
global REFRESH_TOKEN
|
| 32 |
+
REFRESH_TOKEN = token_data.get("refresh_token", REFRESH_TOKEN)
|
| 33 |
+
|
| 34 |
+
return {
|
| 35 |
+
"access_token": token_data.get("access_token"),
|
| 36 |
+
"refresh_token": REFRESH_TOKEN,
|
| 37 |
+
"expires_at": token_data.get("expires_at")
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
# ✅ Route to Handle OAuth Callback
|
| 41 |
+
@app.get("/callback")
|
| 42 |
+
def handle_callback(code: str = Query(..., description="Authorization code from Strava")):
|
| 43 |
+
token_response = exchange_code_for_token(code)
|
| 44 |
+
return token_response # Show new tokens in JSON response
|
| 45 |
|
| 46 |
# Function to get a valid Strava access token
|
| 47 |
def get_access_token():
|
|
|
|
| 53 |
"grant_type": "refresh_token",
|
| 54 |
}
|
| 55 |
response = requests.post(url, data=payload)
|
| 56 |
+
|
| 57 |
if response.status_code != 200:
|
| 58 |
return None # Token request failed
|
| 59 |
|
| 60 |
token_data = response.json()
|
| 61 |
return token_data.get("access_token", None)
|
| 62 |
|
| 63 |
+
# Function to expand deep links (strava.app.link/XYZ → actual Strava activity URL)
|
| 64 |
+
def expand_short_url(short_url):
|
| 65 |
+
try:
|
| 66 |
+
response = requests.head(short_url, allow_redirects=True)
|
| 67 |
+
expanded_url = response.url
|
| 68 |
+
return expanded_url
|
| 69 |
+
except requests.RequestException:
|
| 70 |
+
return None # Return None if expansion fails
|
| 71 |
+
|
| 72 |
+
# Function to extract Activity ID from any input (Deep Link, Direct Link, Raw ID)
|
| 73 |
def extract_activity_id(activity_input):
|
| 74 |
# Case 1: If input is a raw activity ID (only numbers)
|
| 75 |
if activity_input.isdigit():
|
|
|
|
| 77 |
|
| 78 |
# Case 2: If input is a deep link (e.g., strava.app.link/XYZ)
|
| 79 |
if "strava.app.link" in activity_input:
|
| 80 |
+
expanded_url = expand_short_url(activity_input)
|
| 81 |
+
if expanded_url:
|
| 82 |
+
activity_input = expanded_url # Replace with actual Strava URL
|
|
|
|
|
|
|
| 83 |
|
| 84 |
# Case 3: Extract Activity ID from direct Strava URL
|
| 85 |
match = re.search(r"/activities/(\d+)", activity_input)
|
|
|
|
| 100 |
response = requests.get(url, headers=headers)
|
| 101 |
|
| 102 |
if response.status_code != 200:
|
| 103 |
+
return {
|
| 104 |
+
"error": "Failed to fetch activity data.",
|
| 105 |
+
"status_code": response.status_code,
|
| 106 |
+
"response_text": response.text
|
| 107 |
+
}
|
| 108 |
|
| 109 |
return response.json()
|
| 110 |
|
|
|
|
| 113 |
def analyze_activity(activity_input: str = Query(..., description="Strava Activity URL, Deep Link, or ID")):
|
| 114 |
return fetch_activity_data(activity_input)
|
| 115 |
|
| 116 |
+
# ✅ Run FastAPI App in Hugging Face
|
| 117 |
if __name__ == "__main__":
|
|
|
|
| 118 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|