2010-10-02 18:23:33 +02:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-10-02 18:23:33 +02:00
|
|
|
|
|
|
|
Clementine is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Clementine is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2010-10-10 23:45:01 +02:00
|
|
|
#include "echonestbiographies.h"
|
2014-02-06 14:48:00 +01:00
|
|
|
|
|
|
|
#include <memory>
|
2010-10-02 18:23:33 +02:00
|
|
|
|
|
|
|
#include <echonest/Artist.h>
|
|
|
|
|
2014-02-06 14:48:00 +01:00
|
|
|
#include "songinfotextview.h"
|
|
|
|
#include "core/logging.h"
|
2015-10-14 03:01:08 +02:00
|
|
|
#include "ui/iconloader.h"
|
2010-10-02 18:23:33 +02:00
|
|
|
|
2010-10-10 23:45:01 +02:00
|
|
|
struct EchoNestBiographies::Request {
|
2010-10-02 18:23:33 +02:00
|
|
|
Request(int id) : id_(id), artist_(new Echonest::Artist) {}
|
|
|
|
|
|
|
|
int id_;
|
2014-02-06 14:48:00 +01:00
|
|
|
std::unique_ptr<Echonest::Artist> artist_;
|
2010-10-02 18:23:33 +02:00
|
|
|
};
|
|
|
|
|
2010-10-10 23:45:01 +02:00
|
|
|
EchoNestBiographies::EchoNestBiographies() {
|
2010-10-09 14:39:49 +02:00
|
|
|
site_relevance_["wikipedia"] = 100;
|
|
|
|
site_relevance_["lastfm"] = 60;
|
|
|
|
site_relevance_["amazon"] = 30;
|
|
|
|
|
2015-10-14 03:01:08 +02:00
|
|
|
site_icons_["amazon"] = IconLoader::Load("amazon", IconLoader::Provider);
|
|
|
|
site_icons_["aol"] = IconLoader::Load("aol", IconLoader::Provider);
|
|
|
|
site_icons_["cdbaby"] = IconLoader::Load("cdbaby", IconLoader::Provider);
|
|
|
|
site_icons_["lastfm"] = IconLoader::Load("as", IconLoader::Lastfm);
|
|
|
|
site_icons_["mog"] = IconLoader::Load("mog", IconLoader::Provider);
|
|
|
|
site_icons_["mtvmusic"] = IconLoader::Load("mtvmusic", IconLoader::Provider);
|
|
|
|
site_icons_["myspace"] = IconLoader::Load("myspace", IconLoader::Provider);
|
|
|
|
site_icons_["wikipedia"] = IconLoader::Load("wikipedia", IconLoader::Provider);
|
2010-10-02 18:23:33 +02:00
|
|
|
}
|
|
|
|
|
2010-10-10 23:45:01 +02:00
|
|
|
void EchoNestBiographies::FetchInfo(int id, const Song& metadata) {
|
2014-02-06 14:48:00 +01:00
|
|
|
std::shared_ptr<Request> request(new Request(id));
|
2010-10-10 18:09:20 +02:00
|
|
|
request->artist_->setName(metadata.artist());
|
2010-10-02 18:23:33 +02:00
|
|
|
|
2010-10-10 23:45:01 +02:00
|
|
|
QNetworkReply* reply = request->artist_->fetchBiographies();
|
|
|
|
connect(reply, SIGNAL(finished()), SLOT(RequestFinished()));
|
|
|
|
requests_[reply] = request;
|
2010-10-02 18:23:33 +02:00
|
|
|
}
|
|
|
|
|
2010-10-10 23:45:01 +02:00
|
|
|
void EchoNestBiographies::RequestFinished() {
|
|
|
|
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!reply || !requests_.contains(reply)) return;
|
2010-10-02 18:23:33 +02:00
|
|
|
reply->deleteLater();
|
|
|
|
|
2010-10-10 23:45:01 +02:00
|
|
|
RequestPtr request = requests_.take(reply);
|
2010-10-09 12:32:18 +02:00
|
|
|
|
2010-10-10 23:45:01 +02:00
|
|
|
try {
|
|
|
|
request->artist_->parseProfile(reply);
|
2014-02-07 16:34:20 +01:00
|
|
|
}
|
|
|
|
catch (Echonest::ParseError e) {
|
|
|
|
qLog(Warning) << "Error parsing echonest reply:" << e.errorType()
|
|
|
|
<< e.what();
|
2010-10-02 18:23:33 +02:00
|
|
|
}
|
|
|
|
|
2010-10-09 14:39:49 +02:00
|
|
|
QSet<QString> already_seen;
|
|
|
|
|
2014-02-06 14:48:00 +01:00
|
|
|
for (const Echonest::Biography& bio : request->artist_->biographies()) {
|
2010-10-09 15:34:28 +02:00
|
|
|
QString canonical_site = bio.site().toLower();
|
2014-02-07 16:34:20 +01:00
|
|
|
canonical_site.replace(QRegExp("[^a-z]"), "");
|
2010-10-09 15:34:28 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (already_seen.contains(canonical_site)) continue;
|
2010-10-09 15:34:28 +02:00
|
|
|
already_seen.insert(canonical_site);
|
2010-10-09 14:39:49 +02:00
|
|
|
|
|
|
|
CollapsibleInfoPane::Data data;
|
2010-10-11 21:49:12 +02:00
|
|
|
data.id_ = "echonest/bio/" + bio.site();
|
2010-10-09 14:39:49 +02:00
|
|
|
data.title_ = tr("Biography from %1").arg(bio.site());
|
|
|
|
data.type_ = CollapsibleInfoPane::Data::Type_Biography;
|
|
|
|
|
2010-10-09 15:34:28 +02:00
|
|
|
if (site_relevance_.contains(canonical_site))
|
|
|
|
data.relevance_ = site_relevance_[canonical_site];
|
|
|
|
if (site_icons_.contains(canonical_site))
|
|
|
|
data.icon_ = site_icons_[canonical_site];
|
2010-10-09 14:39:49 +02:00
|
|
|
|
2010-10-16 16:11:23 +02:00
|
|
|
SongInfoTextView* editor = new SongInfoTextView;
|
2013-12-28 00:10:11 +01:00
|
|
|
QString text;
|
|
|
|
// Add a link to the bio webpage at the top if we have one
|
|
|
|
if (!bio.url().isEmpty()) {
|
2014-12-27 00:47:24 +01:00
|
|
|
QString bio_url = bio.url().toEncoded();
|
|
|
|
if (bio.site() == "facebook") {
|
|
|
|
bio_url.replace("graph.facebook.com", "www.facebook.com");
|
|
|
|
}
|
|
|
|
text += "<p><a href=\"" + bio_url + "\">" +
|
2014-02-07 16:34:20 +01:00
|
|
|
tr("Open in your browser") + "</a></p>";
|
2013-12-28 00:10:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
text += bio.text();
|
2011-07-26 23:55:39 +02:00
|
|
|
if (bio.site() == "last.fm") {
|
2014-02-07 16:34:20 +01:00
|
|
|
// Echonest lost formatting and it seems there is currently no plans on
|
|
|
|
// Echonest side for changing this.
|
2011-07-26 23:55:39 +02:00
|
|
|
// But with last.fm, we can guess newlines: " " corresponds to a newline
|
|
|
|
// (this seems to be because on last.fm' website, extra blank is inserted
|
|
|
|
// before <br /> tag, and this blank is kept).
|
2014-02-07 16:34:20 +01:00
|
|
|
// This is tricky, but this make the display nicer for last.fm
|
|
|
|
// biographies.
|
|
|
|
text.replace(" ", "<p>");
|
2011-07-26 23:55:39 +02:00
|
|
|
}
|
2013-12-28 00:10:11 +01:00
|
|
|
editor->SetHtml(text);
|
2010-10-09 14:39:49 +02:00
|
|
|
data.contents_ = editor;
|
2010-10-02 18:23:33 +02:00
|
|
|
|
2010-10-09 14:39:49 +02:00
|
|
|
emit InfoReady(request->id_, data);
|
2010-10-02 18:23:33 +02:00
|
|
|
}
|
2010-10-09 15:34:28 +02:00
|
|
|
|
2010-10-10 23:45:01 +02:00
|
|
|
emit Finished(request->id_);
|
2010-10-02 18:23:33 +02:00
|
|
|
}
|