Skip to content

Commit 699dd5a

Browse files
Feat: automate the ngrok workflow using pyngrok package to expose the local web server
1 parent 5b39c82 commit 699dd5a

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Flask
22
line-bot-sdk
33
stravalib
44
requests
5+
pyngrok
56
pandas
67
seaborn
78
matplotlib

src/application.py

+23-14
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import requests
2+
import os
3+
from pyngrok import ngrok
4+
from flask import Flask, request
25
from authentication import *
36
from sendLINEMessage import *
4-
from flask import Flask, request
57
from StravaAnalysisTool_Kernel import *
68

79

10+
811
app = Flask(__name__)
912
processed_activities = {}
1013
VERIFY_TOKEN = "STRAVA"
11-
HEROKU_APP_URL = "https://my-strava-webhook.herokuapp.com"
1214

1315

1416
"""
@@ -21,9 +23,9 @@ def view_subscription():
2123
try:
2224
base_url = "https://www.strava.com/api/v3/push_subscriptions"
2325
params = {
24-
'client_id': STRAVA_CLIENT_ID,
26+
'client_id': STRAVA_CLIENT_ID,
2527
'client_secret': STRAVA_CLIENT_SECRET
26-
}
28+
}
2729
r = requests.get(base_url, params=params)
2830
except requests.exceptions.RequestException:
2931
return None
@@ -32,29 +34,36 @@ def delete_subscription(subscription_id):
3234
try:
3335
base_url = "https://www.strava.com/api/v3/push_subscriptions/{}".format(subscription_id)
3436
params = {
35-
'client_id': STRAVA_CLIENT_ID,
36-
'client_secret': STRAVA_CLIENT_SECRET
37-
}
37+
'client_id': STRAVA_CLIENT_ID,
38+
'client_secret': STRAVA_CLIENT_SECRET
39+
}
3840
requests.delete(base_url, params=params)
3941
except requests.exceptions.RequestException:
4042
return None
4143
def create_subscription(callback_url):
4244
try:
4345
base_url = "https://www.strava.com/api/v3/push_subscriptions"
4446
data = {
45-
'client_id': STRAVA_CLIENT_ID,
46-
'client_secret': STRAVA_CLIENT_SECRET,
47-
'callback_url': callback_url,
48-
'verify_token': VERIFY_TOKEN
49-
}
47+
'client_id': STRAVA_CLIENT_ID,
48+
'client_secret': STRAVA_CLIENT_SECRET,
49+
'callback_url': callback_url,
50+
'verify_token': VERIFY_TOKEN
51+
}
5052
requests.post(base_url, data=data)
5153
except requests.exceptions.RequestException:
5254
return None
5355
existing_subscription = view_subscription()
5456
if existing_subscription:
5557
existing_subscription_id = existing_subscription[0]["id"]
5658
delete_subscription(existing_subscription_id)
57-
create_subscription(HEROKU_APP_URL + "/webhook")
59+
on_heroku = 'DYNO' in os.environ
60+
if on_heroku:
61+
heroku_app_url = "https://my-strava-webhook.herokuapp.com"
62+
create_subscription(heroku_app_url + "/webhook")
63+
else:
64+
tunnels = ngrok.connect(5000)
65+
ngrok_url = tunnels.public_url
66+
create_subscription(ngrok_url + "/webhook")
5867
return ('SUCCESS', 200)
5968

6069

@@ -95,7 +104,7 @@ def webhook_post():
95104
if (latest_activity["id"] not in processed_activities):
96105
msg = "https://www.strava.com/activities/" + str(latest_activity["id"])
97106
if ((latest_activity["type"] == 'Ride') or (latest_activity["type"] == 'VirtualRide')) and \
98-
(latest_activity["trainer"] == False):
107+
(latest_activity["trainer"] == False):
99108
athlete = get_Athlete(access_token)
100109
recent_ride_totals = get_Recent_Ride_Totals(athlete['id'], access_token)
101110
msg = msg + "\n\n" + \

0 commit comments

Comments
 (0)