-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgarmin-connect.ls
212 lines (180 loc) · 8.17 KB
/
garmin-connect.ls
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# Dependencies
# #############################################
require! {
https: { request }
'prelude-ls': { keys, obj-to-pairs, map, join }
axios
'@3846masa/axios-cookiejar-support' : axios-cookie-jar-support
'tough-cookie' : tough
querystring
fs : { write-file }
'async-ls' : { callbacks }
}
{ serial-map } = callbacks
axios-cookie-jar-support(axios)
cookie-jar = new tough.CookieJar()
axios.defaults.jar = cookie-jar
axios.defaults.with-credentials = true
# #############################################
# Alias
# #############################################
say = console.log
# #############################################
# Helpers
# #############################################
format-query = (query) ->
query
|> obj-to-pairs
|> map (pair) -> pair.join('=')
|> join('&')
# #############################################
# URL and queries
# #############################################
query-login = ->
format-query(
'service' : 'https%3A%2F%2Fconnect.garmin.com%2Fpost-auth%2Flogin'
'webhost' : 'olaxpw-connect04'
'source' : 'https%3A%2F%2Fconnect.garmin.com%2Fen-US%2Fsignin'
'redirectAfterAccountLoginUrl' : 'https%3A%2F%2Fconnect.garmin.com%2Fpost-auth%2Flogin'
'redirectAfterAccountCreationUrl' : 'https%3A%2F%2Fconnect.garmin.com%2Fpost-auth%2Flogin'
'gauthHost' : 'https%3A%2F%2Fsso.garmin.com%2Fsso'
'locale' : 'en_US'
'id' : 'gauth-widget'
'cssUrl': 'https%3A%2F%2Fstatic.garmincdn.com%2Fcom.garmin.connect%2Fui%2Fcss%2Fgauth-custom-v1.1-min.css'
'clientId' : 'GarminConnect'
'rememberMeShown' : 'true'
'rememberMeChecked' : 'false'
'createAccountShown' : 'true'
'openCreateAccount' : 'false'
'usernameShown' : 'false'
'displayNameShown' : 'false'
'consumeServiceTicket' : 'false'
'initialFocus' : 'true'
'embedWidget' : 'false'
'generateExtraServiceTicket' : 'false')
query-search = (start, limit) -> format-query(start:start, limit:limit)
url-login = -> "https://sso.garmin.com/sso/login?#{query-login!}"
url-authenticate = (ticket) -> "https://connect.garmin.com/post-auth/login?ticket=#{ticket}"
url-search = (start, limit) -> "https://connect.garmin.com/proxy/activity-search-service-1.2/json/activities?#{query-search(start, limit)}"
url-gpx-activity = (activity-id) -> "https://connect.garmin.com/modern/proxy/download-service/export/tcx/activity/#{activity-id}"
url-tcx-activity = (activity-id) -> "https://connect.garmin.com/modern/proxy/download-service/export/tcx/activity/#{activity-id}"
url-original-activity = (activity-id) -> "https://connect.garmin.com/modern/proxy/download-service/files/activity/#{activity-id}"
# #############################################
# Regexes
# #############################################
regex-login-ticket = /ticket=([^\"]+)\"/
# #############################################
# create client
# #############################################
export create-client = -> axios
# cookie-jar = new CookieJar()
# axios-create-client(
# jar : cookie-jar
# with-credentials : true
# headers :
# 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/1337 Safari/537.36')
# fetch login page
# #############################################
export fetch-login = (client, callback) ->
client.post(url-login!)
.then((response) -> callback(null))
.catch((error) -> callback(error))
# login to garmin sso
# #############################################
export login = (client, username, password, callback) ->
post-data =
username : username
password : password
embed : \false
lt : \e1s1
_event-id : \submit
display-name-required : \false
client.post(url-login!, querystring.stringify(post-data))
.then((response) ->
login-response = response.data
if login-response.includes('Login Successful')
ticket-match = login-response.match(regex-login-ticket).1
callback(null, ticket-match)
else
callback('Login failed'))
.catch((error) -> callback(error))
# transfer authentication to connect.garmin.com
# #############################################
export authenticate = (client, ticket, callback) ->
client.post(url-authenticate(ticket))
.then((response) -> callback(null, client))
.catch((error) -> callback(error))
# get list of activities from garmin
# #############################################
export get-activities-list = (client, start, limit, callback) ->
client.get(url-search(start, limit))
.then((response) -> callback(null, {
count: response.data.results.total-found
activities: map((.activity), response.data.results.activities) }))
.catch((error) -> callback(error))
# get a list of all activities
# #############################################
export get-all-activities-list = (client, callback) ->
(err, result) <- get-activities-list(client, 0, 100)
callback(err, result)
# download activity
# #############################################
export get-activity = (client, pre, post, activity-id, callback) -->
pre(activity-id) if pre?
client.get(url-tcx-activity(activity-id))
.then((response) ->
post(activity-id, response.data) if post?
callback(null, response))
.catch((error) -> callback(error))
# store activities list
# #############################################
export store-activities-list = (path, data, callback) -->
(err) <- write-file("#{path}/activities-list.json", JSON.stringify(data, null, 2), encoding:\utf8)
say "Activity list stored: #{path}/activities-list.json" unless err?
callback(err)
# store activity
# #############################################
export store-activity = (output-directory, activity-id, data, callback) ->
(err) <- write-file("#{output-directory}/#{activity-id}.tcx", data, encoding:\utf8)
say "Activity stored: #{output-directory}/#{activity-id}.tcx" unless err?
callback(err)
# automate login, downloading and storing of all activities
# not allready downloaded
# #############################################
export automation = (bail-if-err, username, password, start, limit, output-directory, callback) ->
# Create a new client that we can use for our requests
client = create-client!
# Make a dummy request to login page so that we get a session
(err) <- fetch-login(client)
bail-if-err(err, "Could not fetch login page")
say "Login page loaded"
# Login to garmin sso
(err, ticket) <- login(client, username, password)
bail-if-err(err, "Could not post to login page")
say "SSO authenticated"
# Transfer authentication to garmin connect
(err) <- authenticate(client, ticket)
bail-if-err(err, "Could not authenticate on garmin connect")
say "SSO transfered to connect.garmin.com"
# Get all activities
(err, activities-list) <- get-activities-list(client, start, limit)
bail-if-err(err, "Could not download activities list")
say "Activities list is downloaded"
# Store the list of activities in a file
(err) <- store-activities-list(output-directory, activities-list)
bail-if-err(err, "Could not store activities list")
say "Activities list is stored in #{output-directory}"
activities-ids = map((.activity-id), activities-list.activities)
# For each activity this will be executed before each download starts
pre-get-activity = (activity-id) ->
say "Downloading activity ##{activity-id}"
# For each activity this will be executed after each download is finished
post-get-activity = (activity-id, data) ->
(err) <- store-activity(output-directory, acivitity-id, data)
say err if err?
say "Done with ##{activity-id}" if not err?
# Download all activities
(err, activity) <- serial-map(get-activity(client, pre-get-activity, post-get-activity), activities-ids)
bail-if-err(err, "Could not download activity")
# Inform caller that we are done
callback!