parent
f30db4b070
commit
de046cdf40
|
@ -26,6 +26,7 @@
|
||||||
#include "core/logging.h"
|
#include "core/logging.h"
|
||||||
#include "core/network.h"
|
#include "core/network.h"
|
||||||
#include "songinfo/songinfotextview.h"
|
#include "songinfo/songinfotextview.h"
|
||||||
|
#include "ui/iconloader.h"
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
const char* kArtistBioUrl = "https://data.clementine-player.org/fetchbio";
|
const char* kArtistBioUrl = "https://data.clementine-player.org/fetchbio";
|
||||||
|
@ -35,6 +36,9 @@ const char* kWikipediaImageListUrl =
|
||||||
const char* kWikipediaImageInfoUrl =
|
const char* kWikipediaImageInfoUrl =
|
||||||
"https://%1.wikipedia.org/w/"
|
"https://%1.wikipedia.org/w/"
|
||||||
"api.php?action=query&prop=imageinfo&iiprop=url|size&format=json";
|
"api.php?action=query&prop=imageinfo&iiprop=url|size&format=json";
|
||||||
|
const char* kWikipediaExtractUrl =
|
||||||
|
"https://%1.wikipedia.org/w/"
|
||||||
|
"api.php?action=query&format=json&prop=extracts";
|
||||||
const int kMinimumImageSize = 400;
|
const int kMinimumImageSize = 400;
|
||||||
|
|
||||||
QString GetLocale() {
|
QString GetLocale() {
|
||||||
|
@ -72,6 +76,14 @@ void ArtistBiography::FetchInfo(int id, const Song& metadata) {
|
||||||
QString body = response["articleBody"].toString();
|
QString body = response["articleBody"].toString();
|
||||||
QString url = response["url"].toString();
|
QString url = response["url"].toString();
|
||||||
|
|
||||||
|
CountdownLatch* latch = new CountdownLatch;
|
||||||
|
|
||||||
|
if (url.contains("wikipedia.org")) {
|
||||||
|
FetchWikipediaImages(id, url, latch);
|
||||||
|
FetchWikipediaArticle(id, url, latch);
|
||||||
|
} else {
|
||||||
|
latch->Wait();
|
||||||
|
// Use the simple article body from KG.
|
||||||
if (!body.isEmpty()) {
|
if (!body.isEmpty()) {
|
||||||
CollapsibleInfoPane::Data data;
|
CollapsibleInfoPane::Data data;
|
||||||
data.id_ = url;
|
data.id_ = url;
|
||||||
|
@ -88,12 +100,13 @@ void ArtistBiography::FetchInfo(int id, const Song& metadata) {
|
||||||
data.contents_ = editor;
|
data.contents_ = editor;
|
||||||
emit InfoReady(id, data);
|
emit InfoReady(id, data);
|
||||||
}
|
}
|
||||||
|
latch->CountDown();
|
||||||
if (url.contains("wikipedia.org")) {
|
|
||||||
FetchWikipediaImages(id, url);
|
|
||||||
} else {
|
|
||||||
emit Finished(id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
NewClosure(latch, SIGNAL(Done()), [this, id, latch]() {
|
||||||
|
latch->deleteLater();
|
||||||
|
emit Finished(id);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -143,10 +156,25 @@ QUrl ExtractImageUrl(const QVariantMap& json) {
|
||||||
return QUrl();
|
return QUrl();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString ExtractExtract(const QVariantMap& json) {
|
||||||
|
for (auto it = json.constBegin(); it != json.constEnd(); ++it) {
|
||||||
|
if (it.value().type() == QVariant::Map) {
|
||||||
|
QString extract = ExtractExtract(it.value().toMap());
|
||||||
|
if (!extract.isEmpty()) {
|
||||||
|
return extract;
|
||||||
|
}
|
||||||
|
} else if (it.key() == "extract") {
|
||||||
|
return it.value().toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return QString::null;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
void ArtistBiography::FetchWikipediaImages(int id,
|
void ArtistBiography::FetchWikipediaImages(int id, const QString& wikipedia_url,
|
||||||
const QString& wikipedia_url) {
|
CountdownLatch* latch) {
|
||||||
|
latch->Wait();
|
||||||
qLog(Debug) << wikipedia_url;
|
qLog(Debug) << wikipedia_url;
|
||||||
QRegExp regex("([a-z]+)\\.wikipedia\\.org/wiki/(.*)");
|
QRegExp regex("([a-z]+)\\.wikipedia\\.org/wiki/(.*)");
|
||||||
if (regex.indexIn(wikipedia_url) == -1) {
|
if (regex.indexIn(wikipedia_url) == -1) {
|
||||||
|
@ -162,7 +190,7 @@ void ArtistBiography::FetchWikipediaImages(int id,
|
||||||
|
|
||||||
QNetworkRequest request(url);
|
QNetworkRequest request(url);
|
||||||
QNetworkReply* reply = network_->get(request);
|
QNetworkReply* reply = network_->get(request);
|
||||||
NewClosure(reply, SIGNAL(finished()), [this, id, reply, language]() {
|
NewClosure(reply, SIGNAL(finished()), [this, id, reply, language, latch]() {
|
||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
|
|
||||||
QJson::Parser parser;
|
QJson::Parser parser;
|
||||||
|
@ -170,12 +198,6 @@ void ArtistBiography::FetchWikipediaImages(int id,
|
||||||
|
|
||||||
QStringList image_titles = ExtractImageTitles(response);
|
QStringList image_titles = ExtractImageTitles(response);
|
||||||
|
|
||||||
CountdownLatch* latch = new CountdownLatch;
|
|
||||||
NewClosure(latch, SIGNAL(Done()), [this, latch, id]() {
|
|
||||||
latch->deleteLater();
|
|
||||||
emit Finished(id);
|
|
||||||
});
|
|
||||||
|
|
||||||
for (const QString& image_title : image_titles) {
|
for (const QString& image_title : image_titles) {
|
||||||
latch->Wait();
|
latch->Wait();
|
||||||
QUrl url(QString(kWikipediaImageInfoUrl).arg(language));
|
QUrl url(QString(kWikipediaImageInfoUrl).arg(language));
|
||||||
|
@ -196,5 +218,53 @@ void ArtistBiography::FetchWikipediaImages(int id,
|
||||||
latch->CountDown();
|
latch->CountDown();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
latch->CountDown();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void ArtistBiography::FetchWikipediaArticle(int id,
|
||||||
|
const QString& wikipedia_url,
|
||||||
|
CountdownLatch* latch) {
|
||||||
|
latch->Wait();
|
||||||
|
QRegExp regex("([a-z]+)\\.wikipedia\\.org/wiki/(.*)");
|
||||||
|
if (regex.indexIn(wikipedia_url) == -1) {
|
||||||
|
emit Finished(id);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
QString wiki_title = QUrl::fromPercentEncoding(regex.cap(2).toUtf8());
|
||||||
|
QString language = regex.cap(1);
|
||||||
|
|
||||||
|
QUrl url(QString(kWikipediaExtractUrl).arg(language));
|
||||||
|
url.addQueryItem("titles", wiki_title);
|
||||||
|
QNetworkRequest request(url);
|
||||||
|
QNetworkReply* reply = network_->get(request);
|
||||||
|
|
||||||
|
qLog(Debug) << "Article url:" << url;
|
||||||
|
|
||||||
|
NewClosure(reply, SIGNAL(finished()),
|
||||||
|
[this, id, reply, wikipedia_url, latch]() {
|
||||||
|
reply->deleteLater();
|
||||||
|
|
||||||
|
QJson::Parser parser;
|
||||||
|
QVariantMap json = parser.parse(reply).toMap();
|
||||||
|
QString html = ExtractExtract(json);
|
||||||
|
|
||||||
|
CollapsibleInfoPane::Data data;
|
||||||
|
data.id_ = wikipedia_url;
|
||||||
|
data.title_ = tr("Biography");
|
||||||
|
data.type_ = CollapsibleInfoPane::Data::Type_Biography;
|
||||||
|
data.icon_ = IconLoader::Load("wikipedia", IconLoader::Provider);
|
||||||
|
|
||||||
|
QString text;
|
||||||
|
text += "<p><a href=\"" + wikipedia_url + "\">" +
|
||||||
|
tr("Open in your browser") + "</a></p>";
|
||||||
|
|
||||||
|
text += html;
|
||||||
|
SongInfoTextView* editor = new SongInfoTextView;
|
||||||
|
editor->SetHtml(text);
|
||||||
|
data.contents_ = editor;
|
||||||
|
emit InfoReady(id, data);
|
||||||
|
latch->CountDown();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,7 @@
|
||||||
|
|
||||||
#include "songinfoprovider.h"
|
#include "songinfoprovider.h"
|
||||||
|
|
||||||
|
class CountdownLatch;
|
||||||
class NetworkAccessManager;
|
class NetworkAccessManager;
|
||||||
|
|
||||||
class ArtistBiography : public SongInfoProvider {
|
class ArtistBiography : public SongInfoProvider {
|
||||||
|
@ -34,7 +35,9 @@ class ArtistBiography : public SongInfoProvider {
|
||||||
void FetchInfo(int id, const Song& metadata) override;
|
void FetchInfo(int id, const Song& metadata) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void FetchWikipediaImages(int id, const QString& title);
|
void FetchWikipediaImages(int id, const QString& title,
|
||||||
|
CountdownLatch* latch);
|
||||||
|
void FetchWikipediaArticle(int id, const QString& url, CountdownLatch* latch);
|
||||||
|
|
||||||
std::unique_ptr<NetworkAccessManager> network_;
|
std::unique_ptr<NetworkAccessManager> network_;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue