Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

relaxing peloton api timings #2696

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 30 additions & 17 deletions src/peloton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,22 +490,28 @@ void peloton::startEngine() {

QSettings settings;
timer->stop();
connect(mgr, &QNetworkAccessManager::finished, this, &peloton::login_onfinish);
QUrl url(QStringLiteral("https://api.onepeloton.com/auth/login"));
QNetworkRequest request(url);

request.setHeader(QNetworkRequest::ContentTypeHeader, QStringLiteral("application/json"));
request.setHeader(QNetworkRequest::UserAgentHeader, QStringLiteral("qdomyos-zwift"));

QJsonObject obj;
obj[QStringLiteral("username_or_email")] =
settings.value(QZSettings::peloton_username, QZSettings::default_peloton_username).toString();
obj[QStringLiteral("password")] =
settings.value(QZSettings::peloton_password, QZSettings::default_peloton_password).toString();
QJsonDocument doc(obj);
QByteArray data = doc.toJson();

mgr->post(request, data);
uint64_t diff = qAbs(lastLoginSuccesful.secsTo(QDateTime::currentDateTime()));
qDebug() << lastLoginSuccesful << diff;
if(!lastLoginSuccesful.isValid() || diff > (60 * 60 * 2) ) {
connect(mgr, &QNetworkAccessManager::finished, this, &peloton::login_onfinish);
QUrl url(QStringLiteral("https://api.onepeloton.com/auth/login"));
QNetworkRequest request(url);

request.setHeader(QNetworkRequest::ContentTypeHeader, QStringLiteral("application/json"));
request.setHeader(QNetworkRequest::UserAgentHeader, QStringLiteral("qdomyos-zwift"));

QJsonObject obj;
obj[QStringLiteral("username_or_email")] =
settings.value(QZSettings::peloton_username, QZSettings::default_peloton_username).toString();
obj[QStringLiteral("password")] =
settings.value(QZSettings::peloton_password, QZSettings::default_peloton_password).toString();
QJsonDocument doc(obj);
QByteArray data = doc.toJson();

mgr->post(request, data);
} else {
getWorkoutList(1);
}
}

void peloton::login_onfinish(QNetworkReply *reply) {
Expand All @@ -530,9 +536,14 @@ void peloton::login_onfinish(QNetworkReply *reply) {
emit loginState(false);
return;
}

lastLoginSuccesful = QDateTime::currentDateTime();

session_id = document[QStringLiteral("session_id")].toString();
user_id = document[QStringLiteral("user_id")].toString();
total_workout = document[QStringLiteral("user_data")][QStringLiteral("total_workouts")].toInt();

qDebug() << session_id << user_id;

emit loginState(!user_id.isEmpty());

Expand Down Expand Up @@ -1453,9 +1464,11 @@ void peloton::getWorkoutList(int num) {
QStringLiteral("&limit=") + QString::number(limit));
qDebug() << "peloton::getWorkoutList" << url;
QNetworkRequest request(url);

request.setHeader(QNetworkRequest::ContentTypeHeader, QStringLiteral("application/json"));
request.setHeader(QNetworkRequest::UserAgentHeader, QStringLiteral("qdomyos-zwift"));
request.setRawHeader(QByteArray("peloton_session_id"), session_id.toUtf8());
qDebug() << request.rawHeader("peloton_session_id");

mgr->get(request);
}
Expand Down
4 changes: 3 additions & 1 deletion src/peloton.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ class peloton : public QObject {

_PELOTON_API currentApi() { return current_api; }

QString user_id;
QString session_id = QLatin1String("");
QString user_id = QLatin1String("");
QDateTime lastLoginSuccesful = QDateTime();
QString current_workout_id = QLatin1String("");
QString current_workout_name = QLatin1String("");
QString current_workout_status = QLatin1String("");
Expand Down
Loading