Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
}
Expand Down
60 changes: 55 additions & 5 deletions src/lyrics_fetcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "config.h"
#include "curl_handle.h"
#include "settings.h"

#include <cstdlib>
#include <cstring>
Expand All @@ -28,6 +29,15 @@
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <boost/regex.hpp>
#include <boost/range/algorithm_ext/erase.hpp>

#include <mpd/song.h>
#include <song.h>

#ifdef HAVE_TAGLIB_H
#include <fileref.h>
#include <tpropertymap.h>
#endif // HAVE_TAGLIB_H

#include "charset.h"
#include "lyrics_fetcher.h"
Expand Down Expand Up @@ -60,6 +70,10 @@ std::istream &operator>>(std::istream &is, LyricsFetcher_ &fetcher)
fetcher = std::make_unique<ZeneszovegFetcher>();
else if (s == "internet")
fetcher = std::make_unique<InternetLyricsFetcher>();
#ifdef HAVE_TAGLIB_H
else if (s == "tags")
fetcher = std::make_unique<TagsLyricsFetcher>();
#endif // HAVE_TAGLIB_H
else
is.setstate(std::ios::failbit);
return is;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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: ";
Expand All @@ -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
20 changes: 17 additions & 3 deletions src/lyrics_fetcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

#include "config.h"

#include <song.h>

#include <memory>
#include <string>

Expand All @@ -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;
Expand All @@ -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; }
Expand Down Expand Up @@ -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; }
Expand All @@ -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
2 changes: 1 addition & 1 deletion src/screens/lyrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ boost::optional<std::string> 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)
Expand Down