1
1
import requests
2
+ import os
3
+ from pyngrok import ngrok
4
+ from flask import Flask , request
2
5
from authentication import *
3
6
from sendLINEMessage import *
4
- from flask import Flask , request
5
7
from StravaAnalysisTool_Kernel import *
6
8
7
9
10
+
8
11
app = Flask (__name__ )
9
12
processed_activities = {}
10
13
VERIFY_TOKEN = "STRAVA"
11
- HEROKU_APP_URL = "https://my-strava-webhook.herokuapp.com"
12
14
13
15
14
16
"""
@@ -21,9 +23,9 @@ def view_subscription():
21
23
try :
22
24
base_url = "https://www.strava.com/api/v3/push_subscriptions"
23
25
params = {
24
- 'client_id' : STRAVA_CLIENT_ID ,
26
+ 'client_id' : STRAVA_CLIENT_ID ,
25
27
'client_secret' : STRAVA_CLIENT_SECRET
26
- }
28
+ }
27
29
r = requests .get (base_url , params = params )
28
30
except requests .exceptions .RequestException :
29
31
return None
@@ -32,29 +34,36 @@ def delete_subscription(subscription_id):
32
34
try :
33
35
base_url = "https://www.strava.com/api/v3/push_subscriptions/{}" .format (subscription_id )
34
36
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
+ }
38
40
requests .delete (base_url , params = params )
39
41
except requests .exceptions .RequestException :
40
42
return None
41
43
def create_subscription (callback_url ):
42
44
try :
43
45
base_url = "https://www.strava.com/api/v3/push_subscriptions"
44
46
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
+ }
50
52
requests .post (base_url , data = data )
51
53
except requests .exceptions .RequestException :
52
54
return None
53
55
existing_subscription = view_subscription ()
54
56
if existing_subscription :
55
57
existing_subscription_id = existing_subscription [0 ]["id" ]
56
58
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" )
58
67
return ('SUCCESS' , 200 )
59
68
60
69
@@ -95,7 +104,7 @@ def webhook_post():
95
104
if (latest_activity ["id" ] not in processed_activities ):
96
105
msg = "https://www.strava.com/activities/" + str (latest_activity ["id" ])
97
106
if ((latest_activity ["type" ] == 'Ride' ) or (latest_activity ["type" ] == 'VirtualRide' )) and \
98
- (latest_activity ["trainer" ] == False ):
107
+ (latest_activity ["trainer" ] == False ):
99
108
athlete = get_Athlete (access_token )
100
109
recent_ride_totals = get_Recent_Ride_Totals (athlete ['id' ], access_token )
101
110
msg = msg + "\n \n " + \
0 commit comments