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>
|
2020-09-18 16:15:19 +02:00
|
|
|
#include <QJsonArray>
|
2015-04-11 22:52:31 +02:00
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
2016-10-07 13:29:50 +02:00
|
|
|
#include <QUrl>
|
|
|
|
#include <QUrlQuery>
|
2020-09-18 16:15:19 +02:00
|
|
|
#include <QVBoxLayout>
|
|
|
|
#include <QXmlStreamWriter>
|
2012-06-04 14:06:43 +02:00
|
|
|
|
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-06-27 15:45:40 +02:00
|
|
|
namespace {
|
|
|
|
const char* kSongkickArtistCalendarUrl =
|
|
|
|
"https://api.songkick.com/api/3.0/artists/%1/calendar.json";
|
|
|
|
const char* kSongkickArtistSearchUrl =
|
|
|
|
"https://api.songkick.com/api/3.0/search/artists.json";
|
|
|
|
const char* kSongkickApiKey = "8rgKfy1WU6IlJFfN";
|
|
|
|
} // namespace
|
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)));
|
2016-06-27 15:45:40 +02:00
|
|
|
connect(geolocator, SIGNAL(Finished(Geolocator::LatLng)), geolocator,
|
|
|
|
SLOT(deleteLater()));
|
2012-05-30 01:31:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SongkickConcerts::FetchInfo(int id, const Song& metadata) {
|
2016-06-27 15:45:40 +02:00
|
|
|
if (metadata.artist().isEmpty()) {
|
|
|
|
emit Finished(id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QUrl url(kSongkickArtistSearchUrl);
|
2016-10-07 13:29:50 +02:00
|
|
|
QUrlQuery url_query;
|
|
|
|
url_query.addQueryItem("apikey", kSongkickApiKey);
|
|
|
|
url_query.addQueryItem("query", metadata.artist());
|
|
|
|
url.setQuery(url_query);
|
2016-06-27 15:45:40 +02:00
|
|
|
|
|
|
|
QNetworkRequest request(url);
|
|
|
|
QNetworkReply* reply = network_.get(request);
|
2020-02-13 08:44:08 +01:00
|
|
|
connect(reply, &QNetworkReply::finished,
|
|
|
|
[=] { this->ArtistSearchFinished(reply, id); });
|
2012-05-30 01:31:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SongkickConcerts::ArtistSearchFinished(QNetworkReply* reply, int id) {
|
|
|
|
reply->deleteLater();
|
2016-06-27 15:45:40 +02:00
|
|
|
|
2020-02-13 07:04:22 +01:00
|
|
|
if (reply->error() != QNetworkReply::NoError) {
|
|
|
|
qLog(Debug) << "Songkick request error" << reply->errorString();
|
|
|
|
emit Finished(id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonParseError error;
|
|
|
|
QJsonDocument document = QJsonDocument::fromJson(reply->readAll(), &error);
|
|
|
|
if (error.error != QJsonParseError::NoError) {
|
|
|
|
qLog(Error) << "Error parsing Songkick artist reply:"
|
|
|
|
<< error.errorString();
|
|
|
|
emit Finished(id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-07 13:29:50 +02:00
|
|
|
QJsonObject json = document.object();
|
2016-06-27 15:45:40 +02:00
|
|
|
|
2016-10-07 13:29:50 +02:00
|
|
|
QJsonObject results_page = json["resultsPage"].toObject();
|
|
|
|
QJsonObject results = results_page["results"].toObject();
|
|
|
|
QJsonArray artists = results["artist"].toArray();
|
2016-06-27 15:45:40 +02:00
|
|
|
|
|
|
|
if (artists.isEmpty()) {
|
2020-02-13 07:04:22 +01:00
|
|
|
qLog(Debug) << "No artist found in songkick results.";
|
2012-08-27 11:49:54 +02:00
|
|
|
emit Finished(id);
|
2016-06-27 15:45:40 +02:00
|
|
|
return;
|
2012-05-30 01:31:27 +02:00
|
|
|
}
|
2016-06-27 15:45:40 +02:00
|
|
|
|
2016-10-07 13:29:50 +02:00
|
|
|
QJsonObject artist = artists.first().toObject();
|
2020-02-13 07:04:22 +01:00
|
|
|
// Calling toString on a value that is not a string type will return an empty
|
|
|
|
// string. But QVariant will do the formatting.
|
|
|
|
QString artist_id = artist["id"].toVariant().toString();
|
2016-06-27 15:45:40 +02:00
|
|
|
FetchSongkickCalendar(artist_id, 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));
|
2016-10-07 13:29:50 +02:00
|
|
|
QUrlQuery url_query;
|
|
|
|
url_query.addQueryItem("per_page", "5");
|
|
|
|
url_query.addQueryItem("apikey", kSongkickApiKey);
|
|
|
|
url.setQuery(url_query);
|
2012-05-30 22:30:02 +02:00
|
|
|
qLog(Debug) << url;
|
|
|
|
QNetworkReply* reply = network_.get(QNetworkRequest(url));
|
2020-02-13 08:44:08 +01:00
|
|
|
connect(reply, &QNetworkReply::finished,
|
|
|
|
[=] { this->CalendarRequestFinished(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) {
|
2015-04-11 22:52:31 +02:00
|
|
|
reply->deleteLater();
|
|
|
|
|
2020-02-13 07:04:22 +01:00
|
|
|
if (reply->error() != QNetworkReply::NoError) {
|
|
|
|
qLog(Debug) << "Songkick request error" << reply->errorString();
|
|
|
|
emit Finished(id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-11 22:52:31 +02:00
|
|
|
QJsonParseError error;
|
2020-09-18 16:15:19 +02:00
|
|
|
QJsonDocument json_document =
|
|
|
|
QJsonDocument::fromJson(reply->readAll(), &error);
|
2012-06-04 14:06:43 +02:00
|
|
|
|
2015-04-11 22:52:31 +02:00
|
|
|
if (error.error != QJsonParseError::NoError) {
|
2020-02-13 07:04:22 +01:00
|
|
|
qLog(Error) << "Error parsing Songkick calendar reply:"
|
|
|
|
<< error.errorString();
|
2012-08-27 11:49:54 +02:00
|
|
|
emit Finished(id);
|
2012-06-04 14:06:43 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-11 22:52:31 +02:00
|
|
|
QJsonObject json_root = json_document.object();
|
|
|
|
QJsonObject json_results_page = json_root["resultsPage"].toObject();
|
|
|
|
QJsonObject json_results = json_results_page["results"].toObject();
|
|
|
|
QJsonArray json_events = json_results["event"].toArray();
|
2012-08-27 11:49:54 +02:00
|
|
|
|
2015-04-11 22:52:31 +02:00
|
|
|
if (json_events.isEmpty()) {
|
2020-02-13 07:04:22 +01:00
|
|
|
qLog(Debug) << "No events found in songkick results.";
|
2012-08-27 11:49:54 +02:00
|
|
|
emit Finished(id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-27 13:24:28 +02:00
|
|
|
QWidget* container = new QWidget;
|
|
|
|
QVBoxLayout* layout = new QVBoxLayout(container);
|
|
|
|
|
2015-04-11 22:52:31 +02:00
|
|
|
for (const QJsonValue& v : json_events) {
|
|
|
|
QJsonObject json_event = v.toObject();
|
|
|
|
QString display_name = json_event["displayName"].toString();
|
|
|
|
QString start_date = json_event["start"].toObject()["date"].toString();
|
|
|
|
QString city = json_event["location"].toObject()["city"].toString();
|
|
|
|
QString uri = json_event["uri"].toString();
|
2020-02-13 07:04:22 +01:00
|
|
|
QString lat;
|
|
|
|
QString lng;
|
2012-08-27 13:24:28 +02:00
|
|
|
|
|
|
|
// Try to get the lat/lng coordinates of the venue.
|
2015-04-11 22:52:31 +02:00
|
|
|
QJsonObject json_venue = json_event["venue"].toObject();
|
2020-02-13 04:52:03 +01:00
|
|
|
const bool valid_latlng =
|
|
|
|
json_venue.contains("lng") && json_venue.contains("lat");
|
2012-08-27 13:24:28 +02:00
|
|
|
|
2020-02-13 07:04:22 +01:00
|
|
|
if (valid_latlng) {
|
|
|
|
lat = json_venue["lat"].toVariant().toString();
|
|
|
|
lng = json_venue["lng"].toVariant().toString();
|
|
|
|
if (latlng_.IsValid()) {
|
|
|
|
static const int kFilterDistanceMetres = 250 * 1e3; // 250km
|
|
|
|
Geolocator::LatLng latlng(lat, lng);
|
|
|
|
if (latlng_.IsValid() && latlng.IsValid()) {
|
|
|
|
int distance_metres = latlng_.Distance(latlng);
|
|
|
|
if (distance_metres > kFilterDistanceMetres) {
|
|
|
|
qLog(Debug) << "Filtered concert:" << display_name
|
|
|
|
<< "as too far away:" << distance_metres;
|
|
|
|
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) {
|
2020-02-13 07:04:22 +01:00
|
|
|
widget->SetMap(lat, lng, json_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;
|
|
|
|
}
|