Split price retrieval on multiple request
This commit is contained in:
parent
bf92e67002
commit
b40abfa1ad
2 changed files with 64 additions and 13 deletions
|
@ -121,12 +121,15 @@ DBPriceUpdater::DBPriceUpdater(const DeckList *_deck)
|
||||||
*/
|
*/
|
||||||
void DBPriceUpdater::updatePrices()
|
void DBPriceUpdater::updatePrices()
|
||||||
{
|
{
|
||||||
QString q = "https://api.deckbrew.com/mtg/cards";
|
QString base = "https://api.deckbrew.com/mtg/cards", q = "";
|
||||||
QStringList cards = deck->getCardList();
|
QStringList cards = deck->getCardList();
|
||||||
muidMap.clear();
|
muidMap.clear();
|
||||||
|
urls.clear();
|
||||||
CardInfo * card;
|
CardInfo * card;
|
||||||
int muid;
|
int muid;
|
||||||
SetList sets;
|
SetList sets;
|
||||||
|
bool bNotFirst=false;
|
||||||
|
|
||||||
for (int i = 0; i < cards.size(); ++i) {
|
for (int i = 0; i < cards.size(); ++i) {
|
||||||
card = db->getCard(cards[i], false);
|
card = db->getCard(cards[i], false);
|
||||||
sets = card->getSets();
|
sets = card->getSets();
|
||||||
|
@ -134,11 +137,35 @@ void DBPriceUpdater::updatePrices()
|
||||||
{
|
{
|
||||||
muid=card->getMuId(sets[j]->getShortName());
|
muid=card->getMuId(sets[j]->getShortName());
|
||||||
//qDebug() << "muid " << muid << " card: " << cards[i] << endl;
|
//qDebug() << "muid " << muid << " card: " << cards[i] << endl;
|
||||||
q += (i ? "&" : "?") + QString("m=%1").arg(muid);
|
if(bNotFirst)
|
||||||
|
{
|
||||||
|
q += QString("&m=%1").arg(muid);
|
||||||
|
} else {
|
||||||
|
q += QString("?m=%1").arg(muid);
|
||||||
|
bNotFirst = true;
|
||||||
|
}
|
||||||
muidMap.insert(muid, cards[i]);
|
muidMap.insert(muid, cards[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(q.length() > 240)
|
||||||
|
{
|
||||||
|
urls.append(base + q);
|
||||||
|
bNotFirst=false;
|
||||||
|
q = "";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
QUrl url(q);
|
if(q.length() > 0)
|
||||||
|
urls.append(base + q);
|
||||||
|
|
||||||
|
requestNext();
|
||||||
|
}
|
||||||
|
|
||||||
|
void DBPriceUpdater::requestNext()
|
||||||
|
{
|
||||||
|
if(urls.empty())
|
||||||
|
return;
|
||||||
|
|
||||||
|
QUrl url(urls.takeFirst(), QUrl::TolerantMode);
|
||||||
//qDebug() << "request prices from: " << url.toString() << endl;
|
//qDebug() << "request prices from: " << url.toString() << endl;
|
||||||
QNetworkReply *reply = nam->get(QNetworkRequest(url));
|
QNetworkReply *reply = nam->get(QNetworkRequest(url));
|
||||||
connect(reply, SIGNAL(finished()), this, SLOT(downloadFinished()));
|
connect(reply, SIGNAL(finished()), this, SLOT(downloadFinished()));
|
||||||
|
@ -158,8 +185,13 @@ void DBPriceUpdater::downloadFinished()
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
QMessageBox::critical(this, tr("Error"), tr("A problem has occured while fetching card prices."));
|
QMessageBox::critical(this, tr("Error"), tr("A problem has occured while fetching card prices."));
|
||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
deleteLater();
|
if(urls.isEmpty())
|
||||||
return;
|
{
|
||||||
|
deleteLater();
|
||||||
|
emit finishedUpdate();
|
||||||
|
} else {
|
||||||
|
requestNext();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(resultMap.contains("errors"))
|
if(resultMap.contains("errors"))
|
||||||
|
@ -173,8 +205,13 @@ void DBPriceUpdater::downloadFinished()
|
||||||
#endif
|
#endif
|
||||||
);
|
);
|
||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
deleteLater();
|
if(urls.isEmpty())
|
||||||
return;
|
{
|
||||||
|
deleteLater();
|
||||||
|
emit finishedUpdate();
|
||||||
|
} else {
|
||||||
|
requestNext();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Good results are a list
|
// Good results are a list
|
||||||
|
@ -182,8 +219,13 @@ void DBPriceUpdater::downloadFinished()
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
QMessageBox::critical(this, tr("Error"), tr("A problem has occured while fetching card prices."));
|
QMessageBox::critical(this, tr("Error"), tr("A problem has occured while fetching card prices."));
|
||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
deleteLater();
|
if(urls.isEmpty())
|
||||||
return;
|
{
|
||||||
|
deleteLater();
|
||||||
|
emit finishedUpdate();
|
||||||
|
} else {
|
||||||
|
requestNext();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QMap<QString, float> cardsPrice;
|
QMap<QString, float> cardsPrice;
|
||||||
|
@ -226,11 +268,18 @@ void DBPriceUpdater::downloadFinished()
|
||||||
DecklistCardNode *currentCard = dynamic_cast<DecklistCardNode *>(currentZone->at(j));
|
DecklistCardNode *currentCard = dynamic_cast<DecklistCardNode *>(currentZone->at(j));
|
||||||
if (!currentCard)
|
if (!currentCard)
|
||||||
continue;
|
continue;
|
||||||
currentCard->setPrice(cardsPrice[currentCard->getName()]);
|
float price = cardsPrice[currentCard->getName()];
|
||||||
|
if(price > 0)
|
||||||
|
currentCard->setPrice(price);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
deleteLater();
|
if(urls.isEmpty())
|
||||||
emit finishedUpdate();
|
{
|
||||||
|
deleteLater();
|
||||||
|
emit finishedUpdate();
|
||||||
|
} else {
|
||||||
|
requestNext();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#ifndef PRICEUPDATER_H
|
#ifndef PRICEUPDATER_H
|
||||||
#define PRICEUPDATER_H
|
#define PRICEUPDATER_H
|
||||||
|
|
||||||
#include <QNetworkReply>
|
#include <QNetworkAccessManager>
|
||||||
#include "decklist.h"
|
#include "decklist.h"
|
||||||
|
|
||||||
class QNetworkAccessManager;
|
class QNetworkAccessManager;
|
||||||
|
@ -46,8 +46,10 @@ class DBPriceUpdater : public AbstractPriceUpdater
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
protected:
|
protected:
|
||||||
MuidStringMap muidMap;
|
MuidStringMap muidMap;
|
||||||
|
QList<QString> urls;
|
||||||
protected:
|
protected:
|
||||||
virtual void downloadFinished();
|
virtual void downloadFinished();
|
||||||
|
void requestNext();
|
||||||
public:
|
public:
|
||||||
DBPriceUpdater(const DeckList *deck);
|
DBPriceUpdater(const DeckList *deck);
|
||||||
virtual void updatePrices();
|
virtual void updatePrices();
|
||||||
|
|
Loading…
Reference in a new issue