2012-05-30 01:32:34 +02:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2012, David Sansome <me@davidsansome.com>
|
|
|
|
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
2012-05-30 01:31:27 +02:00
|
|
|
#include "songkickconcerts.h"
|
|
|
|
|
2012-06-04 14:40:08 +02:00
|
|
|
#include <QImage>
|
2012-08-27 13:24:28 +02:00
|
|
|
#include <QVBoxLayout>
|
2012-06-04 14:06:43 +02:00
|
|
|
#include <QXmlStreamWriter>
|
|
|
|
|
2012-05-30 01:31:27 +02:00
|
|
|
#include <echonest/Artist.h>
|
2016-02-11 17:59:11 +01:00
|
|
|
#include <echonest/TypeInformation.h>
|
2012-06-04 14:06:43 +02:00
|
|
|
|
|
|
|
#include <qjson/parser.h>
|
|
|
|
|
2012-05-30 01:31:27 +02:00
|
|
|
#include "core/closure.h"
|
2012-11-22 15:15:07 +01:00
|
|
|
#include "core/logging.h"
|
2012-08-27 13:24:28 +02:00
|
|
|
#include "songkickconcertwidget.h"
|
2015-10-14 03:01:08 +02:00
|
|
|
#include "ui/iconloader.h"
|
2012-05-30 01:31:27 +02:00
|
|
|
|
2016-02-11 17:59:11 +01:00
|
|
|
const char* SongkickConcerts::kSongkickArtistBucket = "songkick";
|
2012-05-30 01:31:27 +02:00
|
|
|
const char* SongkickConcerts::kSongkickArtistCalendarUrl =
|
2014-01-28 12:57:05 +01:00
|
|
|
"https://api.songkick.com/api/3.0/artists/%1/calendar.json?"
|
2012-05-30 22:30:02 +02:00
|
|
|
"per_page=5&"
|
|
|
|
"apikey=8rgKfy1WU6IlJFfN";
|
2012-05-30 01:31:27 +02:00
|
|
|
|
|
|
|
SongkickConcerts::SongkickConcerts() {
|
2012-06-20 14:12:37 +02:00
|
|
|
Geolocator* geolocator = new Geolocator;
|
|
|
|
geolocator->Geolocate();
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(geolocator, SIGNAL(Finished(Geolocator::LatLng)),
|
|
|
|
SLOT(GeolocateFinished(Geolocator::LatLng)));
|
|
|
|
NewClosure(geolocator, SIGNAL(Finished(Geolocator::LatLng)), geolocator,
|
|
|
|
SLOT(deleteLater()));
|
2012-05-30 01:31:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SongkickConcerts::FetchInfo(int id, const Song& metadata) {
|
|
|
|
Echonest::Artist::SearchParams params;
|
2014-02-07 16:34:20 +01:00
|
|
|
params.push_back(
|
|
|
|
qMakePair(Echonest::Artist::Name, QVariant(metadata.artist())));
|
2012-05-30 01:31:27 +02:00
|
|
|
qLog(Debug) << "Params:" << params;
|
2016-02-11 17:59:11 +01:00
|
|
|
QNetworkReply* reply = Echonest::Artist::search(
|
|
|
|
params,
|
|
|
|
Echonest::ArtistInformation(Echonest::ArtistInformation::NoInformation,
|
|
|
|
QStringList() << kSongkickArtistBucket));
|
2012-05-30 01:31:27 +02:00
|
|
|
qLog(Debug) << reply->request().url();
|
2014-02-07 16:34:20 +01:00
|
|
|
NewClosure(reply, SIGNAL(finished()), this,
|
|
|
|
SLOT(ArtistSearchFinished(QNetworkReply*, int)), reply, id);
|
2012-05-30 01:31:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SongkickConcerts::ArtistSearchFinished(QNetworkReply* reply, int id) {
|
|
|
|
reply->deleteLater();
|
|
|
|
try {
|
|
|
|
Echonest::Artists artists = Echonest::Artist::parseSearch(reply);
|
|
|
|
if (artists.isEmpty()) {
|
|
|
|
qLog(Debug) << "Failed to find artist in echonest";
|
2012-08-27 11:49:54 +02:00
|
|
|
emit Finished(id);
|
2012-05-30 01:31:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Echonest::Artist& artist = artists[0];
|
|
|
|
const Echonest::ForeignIds& foreign_ids = artist.foreignIds();
|
|
|
|
QString songkick_id;
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const Echonest::ForeignId& id : foreign_ids) {
|
2012-05-30 01:31:27 +02:00
|
|
|
if (id.catalog == "songkick") {
|
|
|
|
songkick_id = id.foreign_id;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (songkick_id.isEmpty()) {
|
|
|
|
qLog(Debug) << "Failed to fetch songkick foreign id for artist";
|
2012-08-27 11:49:54 +02:00
|
|
|
emit Finished(id);
|
2012-05-30 01:31:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-05-30 22:30:02 +02:00
|
|
|
QStringList split = songkick_id.split(':');
|
|
|
|
if (split.count() != 3) {
|
|
|
|
qLog(Error) << "Weird songkick id";
|
2012-08-27 11:49:54 +02:00
|
|
|
emit Finished(id);
|
2012-05-30 22:30:02 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
FetchSongkickCalendar(split[2], id);
|
2016-02-11 17:59:11 +01:00
|
|
|
} catch (Echonest::ParseError& e) {
|
2012-05-30 01:31:27 +02:00
|
|
|
qLog(Error) << "Error parsing echonest reply:" << e.errorType() << e.what();
|
2012-08-27 11:49:54 +02:00
|
|
|
emit Finished(id);
|
2012-05-30 01:31:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SongkickConcerts::FetchSongkickCalendar(const QString& artist_id, int id) {
|
|
|
|
QUrl url(QString(kSongkickArtistCalendarUrl).arg(artist_id));
|
2012-05-30 22:30:02 +02:00
|
|
|
qLog(Debug) << url;
|
|
|
|
QNetworkReply* reply = network_.get(QNetworkRequest(url));
|
2014-02-07 16:34:20 +01:00
|
|
|
NewClosure(reply, SIGNAL(finished()), this,
|
|
|
|
SLOT(CalendarRequestFinished(QNetworkReply*, int)), reply, id);
|
2012-05-30 01:31:27 +02:00
|
|
|
}
|
|
|
|
|
2012-05-30 22:30:02 +02:00
|
|
|
void SongkickConcerts::CalendarRequestFinished(QNetworkReply* reply, int id) {
|
2012-06-04 14:06:43 +02:00
|
|
|
QJson::Parser parser;
|
|
|
|
bool ok = false;
|
|
|
|
QVariant result = parser.parse(reply, &ok);
|
|
|
|
|
|
|
|
if (!ok) {
|
|
|
|
qLog(Error) << "Error parsing Songkick reply";
|
2012-08-27 11:49:54 +02:00
|
|
|
emit Finished(id);
|
2012-06-04 14:06:43 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariantMap root = result.toMap();
|
|
|
|
QVariantMap results_page = root["resultsPage"].toMap();
|
|
|
|
QVariantMap results = results_page["results"].toMap();
|
|
|
|
QVariantList events = results["event"].toList();
|
2012-08-27 11:49:54 +02:00
|
|
|
|
|
|
|
if (events.isEmpty()) {
|
|
|
|
emit Finished(id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-27 13:24:28 +02:00
|
|
|
QWidget* container = new QWidget;
|
|
|
|
QVBoxLayout* layout = new QVBoxLayout(container);
|
|
|
|
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const QVariant& v : events) {
|
2012-06-04 14:06:43 +02:00
|
|
|
QVariantMap event = v.toMap();
|
2012-08-27 13:24:28 +02:00
|
|
|
QString display_name = event["displayName"].toString();
|
|
|
|
QString start_date = event["start"].toMap()["date"].toString();
|
|
|
|
QString city = event["location"].toMap()["city"].toString();
|
|
|
|
QString uri = event["uri"].toString();
|
|
|
|
|
|
|
|
// Try to get the lat/lng coordinates of the venue.
|
|
|
|
QVariantMap venue = event["venue"].toMap();
|
2014-02-07 16:34:20 +01:00
|
|
|
const bool valid_latlng = venue["lng"].isValid() && venue["lat"].isValid();
|
2012-08-27 13:24:28 +02:00
|
|
|
|
|
|
|
if (valid_latlng && latlng_.IsValid()) {
|
|
|
|
static const int kFilterDistanceMetres = 250 * 1e3; // 250km
|
2014-02-07 16:34:20 +01:00
|
|
|
Geolocator::LatLng latlng(venue["lat"].toString(),
|
|
|
|
venue["lng"].toString());
|
2012-08-27 13:24:28 +02:00
|
|
|
if (latlng_.IsValid() && latlng.IsValid()) {
|
|
|
|
int distance_metres = latlng_.Distance(latlng);
|
|
|
|
if (distance_metres > kFilterDistanceMetres) {
|
2014-02-07 16:34:20 +01:00
|
|
|
qLog(Debug) << "Filtered concert:" << display_name
|
|
|
|
<< "as too far away:" << distance_metres;
|
2012-08-27 13:24:28 +02:00
|
|
|
continue;
|
2012-06-20 14:12:37 +02:00
|
|
|
}
|
|
|
|
}
|
2012-08-27 13:24:28 +02:00
|
|
|
}
|
2012-06-20 14:12:37 +02:00
|
|
|
|
2012-08-27 13:24:28 +02:00
|
|
|
SongKickConcertWidget* widget = new SongKickConcertWidget(container);
|
|
|
|
widget->Init(display_name, uri, start_date, city);
|
|
|
|
|
|
|
|
if (valid_latlng) {
|
|
|
|
widget->SetMap(venue["lat"].toString(), venue["lng"].toString(),
|
|
|
|
venue["displayName"].toString());
|
2012-06-04 14:06:43 +02:00
|
|
|
}
|
2012-08-27 13:24:28 +02:00
|
|
|
|
|
|
|
layout->addWidget(widget);
|
2012-06-04 14:06:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CollapsibleInfoPane::Data data;
|
|
|
|
data.type_ = CollapsibleInfoPane::Data::Type_Biography;
|
|
|
|
data.id_ = QString("songkick/%1").arg(id);
|
|
|
|
data.title_ = tr("Upcoming Concerts");
|
2015-10-14 03:01:08 +02:00
|
|
|
data.icon_ = IconLoader::Load("songkick", IconLoader::Provider);
|
2012-08-27 13:24:28 +02:00
|
|
|
data.contents_ = container;
|
2012-06-04 14:06:43 +02:00
|
|
|
|
|
|
|
emit InfoReady(id, data);
|
|
|
|
emit Finished(id);
|
2012-05-30 01:31:27 +02:00
|
|
|
}
|
2012-06-04 14:40:08 +02:00
|
|
|
|
2012-06-20 14:12:37 +02:00
|
|
|
void SongkickConcerts::GeolocateFinished(Geolocator::LatLng latlng) {
|
|
|
|
latlng_ = latlng;
|
|
|
|
}
|