Audd never got back to us after suggesting we could have an API token,

so the functionality is disabled and the api key parsing has been moved
to prefs in the mean time.
This commit is contained in:
casey langen 2019-04-26 22:10:16 -07:00
parent 2b953f4b39
commit e6874f45eb
6 changed files with 31 additions and 11 deletions

View File

@ -36,6 +36,8 @@
#include "Auddio.h" #include "Auddio.h"
#include "Common.h" #include "Common.h"
#include <core/sdk/HttpClient.h> #include <core/sdk/HttpClient.h>
#include <core/support/Preferences.h>
#include <core/support/PreferenceKeys.h>
#include <curl/curl.h> #include <curl/curl.h>
#include <sstream> #include <sstream>
#include <json.hpp> #include <json.hpp>
@ -43,9 +45,9 @@
/* https://api.audd.io/findLyrics/?q=the%20beatles%20sgt%20pepper%20reprise */ /* https://api.audd.io/findLyrics/?q=the%20beatles%20sgt%20pepper%20reprise */
using namespace musik::core; using namespace musik::core;
using namespace musik::core::prefs;
using AuddioClient = musik::core::sdk::HttpClient<std::stringstream>; using AuddioClient = musik::core::sdk::HttpClient<std::stringstream>;
const std::string apiToken = "";
static std::shared_ptr<AuddioClient> createClient() { static std::shared_ptr<AuddioClient> createClient() {
return AuddioClient::Create(std::stringstream()); return AuddioClient::Create(std::stringstream());
@ -63,14 +65,29 @@ static std::string encode(std::string value) {
return value; return value;
} }
static std::string getApiToken() {
auto prefs = Preferences::ForComponent(components::Settings);
return prefs->GetString(keys::AuddioApiToken);
}
namespace musik { namespace core { namespace auddio { namespace musik { namespace core { namespace auddio {
bool Available() {
return getApiToken().size() > 0;
}
void FindLyrics(TrackPtr track, LyricsCallback callback) { void FindLyrics(TrackPtr track, LyricsCallback callback) {
std::string artist = encode(track->GetString("artist")); std::string apiToken = getApiToken();
std::string title = encode(track->GetString("title"));
std::string url = if (!apiToken.size()) {
"https://api.audd.io/findLyrics/?q=" + callback(track, "apiToken");
artist + "%20" + title + }
"&api_token=" + apiToken;
std::string artist = encode(track->GetString("artist"));
std::string title = encode(track->GetString("title"));
std::string url =
"https://api.audd.io/findLyrics/?q=" +
artist + "%20" + title +
"&api_token=" + apiToken;
auto client = createClient(); auto client = createClient();
client->Url(url) client->Url(url)

View File

@ -38,7 +38,7 @@
#include <core/library/track/Track.h> #include <core/library/track/Track.h>
namespace musik { namespace core { namespace auddio { namespace musik { namespace core { namespace auddio {
using LyricsCallback = std::function<void(musik::core::TrackPtr track, std::string&)>; using LyricsCallback = std::function<void(musik::core::TrackPtr track, std::string)>;
bool Available();
void FindLyrics(musik::core::TrackPtr track, LyricsCallback callback); void FindLyrics(musik::core::TrackPtr track, LyricsCallback callback);
} } } } } }

View File

@ -62,6 +62,7 @@ namespace musik { namespace core { namespace prefs {
const std::string keys::LastFmSessionId = "LastFmSessionId"; const std::string keys::LastFmSessionId = "LastFmSessionId";
const std::string keys::LastFmUsername = "LastFmUsername"; const std::string keys::LastFmUsername = "LastFmUsername";
const std::string keys::DisableAlbumArtistFallback = "DisableAlbumArtistFallback"; const std::string keys::DisableAlbumArtistFallback = "DisableAlbumArtistFallback";
const std::string keys::AuddioApiToken = "AuddioApiToken";
} } } } } }

View File

@ -66,6 +66,7 @@ namespace musik { namespace core { namespace prefs {
extern const std::string LastFmSessionId; extern const std::string LastFmSessionId;
extern const std::string LastFmUsername; extern const std::string LastFmUsername;
extern const std::string DisableAlbumArtistFallback; extern const std::string DisableAlbumArtistFallback;
extern const std::string AuddioApiToken;
} }
} } } } } }

View File

@ -117,7 +117,7 @@ void LyricsLayout::LoadLyricsForCurrentTrack() {
if (track && track->GetId() != this->currentTrackId) { if (track && track->GetId() != this->currentTrackId) {
this->currentTrackId = track->GetId(); this->currentTrackId = track->GetId();
this->SetState(State::Loading); this->SetState(State::Loading);
auddio::FindLyrics(track, [this](TrackPtr track, std::string& lyrics) { auddio::FindLyrics(track, [this](TrackPtr track, std::string lyrics) {
if (this->currentTrackId == track->GetId()) { if (this->currentTrackId == track->GetId()) {
if (lyrics.size()) { if (lyrics.size()) {
this->UpdateAdapter(lyrics); this->UpdateAdapter(lyrics);

View File

@ -39,6 +39,7 @@
#include <cursespp/ToastOverlay.h> #include <cursespp/ToastOverlay.h>
#include <core/runtime/Message.h> #include <core/runtime/Message.h>
#include <core/support/Auddio.h>
#include <app/util/Messages.h> #include <app/util/Messages.h>
#include <app/util/PreferenceKeys.h> #include <app/util/PreferenceKeys.h>
@ -146,7 +147,7 @@ bool MainLayout::KeyPress(const std::string& key) {
this->SetLayout(consoleLayout); this->SetLayout(consoleLayout);
return true; return true;
} }
else if (Hotkeys::Is(Hotkeys::NavigateLyrics, key)) { else if (auddio::Available() && Hotkeys::Is(Hotkeys::NavigateLyrics, key)) {
this->Broadcast(message::JumpToLyrics); this->Broadcast(message::JumpToLyrics);
return true; return true;
} }