forked from CMon/QHumble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhumblebundleapi.cpp
315 lines (261 loc) · 10.2 KB
/
humblebundleapi.cpp
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include "humblebundleapi.h"
#include <QNetworkAccessManager>
#include <QNetworkCookieJar>
#include <QNetworkCookie>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QUrlQuery>
#include <QUrl>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <settings.h>
#include "db/purchase.h"
#include "db/product.h"
#include "db/download.h"
#include "db/file.h"
#include <QSqlRecord>
#include <QDebug>
namespace URLS {
const QString apiBaseUrl = "https://hr-humblebundle.appspot.com/api/v1/";
const QString loginUrl = "https://hr-humblebundle.appspot.com/processlogin";
const QString orderListUrl = apiBaseUrl + "user/order" ;
const QString orderUrl = apiBaseUrl + "order/%1" ; // %1 is the order_id
//const QString claimedEntitiesUrl = apiBaseUrl + "user/claimed/entities";
//const QString signedDownloadUrl = apiBaseUrl + "user/Download/%1/sign"; // %1 is the machine_name
}
HumbleBundleAPI::HumbleBundleAPI(QApplication *parent)
:
parentApp(parent),
networkAccessManager_(new QNetworkAccessManager(this)),
isLoggedIn_(false)
{
connect(networkAccessManager_, &QNetworkAccessManager::finished, this, &HumbleBundleAPI::onFinished);
if(Settings::getSessionToken().size() > 0) {
isLoggedIn_ = true;
//networkAccessManager_->cookieJar()->insertCookie(QNetworkCookie("_simpleauth_sess",Settings::getSessionToken().toLatin1()));
}
}
void HumbleBundleAPI::setCredentials(const QString & login, const QString & password, const QString & twoFactor = "", const QString & reCaptchaResponse = "")
{
username_ = login;
password_ = password;
twoFactor_ = twoFactor;
reCaptcha_ = reCaptchaResponse;
this->login();
}
void HumbleBundleAPI::updateOrderList()
{
QUrlQuery queryUrl;
queryUrl.addQueryItem("ajax", "true");
QUrl url(URLS::orderListUrl);
url.setQuery(queryUrl);
QNetworkRequest request;
request.setUrl(url);
request.setRawHeader("Accept", "application/json");
request.setRawHeader("Accept-Charset", "utf-8");
request.setRawHeader("Keep-Alive", "true");
request.setRawHeader("X-Requested-By", "hb_android_app");
request.setHeader(QNetworkRequest::CookieHeader,QVariant::fromValue(QNetworkCookie("_simpleauth_sess",Settings::getSessionToken().toLatin1())));
networkAccessManager_->get(request);
}
bool HumbleBundleAPI::isRefreshNeeded()
{
if(db.getPurchaseCount() > 0)
{
return false;
}
return true;
}
void HumbleBundleAPI::updateOrder(const QString & orderId)
{
QUrlQuery queryUrl;
queryUrl.addQueryItem("ajax", "true");
QUrl url(URLS::orderUrl.arg(orderId));
url.setQuery(queryUrl);
QNetworkRequest request;
request.setUrl(url);
request.setRawHeader("Accept", "application/json");
request.setRawHeader("Accept-Charset", "utf-8");
request.setRawHeader("Keep-Alive", "true");
request.setRawHeader("X-Requested-By", "hb_android_app");
request.setHeader(QNetworkRequest::CookieHeader,QVariant::fromValue(QNetworkCookie("_simpleauth_sess",Settings::getSessionToken().toLatin1())));
networkAccessManager_->get(request);
}
QVariant HumbleBundleAPI::purchaseModel()
{
QSqlQueryModel *model = db.getPurchaseModel();
return QVariant::fromValue(model);
}
QVariant HumbleBundleAPI::productsModel(int purchaseId)
{
QSqlQueryModel *model = db.getProductModel(purchaseId);
return QVariant::fromValue(model);
}
QVariant HumbleBundleAPI::productPlatforms(int product)
{
QSqlQueryModel *model = db.getProductPlatforms(product);
return QVariant::fromValue(model);
}
QVariant HumbleBundleAPI::downloadsModel(int product)
{
QSqlQueryModel *model = db.getDownloadModel(product);
return QVariant::fromValue(model);
}
QVariant HumbleBundleAPI::filesModel(int download)
{
QSqlQueryModel *model = db.getFileModel(download);
return QVariant::fromValue(model);
}
QVariant HumbleBundleAPI::filterablePurchaseModel()
{
QSortFilterProxyModel *model = db.getFilterablePurchaseModel();
return QVariant::fromValue(model);
}
void HumbleBundleAPI::login()
{
QUrlQuery queryUrl;
queryUrl.addQueryItem("ajax", "true");
queryUrl.addQueryItem("username", username_);
queryUrl.addQueryItem("password", password_);
if(twoFactor_.size() > 0) {
queryUrl.addQueryItem("code", twoFactor_);
}
queryUrl.addQueryItem("recaptcha_response_field", reCaptcha_);
queryUrl.addQueryItem("recaptcha_challenge_field","");
QNetworkRequest request;
request.setUrl(QUrl(URLS::loginUrl));
request.setRawHeader("Accept", "application/json");
request.setRawHeader("Accept-Charset", "utf-8");
request.setRawHeader("Keep-Alive", "true");
request.setRawHeader("content-type", "application/x-www-form-urlencoded");
request.setRawHeader("X-Requested-By", "hb_android_app");
networkAccessManager_->post(request, queryUrl.toString(QUrl::FullyEncoded).toUtf8());
}
void HumbleBundleAPI::parseOrderList(const QByteArray & json)
{
//delete DB. TODO: eventually find a good way to only update entries when refreshing.
db.clearDB();
//Retrieve details of each purchase and add them to the DB.
QJsonDocument jDoc = QJsonDocument::fromJson(json);
QJsonArray array = jDoc.array();
refreshCounter = array.size();
for (int i = 0; i < array.size(); ++i) {
QJsonObject object = array[i].toObject();
updateOrder(object["gamekey"].toString());
}
}
void HumbleBundleAPI::parseProductList(const QByteArray & json)
{
QJsonDocument jDoc = QJsonDocument::fromJson(json);
QJsonObject object = jDoc.object();
int lastPurchaseId = -1;
Purchase purchase;
purchase.setType(object["product"].toObject()["category"].toString());
purchase.setIntName(object["product"].toObject()["machine_name"].toString());
purchase.setHumanName(object["product"].toObject()["human_name"].toString());
purchase.setHumbleId(object["gamekey"].toString());
lastPurchaseId = db.purchaseExists(purchase);
if(lastPurchaseId != -1){
db.updatePurchase(purchase);
} else {
lastPurchaseId = db.addPurchase(purchase);
}
parentApp->processEvents();
//Seperate Purchase into products
QJsonArray subProducts = object["subproducts"].toArray();
for (int i = 0; i < subProducts.size(); ++i) {
int lastProductId = -1;
QJsonObject jsonProduct = subProducts[i].toObject();
Product product;
product.setIconURL(jsonProduct["icon"].toString());
product.setHumanName(jsonProduct["human_name"].toString());
product.setIntName(jsonProduct["machine_name"].toString());
product.setPurchaseId(lastPurchaseId);
//Check if product matches valid product to add to DB.
if(jsonProduct["downloads"].toArray().size() == 0 || jsonProduct["library_family_name"].toString() == "hidden") {
continue;
}
lastProductId = db.productExists(product);
if(lastProductId != -1) {
db.updateProduct(product);
} else {
lastProductId = db.addProduct(product);
}
parentApp->processEvents();
QJsonArray downloadArray = jsonProduct["downloads"].toArray();
for (int j = 0; j < downloadArray.size(); ++j) {
int lastDownloadId = -1;
QJsonObject jsonDownload = downloadArray[j].toObject();
Download download;
download.setIntName(jsonDownload["machine_name"].toString());
download.setPlatform(jsonDownload["platform"].toString());
download.setProductId(lastProductId);
lastDownloadId = db.downloadExists(download);
if(lastDownloadId != -1) {
db.updateDownload(download);
} else {
lastDownloadId = db.addDownload(download);
}
parentApp->processEvents();
QJsonArray fileArray = jsonDownload["download_struct"].toArray();
db.eraseFilesForDownload(lastDownloadId);
for(int k = 0; k < fileArray.size(); ++k) {
QJsonObject jsonFile = fileArray[k].toObject();
File file;
file.setMD5(jsonFile["md5"].toString());
file.setName(jsonFile["name"].toString());
file.setWebURL(jsonFile["url"].toObject()["web"].toString());
file.setBitTorrentURL(jsonFile["url"].toObject()["bittorrent"].toString());
file.setFileSize(jsonFile["file_size"].toInt());
file.setDownloadId(lastDownloadId);
db.addFile(file);
parentApp->processEvents();
}
}
}
completedParsingOnePurchase();
}
void HumbleBundleAPI::completedParsingOnePurchase()
{
refreshCounter--;
if(refreshCounter <= 0) {
emit orderListUpdated();
}
}
void HumbleBundleAPI::onFinished(QNetworkReply * reply)
{
const QString requestUrl = reply->url().toString();
const bool loggedInReply = requestUrl.startsWith(URLS::loginUrl);
const bool orderListReply = requestUrl.startsWith(URLS::orderListUrl);
const bool orderReply = requestUrl.startsWith(URLS::orderUrl.arg(""));
if (reply->error() != QNetworkReply::NoError) {
if (loggedInReply || reply->error() == QNetworkReply::AuthenticationRequiredError) {
isLoggedIn_ = false;
emit loginError(reply->url().toString() +"error(" + reply->error() +"): " + reply->errorString() + "content: " + reply->readAll());
}
qDebug() << reply->url() << "error(" << reply->error() <<"): " << reply->errorString() << "content: " << reply->readAll();
return;
}
if (loggedInReply) {
isLoggedIn_ = true;
//Find session token
for (const QNetworkCookie & cookie:networkAccessManager_->cookieJar()->cookiesForUrl(requestUrl)) {
if(cookie.name() == "_simpleauth_sess") {
QString editedCookie = cookie.value();
editedCookie = editedCookie.replace("\"","");
editedCookie = editedCookie.replace("\\075","=");
Settings::setSessionToken(editedCookie);
}
}
emit loginSucceeded();
} else if (orderListReply) {
qDebug() << "Updating purchase list.";
parseOrderList(reply->readAll());
} else if (orderReply) {
qDebug() << "Updating product";
parseProductList(reply->readAll());
} else {
// qDebug() << reply->url() << reply->readAll();
}
}