Add lyrics from lyrics.ovh

This commit is contained in:
Jonas Kvinge 2019-08-12 18:11:01 +02:00
parent 6aabf82c11
commit c3903a7b35
13 changed files with 293 additions and 56 deletions

View File

@ -20,7 +20,7 @@ Strawberry is a music player and music collection organizer. It is a fork of Cle
* Edit tags on music files
* Fetch tags from MusicBrainz
* Album cover art from Last.fm, Musicbrainz, Discogs, Deezer and Tidal
* Song lyrics from AudD
* Song lyrics from AudD and lyrics.ovh
* Support for multiple backends
* Audio analyzer
* Audio equalizer

2
debian/control vendored
View File

@ -58,7 +58,7 @@ Description: Audio player and music collection organizer
- Edit tags on music files
- Fetch tags from MusicBrainz
- Album cover art from Lastfm, Musicbrainz, Discogs, Deezer and Tidal
- Song lyrics from AudD
- Song lyrics from AudD and lyrics.ovh
- Support for multiple backends
- Audio analyzer
- Audio equalizer

View File

@ -27,7 +27,7 @@ Features:
.br
- Album cover art from Lastfm, Musicbrainz, Discogs, Deezer and Tidal
.br
- Song lyrics from AudD
- Song lyrics from AudD and lyrics.ovh
.br
- Support for multiple backends
.br

View File

@ -102,7 +102,7 @@ Features:
- Edit tags on music files
- Fetch tags from MusicBrainz
- Album cover art from Last.fm, Musicbrainz, Discogs, Deezer and Tidal
- Song lyrics from AudD
- Song lyrics from AudD and lyrics.ovh
- Support for multiple backends
- Audio analyzer
- Audio equalizer

View File

@ -29,7 +29,7 @@
<li>Edit tags on music files</li>
<li>Fetch tags from MusicBrainz</li>
<li>Album cover art from Last.fm, Musicbrainz and Discogs</li>
<li>Song lyrics from AudD</li>
<li>Song lyrics from AudD and lyrics.ovh</li>
<li>Support for multiple backends</li>
<li>Audio analyzer</li>
<li>Audio equalizer</li>

View File

@ -207,7 +207,9 @@ set(SOURCES
lyrics/lyricsprovider.cpp
lyrics/lyricsfetcher.cpp
lyrics/lyricsfetchersearch.cpp
lyrics/jsonlyricsprovider.cpp
lyrics/auddlyricsprovider.cpp
lyrics/ovhlyricsprovider.cpp
settings/settingsdialog.cpp
settings/settingspage.cpp
@ -388,7 +390,9 @@ set(HEADERS
lyrics/lyricsprovider.h
lyrics/lyricsfetcher.h
lyrics/lyricsfetchersearch.h
lyrics/jsonlyricsprovider.h
lyrics/auddlyricsprovider.h
lyrics/ovhlyricsprovider.h
settings/settingsdialog.h
settings/settingspage.h

View File

@ -59,6 +59,7 @@
#include "lyrics/lyricsproviders.h"
#include "lyrics/lyricsprovider.h"
#include "lyrics/auddlyricsprovider.h"
#include "lyrics/ovhlyricsprovider.h"
#include "scrobbler/audioscrobbler.h"
@ -135,6 +136,7 @@ class ApplicationImpl {
lyrics_providers_([=]() {
LyricsProviders *lyrics_providers = new LyricsProviders(app);
lyrics_providers->AddProvider(new AuddLyricsProvider(app));
lyrics_providers->AddProvider(new OVHLyricsProvider(app));
return lyrics_providers;
}),
internet_services_([=]() {

View File

@ -43,7 +43,7 @@
#include "core/logging.h"
#include "core/network.h"
#include "core/utilities.h"
#include "lyricsprovider.h"
#include "jsonlyricsprovider.h"
#include "lyricsfetcher.h"
#include "auddlyricsprovider.h"
@ -51,7 +51,7 @@ const char *AuddLyricsProvider::kUrlSearch = "https://api.audd.io/findLyrics/";
const char *AuddLyricsProvider::kAPITokenB64 = "ZjA0NjQ4YjgyNDM3ZTc1MjY3YjJlZDI5ZDBlMzQxZjk=";
const int AuddLyricsProvider::kMaxLength = 6000;
AuddLyricsProvider::AuddLyricsProvider(QObject *parent) : LyricsProvider("AudD", parent), network_(new NetworkAccessManager(this)) {}
AuddLyricsProvider::AuddLyricsProvider(QObject *parent) : JsonLyricsProvider("AudD", parent), network_(new NetworkAccessManager(this)) {}
bool AuddLyricsProvider::StartSearch(const QString &artist, const QString &album, const QString &title, const quint64 id) {
@ -74,8 +74,7 @@ bool AuddLyricsProvider::StartSearch(const QString &artist, const QString &album
}
void AuddLyricsProvider::CancelSearch(quint64 id) {
}
void AuddLyricsProvider::CancelSearch(quint64 id) {}
void AuddLyricsProvider::HandleSearchReply(QNetworkReply *reply, const quint64 id, const QString &artist, const QString &title) {
@ -127,50 +126,6 @@ void AuddLyricsProvider::HandleSearchReply(QNetworkReply *reply, const quint64 i
}
QJsonObject AuddLyricsProvider::ExtractJsonObj(QNetworkReply *reply, const quint64 id) {
if (reply->error() != QNetworkReply::NoError) {
QString failure_reason = QString("%1 (%2)").arg(reply->errorString()).arg(reply->error());
Error(id, failure_reason);
return QJsonObject();
}
if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
QString failure_reason = QString("Received HTTP code %1").arg(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt());
Error(id, failure_reason);
return QJsonObject();
}
QByteArray data(reply->readAll());
QJsonParseError error;
QJsonDocument json_doc = QJsonDocument::fromJson(data, &error);
if (error.error != QJsonParseError::NoError) {
Error(id, "Reply from server missing Json data.");
return QJsonObject();
}
if (json_doc.isNull() || json_doc.isEmpty()) {
Error(id, "Received empty Json document.");
return QJsonObject();
}
if (!json_doc.isObject()) {
Error(id, "Json document is not an object.");
return QJsonObject();
}
QJsonObject json_obj = json_doc.object();
if (json_obj.isEmpty()) {
Error(id, "Received empty Json object.");
return QJsonObject();
}
return json_obj;
}
QJsonArray AuddLyricsProvider::ExtractResult(QNetworkReply *reply, const quint64 id, const QString &artist, const QString &title) {
QJsonObject json_obj = ExtractJsonObj(reply, id);

View File

@ -31,10 +31,10 @@
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include "lyricsprovider.h"
#include "jsonlyricsprovider.h"
#include "lyricsfetcher.h"
class AuddLyricsProvider : public LyricsProvider {
class AuddLyricsProvider : public JsonLyricsProvider {
Q_OBJECT
public:
@ -53,7 +53,6 @@ class AuddLyricsProvider : public LyricsProvider {
QNetworkAccessManager *network_;
void Error(const quint64 id, const QString &error, QVariant debug = QVariant());
QJsonObject ExtractJsonObj(QNetworkReply *reply, const quint64 id);
QJsonArray ExtractResult(QNetworkReply *reply, const quint64 id, const QString &artist, const QString &title);
};

View File

@ -0,0 +1,78 @@
/*
* Strawberry Music Player
* Copyright 2019, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry 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.
*
* Strawberry 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 Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "config.h"
#include <QObject>
#include <QByteArray>
#include <QString>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QJsonDocument>
#include <QJsonObject>
#include "lyricsprovider.h"
#include "lyricsfetcher.h"
#include "jsonlyricsprovider.h"
JsonLyricsProvider::JsonLyricsProvider(const QString &name, QObject *parent) : LyricsProvider(name, parent) {}
QJsonObject JsonLyricsProvider::ExtractJsonObj(QNetworkReply *reply, const quint64 id) {
if (reply->error() != QNetworkReply::NoError) {
QString failure_reason = QString("%1 (%2)").arg(reply->errorString()).arg(reply->error());
Error(id, failure_reason);
return QJsonObject();
}
if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
QString failure_reason = QString("Received HTTP code %1").arg(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt());
Error(id, failure_reason);
return QJsonObject();
}
QByteArray data = reply->readAll();
QJsonParseError error;
QJsonDocument json_doc = QJsonDocument::fromJson(data, &error);
if (error.error != QJsonParseError::NoError) {
Error(id, "Reply from server missing Json data.");
return QJsonObject();
}
if (json_doc.isNull() || json_doc.isEmpty()) {
Error(id, "Received empty Json document.");
return QJsonObject();
}
if (!json_doc.isObject()) {
Error(id, "Json document is not an object.");
return QJsonObject();
}
QJsonObject json_obj = json_doc.object();
if (json_obj.isEmpty()) {
Error(id, "Received empty Json object.");
return QJsonObject();
}
return json_obj;
}

View File

@ -0,0 +1,45 @@
/*
* Strawberry Music Player
* Copyright 2019, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry 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.
*
* Strawberry 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 Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef JSONLYRICSPROVIDER_H
#define JSONLYRICSPROVIDER_H
#include "config.h"
#include <QObject>
#include <QVariant>
#include <QString>
#include "lyricsprovider.h"
class QNetworkReply;
class JsonLyricsProvider : public LyricsProvider {
Q_OBJECT
public:
explicit JsonLyricsProvider(const QString &name, QObject *parent = nullptr);
QJsonObject ExtractJsonObj(QNetworkReply *reply, const quint64 id);
private:
virtual void Error(const quint64 id, const QString &error, QVariant debug = QVariant()) = 0;
};
#endif // JSONLYRICSPROVIDER_H

View File

@ -0,0 +1,96 @@
/*
* Strawberry Music Player
* Copyright 2019, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry 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.
*
* Strawberry 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 Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "config.h"
#include <QObject>
#include <QByteArray>
#include <QVariant>
#include <QString>
#include <QUrl>
#include <QUrlQuery>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QJsonObject>
#include "core/closure.h"
#include "core/logging.h"
#include "core/network.h"
#include "core/utilities.h"
#include "lyricsprovider.h"
#include "lyricsfetcher.h"
#include "jsonlyricsprovider.h"
#include "ovhlyricsprovider.h"
const char *OVHLyricsProvider::kUrlSearch = "https://api.lyrics.ovh/v1/";
OVHLyricsProvider::OVHLyricsProvider(QObject *parent) : JsonLyricsProvider("Lyrics.ovh", parent), network_(new NetworkAccessManager(this)) {}
bool OVHLyricsProvider::StartSearch(const QString &artist, const QString &album, const QString &title, const quint64 id) {
QUrl url(kUrlSearch + QString(QUrl::toPercentEncoding(artist)) + "/" + QString(QUrl::toPercentEncoding(title)));
QNetworkReply *reply = network_->get(QNetworkRequest(url));
NewClosure(reply, SIGNAL(finished()), this, SLOT(HandleSearchReply(QNetworkReply*, const quint64, const QString&, const QString&)), reply, id, artist, title);
//qLog(Debug) << "OVHLyrics: Sending request for" << url;
return true;
}
void OVHLyricsProvider::CancelSearch(quint64 id) {}
void OVHLyricsProvider::HandleSearchReply(QNetworkReply *reply, const quint64 id, const QString &artist, const QString &title) {
reply->deleteLater();
QJsonObject json_obj = ExtractJsonObj(reply, id);
if (json_obj.isEmpty()) {
emit SearchFinished(id, LyricsSearchResults());
return;
}
if (json_obj.contains("error")) {
Error(id, json_obj["error"].toString());
qLog(Debug) << "OVHLyrics: No lyrics for" << artist << title;
emit SearchFinished(id, LyricsSearchResults());
return;
}
if (!json_obj.contains("lyrics")) {
emit SearchFinished(id, LyricsSearchResults());
return;
}
LyricsSearchResult result;
result.lyrics = json_obj["lyrics"].toString();
qLog(Debug) << "OVHLyrics: Got lyrics for" << artist << title;
emit SearchFinished(id, LyricsSearchResults() << result);
}
void OVHLyricsProvider::Error(const quint64 id, const QString &error, QVariant debug) {
qLog(Error) << "OVHLyrics:" << error;
if (debug.isValid()) qLog(Debug) << debug;
emit SearchFinished(id, LyricsSearchResults());
}

View File

@ -0,0 +1,58 @@
/*
* Strawberry Music Player
* Copyright 2019, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry 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.
*
* Strawberry 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 Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef OVHLYRICSPROVIDER_H
#define OVHLYRICSPROVIDER_H
#include "config.h"
#include <stdbool.h>
#include <QObject>
#include <QVariant>
#include <QString>
#include "lyricsprovider.h"
#include "jsonlyricsprovider.h"
#include "lyricsfetcher.h"
class QNetworkAccessManager;
class QNetworkReply;
class OVHLyricsProvider : public JsonLyricsProvider {
Q_OBJECT
public:
explicit OVHLyricsProvider(QObject *parent = nullptr);
bool StartSearch(const QString &artist, const QString &album, const QString &title, quint64 id);
void CancelSearch(quint64 id);
private slots:
void HandleSearchReply(QNetworkReply *reply, const quint64 id, const QString &artist, const QString &title);
private:
static const char *kUrlSearch;
QNetworkAccessManager *network_;
void Error(const quint64 id, const QString &error, QVariant debug = QVariant());
};
#endif // OVHLYRICSPROVIDER_H