Migrated scrobbling stuff

This commit is contained in:
emeric
2021-10-18 20:39:47 +02:00
parent fe298e10d9
commit a0489b2d94
106 changed files with 54 additions and 57 deletions
@@ -0,0 +1,28 @@
add_library(lmsscrobbling SHARED
impl/internal/InternalScrobbler.cpp
impl/listenbrainz/ListenBrainzScrobbler.cpp
impl/listenbrainz/ListensSynchronizer.cpp
impl/listenbrainz/Utils.cpp
impl/ScrobblingService.cpp
)
target_include_directories(lmsscrobbling INTERFACE
include
)
target_include_directories(lmsscrobbling PRIVATE
include
impl
)
target_link_libraries(lmsscrobbling PRIVATE
lmsutils
)
target_link_libraries(lmsscrobbling PUBLIC
lmsdatabase
)
install(TARGETS lmsscrobbling DESTINATION lib)
@@ -0,0 +1,56 @@
/*
* Copyright (C) 2021 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 <chrono>
#include <memory>
#include <optional>
#include <Wt/WDateTime.h>
#include "services/scrobbling/Listen.hpp"
namespace Database
{
class Session;
class TrackList;
class User;
}
namespace Scrobbling
{
class IScrobbler
{
public:
virtual ~IScrobbler() = default;
virtual void listenStarted(const Listen& listen) = 0;
virtual void listenFinished(const Listen& listen, std::optional<std::chrono::seconds> duration) = 0;
virtual void addTimedListen(const TimedListen& listen) = 0;
virtual Database::ObjectPtr<Database::TrackList> getListensTrackList(Database::Session& session, Database::ObjectPtr<Database::User> user) = 0;
};
std::unique_ptr<IScrobbler> createScrobbler(std::string_view backendName);
} // ns Scrobbling
@@ -0,0 +1,244 @@
/*
* Copyright (C) 2021 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 "ScrobblingService.hpp"
#include "database/Artist.hpp"
#include "database/Db.hpp"
#include "database/Release.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "database/TrackList.hpp"
#include "database/User.hpp"
#include "internal/InternalScrobbler.hpp"
#include "listenbrainz/ListenBrainzScrobbler.hpp"
namespace Scrobbling
{
using namespace Database;
std::unique_ptr<IScrobblingService>
createScrobblingService(boost::asio::io_context& ioContext, Db& db)
{
return std::make_unique<ScrobblingService>(ioContext, db);
}
ScrobblingService::ScrobblingService(boost::asio::io_context& ioContext, Db& db)
: _db {db}
{
_scrobblers.emplace(Database::Scrobbler::Internal, std::make_unique<InternalScrobbler>(_db));
_scrobblers.emplace(Database::Scrobbler::ListenBrainz, std::make_unique<ListenBrainz::Scrobbler>(ioContext, _db));
}
void
ScrobblingService::listenStarted(const Listen& listen)
{
if (std::optional<Database::Scrobbler> scrobbler {getUserScrobbler(listen.userId)})
_scrobblers[*scrobbler]->listenStarted(listen);
}
void
ScrobblingService::listenFinished(const Listen& listen, std::optional<std::chrono::seconds> duration)
{
if (std::optional<Database::Scrobbler> scrobbler {getUserScrobbler(listen.userId)})
_scrobblers[*scrobbler]->listenFinished(listen, duration);
}
void
ScrobblingService::addTimedListen(const TimedListen& listen)
{
if (std::optional<Database::Scrobbler> scrobbler {getUserScrobbler(listen.userId)})
_scrobblers[*scrobbler]->addTimedListen(listen);
}
std::optional<Database::Scrobbler>
ScrobblingService::getUserScrobbler(Database::UserId userId)
{
std::optional<Database::Scrobbler> scrobbler;
Session& session {_db.getTLSSession()};
auto transaction {session.createSharedTransaction()};
if (const User::pointer user {User::getById(session, userId)})
scrobbler = user->getScrobbler();
return scrobbler;
}
ScrobblingService::ArtistContainer
ScrobblingService::getRecentArtists(UserId userId,
const std::vector<ClusterId>& clusterIds,
std::optional<TrackArtistLinkType> linkType,
std::optional<Range> range,
bool& moreResults)
{
ArtistContainer res;
Session& session {_db.getTLSSession()};
auto transaction {session.createSharedTransaction()};
const User::pointer user {User::getById(session, userId)};
if (!user)
return res;
const ObjectPtr<TrackList> history {getListensTrackList(session, user)};
if (history)
{
for (const Artist::pointer& artist : history->getArtistsReverse(clusterIds, linkType, range, moreResults))
res.push_back(artist->getId());
}
return res;
}
ScrobblingService::ReleaseContainer
ScrobblingService::getRecentReleases(Database::UserId userId,
const std::vector<Database::ClusterId>& clusterIds,
std::optional<Database::Range> range,
bool& moreResults)
{
ReleaseContainer res;
Session& session {_db.getTLSSession()};
auto transaction {session.createSharedTransaction()};
const User::pointer user {User::getById(session, userId)};
if (!user)
return res;
const ObjectPtr<TrackList> history {getListensTrackList(session, user)};
if (history)
{
for (const Release::pointer& release : history->getReleasesReverse(clusterIds, range, moreResults))
res.push_back(release->getId());
}
return res;
}
ScrobblingService::TrackContainer
ScrobblingService::getRecentTracks(Database::UserId userId,
const std::vector<Database::ClusterId>& clusterIds,
std::optional<Database::Range> range,
bool& moreResults)
{
TrackContainer res;
Session& session {_db.getTLSSession()};
auto transaction {session.createSharedTransaction()};
const User::pointer user {User::getById(session, userId)};
if (!user)
return res;
const ObjectPtr<TrackList> history {getListensTrackList(session, user)};
if (history)
{
for (const Track::pointer& track : history->getTracksReverse(clusterIds, range, moreResults))
res.push_back(track->getId());
}
return res;
}
// Top
ScrobblingService::ArtistContainer
ScrobblingService::getTopArtists(UserId userId,
const std::vector<Database::ClusterId>& clusterIds,
std::optional<Database::TrackArtistLinkType> linkType,
std::optional<Database::Range> range,
bool& moreResults)
{
ArtistContainer res;
Session& session {_db.getTLSSession()};
auto transaction {session.createSharedTransaction()};
const User::pointer user {User::getById(session, userId)};
if (!user)
return res;
const ObjectPtr<TrackList> history {getListensTrackList(session, user)};
if (history)
{
for (const Artist::pointer& artist : history->getTopArtists(clusterIds, linkType, range, moreResults))
res.push_back(artist->getId());
}
return res;
}
ScrobblingService::ReleaseContainer
ScrobblingService::getTopReleases(Database::UserId userId,
const std::vector<Database::ClusterId>& clusterIds,
std::optional<Database::Range> range,
bool& moreResults)
{
ReleaseContainer res;
Session& session {_db.getTLSSession()};
auto transaction {session.createSharedTransaction()};
const User::pointer user {User::getById(session, userId)};
if (!user)
return res;
const ObjectPtr<TrackList> history {getListensTrackList(session, user)};
if (history)
{
for (const Release::pointer& release : history->getTopReleases(clusterIds, range, moreResults))
res.push_back(release->getId());
}
return res;
}
ScrobblingService::TrackContainer
ScrobblingService::getTopTracks(Database::UserId userId,
const std::vector<Database::ClusterId>& clusterIds,
std::optional<Database::Range> range,
bool& moreResults)
{
TrackContainer res;
Session& session {_db.getTLSSession()};
auto transaction {session.createSharedTransaction()};
const User::pointer user {User::getById(session, userId)};
if (!user)
return res;
if (const ObjectPtr<TrackList> history {getListensTrackList(session, user)})
{
for (const Track::pointer& track : history->getTopTracks(clusterIds, range, moreResults))
res.push_back(track->getId());
}
return res;
}
Database::ObjectPtr<Database::TrackList>
ScrobblingService::getListensTrackList(Session& session, Database::ObjectPtr<Database::User> user)
{
return _scrobblers[user->getScrobbler()]->getListensTrackList(session, user);
}
} // ns Scrobbling
@@ -0,0 +1,82 @@
/*
* Copyright (C) 2021 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 <memory>
#include <optional>
#include <unordered_map>
#include "services/scrobbling/IScrobblingService.hpp"
#include "IScrobbler.hpp"
namespace Scrobbling
{
class ScrobblingService : public IScrobblingService
{
public:
ScrobblingService(boost::asio::io_context& ioContext, Database::Db& db);
private:
void listenStarted(const Listen& listen) override;
void listenFinished(const Listen& listen, std::optional<std::chrono::seconds> duration) override;
void addTimedListen(const TimedListen& listen) override;
ArtistContainer getRecentArtists(Database::UserId userId,
const std::vector<Database::ClusterId>& clusterIds,
std::optional<Database::TrackArtistLinkType> linkType,
std::optional<Database::Range> range,
bool& moreResults) override;
ReleaseContainer getRecentReleases(Database::UserId userId,
const std::vector<Database::ClusterId>& clusterIds,
std::optional<Database::Range> range,
bool& moreResults) override;
TrackContainer getRecentTracks(Database::UserId userId,
const std::vector<Database::ClusterId>& clusterIds,
std::optional<Database::Range> range,
bool& moreResults) override;
ArtistContainer getTopArtists(Database::UserId userId,
const std::vector<Database::ClusterId>& clusterIds,
std::optional<Database::TrackArtistLinkType> linkType,
std::optional<Database::Range> range,
bool& moreResults) override;
ReleaseContainer getTopReleases(Database::UserId userId,
const std::vector<Database::ClusterId>& clusterIds,
std::optional<Database::Range> range,
bool& moreResults) override;
TrackContainer getTopTracks(Database::UserId userId,
const std::vector<Database::ClusterId>& clusterIds,
std::optional<Database::Range> range,
bool& moreResults) override;
Database::ObjectPtr<Database::TrackList> getListensTrackList(Database::Session& session, Database::ObjectPtr<Database::User> user);
std::optional<Database::Scrobbler> getUserScrobbler(Database::UserId userId);
Database::Db& _db;
std::unordered_map<Database::Scrobbler, std::unique_ptr<IScrobbler>> _scrobblers;
};
} // ns Scrobbling
@@ -0,0 +1,82 @@
/*
* Copyright (C) 2021 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 "InternalScrobbler.hpp"
#include "database/Db.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "database/TrackList.hpp"
#include "database/User.hpp"
#include "utils/Logger.hpp"
namespace Scrobbling
{
static const std::string historyTracklistName {"__scrobbler_internal_history__"};
InternalScrobbler::InternalScrobbler(Database::Db& db)
: _db {db}
{}
void
InternalScrobbler::listenStarted(const Listen& /*listen*/)
{
// nothing to do
}
void
InternalScrobbler::listenFinished(const Listen& listen, std::optional<std::chrono::seconds> duration)
{
// record tracks that have been played for at least of few seconds...
if (duration && *duration < std::chrono::seconds {5})
return;
addTimedListen({listen, Wt::WDateTime::currentDateTime()});
}
void
InternalScrobbler::addTimedListen(const TimedListen& listen)
{
Database::Session& session {_db.getTLSSession()};
auto transaction {session.createUniqueTransaction()};
const Database::User::pointer user {Database::User::getById(session, listen.userId)};
if (!user)
return;
Database::TrackList::pointer tracklist {getListensTrackList(session, user)};
if (!tracklist)
tracklist = Database::TrackList::create(session, historyTracklistName, Database::TrackList::Type::Internal, false, user);
const Database::Track::pointer track {Database::Track::getById(session, listen.trackId)};
if (!track)
return;
Database::TrackListEntry::create(session, track, getListensTrackList(session, user), listen.listenedAt);
}
Database::TrackList::pointer
InternalScrobbler::getListensTrackList(Database::Session& session, Database::ObjectPtr<Database::User> user)
{
return Database::TrackList::get(session, historyTracklistName, Database::TrackList::Type::Internal, user);
}
} // Scrobbling
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2021 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 "IScrobbler.hpp"
namespace Database
{
class Db;
}
namespace Scrobbling
{
class InternalScrobbler final : public IScrobbler
{
public:
InternalScrobbler(Database::Db& db);
private:
void listenStarted(const Listen& listen) override;
void listenFinished(const Listen& listen, std::optional<std::chrono::seconds> duration) override;
void addTimedListen(const TimedListen& listen) override;
Database::ObjectPtr<Database::TrackList> getListensTrackList(Database::Session& session, Database::ObjectPtr<Database::User> user) override;
Database::Db& _db;
};
} // Scrobbling
@@ -0,0 +1,92 @@
/*
* Copyright (C) 2021 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 <boost/asio/io_context.hpp>
#include <boost/asio/io_context_strand.hpp>
#include <boost/asio/steady_timer.hpp>
#include <Wt/Dbo/Dbo.h>
#include "SendQueue.hpp"
namespace Database
{
class Db;
class Session;
class User;
}
namespace Scrobbling::ListenBrainz
{
class ListensSynchronizer
{
public:
FeedbackSynchronizer(boost::asio::io_context& ioContext, Database::Db& db, SendQueue& sendQueue);
// void updateFeedback(const TimedListen& listen);
private:
struct UserContext
{
UserContext(Database::UserId id) : userId {id} {}
UserContext(const UserContext&) = delete;
UserContext(UserContext&&) = delete;
UserContext& operator=(const UserContext&) = delete;
UserContext& operator=(UserContext&&) = delete;
const Database::UserId userId;
bool fetching {};
std::optional<std::size_t> listenCount {};
// resetted at each fetch
std::string listenBrainzUserName; // need to be resolved first
Wt::WDateTime maxDateTime;
std::size_t fetchedListenCount{};
std::size_t matchedListenCount{};
std::size_t importedListenCount{};
};
UserContext& getUserContext(Database::UserId userId);
bool isFetching() const;
void scheduleGetListens(std::chrono::seconds fromNow);
void startGetListens();
void startGetListens(UserContext& context);
void onGetListensEnded(UserContext& context);
void enqueValidateToken(UserContext& context);
void enqueGetListenCount(UserContext& context);
void enqueGetListens(UserContext& context);
std::optional<SendQueue::RequestData> createValidateTokenRequestData(Database::UserId userId);
std::optional<SendQueue::RequestData> createGetListensRequestData(std::string_view listenBrainzUserName, const Wt::WDateTime& maxDateTime);
void processGetListensResponse(std::string_view body, UserContext& context);
boost::asio::io_context& _ioContext;
boost::asio::io_context::strand _strand {_ioContext};
Database::Db& _db;
SendQueue& _sendQueue;
boost::asio::steady_timer _getListensTimer {_ioContext};
std::unordered_map<Database::UserId, UserContext> _userContexts;
const std::size_t _maxSyncFeedbackCount;
const std::chrono::hours _syncFeedbackPeriod;
};
} // Scrobbling::ListenBrainz
@@ -0,0 +1,225 @@
/*
* Copyright (C) 2021 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 "ListenBrainzScrobbler.hpp"
#include <Wt/Json/Array.h>
#include <Wt/Json/Object.h>
#include <Wt/Json/Value.h>
#include <Wt/Json/Serializer.h>
#include "database/Artist.hpp"
#include "database/Db.hpp"
#include "database/Release.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "database/TrackList.hpp"
#include "database/User.hpp"
#include "utils/IConfig.hpp"
#include "utils/http/IClient.hpp"
#include "utils/Logger.hpp"
#include "utils/Service.hpp"
#include "Utils.hpp"
#define LOG(sev) LMS_LOG(SCROBBLING, sev) << "[listenbrainz] - "
namespace
{
bool
canBeScrobbled(Database::Session& session, Database::TrackId trackId, std::chrono::seconds duration)
{
auto transaction {session.createSharedTransaction()};
const Database::Track::pointer track {Database::Track::getById(session, trackId)};
if (!track)
return false;
const bool res {duration >= std::chrono::minutes(4) || (duration >= track->getDuration() / 2)};
if (!res)
LOG(DEBUG) << "Track cannot be scrobbled since played duration is too short: " << duration.count() << "s, total duration = " << std::chrono::duration_cast<std::chrono::seconds>(track->getDuration()).count() << "s";
return res;
}
std::optional<Wt::Json::Object>
listenToJsonPayload(Database::Session& session, const Scrobbling::Listen& listen, const Wt::WDateTime& timePoint)
{
auto transaction {session.createSharedTransaction()};
const Database::Track::pointer track {Database::Track::getById(session, listen.trackId)};
if (!track)
return std::nullopt;
auto artists {track->getArtists({Database::TrackArtistLinkType::Artist})};
if (artists.empty())
artists = track->getArtists({Database::TrackArtistLinkType::ReleaseArtist});
if (artists.empty())
{
LOG(DEBUG) << "Track cannot be scrobbled since it does not have any artist";
return std::nullopt;
}
Wt::Json::Object additionalInfo;
additionalInfo["listening_from"] = "LMS";
if (track->getRelease())
{
if (auto MBID {track->getRelease()->getMBID()})
additionalInfo["release_mbid"] = Wt::Json::Value {std::string {MBID->getAsString()}};
}
{
Wt::Json::Array artistMBIDs;
for (const Database::Artist::pointer& artist : artists)
{
if (auto MBID {artist->getMBID()})
artistMBIDs.push_back(Wt::Json::Value {std::string {MBID->getAsString()}});
}
if (!artistMBIDs.empty())
additionalInfo["artist_mbids"] = std::move(artistMBIDs);
}
if (auto MBID {track->getTrackMBID()})
additionalInfo["track_mbid"] = Wt::Json::Value {std::string {MBID->getAsString()}};
if (auto MBID {track->getRecordingMBID()})
additionalInfo["recording_mbid"] = Wt::Json::Value {std::string {MBID->getAsString()}};
if (const std::optional<std::size_t> trackNumber {track->getTrackNumber()})
additionalInfo["tracknumber"] = Wt::Json::Value {static_cast<long long int>(*trackNumber)};
Wt::Json::Object trackMetadata;
trackMetadata["additional_info"] = std::move(additionalInfo);
trackMetadata["artist_name"] = Wt::Json::Value {artists.front()->getName()};
trackMetadata["track_name"] = Wt::Json::Value {track->getName()};
if (track->getRelease())
trackMetadata["release_name"] = Wt::Json::Value {track->getRelease()->getName()};
Wt::Json::Object payload;
payload["track_metadata"] = std::move(trackMetadata);
if (timePoint.isValid())
payload["listened_at"] = Wt::Json::Value {static_cast<long long int>(timePoint.toTime_t())};
return payload;
}
std::string
listenToJsonString(Database::Session& session, const Scrobbling::Listen& listen, const Wt::WDateTime& timePoint, std::string_view listenType)
{
std::string res;
std::optional<Wt::Json::Object> payload {listenToJsonPayload(session, listen, timePoint)};
if (!payload)
return res;
Wt::Json::Object root;
root["listen_type"] = Wt::Json::Value {std::string {listenType}};
root["payload"] = Wt::Json::Array {std::move(*payload)};
res = Wt::Json::serialize(root);
return res;
}
}
namespace Scrobbling::ListenBrainz
{
Scrobbler::Scrobbler(boost::asio::io_context& ioContext, Database::Db& db)
: _ioContext {ioContext}
, _db {db}
, _baseAPIUrl {Service<IConfig>::get()->getString("listenbrainz-api-base-url", "https://api.listenbrainz.org")}
, _listensSynchronizer {_ioContext, db, _baseAPIUrl}
{
LOG(INFO) << "Starting ListenBrainz scrobbler... API endpoint = '" << _baseAPIUrl;
}
Scrobbler::~Scrobbler()
{
LOG(INFO) << "Stopped ListenBrainz scrobbler!";
}
void
Scrobbler::listenStarted(const Listen& listen)
{
enqueListen(listen, Wt::WDateTime {});
}
void
Scrobbler::listenFinished(const Listen& listen, std::optional<std::chrono::seconds> duration)
{
if (duration && !canBeScrobbled(_db.getTLSSession(), listen.trackId, *duration))
return;
const Listen timedListen {listen};
const Wt::WDateTime now {Wt::WDateTime::currentDateTime()};
enqueListen(timedListen, now);
}
void
Scrobbler::addTimedListen(const TimedListen& listen)
{
assert(listen.listenedAt.isValid());
enqueListen(listen, listen.listenedAt);
}
Database::TrackList::pointer
Scrobbler::getListensTrackList(Database::Session& session, Database::User::pointer user)
{
return Utils::getListensTrackList(session, user);
}
void
Scrobbler::enqueListen(const Listen& listen, const Wt::WDateTime& timePoint)
{
Http::ClientPOSTRequestParameters request;
request.url = _baseAPIUrl + "/1/submit-listens";
if (timePoint.isValid())
{
request.priority = Http::ClientRequestParameters::Priority::Normal;
request.onSuccessFunc = [=](std::string_view)
{
_listensSynchronizer.saveListen(TimedListen {listen, timePoint});
};
}
else
{
// We want "listen now" to appear as soon as possible
request.priority = Http::ClientRequestParameters::Priority::High;
}
std::string bodyText {listenToJsonString(_db.getTLSSession(), listen, timePoint, timePoint.isValid() ? "single" : "playing_now")};
if (bodyText.empty())
{
LOG(DEBUG) << "Cannot convert listen to json: skipping";
return;
}
const std::optional<UUID> listenBrainzToken {Utils::getListenBrainzToken(_db.getTLSSession(), listen.userId)};
if (!listenBrainzToken)
return;
request.message.addBodyText(bodyText);
request.message.addHeader("Authorization", "Token " + std::string {listenBrainzToken->getAsString()});
request.message.addHeader("Content-Type", "application/json");
Service<Http::IClient>::get()->sendPOSTRequest(std::move(request));
}
} // namespace Scrobbling::ListenBrainz
@@ -0,0 +1,64 @@
/*
* Copyright (C) 2021 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 <optional>
#include <boost/asio/io_context.hpp>
#include "IScrobbler.hpp"
#include "ListensSynchronizer.hpp"
namespace Database
{
class Db;
class Session;
class TrackList;
}
namespace Scrobbling::ListenBrainz
{
class Scrobbler final : public IScrobbler
{
public:
Scrobbler(boost::asio::io_context& ioContext, Database::Db& db);
~Scrobbler();
Scrobbler(const Scrobbler&) = delete;
Scrobbler(const Scrobbler&&) = delete;
Scrobbler& operator=(const Scrobbler&) = delete;
Scrobbler& operator=(const Scrobbler&&) = delete;
private:
void listenStarted(const Listen& listen) override;
void listenFinished(const Listen& listen, std::optional<std::chrono::seconds> duration) override;
void addTimedListen(const TimedListen& listen) override;
Database::ObjectPtr<Database::TrackList> getListensTrackList(Database::Session& session, Database::ObjectPtr<Database::User> user) override;
// Submit listens
void enqueListen(const Listen& listen, const Wt::WDateTime& timePoint);
//std::optional<SendQueue::RequestData> createSubmitListenRequestData(const Listen& listen, const Wt::WDateTime& timePoint);
boost::asio::io_context& _ioContext;
Database::Db& _db;
std::string _baseAPIUrl;
ListensSynchronizer _listensSynchronizer;
};
} // Scrobbling::ListenBrainz
@@ -0,0 +1,482 @@
/*
* Copyright (C) 2021 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 "ListenBrainzScrobbler.hpp"
#include <boost/asio/bind_executor.hpp>
#include <Wt/Json/Array.h>
#include <Wt/Json/Object.h>
#include <Wt/Json/Value.h>
#include <Wt/Json/Serializer.h>
#include "database/Artist.hpp"
#include "database/Db.hpp"
#include "database/Release.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "database/TrackList.hpp"
#include "database/User.hpp"
#include "services/scrobbling/Exception.hpp"
#include "utils/IConfig.hpp"
#include "utils/http/IClient.hpp"
#include "utils/Logger.hpp"
#include "utils/Service.hpp"
#include "Utils.hpp"
#define LOG(sev) LMS_LOG(SCROBBLING, sev) << "[listenbrainz Synchronizer] - "
namespace
{
using namespace Scrobbling::ListenBrainz;
std::string
parseValidateToken(std::string_view msgBody)
{
std::string listenBrainzUserName;
Wt::Json::ParseError error;
Wt::Json::Object root;
if (!Wt::Json::parse(std::string {msgBody}, root, error))
{
LOG(ERROR) << "Cannot parse 'validate-token' result: " << error.what();
return listenBrainzUserName;
}
if (!root.get("valid").orIfNull(false))
{
LOG(INFO) << "Invalid listenbrainz user";
return listenBrainzUserName;
}
listenBrainzUserName = root.get("user_name").orIfNull("");
return listenBrainzUserName;
}
std::optional<std::size_t>
parseListenCount(std::string_view msgBody)
{
try
{
Wt::Json::Object root;
Wt::Json::parse(std::string {msgBody}, root);
const Wt::Json::Object& payload {static_cast<const Wt::Json::Object&>(root.get("payload"))};
return static_cast<int>(payload.get("count"));
}
catch (const Wt::WException& e)
{
LOG(ERROR) << "Cannot parse listen count response: " << e.what();
return std::nullopt;
}
}
Database::Track::pointer
tryMatchListen(Database::Session& session, const Wt::Json::Object& metadata)
{
Database::Track::pointer track;
// first try to get the associated track using MBIDs, and then fallback on names
if (metadata.type("additional_info") == Wt::Json::Type::Object)
{
const Wt::Json::Object& additionalInfo = metadata.get("additional_info");
if (std::optional<UUID> recordingMBID {UUID::fromString(additionalInfo.get("recording_mbid").orIfNull(""))})
{
const auto tracks {Database::Track::getByRecordingMBID(session, *recordingMBID)};
// if duplicated files, do not record it (let the user correct its database)
if (tracks.size() == 1)
track = tracks.front();
}
}
if (track)
return track;
// these fields are mandatory
const std::string trackName {static_cast<std::string>(metadata.get("track_name"))};
const std::string releaseName {static_cast<std::string>(metadata.get("release_name"))};
auto tracks {Database::Track::getByNameAndReleaseName(session, trackName, releaseName)};
if (tracks.size() > 1)
{
tracks.erase(std::remove_if(std::begin(tracks), std::end(tracks),
[&](const Database::Track::pointer track)
{
if (std::string artistName {metadata.get("artist_name").orIfNull("")}; !artistName.empty())
{
const auto& artists {track->getArtists({Database::TrackArtistLinkType::Artist})};
if (std::none_of(std::begin(artists), std::end(artists), [&](const Database::Artist::pointer& artist) { return artist->getName() == artistName; }))
return true;
}
if (metadata.type("additional_info") == Wt::Json::Type::Object)
{
const Wt::Json::Object& additionalInfo = metadata.get("additional_info");
if (track->getTrackNumber())
{
int otherTrackNumber {additionalInfo.get("tracknumber").orIfNull(-1)};
if (otherTrackNumber > 0 && static_cast<std::size_t>(otherTrackNumber) != *track->getTrackNumber())
return true;
}
if (auto releaseMBID {track->getRelease()->getMBID()})
{
if (std::optional<UUID> otherReleaseMBID {UUID::fromString(additionalInfo.get("release_mbid").orIfNull(""))})
{
if (otherReleaseMBID->getAsString() != releaseMBID->getAsString())
return true;
}
}
}
return false;
}), std::end(tracks));
}
if (tracks.size() == 1)
track = tracks.front();
return track;
}
struct ParseGetListensResult
{
Wt::WDateTime oldestEntry;
std::size_t listenCount{};
std::vector<Scrobbling::TimedListen> matchedListens;
};
ParseGetListensResult
parseGetListens(Database::Session& session, std::string_view msgBody, Database::UserId userId)
{
ParseGetListensResult result;
try
{
Wt::Json::Object root;
Wt::Json::parse(std::string {msgBody}, root);
const Wt::Json::Object& payload = root.get("payload");
const Wt::Json::Array& listens = payload.get("listens");
LOG(DEBUG) << "Got " << listens.size() << " listens";
if (listens.empty())
return result;
auto transaction {session.createSharedTransaction()};
for (const Wt::Json::Value& value : listens)
{
const Wt::Json::Object& listen = value;
const Wt::WDateTime listenedAt {Wt::WDateTime::fromTime_t(static_cast<int>(listen.get("listened_at")))};
const Wt::Json::Object& metadata = listen.get("track_metadata");
if (!listenedAt.isValid())
{
LOG(ERROR) << "bad listened_at field!";
continue;
}
result.listenCount++;
if (!result.oldestEntry.isValid())
result.oldestEntry = listenedAt;
else if (listenedAt < result.oldestEntry)
result.oldestEntry = listenedAt;
if (const Database::Track::pointer track {tryMatchListen(session, metadata)})
result.matchedListens.emplace_back(Scrobbling::TimedListen {{userId, track->getId()}, listenedAt});
}
}
catch (const Wt::WException& error)
{
LOG(ERROR) << "Cannot parse 'get-listens' result: " << error.what();
}
return result;
}
}
namespace Scrobbling::ListenBrainz
{
ListensSynchronizer::ListensSynchronizer(boost::asio::io_context& ioContext, Database::Db& db, std::string_view baseAPIUrl)
: _ioContext {ioContext}
, _db {db}
, _baseAPIUrl {baseAPIUrl}
, _maxSyncListenCount {Service<IConfig>::get()->getULong("listenbrainz-max-sync-listen-count", 1000)}
, _syncListensPeriod {Service<IConfig>::get()->getULong("listenbrainz-sync-listens-period-hours", 1)}
{
LOG(INFO) << "Starting Listens synchronizer, maxSyncListenCount = " << _maxSyncListenCount << ", _syncListensPeriod = " << _syncListensPeriod.count() << " hours";
scheduleGetListens(std::chrono::seconds {30});
}
void
ListensSynchronizer::saveListen(const TimedListen& listen)
{
_strand.dispatch([=]
{
Database::Session& session {_db.getTLSSession()};
auto transaction {session.createUniqueTransaction()};
const Database::User::pointer user {Database::User::getById(session, listen.userId)};
if (!user)
return;
const Database::Track::pointer track {Database::Track::getById(session, listen.trackId)};
if (!track)
return;
Database::TrackListEntry::create(session, track, Utils::getOrCreateListensTrackList(session, user), listen.listenedAt);
UserContext& context {getUserContext(listen.userId)};
if (context.listenCount)
(*context.listenCount)++;
});
}
ListensSynchronizer::UserContext&
ListensSynchronizer::getUserContext(Database::UserId userId)
{
auto itContext {_userContexts.find(userId)};
if (itContext == std::cend(_userContexts))
{
auto [itNewContext, inserted] {_userContexts.emplace(userId, userId)};
itContext = itNewContext;
}
return itContext->second;
}
bool
ListensSynchronizer::isFetching() const
{
return std::any_of(std::cbegin(_userContexts), std::cend(_userContexts), [](const auto& contextEntry)
{
const auto& [userId, context] {contextEntry};
return context.fetching;
});
}
void
ListensSynchronizer::scheduleGetListens(std::chrono::seconds fromNow)
{
if (_syncListensPeriod.count() == 0 || _maxSyncListenCount == 0)
return;
LOG(DEBUG) << "Scheduled sync in " << fromNow.count() << " seconds...";
_getListensTimer.expires_after(fromNow);
_getListensTimer.async_wait(boost::asio::bind_executor(_strand, [this] (const boost::system::error_code& ec)
{
if (ec == boost::asio::error::operation_aborted)
{
LOG(DEBUG) << "getListens aborted";
return;
}
else if (ec)
{
throw Exception {"GetListens timer failure: " + std::string {ec.message()} };
}
startGetListens();
}));
}
void
ListensSynchronizer::startGetListens()
{
LOG(DEBUG) << "GetListens started!!!";
assert(!isFetching());
std::vector<Database::UserId> userIds;
{
Database::Session& session {_db.getTLSSession()};
auto transaction {session.createSharedTransaction()};
userIds = Database::User::getAllIds(_db.getTLSSession());
}
for (const Database::UserId userId : userIds)
{
if (Utils::getListenBrainzToken(_db.getTLSSession(), userId))
startGetListens(getUserContext(userId));
}
if (!isFetching())
scheduleGetListens(_syncListensPeriod);
}
void
ListensSynchronizer::startGetListens(UserContext& context)
{
context.fetching = true;
context.listenBrainzUserName = "";
context.maxDateTime = {};
context.fetchedListenCount = 0;
context.matchedListenCount = 0;
context.importedListenCount = 0;
enqueValidateToken(context);
}
void
ListensSynchronizer::onGetListensEnded(UserContext& context)
{
_strand.dispatch([this, &context]
{
LOG(DEBUG) << "Fetch done for user " << context.userId.getValue() << ", fetched: " << context.fetchedListenCount << ", matched: " << context.matchedListenCount << ", imported: " << context.importedListenCount;
context.fetching = false;
if (!isFetching())
scheduleGetListens(_syncListensPeriod);
});
}
void
ListensSynchronizer::enqueValidateToken(UserContext& context)
{
assert(context.listenBrainzUserName.empty());
const std::optional<UUID> listenBrainzToken {Utils::getListenBrainzToken(_db.getTLSSession(), context.userId)};
if (!listenBrainzToken)
{
onGetListensEnded(context);
return;
}
Http::ClientGETRequestParameters request;
request.priority = Http::ClientRequestParameters::Priority::Low;
request.url = _baseAPIUrl + "/1/validate-token";
request.headers = { {"Authorization", "Token " + std::string {listenBrainzToken->getAsString()}} };
request.onSuccessFunc = [this, &context] (std::string_view msgBody)
{
context.listenBrainzUserName = parseValidateToken(msgBody);
if (context.listenBrainzUserName.empty())
{
onGetListensEnded(context);
return;
}
enqueGetListenCount(context);
};
request.onFailureFunc = [this, &context]
{
onGetListensEnded(context);
};
Service<Http::IClient>::get()->sendGETRequest(std::move(request));
}
void
ListensSynchronizer::enqueGetListenCount(UserContext& context)
{
assert(!context.listenBrainzUserName.empty());
Http::ClientGETRequestParameters request;
request.url = _baseAPIUrl + "/1/user/" + std::string {context.listenBrainzUserName} + "/listen-count";
request.priority = Http::ClientRequestParameters::Priority::Low;
request.onSuccessFunc = [=, &context] (std::string_view msgBody)
{
const auto listenCount = parseListenCount(msgBody);
if (listenCount)
LOG(DEBUG) << "Listen count for listenbrainz user '" << context.listenBrainzUserName << "' = " << *listenCount;
bool needSync {listenCount && (!context.listenCount || *context.listenCount != *listenCount)};
context.listenCount = listenCount;
if (!needSync)
{
onGetListensEnded(context);
return;
}
context.maxDateTime = Wt::WDateTime::currentDateTime();
enqueGetListens(context);
};
request.onFailureFunc = [this, &context]
{
onGetListensEnded(context);
};
Service<Http::IClient>::get()->sendGETRequest(std::move(request));
}
void
ListensSynchronizer::enqueGetListens(UserContext& context)
{
assert(!context.listenBrainzUserName.empty());
Http::ClientGETRequestParameters request;
request.url = _baseAPIUrl + "/1/user/" + context.listenBrainzUserName + "/listens?max_ts=" + std::to_string(context.maxDateTime.toTime_t());
request.priority = Http::ClientRequestParameters::Priority::Low;
request.onSuccessFunc = [=, &context] (std::string_view msgBody)
{
processGetListensResponse(msgBody, context);
if (context.fetchedListenCount >= _maxSyncListenCount || !context.maxDateTime.isValid())
{
onGetListensEnded(context);
return;
}
enqueGetListens(context);
};
request.onFailureFunc = [=, &context]
{
onGetListensEnded(context);
};
Service<Http::IClient>::get()->sendGETRequest(std::move(request));
}
void
ListensSynchronizer::processGetListensResponse(std::string_view msgBody, UserContext& context)
{
Database::Session& session {_db.getTLSSession()};
const ParseGetListensResult parseResult {parseGetListens(session, msgBody, context.userId)};
context.fetchedListenCount += parseResult.listenCount;
context.matchedListenCount += parseResult.matchedListens.size();
context.maxDateTime = parseResult.oldestEntry;
if (parseResult.matchedListens.empty())
return;
auto transaction {session.createUniqueTransaction()};
Database::User::pointer user {Database::User::getById(session, context.userId)};
if (!user)
return;
Database::TrackList::pointer tracklist {Utils::getOrCreateListensTrackList(session, user)};
for (const TimedListen& listen : parseResult.matchedListens)
{
const Database::Track::pointer track {Database::Track::getById(session, listen.trackId)};
if (!track)
continue;
if (!tracklist->getEntryByTrackAndDateTime(track, listen.listenedAt))
{
context.importedListenCount++;
Database::TrackListEntry::create(session, track, tracklist, listen.listenedAt);
}
}
}
} // namespace Scrobbling::ListenBrainz
@@ -0,0 +1,94 @@
/*
* Copyright (C) 2021 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 <optional>
#include <unordered_map>
#include <boost/asio/io_context.hpp>
#include <boost/asio/io_context_strand.hpp>
#include <boost/asio/steady_timer.hpp>
#include <Wt/Dbo/Dbo.h>
#include "database/Types.hpp"
#include "services/scrobbling/Listen.hpp"
namespace Database
{
class Db;
class Session;
class TrackList;
class User;
}
namespace Scrobbling::ListenBrainz
{
class ListensSynchronizer
{
public:
ListensSynchronizer(boost::asio::io_context& ioContext, Database::Db& db, std::string_view baseAPIUrl);
void saveListen(const TimedListen& listen);
private:
struct UserContext
{
UserContext(Database::UserId id) : userId {id} {}
UserContext(const UserContext&) = delete;
UserContext(UserContext&&) = delete;
UserContext& operator=(const UserContext&) = delete;
UserContext& operator=(UserContext&&) = delete;
const Database::UserId userId;
bool fetching {};
std::optional<std::size_t> listenCount {};
// resetted at each fetch
std::string listenBrainzUserName; // need to be resolved first
Wt::WDateTime maxDateTime;
std::size_t fetchedListenCount{};
std::size_t matchedListenCount{};
std::size_t importedListenCount{};
};
UserContext& getUserContext(Database::UserId userId);
bool isFetching() const;
void scheduleGetListens(std::chrono::seconds fromNow);
void startGetListens();
void startGetListens(UserContext& context);
void onGetListensEnded(UserContext& context);
void enqueValidateToken(UserContext& context);
void enqueGetListenCount(UserContext& context);
void enqueGetListens(UserContext& context);
void processGetListensResponse(std::string_view body, UserContext& context);
boost::asio::io_context& _ioContext;
boost::asio::io_context::strand _strand {_ioContext};
Database::Db& _db;
std::string _baseAPIUrl;
boost::asio::steady_timer _getListensTimer {_ioContext};
std::unordered_map<Database::UserId, UserContext> _userContexts;
const std::size_t _maxSyncListenCount;
const std::chrono::hours _syncListensPeriod;
};
} // Scrobbling::ListenBrainz
@@ -0,0 +1,63 @@
/*
* Copyright (C) 2021 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 "Utils.hpp"
#include <string_view>
#include "database/Session.hpp"
#include "database/TrackList.hpp"
#include "database/User.hpp"
static constexpr std::string_view historyTracklistName {"__scrobbler_listenbrainz_history__"};
namespace Scrobbling::ListenBrainz::Utils
{
std::optional<UUID>
getListenBrainzToken(Database::Session& session, Database::UserId userId)
{
auto transaction {session.createSharedTransaction()};
const Database::User::pointer user {Database::User::getById(session, userId)};
if (!user)
return std::nullopt;
if (user->getScrobbler() != Database::Scrobbler::ListenBrainz)
return std::nullopt;
return user->getListenBrainzToken();
}
Database::TrackList::pointer
getListensTrackList(Database::Session& session, Database::User::pointer user)
{
return Database::TrackList::get(session, historyTracklistName, Database::TrackList::Type::Internal, user);
}
Database::TrackList::pointer
getOrCreateListensTrackList(Database::Session& session, Database::User::pointer user)
{
Database::TrackList::pointer tracklist {getListensTrackList(session, user)};
if (!tracklist)
tracklist = Database::TrackList::create(session, historyTracklistName, Database::TrackList::Type::Internal, false, user);
return tracklist;
}
}
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2021 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 <Wt/Dbo/ptr.h>
#include "utils/UUID.hpp"
#include "database/Types.hpp"
namespace Database
{
class Session;
class TrackList;
class User;
}
namespace Scrobbling::ListenBrainz::Utils
{
std::optional<UUID> getListenBrainzToken(Database::Session& session, Database::UserId userId);
Database::ObjectPtr<Database::TrackList> getOrCreateListensTrackList(Database::Session& session, Database::ObjectPtr<Database::User> user);
Database::ObjectPtr<Database::TrackList> getListensTrackList(Database::Session& session, Database::ObjectPtr<Database::User> user);
}
@@ -0,0 +1,32 @@
/*
* Copyright (C) 2019 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 "utils/Exception.hpp"
namespace Scrobbling
{
class Exception : public LmsException
{
public:
using LmsException::LmsException;
};
}
@@ -0,0 +1,103 @@
/*
* Copyright (C) 2021 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 <boost/asio/io_service.hpp>
#include <chrono>
#include <memory>
#include <optional>
#include <vector>
#include <Wt/WDateTime.h>
#include "services/scrobbling/Listen.hpp"
#include "database/Types.hpp"
namespace Database
{
class Artist;
class Db;
class Release;
class Session;
class Track;
class User;
}
namespace Scrobbling
{
class IScrobblingService
{
public:
virtual ~IScrobblingService() = default;
// Scrobbling
virtual void listenStarted(const Listen& listen) = 0;
virtual void listenFinished(const Listen& listen, std::optional<std::chrono::seconds> playedDuration = std::nullopt) = 0;
virtual void addTimedListen(const TimedListen& listen) = 0;
// Stats
template <typename IdType>
using ResultContainer = std::vector<IdType>;
using ArtistContainer = ResultContainer<Database::ArtistId>;
using ReleaseContainer = ResultContainer<Database::ReleaseId>;
using TrackContainer = ResultContainer<Database::TrackId>;
// From most recent to oldest
virtual ArtistContainer getRecentArtists(Database::UserId userId,
const std::vector<Database::ClusterId>& clusterIds,
std::optional<Database::TrackArtistLinkType> linkType,
std::optional<Database::Range> range,
bool& moreResults) = 0;
virtual ReleaseContainer getRecentReleases(Database::UserId userId,
const std::vector<Database::ClusterId>& clusterIds,
std::optional<Database::Range> range,
bool& moreResults) = 0;
virtual TrackContainer getRecentTracks(Database::UserId userId,
const std::vector<Database::ClusterId>& clusterIds,
std::optional<Database::Range> range,
bool& moreResults) = 0;
// Top
virtual ArtistContainer getTopArtists(Database::UserId userId,
const std::vector<Database::ClusterId>& clusterIds,
std::optional<Database::TrackArtistLinkType> linkType,
std::optional<Database::Range> range,
bool& moreResults) = 0;
virtual ReleaseContainer getTopReleases(Database::UserId userId,
const std::vector<Database::ClusterId>& clusterIds,
std::optional<Database::Range> range,
bool& moreResults) = 0;
virtual TrackContainer getTopTracks(Database::UserId userId,
const std::vector<Database::ClusterId>& clusterIds,
std::optional<Database::Range> range,
bool& moreResults) = 0;
};
std::unique_ptr<IScrobblingService> createScrobblingService(boost::asio::io_service& ioService, Database::Db& db);
} // ns Scrobbling
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2021 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 <Wt/WDateTime.h>
#include "database/Types.hpp"
namespace Scrobbling
{
struct Listen
{
Database::UserId userId {};
Database::TrackId trackId {};
};
struct TimedListen : public Listen
{
Wt::WDateTime listenedAt;
};
} // ns Scrobbling