new autodownloader partially done

This commit is contained in:
Max-Wilhelm Bruker 2011-02-11 16:05:43 +01:00
parent c20ca2d1e6
commit 3a5fce9613
5 changed files with 110 additions and 37 deletions

View file

@ -94,9 +94,9 @@ void PictureLoadingThread::run()
break;
if (image.load(QString("%1/%2/%3%4.full.jpg").arg(picsPath).arg(sortedSets[i]->getShortName()).arg(correctedName).arg(1)))
break;
if (image.load(QString("%1/%2/%3/%4.full.jpg").arg(picsPath).arg("downloadedPics").arg(sortedSets[i]->getShortName()).arg(correctedName)))
break;
}
if (image.isNull())
image.load(QString("%1/%2/%3.full.jpg").arg(picsPath).arg("downloadedPics").arg(correctedName));
emit imageLoaded(card, image);
}
@ -117,8 +117,8 @@ void PictureLoadingThread::setPicsPath(const QString &path)
_picsPath = path;
}
CardInfo::CardInfo(CardDatabase *_db, const QString &_name, const QString &_manacost, const QString &_cardtype, const QString &_powtough, const QString &_text, const QStringList &_colors, bool _cipt, int _tableRow, const SetList &_sets, const QMap<QString, QString> &_picURLs)
: db(_db), name(_name), sets(_sets), manacost(_manacost), cardtype(_cardtype), powtough(_powtough), text(_text), colors(_colors), picURLs(_picURLs), cipt(_cipt), tableRow(_tableRow), pixmap(NULL)
CardInfo::CardInfo(CardDatabase *_db, const QString &_name, const QString &_manacost, const QString &_cardtype, const QString &_powtough, const QString &_text, const QStringList &_colors, bool _cipt, int _tableRow, const SetList &_sets, const QMap<QString, QString> &_picURLs, const QMap<QString, QString> &_picURLsHq, const QMap<QString, QString> &_picURLsSt)
: db(_db), name(_name), sets(_sets), manacost(_manacost), cardtype(_cardtype), powtough(_powtough), text(_text), colors(_colors), picURLs(_picURLs), picURLsHq(_picURLsHq), picURLsSt(_picURLsSt), cipt(_cipt), tableRow(_tableRow), pixmap(NULL)
{
for (int i = 0; i < sets.size(); i++)
sets[i]->append(this);
@ -198,7 +198,7 @@ void CardInfo::imageLoaded(const QImage &image)
*pixmap = QPixmap::fromImage(image);
emit pixmapUpdated();
} else if (settingsCache->getPicDownload())
db->startPicDownload(this);
db->startPicDownload(this, false);
}
QPixmap *CardInfo::getPixmap(QSize size)
@ -267,6 +267,8 @@ QXmlStreamWriter &operator<<(QXmlStreamWriter &xml, const CardInfo *info)
for (int i = 0; i < sets.size(); i++) {
xml.writeStartElement("set");
xml.writeAttribute("picURL", info->getPicURL(sets[i]->getShortName()));
xml.writeAttribute("picURLHq", info->getPicURLHq(sets[i]->getShortName()));
xml.writeAttribute("picURLSt", info->getPicURLSt(sets[i]->getShortName()));
xml.writeCharacters(sets[i]->getShortName());
xml.writeEndElement();
}
@ -379,12 +381,16 @@ void CardDatabase::clearPixmapCache()
noCard->clearPixmapCache();
}
void CardDatabase::startPicDownload(CardInfo *card)
void CardDatabase::startPicDownload(CardInfo *card, bool stripped)
{
if (card->getPicURLs().isEmpty())
SetList sortedSets = card->getSets();
if (sortedSets.isEmpty())
return;
sortedSets.sortByKey();
for (int i = 0; i < sortedSets.size(); ++i)
qDebug() << sortedSets[i]->getShortName();
cardsToDownload.append(card);
cardsToDownload.append(PictureToDownload(card, stripped, sortedSets.first()->getShortName()));
if (!downloadRunning)
startNextPicDownload();
}
@ -400,7 +406,17 @@ void CardDatabase::startNextPicDownload()
downloadRunning = true;
cardBeingDownloaded = cardsToDownload.takeFirst();
QNetworkRequest req(QUrl(cardBeingDownloaded->getPicURL()));
QString picUrl;
if (cardBeingDownloaded.getStripped())
picUrl = cardBeingDownloaded.getCard()->getPicURLSt(cardBeingDownloaded.getSetName());
else if (cardBeingDownloaded.getHq())
picUrl = cardBeingDownloaded.getCard()->getPicURLHq(cardBeingDownloaded.getSetName());
else
picUrl = cardBeingDownloaded.getCard()->getPicURL(cardBeingDownloaded.getSetName());
QUrl url(picUrl);
QNetworkRequest req(url);
qDebug() << "starting picture download:" << req.url();
networkManager->get(req);
}
@ -416,13 +432,26 @@ void CardDatabase::picDownloadFinished(QNetworkReply *reply)
return;
dir.mkdir("downloadedPics");
}
QFile newPic(picsPath + "/downloadedPics/" + cardBeingDownloaded->getCorrectedName() + ".full.jpg");
if (!QDir(QString(picsPath + "/downloadedPics/" + cardBeingDownloaded.getSetName())).exists()) {
QDir dir(QString(picsPath + "/downloadedPics"));
dir.mkdir(cardBeingDownloaded.getSetName());
}
QString suffix;
if (!cardBeingDownloaded.getStripped())
suffix = ".full";
QFile newPic(picsPath + "/downloadedPics/" + cardBeingDownloaded.getSetName() + "/" + cardBeingDownloaded.getCard()->getCorrectedName() + suffix + ".jpg");
if (!newPic.open(QIODevice::WriteOnly))
return;
newPic.write(picData);
newPic.close();
cardBeingDownloaded->updatePixmapCache();
cardBeingDownloaded.getCard()->updatePixmapCache();
} else {
qDebug() << "Download finished, received invalid picture. URL:" << reply->request().url();
cardBeingDownloaded.setHq(false);
cardsToDownload.prepend(cardBeingDownloaded);
}
reply->deleteLater();
@ -457,7 +486,7 @@ void CardDatabase::loadCardsFromXml(QXmlStreamReader &xml)
if (xml.name() == "card") {
QString name, manacost, type, pt, text;
QStringList colors;
QMap<QString, QString> picURLs;
QMap<QString, QString> picURLs, picURLsHq, picURLsSt;
SetList sets;
int tableRow = 0;
bool cipt = false;
@ -475,10 +504,14 @@ void CardDatabase::loadCardsFromXml(QXmlStreamReader &xml)
else if (xml.name() == "text")
text = xml.readElementText();
else if (xml.name() == "set") {
QString url = xml.attributes().value("picURL").toString();
QString picURL = xml.attributes().value("picURL").toString();
QString picURLHq = xml.attributes().value("picURLHq").toString();
QString picURLSt = xml.attributes().value("picURLSt").toString();
QString setName = xml.readElementText();
sets.append(getSet(setName));
picURLs.insert(setName, url);
picURLs.insert(setName, picURL);
picURLsHq.insert(setName, picURLHq);
picURLsSt.insert(setName, picURLSt);
} else if (xml.name() == "color")
colors << xml.readElementText();
else if (xml.name() == "tablerow")
@ -486,7 +519,7 @@ void CardDatabase::loadCardsFromXml(QXmlStreamReader &xml)
else if (xml.name() == "cipt")
cipt = (xml.readElementText() == "1");
}
cardHash.insert(name, new CardInfo(this, name, manacost, type, pt, text, colors, cipt, tableRow, sets, picURLs));
cardHash.insert(name, new CardInfo(this, name, manacost, type, pt, text, colors, cipt, tableRow, sets, picURLs, picURLsHq, picURLsSt));
}
}
}
@ -561,6 +594,17 @@ bool CardDatabase::loadCardDatabase(const QString &path)
if (!path.isEmpty())
loadSuccess = loadFromFile(path);
else loadSuccess = false;
if (loadSuccess) {
SetList allSets;
QHashIterator<QString, CardSet *> setsIterator(setHash);
while (setsIterator.hasNext())
allSets.append(setsIterator.next().value());
allSets.sortByKey();
for (int i = 0; i < allSets.size(); ++i)
allSets[i]->setSortKey(i);
}
return loadSuccess;
}

View file

@ -66,7 +66,7 @@ private:
QString powtough;
QString text;
QStringList colors;
QMap<QString, QString> picURLs;
QMap<QString, QString> picURLs, picURLsHq, picURLsSt;
bool cipt;
int tableRow;
QPixmap *pixmap;
@ -82,7 +82,9 @@ public:
bool cipt = false,
int _tableRow = 0,
const SetList &_sets = SetList(),
const QMap<QString, QString> &_picURLs = QMap<QString, QString>());
const QMap<QString, QString> &_picURLs = QMap<QString, QString>(),
const QMap<QString, QString> &_picURLsHq = QMap<QString, QString>(),
const QMap<QString, QString> &_picURLsSt = QMap<QString, QString>());
~CardInfo();
const QString &getName() const { return name; }
const SetList &getSets() const { return sets; }
@ -94,6 +96,8 @@ public:
void setText(const QString &_text) { text = _text; }
const QStringList &getColors() const { return colors; }
QString getPicURL(const QString &set) const { return picURLs.value(set); }
QString getPicURLHq(const QString &set) const { return picURLsHq.value(set); }
QString getPicURLSt(const QString &set) const { return picURLsSt.value(set); }
QString getPicURL() const;
const QMap<QString, QString> &getPicURLs() const { return picURLs; }
QString getMainCardType() const;
@ -101,6 +105,8 @@ public:
int getTableRow() const { return tableRow; }
void setTableRow(int _tableRow) { tableRow = _tableRow; }
void setPicURL(const QString &_set, const QString &_picURL) { picURLs.insert(_set, _picURL); }
void setPicURLHq(const QString &_set, const QString &_picURL) { picURLsHq.insert(_set, _picURL); }
void setPicURLSt(const QString &_set, const QString &_picURL) { picURLsSt.insert(_set, _picURL); }
void addToSet(CardSet *set);
QPixmap *loadPixmap();
QPixmap *getPixmap(QSize size);
@ -113,14 +119,30 @@ signals:
void pixmapUpdated();
};
class PictureToDownload {
private:
CardInfo *card;
bool stripped;
QString setName;
bool hq;
public:
PictureToDownload(CardInfo *_card = 0, bool _stripped = false, const QString &_setName = QString(), bool _hq = true)
: card(_card), stripped(_stripped), setName(_setName), hq(_hq) { }
CardInfo *getCard() const { return card; }
bool getStripped() const { return stripped; }
QString getSetName() const { return setName; }
bool getHq() const { return hq; }
void setHq(bool _hq) { hq = _hq; }
};
class CardDatabase : public QObject {
Q_OBJECT
protected:
QHash<QString, CardInfo *> cardHash;
QHash<QString, CardSet *> setHash;
QNetworkAccessManager *networkManager;
QList<CardInfo *> cardsToDownload;
CardInfo *cardBeingDownloaded;
QList<PictureToDownload> cardsToDownload;
PictureToDownload cardBeingDownloaded;
bool downloadRunning;
bool loadSuccess;
CardInfo *noCard;
@ -139,7 +161,7 @@ public:
SetList getSetList() const;
bool loadFromFile(const QString &fileName);
bool saveToFile(const QString &fileName);
void startPicDownload(CardInfo *card);
void startPicDownload(CardInfo *card, bool stripped);
QStringList getAllColors() const;
QStringList getAllMainCardTypes() const;
bool getLoadSuccess() const { return loadSuccess; }

View file

@ -1,6 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<cockatrice_setdatabase version="20110126">
<picture_url>http://gatherer.wizards.com/Handlers/Image.ashx?multiverseid=%1&amp;type=card</picture_url>
<picture_url>http://gatherer.wizards.com/Handlers/Image.ashx?multiverseid=!cardid!&amp;type=card</picture_url>
<picture_url_hq>http://mtgpics.chutography.com/!set!/!name!.full.jpg</picture_url_hq>
<picture_url_st>http://mtgpics.chutography.com/!set!/!name!.jpg</picture_url_st>
<set_url>http://gatherer.wizards.com/Pages/Search/Default.aspx?output=spoiler&amp;method=text&amp;set=[&quot;!longname!&quot;]</set_url>
<set import="1">
<name>ARB</name>

View file

@ -3,6 +3,7 @@
#include <QtNetwork>
#include <QXmlStreamReader>
#include <QDomDocument>
#include <QDebug>
OracleImporter::OracleImporter(const QString &_dataDir, QObject *parent)
: CardDatabase(parent), dataDir(_dataDir), setIndex(-1)
@ -59,6 +60,10 @@ void OracleImporter::readSetsFromXml(QXmlStreamReader &xml)
edition = editionLong = editionURL = QString();
} else if (xml.name() == "picture_url")
pictureUrl = xml.readElementText();
else if (xml.name() == "picture_url_hq")
pictureUrlHq = xml.readElementText();
else if (xml.name() == "picture_url_st")
pictureUrlSt = xml.readElementText();
else if (xml.name() == "set_url")
setUrl = xml.readElementText();
}
@ -82,7 +87,7 @@ CardInfo *OracleImporter::addCard(const QString &setName, QString cardName, int
// Workaround for card name weirdness
if (cardName.contains("XX"))
cardName.remove("XX");
cardName = cardName.replace("Æ", "Ae");
cardName = cardName.replace("Æ", "AE");
bool mArtifact = false;
if (cardType.endsWith("Artifact"))
@ -122,7 +127,9 @@ CardInfo *OracleImporter::addCard(const QString &setName, QString cardName, int
cardHash.insert(cardName, card);
}
card->setPicURL(setName, pictureUrl.arg(cardId));
card->setPicURL(setName, getPictureUrl(pictureUrl, cardId, cardName, setName));
card->setPicURLHq(setName, getPictureUrl(pictureUrlHq, cardId, cardName, setName));
card->setPicURLSt(setName, getPictureUrl(pictureUrlSt, cardId, cardName, setName));
return card;
}
@ -145,7 +152,7 @@ int OracleImporter::importTextSpoiler(CardSet *set, const QByteArray &data)
QString errorMsg;
int errorLine, errorColumn;
if (!doc.setContent(bufferContents, &errorMsg, &errorLine, &errorColumn))
qDebug(QString("error: %1, line=%2, column=%3").arg(errorMsg).arg(errorLine).arg(errorColumn).toLatin1());
qDebug() << "error:" << errorMsg << "line:" << errorLine << "column:" << errorColumn;
QDomNodeList divs = doc.elementsByTagName("div");
for (int i = 0; i < divs.size(); ++i) {
@ -195,21 +202,19 @@ int OracleImporter::importTextSpoiler(CardSet *set, const QByteArray &data)
return cards;
}
QString OracleImporter::getURLFromName(QString name) const
QString OracleImporter::getPictureUrl(QString url, int cardId, QString name, const QString &setName) const
{
return pictureUrl.arg(
name
.replace("Æther", "Aether")
return url.replace("!cardid!", QString::number(cardId)).replace("!set!", setName).replace("!name!", name
.replace("ö", "o")
.remove('\'')
.remove("//")
.remove(',')
.remove(':')
.remove('.')
// .remove('\'')
.remove(" // ")
// .remove(',')
// .remove(':')
// .remove('.')
.remove(QRegExp("\\(.*\\)"))
.simplified()
.replace(' ', '_')
.replace('-', '_')
// .replace(' ', '_')
// .replace('-', '_')
);
}

View file

@ -25,13 +25,13 @@ class OracleImporter : public CardDatabase {
Q_OBJECT
private:
QList<SetToDownload> allSets, setsToDownload;
QString pictureUrl, setUrl;
QString pictureUrl, pictureUrlHq, pictureUrlSt, setUrl;
QString dataDir;
int setIndex;
int reqId;
QBuffer *buffer;
QHttp *http;
QString getURLFromName(QString name) const;
QString getPictureUrl(QString url, int cardId, QString name, const QString &setName) const;
void downloadNextFile();
void readSetsFromXml(QXmlStreamReader &xml);