1 #include <QNetworkAccessManager>
2 #include <QNetworkReply>
3 #include <QNetworkRequest>
6 #include <QWebElementCollection>
15 Search *Search::instance()
29 Search::Search(): QObject(0), reply(0), downloadReply(0)
31 manager = new QNetworkAccessManager(this);
32 downloadManager = new QNetworkAccessManager(this);
35 void Search::start(const Query &query)
37 Trace t("Search::start");
41 searchResults.clear();
42 QNetworkRequest request;
43 request.setUrl(QUrl("http://www.gutenberg.org/catalog/world/results"));
44 // request.setRawHeader("User-Agent", "Dorian " + Platform::version());
45 QString title = query.title;
46 if (title.isEmpty()) {
50 data = "title=" + QUrl::toPercentEncoding(title) + "&author=" +
51 QUrl::toPercentEncoding(query.author);
52 qDebug() << "Request:" << (request.url().toString() + "?" + data);
53 reply = manager->post(request, data);
54 connect(reply, SIGNAL(finished()), this, SLOT(finished()));
57 QList<Search::Result> Search::results()
62 bool Search::download(const Search::Result &result, const QString &fileName)
64 Trace t("Search::download");
65 qDebug() << "UID" << result.id;
67 emit beginDownload(0);
68 QUrl url("http://www.gutenberg.org/ebooks/" + result.id + ".epub");
69 qDebug() << "Requesting" << url;
70 QNetworkRequest request;
72 downloadReply = downloadManager->get(request);
73 connect(downloadReply, SIGNAL(finished()), this, SLOT(downloadFinished()));
77 void Search::finished()
79 Trace t("Search::finished");
85 QByteArray data = reply->readAll();
87 // Parse search results
90 QWebFrame *frame = page.mainFrame();
91 frame->setHtml(QString(data));
92 QWebElementCollection tables = frame->findAllElements("table");
93 if (tables.count() == 1) {
94 QWebElement table = tables[0];
95 foreach (QWebElement row, table.findAll("tr")) {
96 QWebElementCollection cols = row.findAll("td");
97 if (cols.count() < 5) {
100 QString id = cols[0].toPlainText().trimmed();
104 QString author = cols[2].toPlainText().trimmed();
105 QStringList titles = cols[3].toPlainText().trimmed().
106 split("\n", QString::SkipEmptyParts);
108 r.authors = author.split("\n", QString::SkipEmptyParts);
110 if (titles.count()) {
113 r.language = cols[4].toPlainText().trimmed();
114 r.source = "Project Gutenberg";
115 searchResults.append(r);
117 qDebug() << " Authors:" << r.authors;
118 qDebug() << " Title:" << r.title;
119 qDebug() << " Language:" << r.language;
123 reply->deleteLater();
128 void Search::downloadFinished()
130 Trace t("Search::downloadFinished");
132 if (!downloadReply) {
136 QVariant header = downloadReply->header(QNetworkRequest::LocationHeader);
137 if (header.isValid()) {
138 qDebug() << "Redirected to" << header;
139 downloadReply->deleteLater();
140 QNetworkRequest request;
141 request.setUrl(header.toUrl());
142 downloadReply = downloadManager->get(request);
143 connect(downloadReply, SIGNAL(finished()), this, SLOT(downloadFinished()));
145 QByteArray data = downloadReply->readAll();
146 qDebug() << "Got" << data.size() << "bytes";
147 downloadReply->deleteLater();