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"
|
2010-10-16 16:11:23 +02:00
|
|
|
#include "songinfotextview.h"
|
2011-04-22 18:50:29 +02:00
|
|
|
#include "core/logging.h"
|
2010-10-02 18:23:33 +02:00
|
|
|
|
|
|
|
#include <echonest/Artist.h>
|
|
|
|
|
|
|
|
#include <boost/scoped_ptr.hpp>
|
|
|
|
|
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_;
|
|
|
|
boost::scoped_ptr<Echonest::Artist> artist_;
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
site_icons_["amazon"] = QIcon(":/providers/amazon.png");
|
|
|
|
site_icons_["aol"] = QIcon(":/providers/aol.png");
|
|
|
|
site_icons_["cdbaby"] = QIcon(":/providers/cdbaby.png");
|
|
|
|
site_icons_["lastfm"] = QIcon(":/last.fm/as.png");
|
|
|
|
site_icons_["mog"] = QIcon(":/providers/mog.png");
|
|
|
|
site_icons_["mtvmusic"] = QIcon(":/providers/mtvmusic.png");
|
|
|
|
site_icons_["myspace"] = QIcon(":/providers/myspace.png");
|
|
|
|
site_icons_["wikipedia"] = QIcon(":/providers/wikipedia.png");
|
2010-10-02 18:23:33 +02:00
|
|
|
}
|
|
|
|
|
2010-10-10 23:45:01 +02:00
|
|
|
void EchoNestBiographies::FetchInfo(int id, const Song& metadata) {
|
2010-10-02 18:23:33 +02:00
|
|
|
boost::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());
|
|
|
|
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);
|
|
|
|
} catch (Echonest::ParseError e) {
|
2011-04-22 18:50:29 +02:00
|
|
|
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;
|
|
|
|
|
2010-10-02 18:23:33 +02:00
|
|
|
foreach (const Echonest::Biography& bio, request->artist_->biographies()) {
|
2010-10-09 15:34:28 +02:00
|
|
|
QString canonical_site = bio.site().toLower();
|
|
|
|
canonical_site.replace(QRegExp("[^a-z]"),"");
|
|
|
|
|
|
|
|
if (already_seen.contains(canonical_site))
|
2010-10-09 14:39:49 +02:00
|
|
|
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()) {
|
|
|
|
text += "<p><a href=\"" + bio.url().toEncoded() + "\">" +
|
|
|
|
tr("Open in your browser") +
|
|
|
|
"</a></p>";
|
|
|
|
}
|
|
|
|
|
|
|
|
text += bio.text();
|
2011-07-26 23:55:39 +02:00
|
|
|
if (bio.site() == "last.fm") {
|
|
|
|
// Echonest lost formatting and it seems there is currently no plans on Echonest side for changing this.
|
|
|
|
// 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).
|
|
|
|
// This is tricky, but this make the display nicer for last.fm biographies.
|
2013-12-28 00:10:11 +01:00
|
|
|
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
|
|
|
}
|