2479:
Initial work on downloading cards through fallback URLs. This change will add in the ability to proceed to the next URL should the first one fail. First trial shows that it's working, but needs more refinement.
This commit is contained in:
parent
e467698d72
commit
8239539946
2 changed files with 90 additions and 64 deletions
|
@ -43,12 +43,21 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
PictureToLoad::PictureToLoad(CardInfoPtr _card) : card(std::move(_card)), setIndex(0)
|
PictureToLoad::PictureToLoad(CardInfoPtr _card) :
|
||||||
|
card(std::move(_card)),
|
||||||
|
customSetPicturesChecked(false),
|
||||||
|
urlInitialized(false),
|
||||||
|
setIndex(0),
|
||||||
|
urlIndex(0)
|
||||||
{
|
{
|
||||||
if (card) {
|
if (card) {
|
||||||
sortedSets = card->getSets();
|
sortedSets = card->getSets();
|
||||||
qSort(sortedSets.begin(), sortedSets.end(), SetDownloadPriorityComparator());
|
qSort(sortedSets.begin(), sortedSets.end(), SetDownloadPriorityComparator());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This will be replaced with an expandable list ideally
|
||||||
|
urlTemplates.append(settingsCache->getPicUrl());
|
||||||
|
urlTemplates.append(settingsCache->getPicUrlFallback());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PictureToLoad::nextSet()
|
bool PictureToLoad::nextSet()
|
||||||
|
@ -56,9 +65,42 @@ bool PictureToLoad::nextSet()
|
||||||
if (setIndex == sortedSets.size() - 1)
|
if (setIndex == sortedSets.size() - 1)
|
||||||
return false;
|
return false;
|
||||||
++setIndex;
|
++setIndex;
|
||||||
|
customSetPicturesChecked = false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool PictureToLoad::nextUrl()
|
||||||
|
{
|
||||||
|
if (!customSetPicturesChecked) {
|
||||||
|
customSetPicturesChecked = true;
|
||||||
|
|
||||||
|
QString setCustomURL = card->getCustomPicURL(getCurrentSet()->getShortName());
|
||||||
|
|
||||||
|
if (!setCustomURL.isEmpty()) {
|
||||||
|
currentUrl = setCustomURL;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for ( ; urlIndex < urlTemplates.size(); ) {
|
||||||
|
currentUrl = transformUrl();
|
||||||
|
urlIndex++;
|
||||||
|
|
||||||
|
if (!currentUrl.isEmpty())
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString PictureToLoad::getCurrentUrl()
|
||||||
|
{
|
||||||
|
if (!urlInitialized)
|
||||||
|
nextUrl();
|
||||||
|
|
||||||
|
return currentUrl;
|
||||||
|
}
|
||||||
|
|
||||||
QString PictureToLoad::getSetName() const
|
QString PictureToLoad::getSetName() const
|
||||||
{
|
{
|
||||||
if (setIndex < sortedSets.size())
|
if (setIndex < sortedSets.size())
|
||||||
|
@ -197,70 +239,45 @@ bool PictureLoaderWorker::cardImageExistsOnDisk(QString &setName, QString &corre
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<QString> PictureLoaderWorker::getAllPicUrls()
|
QString PictureToLoad::transformUrl() const
|
||||||
{
|
{
|
||||||
if (!picDownload)
|
QString transformedUrl = urlTemplates[urlIndex];
|
||||||
return QList<QString>();
|
CardSetPtr set = getCurrentSet();
|
||||||
|
|
||||||
CardInfoPtr card = cardBeingDownloaded.getCard();
|
|
||||||
CardSetPtr set = cardBeingDownloaded.getCurrentSet();
|
|
||||||
QList<QString> urls = QList<QString>();
|
|
||||||
|
|
||||||
// if sets have been defined for the card, they can contain custom picUrls
|
|
||||||
if (set) {
|
|
||||||
QString setCustomURL = card->getCustomPicURL(set->getShortName());
|
|
||||||
|
|
||||||
if (!setCustomURL.isEmpty()) {
|
|
||||||
urls.append(setCustomURL);
|
|
||||||
return urls;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// if a card has a muid, use the default url; if not, use the fallback
|
// if a card has a muid, use the default url; if not, use the fallback
|
||||||
int muid = set ? card->getMuId(set->getShortName()) : 0;
|
int muid = set ? card->getMuId(set->getShortName()) : 0;
|
||||||
|
|
||||||
QList<QString> urlArray = QList<QString>();
|
transformedUrl.replace("!name!", QUrl::toPercentEncoding(card->getName()));
|
||||||
urlArray.append(settingsCache->getPicUrl());
|
transformedUrl.replace("!name_lower!", QUrl::toPercentEncoding(card->getName().toLower()));
|
||||||
urlArray.append(settingsCache->getPicUrlFallback());
|
transformedUrl.replace("!corrected_name!", QUrl::toPercentEncoding(card->getCorrectedName()));
|
||||||
|
transformedUrl.replace("!corrected_name_lower!", QUrl::toPercentEncoding(card->getCorrectedName().toLower()));
|
||||||
for(int i=0; i < 2; i++) {
|
transformedUrl.replace("!cardid!", QUrl::toPercentEncoding(QString::number(muid)));
|
||||||
QString originalUrl = urlArray[i];
|
|
||||||
QString thisUrl = urlArray[i];
|
|
||||||
thisUrl.replace("!name!", QUrl::toPercentEncoding(card->getName()));
|
|
||||||
thisUrl.replace("!name_lower!", QUrl::toPercentEncoding(card->getName().toLower()));
|
|
||||||
thisUrl.replace("!corrected_name!", QUrl::toPercentEncoding(card->getCorrectedName()));
|
|
||||||
thisUrl.replace("!corrected_name_lower!", QUrl::toPercentEncoding(card->getCorrectedName().toLower()));
|
|
||||||
thisUrl.replace("!cardid!", QUrl::toPercentEncoding(QString::number(muid)));
|
|
||||||
if (set) {
|
|
||||||
// renamed from !setnumber! to !collectornumber! on 20160819. Remove the old one when convenient.
|
|
||||||
thisUrl.replace("!setnumber!", QUrl::toPercentEncoding(card->getCollectorNumber(set->getShortName())));
|
|
||||||
thisUrl.replace("!collectornumber!", QUrl::toPercentEncoding(card->getCollectorNumber(set->getShortName())));
|
|
||||||
|
|
||||||
thisUrl.replace("!setcode!", QUrl::toPercentEncoding(set->getShortName()));
|
|
||||||
thisUrl.replace("!setcode_lower!", QUrl::toPercentEncoding(set->getShortName().toLower()));
|
|
||||||
thisUrl.replace("!setname!", QUrl::toPercentEncoding(set->getLongName()));
|
|
||||||
thisUrl.replace("!setname_lower!", QUrl::toPercentEncoding(set->getLongName().toLower()));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (thisUrl.contains("!name!") ||
|
|
||||||
thisUrl.contains("!name_lower!") ||
|
|
||||||
thisUrl.contains("!corrected_name!") ||
|
|
||||||
thisUrl.contains("!corrected_name_lower!") ||
|
|
||||||
thisUrl.contains("!setnumber!") ||
|
|
||||||
thisUrl.contains("!setcode!") ||
|
|
||||||
thisUrl.contains("!setcode_lower!") ||
|
|
||||||
thisUrl.contains("!setname!") ||
|
|
||||||
thisUrl.contains("!setname_lower!") ||
|
|
||||||
thisUrl.contains("!cardid!")) {
|
|
||||||
qDebug() << "Insufficient card data to download" << card->getName() << "Url:" << originalUrl;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
urls.append(thisUrl);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (set) {
|
||||||
|
// renamed from !setnumber! to !collectornumber! on 20160819. Remove the old one when convenient.
|
||||||
|
transformedUrl.replace("!setnumber!", QUrl::toPercentEncoding(card->getCollectorNumber(set->getShortName())));
|
||||||
|
transformedUrl.replace("!collectornumber!", QUrl::toPercentEncoding(card->getCollectorNumber(set->getShortName())));
|
||||||
|
transformedUrl.replace("!setcode!", QUrl::toPercentEncoding(set->getShortName()));
|
||||||
|
transformedUrl.replace("!setcode_lower!", QUrl::toPercentEncoding(set->getShortName().toLower()));
|
||||||
|
transformedUrl.replace("!setname!", QUrl::toPercentEncoding(set->getLongName()));
|
||||||
|
transformedUrl.replace("!setname_lower!", QUrl::toPercentEncoding(set->getLongName().toLower()));
|
||||||
}
|
}
|
||||||
|
|
||||||
return urls;
|
if (transformedUrl.contains("!name!") ||
|
||||||
|
transformedUrl.contains("!name_lower!") ||
|
||||||
|
transformedUrl.contains("!corrected_name!") ||
|
||||||
|
transformedUrl.contains("!corrected_name_lower!") ||
|
||||||
|
transformedUrl.contains("!setnumber!") ||
|
||||||
|
transformedUrl.contains("!setcode!") ||
|
||||||
|
transformedUrl.contains("!setcode_lower!") ||
|
||||||
|
transformedUrl.contains("!setname!") ||
|
||||||
|
transformedUrl.contains("!setname_lower!") ||
|
||||||
|
transformedUrl.contains("!cardid!")) {
|
||||||
|
qDebug() << "Insufficient card data to download" << card->getName() << "Url:" << urlTemplates[urlIndex];
|
||||||
|
return QString();
|
||||||
|
}
|
||||||
|
|
||||||
|
return transformedUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PictureLoaderWorker::startNextPicDownload()
|
void PictureLoaderWorker::startNextPicDownload()
|
||||||
|
@ -275,29 +292,30 @@ void PictureLoaderWorker::startNextPicDownload()
|
||||||
|
|
||||||
cardBeingDownloaded = cardsToDownload.takeFirst();
|
cardBeingDownloaded = cardsToDownload.takeFirst();
|
||||||
|
|
||||||
QList<QString> picUrls = getAllPicUrls();
|
QString picUrl = cardBeingDownloaded.getCurrentUrl();
|
||||||
if (picUrls.isEmpty()) {
|
|
||||||
|
if (picUrl.isEmpty()) {
|
||||||
downloadRunning = false;
|
downloadRunning = false;
|
||||||
picDownloadFailed();
|
picDownloadFailed();
|
||||||
} else {
|
} else {
|
||||||
QUrl url(picUrls[0]); // For now, just use the first one, like always.
|
QUrl url(picUrl); // For now, just use the first one, like always.
|
||||||
|
|
||||||
QNetworkRequest req(url);
|
QNetworkRequest req(url);
|
||||||
qDebug() << "starting picture download:" << cardBeingDownloaded.getCard()->getName() << "Url:" << req.url();
|
qDebug() << "starting picture download:" << cardBeingDownloaded.getCard()->getName() << "Url:" << req.url().toString();
|
||||||
networkManager->get(req);
|
networkManager->get(req);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PictureLoaderWorker::picDownloadFailed()
|
void PictureLoaderWorker::picDownloadFailed()
|
||||||
{
|
{
|
||||||
if (cardBeingDownloaded.nextSet()) {
|
if (cardBeingDownloaded.nextUrl()) {
|
||||||
qDebug() << "Picture NOT found, download failed, moving to next set (newset: "
|
qDebug() << "Picture NOT found, download failed, moving to next url (newurl: " << cardBeingDownloaded.getCurrentUrl() << "set: "
|
||||||
<< cardBeingDownloaded.getSetName() << " card: " << cardBeingDownloaded.getCard()->getName() << ")";
|
<< cardBeingDownloaded.getSetName() << " card: " << cardBeingDownloaded.getCard()->getName() << ")";
|
||||||
mutex.lock();
|
mutex.lock();
|
||||||
loadQueue.prepend(cardBeingDownloaded);
|
loadQueue.prepend(cardBeingDownloaded);
|
||||||
mutex.unlock();
|
mutex.unlock();
|
||||||
} else {
|
} else {
|
||||||
qDebug() << "Picture NOT found, download failed, no more sets to try: BAILING OUT (oldset: "
|
qDebug() << "Picture NOT found, download failed, no more url combinations to try: BAILING OUT (oldset: "
|
||||||
<< cardBeingDownloaded.getSetName() << " card: " << cardBeingDownloaded.getCard()->getName() << ")";
|
<< cardBeingDownloaded.getSetName() << " card: " << cardBeingDownloaded.getCard()->getName() << ")";
|
||||||
imageLoaded(cardBeingDownloaded.getCard(), QImage());
|
imageLoaded(cardBeingDownloaded.getCard(), QImage());
|
||||||
cardBeingDownloaded.clear();
|
cardBeingDownloaded.clear();
|
||||||
|
|
|
@ -18,7 +18,12 @@ private:
|
||||||
|
|
||||||
CardInfoPtr card;
|
CardInfoPtr card;
|
||||||
QList<CardSetPtr> sortedSets;
|
QList<CardSetPtr> sortedSets;
|
||||||
|
QList<QString> urlTemplates;
|
||||||
|
QString currentUrl;
|
||||||
|
bool customSetPicturesChecked;
|
||||||
|
bool urlInitialized;
|
||||||
int setIndex;
|
int setIndex;
|
||||||
|
int urlIndex;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
PictureToLoad(CardInfoPtr _card = CardInfoPtr());
|
PictureToLoad(CardInfoPtr _card = CardInfoPtr());
|
||||||
|
@ -31,8 +36,11 @@ public:
|
||||||
card.clear();
|
card.clear();
|
||||||
}
|
}
|
||||||
CardSetPtr getCurrentSet() const;
|
CardSetPtr getCurrentSet() const;
|
||||||
|
QString getCurrentUrl();
|
||||||
QString getSetName() const;
|
QString getSetName() const;
|
||||||
|
QString transformUrl() const;
|
||||||
bool nextSet();
|
bool nextSet();
|
||||||
|
bool nextUrl();
|
||||||
};
|
};
|
||||||
|
|
||||||
class PictureLoaderWorker : public QObject
|
class PictureLoaderWorker : public QObject
|
||||||
|
|
Loading…
Reference in a new issue