Subsonic API: added getLyrics/getLyricsBySongId support, ref #379
This commit is contained in:
@@ -200,7 +200,8 @@ namespace lms::api::subsonic
|
||||
// Media retrieval
|
||||
{ "/hls", { handleNotImplemented } },
|
||||
{ "/getCaptions", { handleNotImplemented } },
|
||||
{ "/getLyrics", { handleNotImplemented } },
|
||||
{ "/getLyrics", { handleGetLyrics } },
|
||||
{ "/getLyricsBySongId", { handleGetLyricsBySongId } },
|
||||
{ "/getAvatar", { handleNotImplemented } },
|
||||
|
||||
// Media annotation
|
||||
|
||||
@@ -196,7 +196,11 @@ namespace lms::api::subsonic
|
||||
auto valueToPropertyTree = [](const Node::ValueType& value) {
|
||||
boost::property_tree::ptree res;
|
||||
std::visit([&](const auto& rawValue) {
|
||||
res.put_value(rawValue);
|
||||
using RawValueType = std::decay_t<decltype(rawValue)>;
|
||||
if constexpr (std::is_same_v<RawValueType, Node::string>)
|
||||
res.put_value(core::stringUtils::replaceInString(rawValue, "\n", "\\n"));
|
||||
else
|
||||
res.put_value(rawValue);
|
||||
},
|
||||
value);
|
||||
|
||||
|
||||
@@ -31,11 +31,13 @@
|
||||
#include "core/Utils.hpp"
|
||||
#include "database/Session.hpp"
|
||||
#include "database/Track.hpp"
|
||||
#include "database/TrackLyrics.hpp"
|
||||
#include "database/User.hpp"
|
||||
#include "services/artwork/IArtworkService.hpp"
|
||||
|
||||
#include "ParameterParsing.hpp"
|
||||
#include "SubsonicId.hpp"
|
||||
#include "responses/Lyrics.hpp"
|
||||
|
||||
namespace lms::api::subsonic
|
||||
{
|
||||
@@ -189,6 +191,59 @@ namespace lms::api::subsonic
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Response handleGetLyrics(RequestContext& context)
|
||||
{
|
||||
std::string artistName{ getParameterAs<std::string>(context.parameters, "artist").value_or("") };
|
||||
std::string titleName{ getParameterAs<std::string>(context.parameters, "title").value_or("") };
|
||||
|
||||
Response response{ Response::createOkResponse(context.serverProtocolVersion) };
|
||||
|
||||
// best effort search, as this API is really limited
|
||||
auto transaction{ context.dbSession.createReadTransaction() };
|
||||
|
||||
db::Track::FindParameters params;
|
||||
params.name = titleName;
|
||||
params.artistName = artistName;
|
||||
params.range = Range{ 0, 2 };
|
||||
|
||||
// Choice: we return nothing if there are too many results
|
||||
const auto tracks{ db::Track::findIds(context.dbSession, params) };
|
||||
if (tracks.results.size() == 1)
|
||||
{
|
||||
// Choice: we return only the first lyrics if the track has many lyrics
|
||||
bool lyricsSet{};
|
||||
db::TrackLyrics::find(context.dbSession, tracks.results[0], [&](const db::TrackLyrics::pointer& lyrics) {
|
||||
if (lyricsSet)
|
||||
return;
|
||||
response.addNode("lyrics", createLyricsNode(context, lyrics));
|
||||
lyricsSet = true;
|
||||
});
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
Response handleGetLyricsBySongId(RequestContext& context)
|
||||
{
|
||||
// mandatory params
|
||||
db::TrackId id{ getMandatoryParameterAs<db::TrackId>(context.parameters, "id") };
|
||||
|
||||
Response response{ Response::createOkResponse(context.serverProtocolVersion) };
|
||||
Response::Node& lyricsList{ response.createNode("lyricsList") };
|
||||
lyricsList.createEmptyArrayChild("structuredLyrics");
|
||||
|
||||
auto transaction{ context.dbSession.createReadTransaction() };
|
||||
const db::Track::pointer track{ db::Track::find(context.dbSession, id) };
|
||||
if (track)
|
||||
{
|
||||
db::TrackLyrics::find(context.dbSession, track->getId(), [&](const db::TrackLyrics::pointer& lyrics) {
|
||||
lyricsList.addArrayChild("structuredLyrics", createStructuredLyricsNode(context, lyrics));
|
||||
});
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
void handleDownload(RequestContext& context, const Wt::Http::Request& request, Wt::Http::Response& response)
|
||||
{
|
||||
std::shared_ptr<IResourceHandler> resourceHandler;
|
||||
|
||||
@@ -26,6 +26,9 @@
|
||||
|
||||
namespace lms::api::subsonic
|
||||
{
|
||||
Response handleGetLyrics(RequestContext& context);
|
||||
Response handleGetLyricsBySongId(RequestContext& context);
|
||||
|
||||
void handleDownload(RequestContext& context, const Wt::Http::Request& request, Wt::Http::Response& response);
|
||||
void handleStream(RequestContext& context, const Wt::Http::Request& request, Wt::Http::Response& response);
|
||||
void handleGetCoverArt(RequestContext& context, const Wt::Http::Request& request, Wt::Http::Response& response);
|
||||
|
||||
@@ -35,6 +35,12 @@ namespace lms::api::subsonic
|
||||
formPostNode.addArrayValue("versions", 1);
|
||||
}
|
||||
|
||||
{
|
||||
Response::Node& songLyricsNode{ response.createArrayNode("openSubsonicExtensions") };
|
||||
songLyricsNode.setAttribute("name", "songLyrics");
|
||||
songLyricsNode.addArrayValue("versions", 1);
|
||||
}
|
||||
|
||||
return response;
|
||||
};
|
||||
} // namespace lms::api::subsonic
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
* Copyright (C) 2024 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
* LMS 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.
|
||||
*
|
||||
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "responses/Lyrics.hpp"
|
||||
|
||||
#include "database/TrackLyrics.hpp"
|
||||
|
||||
#include "RequestContext.hpp"
|
||||
|
||||
namespace lms::api::subsonic
|
||||
{
|
||||
Response::Node createLyricsNode(RequestContext& context, const db::TrackLyrics::pointer& lyrics)
|
||||
{
|
||||
Response::Node lyricsNode;
|
||||
|
||||
if (!lyrics->getDisplayArtist().empty())
|
||||
lyricsNode.setAttribute("artist", lyrics->getDisplayArtist());
|
||||
|
||||
if (!lyrics->getDisplayTitle().empty())
|
||||
lyricsNode.setAttribute("title", lyrics->getDisplayTitle());
|
||||
|
||||
std::string lyricsText;
|
||||
auto addLine{ [&](std::string&& line) {
|
||||
if (!lyricsText.empty())
|
||||
lyricsText += "\n";
|
||||
lyricsText += std::move(line);
|
||||
} };
|
||||
if (!lyrics->isSynchronized())
|
||||
{
|
||||
std::vector<std::string> lines{ lyrics->getUnsynchronizedLines() };
|
||||
for (std::string& line : lines)
|
||||
addLine(std::move(line));
|
||||
}
|
||||
else
|
||||
{
|
||||
// Reconstruct the lyrics without timestamp if needed
|
||||
db::TrackLyrics::SynchronizedLines lines{ lyrics->getSynchronizedLines() };
|
||||
for (auto& [timestamp, line] : lines)
|
||||
addLine(std::move(line));
|
||||
}
|
||||
|
||||
switch (context.responseFormat)
|
||||
{
|
||||
case ResponseFormat::json:
|
||||
lyricsNode.setAttribute("value", lyricsText);
|
||||
break;
|
||||
case ResponseFormat::xml:
|
||||
lyricsNode.setValue(lyricsText);
|
||||
break;
|
||||
}
|
||||
|
||||
return lyricsNode;
|
||||
}
|
||||
|
||||
Response::Node createStructuredLyricsNode(RequestContext& context, const db::ObjectPtr<db::TrackLyrics>& lyrics)
|
||||
{
|
||||
Response::Node lyricsNode;
|
||||
|
||||
if (!lyrics->getDisplayArtist().empty())
|
||||
lyricsNode.setAttribute("artist", lyrics->getDisplayArtist());
|
||||
|
||||
if (!lyrics->getDisplayTitle().empty())
|
||||
lyricsNode.setAttribute("title", lyrics->getDisplayTitle());
|
||||
|
||||
lyricsNode.setAttribute("lang", lyrics->getLanguage());
|
||||
lyricsNode.setAttribute("synced", lyrics->isSynchronized());
|
||||
if (lyrics->getOffset() != std::chrono::milliseconds{})
|
||||
lyricsNode.setAttribute("offset", lyrics->getOffset().count());
|
||||
|
||||
lyricsNode.createEmptyArrayChild("lines");
|
||||
auto addLine{ [&](std::string&& line, std::optional<std::chrono::milliseconds> timestamp = std::nullopt) {
|
||||
Response::Node lineNode;
|
||||
if (timestamp)
|
||||
lineNode.setAttribute("start", std::chrono::duration_cast<std::chrono::milliseconds>(*timestamp).count());
|
||||
|
||||
switch (context.responseFormat)
|
||||
{
|
||||
case ResponseFormat::json:
|
||||
lineNode.setAttribute("value", std::move(line));
|
||||
break;
|
||||
case ResponseFormat::xml:
|
||||
lineNode.setValue(std::move(line));
|
||||
break;
|
||||
}
|
||||
lyricsNode.addArrayChild("lines", std::move(lineNode));
|
||||
} };
|
||||
|
||||
if (!lyrics->isSynchronized())
|
||||
{
|
||||
std::vector<std::string> lines{ lyrics->getUnsynchronizedLines() };
|
||||
|
||||
for (std::string& line : lines)
|
||||
addLine(std::move(line));
|
||||
}
|
||||
else
|
||||
{
|
||||
db::TrackLyrics::SynchronizedLines lines{ lyrics->getSynchronizedLines() };
|
||||
for (auto& [timestamp, line] : lines)
|
||||
addLine(std::move(line), timestamp);
|
||||
}
|
||||
|
||||
return lyricsNode;
|
||||
}
|
||||
|
||||
} // namespace lms::api::subsonic
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (C) 2024 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
* LMS 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.
|
||||
*
|
||||
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "database/Object.hpp"
|
||||
|
||||
#include "SubsonicResponse.hpp"
|
||||
|
||||
namespace lms::db
|
||||
{
|
||||
class TrackLyrics;
|
||||
}
|
||||
|
||||
namespace lms::api::subsonic
|
||||
{
|
||||
struct RequestContext;
|
||||
|
||||
Response::Node createLyricsNode(RequestContext& context, const db::ObjectPtr<db::TrackLyrics>& lyrics);
|
||||
Response::Node createStructuredLyricsNode(RequestContext& context, const db::ObjectPtr<db::TrackLyrics>& lyrics);
|
||||
} // namespace lms::api::subsonic
|
||||
Reference in New Issue
Block a user