Fix songkicks queries.
- When parsing a response, use fromJson instead of fromBinaryData. fromBinaryData expects a serialized binary format. - Calling toString on a non-string JSON value will return an empty string. Call toVariant().toString() to do the conversion. - Add checks for network reply errors.
This commit is contained in:
parent
61de3c6e93
commit
b3ec030ae4
|
@ -69,7 +69,21 @@ void SongkickConcerts::FetchInfo(int id, const Song& metadata) {
|
||||||
void SongkickConcerts::ArtistSearchFinished(QNetworkReply* reply, int id) {
|
void SongkickConcerts::ArtistSearchFinished(QNetworkReply* reply, int id) {
|
||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
|
|
||||||
QJsonDocument document = QJsonDocument::fromBinaryData(reply->readAll());
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
QJsonObject json = document.object();
|
QJsonObject json = document.object();
|
||||||
|
|
||||||
QJsonObject results_page = json["resultsPage"].toObject();
|
QJsonObject results_page = json["resultsPage"].toObject();
|
||||||
|
@ -77,13 +91,15 @@ void SongkickConcerts::ArtistSearchFinished(QNetworkReply* reply, int id) {
|
||||||
QJsonArray artists = results["artist"].toArray();
|
QJsonArray artists = results["artist"].toArray();
|
||||||
|
|
||||||
if (artists.isEmpty()) {
|
if (artists.isEmpty()) {
|
||||||
|
qLog(Debug) << "No artist found in songkick results.";
|
||||||
emit Finished(id);
|
emit Finished(id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
QJsonObject artist = artists.first().toObject();
|
QJsonObject artist = artists.first().toObject();
|
||||||
QString artist_id = artist["id"].toString();
|
// 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();
|
||||||
FetchSongkickCalendar(artist_id, id);
|
FetchSongkickCalendar(artist_id, id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,11 +118,18 @@ void SongkickConcerts::FetchSongkickCalendar(const QString& artist_id, int id) {
|
||||||
void SongkickConcerts::CalendarRequestFinished(QNetworkReply* reply, int id) {
|
void SongkickConcerts::CalendarRequestFinished(QNetworkReply* reply, int id) {
|
||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
|
|
||||||
|
if (reply->error() != QNetworkReply::NoError) {
|
||||||
|
qLog(Debug) << "Songkick request error" << reply->errorString();
|
||||||
|
emit Finished(id);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
QJsonParseError error;
|
QJsonParseError error;
|
||||||
QJsonDocument json_document = QJsonDocument::fromJson(reply->readAll(), &error);
|
QJsonDocument json_document = QJsonDocument::fromJson(reply->readAll(), &error);
|
||||||
|
|
||||||
if (error.error != QJsonParseError::NoError) {
|
if (error.error != QJsonParseError::NoError) {
|
||||||
qLog(Error) << "Error parsing Songkick reply";
|
qLog(Error) << "Error parsing Songkick calendar reply:"
|
||||||
|
<< error.errorString();
|
||||||
emit Finished(id);
|
emit Finished(id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -117,6 +140,7 @@ void SongkickConcerts::CalendarRequestFinished(QNetworkReply* reply, int id) {
|
||||||
QJsonArray json_events = json_results["event"].toArray();
|
QJsonArray json_events = json_results["event"].toArray();
|
||||||
|
|
||||||
if (json_events.isEmpty()) {
|
if (json_events.isEmpty()) {
|
||||||
|
qLog(Debug) << "No events found in songkick results.";
|
||||||
emit Finished(id);
|
emit Finished(id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -130,22 +154,27 @@ void SongkickConcerts::CalendarRequestFinished(QNetworkReply* reply, int id) {
|
||||||
QString start_date = json_event["start"].toObject()["date"].toString();
|
QString start_date = json_event["start"].toObject()["date"].toString();
|
||||||
QString city = json_event["location"].toObject()["city"].toString();
|
QString city = json_event["location"].toObject()["city"].toString();
|
||||||
QString uri = json_event["uri"].toString();
|
QString uri = json_event["uri"].toString();
|
||||||
|
QString lat;
|
||||||
|
QString lng;
|
||||||
|
|
||||||
// Try to get the lat/lng coordinates of the venue.
|
// Try to get the lat/lng coordinates of the venue.
|
||||||
QJsonObject json_venue = json_event["venue"].toObject();
|
QJsonObject json_venue = json_event["venue"].toObject();
|
||||||
const bool valid_latlng =
|
const bool valid_latlng =
|
||||||
json_venue.contains("lng") && json_venue.contains("lat");
|
json_venue.contains("lng") && json_venue.contains("lat");
|
||||||
|
|
||||||
if (valid_latlng && latlng_.IsValid()) {
|
if (valid_latlng) {
|
||||||
static const int kFilterDistanceMetres = 250 * 1e3; // 250km
|
lat = json_venue["lat"].toVariant().toString();
|
||||||
Geolocator::LatLng latlng(json_venue["lat"].toString(),
|
lng = json_venue["lng"].toVariant().toString();
|
||||||
json_venue["lng"].toString());
|
if (latlng_.IsValid()) {
|
||||||
if (latlng_.IsValid() && latlng.IsValid()) {
|
static const int kFilterDistanceMetres = 250 * 1e3; // 250km
|
||||||
int distance_metres = latlng_.Distance(latlng);
|
Geolocator::LatLng latlng(lat, lng);
|
||||||
if (distance_metres > kFilterDistanceMetres) {
|
if (latlng_.IsValid() && latlng.IsValid()) {
|
||||||
qLog(Debug) << "Filtered concert:" << display_name
|
int distance_metres = latlng_.Distance(latlng);
|
||||||
<< "as too far away:" << distance_metres;
|
if (distance_metres > kFilterDistanceMetres) {
|
||||||
continue;
|
qLog(Debug) << "Filtered concert:" << display_name
|
||||||
|
<< "as too far away:" << distance_metres;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -154,8 +183,7 @@ void SongkickConcerts::CalendarRequestFinished(QNetworkReply* reply, int id) {
|
||||||
widget->Init(display_name, uri, start_date, city);
|
widget->Init(display_name, uri, start_date, city);
|
||||||
|
|
||||||
if (valid_latlng) {
|
if (valid_latlng) {
|
||||||
widget->SetMap(json_venue["lat"].toString(), json_venue["lng"].toString(),
|
widget->SetMap(lat, lng, json_venue["displayName"].toString());
|
||||||
json_venue["displayName"].toString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
layout->addWidget(widget);
|
layout->addWidget(widget);
|
||||||
|
|
Loading…
Reference in New Issue