2
* Copyright (c) 2013 Damien.Grauser (damien.grauser@pev-geneve.ch)
4
* This program is free software; you can redistribute it and/or modify it
5
* under the terms of the GNU General Public License as published by the Free
6
* Software Foundation; either version 2 of the License, or (at your option)
9
* This program is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14
* You should have received a copy of the GNU General Public License along
15
* with this program; if not, write to the Free Software Foundation, Inc., 51
16
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
#include "StravaDownloadDialog.h"
23
#include <QScriptEngine>
24
#include "TimeUtils.h"
28
#include "MetricAggregator.h"
29
#include "RideMetric.h"
31
#include "RideImportWizard.h"
32
#include "TcxRideFile.h"
35
// Download a Strava activity using the Strava API V1
36
// http://www.strava.com/api/v1/streams/9999
37
// which returns a json string with time-series of speed, position, heartrate, etc.
39
// Be aware that these APIs are being deprecated.
40
// The new API coming in early 2013.
42
// TODO work with the v3
43
// http://strava.github.com/api/v3/activities/streams/
45
StravaDownloadDialog::StravaDownloadDialog(MainWindow *mainWindow) :
46
mainWindow(mainWindow)
48
STRAVA_URL_V1 = "http://www.strava.com/api/v1/";
49
STRAVA_URL_V2 = "http://www.strava.com/api/v2/";
50
STRAVA_URL_V3 = "http://www.strava.com/api/v3/";
52
setWindowTitle("Strava download");
53
QVBoxLayout *mainLayout = new QVBoxLayout(this);
55
QGroupBox *groupBox = new QGroupBox(tr("Choose activityId to download: "));
57
QHBoxLayout *hbox = new QHBoxLayout();
58
activityIdEdit = new QLineEdit();
59
hbox->addWidget(activityIdEdit);
61
groupBox->setLayout(hbox);
62
mainLayout->addWidget(groupBox);
65
QVBoxLayout *progressLayout = new QVBoxLayout;
66
progressBar = new QProgressBar(this);
67
progressLabel = new QLabel("", this);
69
progressLayout->addWidget(progressBar);
70
progressLayout->addWidget(progressLabel);
72
QHBoxLayout *buttonLayout = new QHBoxLayout;
74
downloadButton = new QPushButton(tr("&Download Ride"), this);
75
buttonLayout->addWidget(downloadButton);
77
cancelButton = new QPushButton(tr("&Cancel"), this);
78
buttonLayout->addStretch();
79
buttonLayout->addWidget(cancelButton);
81
mainLayout->addLayout(progressLayout);
82
mainLayout->addLayout(buttonLayout);
84
connect(downloadButton, SIGNAL(clicked()), this, SLOT(downloadRide()));
85
connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
89
StravaDownloadDialog::downloadRide()
91
activityId = activityIdEdit->text();
92
if (activityId.trimmed().isEmpty()) {
93
progressLabel->setText(tr("Enter an activity Id"));
97
if (activityId.toLong() == 0) {
98
progressLabel->setText(tr("Enter a valid activity Id"));
102
fileNames = new QStringList();
104
progressBar->setValue(5);
107
//requestListRides();
111
StravaDownloadDialog::requestRideDetail()
115
tmp = new QTemporaryFile(QDir(QDir::tempPath()).absoluteFilePath(".download."+activityId+".XXXXXX.strava"));
116
//QString tmpl = mainWindow->home.absoluteFilePath(".download.XXXXXX."+activityId+".strava"); //
117
//tmp.setFileTemplate(tmpl);
118
tmp->setAutoRemove(true);
121
progressLabel->setText(tr("Failed to create temporary file ")
122
+ tmp->fileName() + ": " + tmp->error());
128
progressLabel->setText(tr("Get activity %1 details").arg(activityId));
130
QEventLoop eventLoop;
131
QNetworkAccessManager networkMgr;
133
connect(&networkMgr, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestRideDetailFinished(QNetworkReply*)));
134
connect(&networkMgr, SIGNAL(finished(QNetworkReply *)), &eventLoop, SLOT(quit()));
136
QUrl url = QUrl(STRAVA_URL_V1 + "rides/"+activityId);
138
QNetworkRequest request = QNetworkRequest(url);
140
networkMgr.get(request);
142
progressBar->setValue(progressBar->value()+5/count);
146
StravaDownloadDialog::requestRideDetailFinished(QNetworkReply *reply)
148
progressBar->setValue(15);
150
if (reply->error() != QNetworkReply::NoError)
151
progressLabel->setText(QString(tr("Error from ride details %1")).arg(reply->error()));
153
QString response = reply->readLine();
158
sc = se.evaluate("("+response+")");
159
QString error = sc.property("error").toString();
160
if (!error.isEmpty()) {
161
progressLabel->setText(error);
165
tmp->write(response.toAscii());
166
progressBar->setValue(progressBar->value()+10/count);
168
requestDownloadRide();
173
StravaDownloadDialog::requestDownloadRide()
177
progressLabel->setText(tr("Download activity %1").arg(activityId));
179
QEventLoop eventLoop;
180
QNetworkAccessManager networkMgr;
182
connect(&networkMgr, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestDownloadRideFinished(QNetworkReply*)));
183
connect(&networkMgr, SIGNAL(finished(QNetworkReply *)), &eventLoop, SLOT(quit()));
185
QUrl url = QUrl(STRAVA_URL_V1 + "streams/"+activityId);
187
QNetworkRequest request = QNetworkRequest(url);
189
networkMgr.get(request);
191
progressBar->setValue(progressBar->value()+20/count);
195
StravaDownloadDialog::requestDownloadRideFinished(QNetworkReply *reply)
197
progressBar->setValue(60);
199
if (reply->error() != QNetworkReply::NoError)
200
progressLabel->setText(QString(tr("Error from upload %1")).arg(reply->error()));
202
QString response = reply->readLine();
204
progressBar->setValue(70);
205
tmp->seek(tmp->size()-2);
206
tmp->write(",\"streams\":");
207
tmp->write(response.toAscii());
208
tmp->write("},\"api_version\":\"1\"}");
210
progressBar->setValue(progressBar->value()+60/count);
212
fileNames->append(tmp->fileName());
214
if (fileNames->count() == count) {
216
RideImportWizard *import = new RideImportWizard(*fileNames, mainWindow->home, mainWindow);
218
progressBar->setValue(100);
224
StravaDownloadDialog::okClicked()
231
StravaDownloadDialog::cancelClicked()
239
StravaDownloadDialog::requestLogin()
241
progressLabel->setText(tr("Login..."));
242
progressBar->setValue(5);
244
QString username = appsettings->cvalue(mainWindow->cyclist, GC_STRUSER).toString();
245
QString password = appsettings->cvalue(mainWindow->cyclist, GC_STRPASS).toString();
247
QNetworkAccessManager networkMgr;
248
QEventLoop eventLoop;
249
connect(&networkMgr, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestLoginFinished(QNetworkReply*)));
250
connect(&networkMgr, SIGNAL(finished(QNetworkReply *)), &eventLoop, SLOT(quit()));
253
/*data += "{\"email\": \"" + username + "\",";
254
data += "\"password\": \"" + password + "\",";
255
data += "\"agreed_to_terms\": \"true\"}";*/
259
params.addQueryItem("email", username);
260
params.addQueryItem("password",password);
261
params.addQueryItem("agreed_to_terms", "true");
262
data = params.encodedQuery();
264
QUrl url = QUrl( STRAVA_URL_V2_SSL + "/authentication/login");
265
QNetworkRequest request = QNetworkRequest(url);
266
//request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
268
request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
270
networkMgr.post( request, data);
275
StravaDownloadDialog::requestLoginFinished(QNetworkReply *reply)
280
if (reply->error() == QNetworkReply::NoError)
282
QString response = reply->readLine();
284
if(response.contains("token"))
289
sc = se.evaluate("("+response+")");
290
token = sc.property("token").toString();
291
athleteId = sc.property("athlete").property("id").toString();
305
#define GC_PREF tr("Golden Cheetah->Preferences")
307
#define GC_PREF tr("Tools->Options")
309
QString advise = QString(tr("Please make sure to fill the Strava login info found under\n %1.")).arg(GC_PREF);
310
QMessageBox err(QMessageBox::Critical, tr("Strava login Error"), advise);
317
StravaDownloadDialog::requestListRides()
319
QString athleteId = "775290"; //Rica 131476
321
progressBar->setValue(0);
322
progressLabel->setText(tr("Download athlete"));
324
QEventLoop eventLoop;
325
QNetworkAccessManager networkMgr;
327
connect(&networkMgr, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestListRidesFinished(QNetworkReply*)));
328
connect(&networkMgr, SIGNAL(finished(QNetworkReply *)), &eventLoop, SLOT(quit()));
330
QUrl url = QUrl(STRAVA_URL_V1 + "rides?athleteId="+athleteId+"&offset=147");//+"&offset=0"
332
QNetworkRequest request = QNetworkRequest(url);
334
networkMgr.get(request);
336
progressBar->setValue(5);
340
StravaDownloadDialog::requestListRidesFinished(QNetworkReply *reply)
342
progressBar->setValue(10);
344
if (reply->error() != QNetworkReply::NoError)
345
qDebug() << "Error from upload " <<reply->error();
347
QString response = reply->readLine();
352
sc = se.evaluate("("+response+")");
353
count = sc.property("rides").property("length").toInteger();
356
for(int i = 0; i < count; i++) {
357
activityId = sc.property("rides").property(i).property("id").toString();