mirror of
https://github.com/clangen/musikcube.git
synced 2025-03-15 07:21:23 +00:00
better lastfm support (#676)
This commit is contained in:
parent
4f4f785390
commit
4815178b57
src/musikcore
@ -79,6 +79,7 @@ using Editor = PlaybackService::Editor;
|
|||||||
#define MESSAGE_LOAD_PLAYBACK_CONTEXT 1011
|
#define MESSAGE_LOAD_PLAYBACK_CONTEXT 1011
|
||||||
#define MESSAGE_MARK_TRACK_PLAYED 1012
|
#define MESSAGE_MARK_TRACK_PLAYED 1012
|
||||||
#define MESSAGE_NEXT_TRACK_EDITED 1013
|
#define MESSAGE_NEXT_TRACK_EDITED 1013
|
||||||
|
#define MESSAGE_MARK_TRACK_PLAYING 1014
|
||||||
|
|
||||||
class StreamMessage: public Message {
|
class StreamMessage: public Message {
|
||||||
public:
|
public:
|
||||||
@ -312,6 +313,9 @@ void PlaybackService::ProcessMessage(IMessage &message) {
|
|||||||
lastfm::Scrobble(this->playingTrack);
|
lastfm::Scrobble(this->playingTrack);
|
||||||
this->MarkTrackAsPlayed(message.UserData1()); /* UserData1 is a trackId */
|
this->MarkTrackAsPlayed(message.UserData1()); /* UserData1 is a trackId */
|
||||||
}
|
}
|
||||||
|
else if (type == MESSAGE_MARK_TRACK_PLAYING) {
|
||||||
|
lastfm::UpdateNowPlaying(this->playingTrack);
|
||||||
|
}
|
||||||
else if (type == MESSAGE_STREAM_EVENT) {
|
else if (type == MESSAGE_STREAM_EVENT) {
|
||||||
StreamMessage* streamMessage = dynamic_cast<StreamMessage*>(&message);
|
StreamMessage* streamMessage = dynamic_cast<StreamMessage*>(&message);
|
||||||
const StreamState eventType = static_cast<StreamState>(streamMessage->GetEventType());
|
const StreamState eventType = static_cast<StreamState>(streamMessage->GetEventType());
|
||||||
@ -517,6 +521,7 @@ void PlaybackService::OnTrackChanged(size_t pos, TrackPtr track) {
|
|||||||
this->playingTrack = track;
|
this->playingTrack = track;
|
||||||
this->TrackChanged(this->index, track);
|
this->TrackChanged(this->index, track);
|
||||||
this->messageQueue.Remove(this, MESSAGE_MARK_TRACK_PLAYED);
|
this->messageQueue.Remove(this, MESSAGE_MARK_TRACK_PLAYED);
|
||||||
|
this->messageQueue.Remove(this, MESSAGE_MARK_TRACK_PLAYING);
|
||||||
|
|
||||||
if (track && this->transport->GetStreamState() == StreamState::Playing) {
|
if (track && this->transport->GetStreamState() == StreamState::Playing) {
|
||||||
/* we consider a track to be played if (1) it enters the playing state and
|
/* we consider a track to be played if (1) it enters the playing state and
|
||||||
@ -525,11 +530,13 @@ void PlaybackService::OnTrackChanged(size_t pos, TrackPtr track) {
|
|||||||
const double duration = this->transport->GetDuration();
|
const double duration = this->transport->GetDuration();
|
||||||
if (duration > 0 && duration < 10.0) {
|
if (duration > 0 && duration < 10.0) {
|
||||||
lastfm::Scrobble(track);
|
lastfm::Scrobble(track);
|
||||||
|
lastfm::UpdateNowPlaying(track);
|
||||||
this->MarkTrackAsPlayed(track->GetId());
|
this->MarkTrackAsPlayed(track->GetId());
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
const int64_t delay = (int64_t)(duration * 0.25f) * 1000LL;
|
const int64_t delay = std::min((int64_t) (duration * 0.25f) * 1000LL, 20000LL);
|
||||||
POST_DELAYED(this, MESSAGE_MARK_TRACK_PLAYED, track->GetId(), 0, delay);
|
POST_DELAYED(this, MESSAGE_MARK_TRACK_PLAYED, track->GetId(), 0, delay);
|
||||||
|
POST_DELAYED(this, MESSAGE_MARK_TRACK_PLAYING, track->GetId(), 0, 2500LL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,7 +59,8 @@ static const std::string API_SECRET = "6dc09da925fe5c115b90320213c53b46";
|
|||||||
static const std::string URL_BASE = "http://ws.audioscrobbler.com/2.0/";
|
static const std::string URL_BASE = "http://ws.audioscrobbler.com/2.0/";
|
||||||
static const std::string GET_TOKEN = "auth.getToken";
|
static const std::string GET_TOKEN = "auth.getToken";
|
||||||
static const std::string GET_SESSION = "auth.getSession";
|
static const std::string GET_SESSION = "auth.getSession";
|
||||||
static const std::string UPDATE_NOW_PLAYING = "track.scrobble";
|
static const std::string UPDATE_NOW_PLAYING = "track.updateNowPlaying";
|
||||||
|
static const std::string UPDATE_RECENT = "track.scrobble";
|
||||||
static const std::string ACCOUNT_LINK_URL_BASE = "http://www.last.fm/api/auth/?api_key=" + API_KEY + "&token=";
|
static const std::string ACCOUNT_LINK_URL_BASE = "http://www.last.fm/api/auth/?api_key=" + API_KEY + "&token=";
|
||||||
|
|
||||||
using namespace musik;
|
using namespace musik;
|
||||||
@ -222,7 +223,7 @@ namespace musik { namespace core { namespace lastfm {
|
|||||||
if (track) {
|
if (track) {
|
||||||
auto session = LoadSession();
|
auto session = LoadSession();
|
||||||
if (session.valid) {
|
if (session.valid) {
|
||||||
std::string postBody = gernateSignedUrlParams(UPDATE_NOW_PLAYING, {
|
std::string postBody = gernateSignedUrlParams(UPDATE_RECENT, {
|
||||||
{ "track", track->GetString("title") },
|
{ "track", track->GetString("title") },
|
||||||
{ "album", track->GetString("album") },
|
{ "album", track->GetString("album") },
|
||||||
{ "artist", track->GetString("artist") },
|
{ "artist", track->GetString("artist") },
|
||||||
@ -232,6 +233,31 @@ namespace musik { namespace core { namespace lastfm {
|
|||||||
{ "sk", session.sessionId }
|
{ "sk", session.sessionId }
|
||||||
});
|
});
|
||||||
|
|
||||||
|
createClient()->Url(URL_BASE)
|
||||||
|
.Mode(LastFmClient::Thread::Background)
|
||||||
|
.Header("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
.Method(LastFmClient::HttpMethod::Post)
|
||||||
|
.PostBody(postBody)
|
||||||
|
.Run();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void UpdateNowPlaying(musik::core::TrackPtr track) {
|
||||||
|
if (track) {
|
||||||
|
auto session = LoadSession();
|
||||||
|
if (session.valid) {
|
||||||
|
std::string postBody = gernateSignedUrlParams(UPDATE_NOW_PLAYING, {
|
||||||
|
{ "track", track->GetString("title") },
|
||||||
|
{ "album", track->GetString("album") },
|
||||||
|
{ "artist", track->GetString("artist") },
|
||||||
|
{ "albumArtist", track->GetString("album_artist") },
|
||||||
|
{ "trackNumber", track->GetString("track") },
|
||||||
|
{ "duration", track->GetString("duration") },
|
||||||
|
{ "sk", session.sessionId }
|
||||||
|
});
|
||||||
|
|
||||||
createClient()->Url(URL_BASE)
|
createClient()->Url(URL_BASE)
|
||||||
.Mode(LastFmClient::Thread::Background)
|
.Mode(LastFmClient::Thread::Background)
|
||||||
.Header("Content-Type", "application/x-www-form-urlencoded")
|
.Header("Content-Type", "application/x-www-form-urlencoded")
|
||||||
@ -243,3 +269,4 @@ namespace musik { namespace core { namespace lastfm {
|
|||||||
}
|
}
|
||||||
|
|
||||||
} } }
|
} } }
|
||||||
|
|
||||||
|
@ -49,8 +49,10 @@ namespace musik { namespace core { namespace lastfm {
|
|||||||
const std::string CreateAccountLinkUrl(const std::string& token);
|
const std::string CreateAccountLinkUrl(const std::string& token);
|
||||||
void CreateSession(const std::string& token, SessionCallback session);
|
void CreateSession(const std::string& token, SessionCallback session);
|
||||||
void Scrobble(musik::core::TrackPtr track);
|
void Scrobble(musik::core::TrackPtr track);
|
||||||
|
void UpdateNowPlaying(musik::core::TrackPtr track);
|
||||||
|
|
||||||
Session LoadSession();
|
Session LoadSession();
|
||||||
void SaveSession(const Session& session);
|
void SaveSession(const Session& session);
|
||||||
void ClearSession();
|
void ClearSession();
|
||||||
} } }
|
} } }
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user