2479: Working Url traversal
Url checking is now working as expected. In set order, starting with custom sets, then going in priority order by url template order, Urls will checked, and the first successful one will be used.
This commit is contained in:
parent
8239539946
commit
cbd430555f
2 changed files with 81 additions and 45 deletions
|
@ -45,19 +45,39 @@ public:
|
||||||
|
|
||||||
PictureToLoad::PictureToLoad(CardInfoPtr _card) :
|
PictureToLoad::PictureToLoad(CardInfoPtr _card) :
|
||||||
card(std::move(_card)),
|
card(std::move(_card)),
|
||||||
customSetPicturesChecked(false),
|
|
||||||
urlInitialized(false),
|
|
||||||
setIndex(0),
|
setIndex(0),
|
||||||
urlIndex(0)
|
urlIndex(0)
|
||||||
{
|
{
|
||||||
if (card) {
|
|
||||||
sortedSets = card->getSets();
|
|
||||||
qSort(sortedSets.begin(), sortedSets.end(), SetDownloadPriorityComparator());
|
|
||||||
}
|
|
||||||
|
|
||||||
// This will be replaced with an expandable list ideally
|
// This will be replaced with an expandable list ideally
|
||||||
urlTemplates.append(settingsCache->getPicUrl());
|
urlTemplates.append(settingsCache->getPicUrl());
|
||||||
urlTemplates.append(settingsCache->getPicUrlFallback());
|
urlTemplates.append(settingsCache->getPicUrlFallback());
|
||||||
|
|
||||||
|
if (card) {
|
||||||
|
sortedSets = card->getSets();
|
||||||
|
qSort(sortedSets.begin(), sortedSets.end(), SetDownloadPriorityComparator());
|
||||||
|
populateSetUrls();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void PictureToLoad::populateSetUrls()
|
||||||
|
{
|
||||||
|
currentSetUrls.clear();
|
||||||
|
|
||||||
|
QString setCustomURL = card->getCustomPicURL(getCurrentSet()->getShortName());
|
||||||
|
|
||||||
|
if (!setCustomURL.isEmpty()) {
|
||||||
|
currentSetUrls.append(setCustomURL);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i; i< urlTemplates.size(); i++) {
|
||||||
|
QString transformedUrl = transformUrl(urlTemplates[i]);
|
||||||
|
|
||||||
|
if (!transformedUrl.isEmpty()) {
|
||||||
|
currentSetUrls.append(transformedUrl);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
urlIndex = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PictureToLoad::nextSet()
|
bool PictureToLoad::nextSet()
|
||||||
|
@ -65,40 +85,52 @@ bool PictureToLoad::nextSet()
|
||||||
if (setIndex == sortedSets.size() - 1)
|
if (setIndex == sortedSets.size() - 1)
|
||||||
return false;
|
return false;
|
||||||
++setIndex;
|
++setIndex;
|
||||||
customSetPicturesChecked = false;
|
populateSetUrls();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PictureToLoad::nextUrl()
|
bool PictureToLoad::nextUrl()
|
||||||
{
|
{
|
||||||
if (!customSetPicturesChecked) {
|
/* If we are past the list of urls currently populated
|
||||||
customSetPicturesChecked = true;
|
* for this set, try to move to the next set. This
|
||||||
|
* will repopulate the list of urls.
|
||||||
QString setCustomURL = card->getCustomPicURL(getCurrentSet()->getShortName());
|
*/
|
||||||
|
if (urlIndex == currentSetUrls.size() - 1) {
|
||||||
if (!setCustomURL.isEmpty()) {
|
if (nextSet()){
|
||||||
currentUrl = setCustomURL;
|
// There is a new set to check, so test the Url again
|
||||||
return true;
|
// UrlIndex would have been reset inside nextSet()
|
||||||
|
if (urlIndex == currentSetUrls.size() - 1) {
|
||||||
|
// The newly populated set did not yield any usable Urls.
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Set was updated, UrlIndex is reset, proceed checking Urls.
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Because there is not another set, there are not more Urls to check.
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
for ( ; urlIndex < urlTemplates.size(); ) {
|
{
|
||||||
currentUrl = transformUrl();
|
// We are still in the middle of the list, increment and return
|
||||||
urlIndex++;
|
// This must be inside the else to protect against UrlIndex being incremented
|
||||||
|
// when the set is moved to the next set.
|
||||||
if (!currentUrl.isEmpty())
|
++urlIndex;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString PictureToLoad::getCurrentUrl()
|
QString PictureToLoad::getCurrentUrl() const
|
||||||
{
|
{
|
||||||
if (!urlInitialized)
|
if (urlIndex < currentSetUrls.size())
|
||||||
nextUrl();
|
return currentSetUrls[urlIndex];
|
||||||
|
else
|
||||||
return currentUrl;
|
return QString("");
|
||||||
}
|
}
|
||||||
|
|
||||||
QString PictureToLoad::getSetName() const
|
QString PictureToLoad::getSetName() const
|
||||||
|
@ -163,13 +195,13 @@ void PictureLoaderWorker::processLoadQueue()
|
||||||
QString setName = cardBeingLoaded.getSetName();
|
QString setName = cardBeingLoaded.getSetName();
|
||||||
QString cardName = cardBeingLoaded.getCard()->getName();
|
QString cardName = cardBeingLoaded.getCard()->getName();
|
||||||
QString correctedCardName = cardBeingLoaded.getCard()->getCorrectedName();
|
QString correctedCardName = cardBeingLoaded.getCard()->getCorrectedName();
|
||||||
qDebug() << "Trying to load picture (set: " << setName << " card: " << cardName << ")";
|
qDebug() << "Trying to load picture for card:" << cardName << " from set:" << setName;
|
||||||
|
|
||||||
if (cardImageExistsOnDisk(setName, correctedCardName))
|
if (cardImageExistsOnDisk(setName, correctedCardName))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (picDownload) {
|
if (picDownload) {
|
||||||
qDebug() << "Picture NOT found, trying to download (set: " << setName << " card: " << cardName << ")";
|
qDebug() << "Picture not found, trying to download";
|
||||||
cardsToDownload.append(cardBeingLoaded);
|
cardsToDownload.append(cardBeingLoaded);
|
||||||
cardBeingLoaded.clear();
|
cardBeingLoaded.clear();
|
||||||
if (!downloadRunning)
|
if (!downloadRunning)
|
||||||
|
@ -239,9 +271,9 @@ bool PictureLoaderWorker::cardImageExistsOnDisk(QString &setName, QString &corre
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString PictureToLoad::transformUrl() const
|
QString PictureToLoad::transformUrl(QString urlTemplate) const
|
||||||
{
|
{
|
||||||
QString transformedUrl = urlTemplates[urlIndex];
|
QString transformedUrl = urlTemplate;
|
||||||
CardSetPtr set = getCurrentSet();
|
CardSetPtr set = getCurrentSet();
|
||||||
|
|
||||||
// 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
|
||||||
|
@ -257,6 +289,7 @@ QString PictureToLoad::transformUrl() const
|
||||||
// renamed from !setnumber! to !collectornumber! on 20160819. Remove the old one when convenient.
|
// renamed from !setnumber! to !collectornumber! on 20160819. Remove the old one when convenient.
|
||||||
transformedUrl.replace("!setnumber!", QUrl::toPercentEncoding(card->getCollectorNumber(set->getShortName())));
|
transformedUrl.replace("!setnumber!", QUrl::toPercentEncoding(card->getCollectorNumber(set->getShortName())));
|
||||||
transformedUrl.replace("!collectornumber!", 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!", QUrl::toPercentEncoding(set->getShortName()));
|
||||||
transformedUrl.replace("!setcode_lower!", QUrl::toPercentEncoding(set->getShortName().toLower()));
|
transformedUrl.replace("!setcode_lower!", QUrl::toPercentEncoding(set->getShortName().toLower()));
|
||||||
transformedUrl.replace("!setname!", QUrl::toPercentEncoding(set->getLongName()));
|
transformedUrl.replace("!setname!", QUrl::toPercentEncoding(set->getLongName()));
|
||||||
|
@ -301,7 +334,8 @@ void PictureLoaderWorker::startNextPicDownload()
|
||||||
QUrl url(picUrl); // 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().toString();
|
qDebug() << "Trying to download picture for card:" << cardBeingDownloaded.getCard()->getName()
|
||||||
|
<< " from set:" << cardBeingDownloaded.getSetName() << "from url:" << picUrl;
|
||||||
networkManager->get(req);
|
networkManager->get(req);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -309,14 +343,15 @@ void PictureLoaderWorker::startNextPicDownload()
|
||||||
void PictureLoaderWorker::picDownloadFailed()
|
void PictureLoaderWorker::picDownloadFailed()
|
||||||
{
|
{
|
||||||
if (cardBeingDownloaded.nextUrl()) {
|
if (cardBeingDownloaded.nextUrl()) {
|
||||||
qDebug() << "Picture NOT found, download failed, moving to next url (newurl: " << cardBeingDownloaded.getCurrentUrl() << "set: "
|
//qDebug() << "Picture NOT found, download failed, moving to next url (url: " << cardBeingDownloaded.getCurrentUrl()
|
||||||
<< cardBeingDownloaded.getSetName() << " card: " << cardBeingDownloaded.getCard()->getName() << ")";
|
// << " set: " << 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 url combinations to try: BAILING OUT (oldset: "
|
qDebug() << "Picture NOT found, download failed, no more url combinations to try: BAILING OUT for card: "
|
||||||
<< cardBeingDownloaded.getSetName() << " card: " << cardBeingDownloaded.getCard()->getName() << ")";
|
<< cardBeingDownloaded.getCard()->getName() << ")";
|
||||||
imageLoaded(cardBeingDownloaded.getCard(), QImage());
|
imageLoaded(cardBeingDownloaded.getCard(), QImage());
|
||||||
cardBeingDownloaded.clear();
|
cardBeingDownloaded.clear();
|
||||||
}
|
}
|
||||||
|
@ -339,7 +374,7 @@ void PictureLoaderWorker::picDownloadFinished(QNetworkReply *reply)
|
||||||
if (statusCode == 301 || statusCode == 302) {
|
if (statusCode == 301 || statusCode == 302) {
|
||||||
QUrl redirectUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
|
QUrl redirectUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
|
||||||
QNetworkRequest req(redirectUrl);
|
QNetworkRequest req(redirectUrl);
|
||||||
qDebug() << "following redirect:" << cardBeingDownloaded.getCard()->getName() << "Url:" << req.url();
|
qDebug() << "following redirect:" << cardBeingDownloaded.getCard()->getName() << "Url:" << req.url().toString();
|
||||||
networkManager->get(req);
|
networkManager->get(req);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -382,7 +417,9 @@ void PictureLoaderWorker::picDownloadFinished(QNetworkReply *reply)
|
||||||
}
|
}
|
||||||
|
|
||||||
imageLoaded(cardBeingDownloaded.getCard(), testImage);
|
imageLoaded(cardBeingDownloaded.getCard(), testImage);
|
||||||
|
qDebug() << "Image successfully downloaded from " << reply->request().url().toString();
|
||||||
} else {
|
} else {
|
||||||
|
qDebug() << "Possible picture at " << reply->request().url().toString() << " could not be loaded";
|
||||||
picDownloadFailed();
|
picDownloadFailed();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,9 +19,7 @@ private:
|
||||||
CardInfoPtr card;
|
CardInfoPtr card;
|
||||||
QList<CardSetPtr> sortedSets;
|
QList<CardSetPtr> sortedSets;
|
||||||
QList<QString> urlTemplates;
|
QList<QString> urlTemplates;
|
||||||
QString currentUrl;
|
QList<QString> currentSetUrls;
|
||||||
bool customSetPicturesChecked;
|
|
||||||
bool urlInitialized;
|
|
||||||
int setIndex;
|
int setIndex;
|
||||||
int urlIndex;
|
int urlIndex;
|
||||||
|
|
||||||
|
@ -36,11 +34,12 @@ public:
|
||||||
card.clear();
|
card.clear();
|
||||||
}
|
}
|
||||||
CardSetPtr getCurrentSet() const;
|
CardSetPtr getCurrentSet() const;
|
||||||
QString getCurrentUrl();
|
QString getCurrentUrl() const;
|
||||||
QString getSetName() const;
|
QString getSetName() const;
|
||||||
QString transformUrl() const;
|
QString transformUrl(QString urlTemplate) const;
|
||||||
bool nextSet();
|
bool nextSet();
|
||||||
bool nextUrl();
|
bool nextUrl();
|
||||||
|
void populateSetUrls();
|
||||||
};
|
};
|
||||||
|
|
||||||
class PictureLoaderWorker : public QObject
|
class PictureLoaderWorker : public QObject
|
||||||
|
|
Loading…
Reference in a new issue