From 3d4c348e133bcdecb77893e627c252ea896df1f4 Mon Sep 17 00:00:00 2001 From: TilCreator Date: Wed, 5 May 2021 17:03:19 +0200 Subject: [PATCH] Add tags lyrics fetcher --- src/configuration.cpp | 4 ++- src/lyrics_fetcher.cpp | 60 ++++++++++++++++++++++++++++++++++++++---- src/lyrics_fetcher.h | 20 +++++++++++--- src/screens/lyrics.cpp | 2 +- 4 files changed, 76 insertions(+), 10 deletions(-) diff --git a/src/configuration.cpp b/src/configuration.cpp index c9eb3fef3..0a5d0d10d 100644 --- a/src/configuration.cpp +++ b/src/configuration.cpp @@ -32,6 +32,7 @@ #include "mpdpp.h" #include "format_impl.h" #include "settings.h" +#include "song.h" #include "utility/string.h" namespace po = boost::program_options; @@ -175,7 +176,8 @@ bool configure(int argc, char **argv) << fetcher->name() << " : " << std::flush; - auto result = fetcher->fetch(std::get<1>(data), std::get<2>(data)); + // The nullptr is a bit hacky, but that argument is only used by the tags fetchers and it is not being tested + auto result = fetcher->fetch(std::get<1>(data), std::get<2>(data), nullptr); std::cout << (result.first ? "ok" : "failed") << "\n"; } diff --git a/src/lyrics_fetcher.cpp b/src/lyrics_fetcher.cpp index 12a6fb2fd..1be175e3c 100644 --- a/src/lyrics_fetcher.cpp +++ b/src/lyrics_fetcher.cpp @@ -20,6 +20,7 @@ #include "config.h" #include "curl_handle.h" +#include "settings.h" #include #include @@ -28,6 +29,15 @@ #include #include #include +#include + +#include +#include + +#ifdef HAVE_TAGLIB_H +#include +#include +#endif // HAVE_TAGLIB_H #include "charset.h" #include "lyrics_fetcher.h" @@ -60,6 +70,10 @@ std::istream &operator>>(std::istream &is, LyricsFetcher_ &fetcher) fetcher = std::make_unique(); else if (s == "internet") fetcher = std::make_unique(); +#ifdef HAVE_TAGLIB_H + else if (s == "tags") + fetcher = std::make_unique(); +#endif // HAVE_TAGLIB_H else is.setstate(std::ios::failbit); return is; @@ -68,7 +82,8 @@ std::istream &operator>>(std::istream &is, LyricsFetcher_ &fetcher) const char LyricsFetcher::msgNotFound[] = "Not found"; LyricsFetcher::Result LyricsFetcher::fetch(const std::string &artist, - const std::string &title) + const std::string &title, + const MPD::Song &song) { Result result; result.first = false; @@ -150,7 +165,8 @@ void LyricsFetcher::postProcess(std::string &data) const /**********************************************************************/ LyricsFetcher::Result GoogleLyricsFetcher::fetch(const std::string &artist, - const std::string &title) + const std::string &title, + const MPD::Song &song) { Result result; result.first = false; @@ -192,7 +208,7 @@ LyricsFetcher::Result GoogleLyricsFetcher::fetch(const std::string &artist, data = unescapeHtmlUtf8(urls[0]); URL = data.c_str(); - return LyricsFetcher::fetch("", ""); + return LyricsFetcher::fetch("", "", song); } bool GoogleLyricsFetcher::isURLOk(const std::string &url) @@ -211,9 +227,10 @@ bool MetrolyricsFetcher::isURLOk(const std::string &url) /**********************************************************************/ LyricsFetcher::Result InternetLyricsFetcher::fetch(const std::string &artist, - const std::string &title) + const std::string &title, + const MPD::Song &song) { - GoogleLyricsFetcher::fetch(artist, title); + GoogleLyricsFetcher::fetch(artist, title, song); LyricsFetcher::Result result; result.first = false; result.second = "The following site may contain lyrics for this song: "; @@ -226,3 +243,36 @@ bool InternetLyricsFetcher::isURLOk(const std::string &url) URL = url; return false; } + +#ifdef HAVE_TAGLIB_H +LyricsFetcher::Result TagsLyricsFetcher::fetch(const std::string &artist, + const std::string &title, + const MPD::Song &song) +{ + LyricsFetcher::Result result; + result.first = false; + + TagLib::FileRef f((Config.mpd_music_dir + "/" + song.getURI()).data()); + if (f.isNull()) { + result.second = "Only works for local files."; + return result; + } + + TagLib::PropertyMap properties = f.file()->properties(); + + if (properties.contains("LYRICS")) { + result.first = true; + result.second = properties["LYRICS"].toString("\n\n").to8Bit(true); + } else if (properties.contains("UNSYNCEDLYRICS")) { + result.first = true; + result.second = properties["UNSYNCEDLYRICS"].toString("\n\n").to8Bit(true); + } else { + result.second = "No lyrics in the tags found, only found:\n" + + properties.toString().to8Bit(true); + } + + boost::remove_erase(result.second, '\r'); + + return result; +} +#endif // HAVE_TAGLIB_H diff --git a/src/lyrics_fetcher.h b/src/lyrics_fetcher.h index dd08939d5..50009d558 100644 --- a/src/lyrics_fetcher.h +++ b/src/lyrics_fetcher.h @@ -23,6 +23,8 @@ #include "config.h" +#include + #include #include @@ -33,7 +35,7 @@ struct LyricsFetcher virtual ~LyricsFetcher() { } virtual const char *name() const = 0; - virtual Result fetch(const std::string &artist, const std::string &title); + virtual Result fetch(const std::string &artist, const std::string &title, const MPD::Song &song); protected: virtual const char *urlTemplate() const = 0; @@ -57,7 +59,7 @@ std::istream &operator>>(std::istream &is, LyricsFetcher_ &fetcher); struct GoogleLyricsFetcher : public LyricsFetcher { - virtual Result fetch(const std::string &artist, const std::string &title); + virtual Result fetch(const std::string &artist, const std::string &title, const MPD::Song &song); protected: virtual const char *urlTemplate() const { return URL; } @@ -156,7 +158,7 @@ struct ZeneszovegFetcher : public GoogleLyricsFetcher struct InternetLyricsFetcher : public GoogleLyricsFetcher { virtual const char *name() const override { return "the Internet"; } - virtual Result fetch(const std::string &artist, const std::string &title) override; + virtual Result fetch(const std::string &artist, const std::string &title, const MPD::Song &song) override; protected: virtual const char *siteKeyword() const override { return nullptr; } @@ -168,4 +170,16 @@ struct InternetLyricsFetcher : public GoogleLyricsFetcher std::string URL; }; +#ifdef HAVE_TAGLIB_H +struct TagsLyricsFetcher : public LyricsFetcher +{ + virtual const char *name() const override { return "tags"; } + virtual Result fetch(const std::string &artist, const std::string &title, const MPD::Song &song) override; + +protected: + virtual const char *urlTemplate() const override { return ""; } + virtual const char *regex() const override { return ""; } +}; +#endif // HAVE_TAGLIB_H + #endif // NCMPCPP_LYRICS_FETCHER_H diff --git a/src/screens/lyrics.cpp b/src/screens/lyrics.cpp index d34ba617b..ea944e896 100644 --- a/src/screens/lyrics.cpp +++ b/src/screens/lyrics.cpp @@ -151,7 +151,7 @@ boost::optional downloadLyrics( << NC::Format::NoBold << "... "; } } - auto result_ = fetcher_->fetch(s_artist, s_title); + auto result_ = fetcher_->fetch(s_artist, s_title, s); if (result_.first == false) { if (shared_buffer)