Implemented a Scrobbling service + ListenBrainz scrobbler. fixes #118
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
|
||||
add_library(lmsscrobbling SHARED
|
||||
impl/internal/InternalScrobbler.cpp
|
||||
impl/listenbrainz/ListenBrainzScrobbler.cpp
|
||||
impl/Scrobbling.cpp
|
||||
)
|
||||
|
||||
target_include_directories(lmsscrobbling INTERFACE
|
||||
include
|
||||
)
|
||||
|
||||
target_include_directories(lmsscrobbling PRIVATE
|
||||
include
|
||||
impl
|
||||
)
|
||||
|
||||
target_link_libraries(lmsscrobbling PUBLIC
|
||||
lmsdatabase
|
||||
lmsutils
|
||||
)
|
||||
|
||||
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 <memory>
|
||||
#include <vector>
|
||||
|
||||
#include <Wt/WDateTime.h>
|
||||
|
||||
#include "scrobbling/Listen.hpp"
|
||||
|
||||
namespace Database
|
||||
{
|
||||
class Db;
|
||||
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::chrono::seconds duration) = 0;
|
||||
|
||||
virtual void addListen(const Listen& listen, const Wt::WDateTime& timePoint) = 0;
|
||||
|
||||
virtual Wt::Dbo::ptr<Database::TrackList> getListensTrackList(Database::Session& session, Wt::Dbo::ptr<Database::User> user) = 0;
|
||||
};
|
||||
|
||||
std::unique_ptr<IScrobbler> createScrobbler(std::string_view backendName);
|
||||
|
||||
} // ns Scrobbling
|
||||
|
||||
@@ -0,0 +1,186 @@
|
||||
/*
|
||||
* 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 "Scrobbling.hpp"
|
||||
|
||||
#include "database/Db.hpp"
|
||||
#include "database/Session.hpp"
|
||||
#include "database/TrackList.hpp"
|
||||
#include "database/User.hpp"
|
||||
|
||||
#include "internal/InternalScrobbler.hpp"
|
||||
#include "listenbrainz/ListenBrainzScrobbler.hpp"
|
||||
|
||||
namespace Scrobbling
|
||||
{
|
||||
std::unique_ptr<IScrobbling>
|
||||
createScrobbling(Database::Db& db)
|
||||
{
|
||||
return std::make_unique<Scrobbling>(db);
|
||||
}
|
||||
|
||||
Scrobbling::Scrobbling(Database::Db& db)
|
||||
: _db {db}
|
||||
{
|
||||
_scrobblers.emplace(Database::Scrobbler::Internal, std::make_unique<InternalScrobbler>(_db));
|
||||
_scrobblers.emplace(Database::Scrobbler::ListenBrainz, std::make_unique<ListenBrainzScrobbler>(_db));
|
||||
}
|
||||
|
||||
void
|
||||
Scrobbling::listenStarted(const Listen& listen)
|
||||
{
|
||||
if (auto scrobbler {getUserScrobbler(listen.userId)})
|
||||
_scrobblers[*scrobbler]->listenStarted(listen);
|
||||
}
|
||||
|
||||
void
|
||||
Scrobbling::listenFinished(const Listen& listen, std::chrono::seconds duration)
|
||||
{
|
||||
if (auto scrobbler {getUserScrobbler(listen.userId)})
|
||||
_scrobblers[*scrobbler]->listenFinished(listen, duration);
|
||||
}
|
||||
|
||||
void
|
||||
Scrobbling::addListen(const Listen& listen, Wt::WDateTime timePoint)
|
||||
{
|
||||
if (auto scrobbler {getUserScrobbler(listen.userId)})
|
||||
_scrobblers[*scrobbler]->addListen(listen, timePoint);
|
||||
}
|
||||
|
||||
std::optional<Database::Scrobbler>
|
||||
Scrobbling::getUserScrobbler(Database::IdType userId)
|
||||
{
|
||||
std::optional<Database::Scrobbler> scrobbler;
|
||||
|
||||
Database::Session& session {_db.getTLSSession()};
|
||||
auto transaction {session.createSharedTransaction()};
|
||||
if (const Database::User::pointer user {Database::User::getById(session, userId)})
|
||||
scrobbler = user->getScrobbler();
|
||||
|
||||
return scrobbler;
|
||||
}
|
||||
|
||||
std::vector<Wt::Dbo::ptr<Database::Artist>>
|
||||
Scrobbling::getRecentArtists(Database::Session& session,
|
||||
Wt::Dbo::ptr<Database::User> user,
|
||||
const std::set<Database::IdType>& clusterIds,
|
||||
std::optional<Database::TrackArtistLinkType> linkType,
|
||||
std::optional<Database::Range> range,
|
||||
bool& moreResults)
|
||||
{
|
||||
const Wt::Dbo::ptr<Database::TrackList> history {getListensTrackList(session, user)};
|
||||
|
||||
std::vector<Wt::Dbo::ptr<Database::Artist>> res;
|
||||
if (history)
|
||||
res = history->getArtistsReverse(clusterIds, linkType, range, moreResults);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
std::vector<Wt::Dbo::ptr<Database::Release>>
|
||||
Scrobbling::getRecentReleases(Database::Session& session,
|
||||
Wt::Dbo::ptr<Database::User> user,
|
||||
const std::set<Database::IdType>& clusterIds,
|
||||
std::optional<Database::Range> range,
|
||||
bool& moreResults)
|
||||
{
|
||||
const Wt::Dbo::ptr<Database::TrackList> history {getListensTrackList(session, user)};
|
||||
|
||||
std::vector<Wt::Dbo::ptr<Database::Release>> res;
|
||||
if (history)
|
||||
res = history->getReleasesReverse(clusterIds, range, moreResults);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
std::vector<Wt::Dbo::ptr<Database::Track>>
|
||||
Scrobbling::getRecentTracks(Database::Session& session,
|
||||
Wt::Dbo::ptr<Database::User> user,
|
||||
const std::set<Database::IdType>& clusterIds,
|
||||
std::optional<Database::Range> range,
|
||||
bool& moreResults)
|
||||
{
|
||||
const Wt::Dbo::ptr<Database::TrackList> history {getListensTrackList(session, user)};
|
||||
|
||||
std::vector<Wt::Dbo::ptr<Database::Track>> res;
|
||||
if (history)
|
||||
res = history->getTracksReverse(clusterIds, range, moreResults);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
// Top
|
||||
std::vector<Wt::Dbo::ptr<Database::Artist>>
|
||||
Scrobbling::getTopArtists(Database::Session& session,
|
||||
Wt::Dbo::ptr<Database::User> user,
|
||||
const std::set<Database::IdType>& clusterIds,
|
||||
std::optional<Database::TrackArtistLinkType> linkType,
|
||||
std::optional<Database::Range> range,
|
||||
bool& moreResults)
|
||||
{
|
||||
const Wt::Dbo::ptr<Database::TrackList> history {getListensTrackList(session, user)};
|
||||
|
||||
std::vector<Wt::Dbo::ptr<Database::Artist>> res;
|
||||
if (history)
|
||||
res = history->getTopArtists(clusterIds, linkType, range, moreResults);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
std::vector<Wt::Dbo::ptr<Database::Release>>
|
||||
Scrobbling::getTopReleases(Database::Session& session,
|
||||
Wt::Dbo::ptr<Database::User> user,
|
||||
const std::set<Database::IdType>& clusterIds,
|
||||
std::optional<Database::Range> range,
|
||||
bool& moreResults)
|
||||
{
|
||||
const Wt::Dbo::ptr<Database::TrackList> history {getListensTrackList(session, user)};
|
||||
|
||||
std::vector<Wt::Dbo::ptr<Database::Release>> res;
|
||||
if (history)
|
||||
res = history->getTopReleases(clusterIds, range, moreResults);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
std::vector<Wt::Dbo::ptr<Database::Track>>
|
||||
Scrobbling::getTopTracks(Database::Session& session,
|
||||
Wt::Dbo::ptr<Database::User> user,
|
||||
const std::set<Database::IdType>& clusterIds,
|
||||
std::optional<Database::Range> range,
|
||||
bool& moreResults)
|
||||
{
|
||||
const Wt::Dbo::ptr<Database::TrackList> history {getListensTrackList(session, user)};
|
||||
|
||||
std::vector<Wt::Dbo::ptr<Database::Track>> res;
|
||||
if (history)
|
||||
res = history->getTopTracks(clusterIds, range, moreResults);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
Wt::Dbo::ptr<Database::TrackList>
|
||||
Scrobbling::getListensTrackList(Database::Session& session, Wt::Dbo::ptr<Database::User> user)
|
||||
{
|
||||
return _scrobblers[user->getScrobbler()]->getListensTrackList(session, user);
|
||||
}
|
||||
|
||||
} // ns Scrobbling
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* 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 "scrobbling/IScrobbling.hpp"
|
||||
#include "IScrobbler.hpp"
|
||||
|
||||
namespace Scrobbling
|
||||
{
|
||||
class Scrobbling : public IScrobbling
|
||||
{
|
||||
public:
|
||||
|
||||
Scrobbling(Database::Db& db);
|
||||
|
||||
private:
|
||||
void listenStarted(const Listen& listen) override;
|
||||
void listenFinished(const Listen& listen, std::chrono::seconds duration) override;
|
||||
void addListen(const Listen& listen, Wt::WDateTime timePoint) override;
|
||||
|
||||
std::vector<Wt::Dbo::ptr<Database::Artist>> getRecentArtists(Database::Session& session,
|
||||
Wt::Dbo::ptr<Database::User> user,
|
||||
const std::set<Database::IdType>& clusterIds,
|
||||
std::optional<Database::TrackArtistLinkType> linkType,
|
||||
std::optional<Database::Range> range,
|
||||
bool& moreResults) override;
|
||||
|
||||
std::vector<Wt::Dbo::ptr<Database::Release>> getRecentReleases(Database::Session& session,
|
||||
Wt::Dbo::ptr<Database::User> user,
|
||||
const std::set<Database::IdType>& clusterIds,
|
||||
std::optional<Database::Range> range,
|
||||
bool& moreResults) override;
|
||||
|
||||
std::vector<Wt::Dbo::ptr<Database::Track>> getRecentTracks(Database::Session& session,
|
||||
Wt::Dbo::ptr<Database::User> user,
|
||||
const std::set<Database::IdType>& clusterIds,
|
||||
std::optional<Database::Range> range,
|
||||
bool& moreResults) override;
|
||||
|
||||
std::vector<Wt::Dbo::ptr<Database::Artist>> getTopArtists(Database::Session& session,
|
||||
Wt::Dbo::ptr<Database::User> user,
|
||||
const std::set<Database::IdType>& clusterIds,
|
||||
std::optional<Database::TrackArtistLinkType> linkType,
|
||||
std::optional<Database::Range> range,
|
||||
bool& moreResults) override;
|
||||
|
||||
std::vector<Wt::Dbo::ptr<Database::Release>> getTopReleases(Database::Session& session,
|
||||
Wt::Dbo::ptr<Database::User> user,
|
||||
const std::set<Database::IdType>& clusterIds,
|
||||
std::optional<Database::Range> range,
|
||||
bool& moreResults) override;
|
||||
|
||||
std::vector<Wt::Dbo::ptr<Database::Track>> getTopTracks(Database::Session& session,
|
||||
Wt::Dbo::ptr<Database::User> user,
|
||||
const std::set<Database::IdType>& clusterIds,
|
||||
std::optional<Database::Range> range,
|
||||
bool& moreResults) override;
|
||||
|
||||
Wt::Dbo::ptr<Database::TrackList> getListensTrackList(Database::Session& session, Wt::Dbo::ptr<Database::User> user);
|
||||
|
||||
std::optional<Database::Scrobbler> getUserScrobbler(Database::IdType userId);
|
||||
|
||||
Database::Db& _db;
|
||||
std::unordered_map<Database::Scrobbler, std::unique_ptr<IScrobbler>> _scrobblers;
|
||||
};
|
||||
|
||||
} // ns Scrobbling
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* 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)
|
||||
{
|
||||
addListen(listen, Wt::WDateTime::currentDateTime());
|
||||
}
|
||||
|
||||
void
|
||||
InternalScrobbler::listenFinished(const Listen& /*event*/, std::chrono::seconds /* duration */)
|
||||
{
|
||||
// nothing to do
|
||||
}
|
||||
|
||||
void
|
||||
InternalScrobbler::addListen(const Listen& listen, const Wt::WDateTime& timePoint)
|
||||
{
|
||||
Database::Session& session {_db.getTLSSession()};
|
||||
|
||||
auto transaction {session.createUniqueTransaction()};
|
||||
|
||||
const Database::User::pointer user {Database::User::getById(session, listen.userId)};
|
||||
if (!user)
|
||||
return;
|
||||
|
||||
Wt::Dbo::ptr<Database::TrackList> 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), timePoint);
|
||||
}
|
||||
|
||||
Wt::Dbo::ptr<Database::TrackList>
|
||||
InternalScrobbler::getListensTrackList(Database::Session& session, Wt::Dbo::ptr<Database::User> user)
|
||||
{
|
||||
return Database::TrackList::get(session, historyTracklistName, Database::TrackList::Type::Internal, user);
|
||||
}
|
||||
|
||||
} // Scrobbling
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* 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 Scrobbling
|
||||
{
|
||||
class InternalScrobbler final : public IScrobbler
|
||||
{
|
||||
public:
|
||||
InternalScrobbler(Database::Db& db);
|
||||
|
||||
private:
|
||||
|
||||
void listenStarted(const Listen& listen) override;
|
||||
void listenFinished(const Listen& listen, std::chrono::seconds duration) override;
|
||||
|
||||
void addListen(const Listen& listen, const Wt::WDateTime& timePoint) override;
|
||||
|
||||
Wt::Dbo::ptr<Database::TrackList> getListensTrackList(Database::Session& session, Wt::Dbo::ptr<Database::User> user) override;
|
||||
|
||||
Database::Db& _db;
|
||||
};
|
||||
} // Scrobbling
|
||||
|
||||
@@ -0,0 +1,400 @@
|
||||
/*
|
||||
* 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/Logger.hpp"
|
||||
#include "utils/Service.hpp"
|
||||
|
||||
#define LOG(sev) LMS_LOG(SCROBBLING, sev) << "[listenbrainz] - "
|
||||
|
||||
namespace StringUtils
|
||||
{
|
||||
template<>
|
||||
std::optional<std::chrono::seconds>
|
||||
readAs(const std::string& str)
|
||||
{
|
||||
std::optional<std::chrono::seconds> res;
|
||||
|
||||
if (const std::optional<std::size_t> value {StringUtils::readAs<std::size_t>(str)})
|
||||
res = std::chrono::seconds {*value};
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
std::optional<UUID>
|
||||
getListenBrainzToken(Database::Session& session, Database::IdType 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();
|
||||
}
|
||||
|
||||
bool
|
||||
canBeScrobbled(Database::Session& session, Database::IdType 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});
|
||||
|
||||
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()}};
|
||||
}
|
||||
|
||||
if (!artists.empty())
|
||||
{
|
||||
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);
|
||||
|
||||
if (!artists.empty())
|
||||
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;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::optional<T>
|
||||
headerReadAs(const Wt::Http::Message& msg, std::string_view headerName)
|
||||
{
|
||||
std::optional<T> res;
|
||||
|
||||
if (const std::string* headerValue {msg.getHeader(std::string {headerName})})
|
||||
res = StringUtils::readAs<T>(*headerValue);
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
namespace Scrobbling
|
||||
{
|
||||
static const std::string historyTracklistName {"__scrobbler_listenbrainz_history__"};
|
||||
|
||||
ListenBrainzScrobbler::ListenBrainzScrobbler(Database::Db& db)
|
||||
: _apiEndpoint {Service<IConfig>::get()->getString("listenbrainz-api-url", "https://api.listenbrainz.org/1/")}
|
||||
, _db {db}
|
||||
{
|
||||
LOG(INFO) << "Starting ListenBrainz scrobbler... API endpoint = '" << _apiEndpoint << "'";
|
||||
|
||||
_client.done().connect([this](Wt::AsioWrapper::error_code ec, Wt::Http::Message msg)
|
||||
{
|
||||
onClientDone(ec, msg);
|
||||
});
|
||||
|
||||
_ioService.setThreadCount(1);
|
||||
_ioService.start();
|
||||
}
|
||||
|
||||
ListenBrainzScrobbler::~ListenBrainzScrobbler()
|
||||
{
|
||||
_ioService.stop();
|
||||
|
||||
LOG(INFO) << "Stopped ListenBrainz scrobbler";
|
||||
}
|
||||
|
||||
void
|
||||
ListenBrainzScrobbler::listenStarted(const Listen& listen)
|
||||
{
|
||||
_ioService.post([=]
|
||||
{
|
||||
enqueListen(listen, Wt::WDateTime {});
|
||||
});
|
||||
}
|
||||
|
||||
void
|
||||
ListenBrainzScrobbler::listenFinished(const Listen& listen, std::chrono::seconds duration)
|
||||
{
|
||||
if (!canBeScrobbled(_db.getTLSSession(), listen.trackId, duration))
|
||||
return;
|
||||
|
||||
Listen timedListen {listen};
|
||||
const Wt::WDateTime now {Wt::WDateTime::currentDateTime().addSecs(-duration.count())};
|
||||
|
||||
_ioService.post([=]
|
||||
{
|
||||
enqueListen(timedListen, now);
|
||||
});
|
||||
}
|
||||
|
||||
void
|
||||
ListenBrainzScrobbler::addListen(const Listen& listen, const Wt::WDateTime& timePoint)
|
||||
{
|
||||
assert(timePoint.isValid());
|
||||
|
||||
_ioService.post([=]
|
||||
{
|
||||
enqueListen(listen, timePoint);
|
||||
});
|
||||
}
|
||||
|
||||
Wt::Dbo::ptr<Database::TrackList>
|
||||
ListenBrainzScrobbler::getListensTrackList(Database::Session& session, Wt::Dbo::ptr<Database::User> user)
|
||||
{
|
||||
return Database::TrackList::get(session, historyTracklistName, Database::TrackList::Type::Internal, user);
|
||||
}
|
||||
|
||||
void
|
||||
ListenBrainzScrobbler::enqueListen(const Listen& listen, const Wt::WDateTime& timePoint)
|
||||
{
|
||||
if (!timePoint.isValid())
|
||||
{
|
||||
// If we are currently throttled, just replace the entry if it has no timePoint
|
||||
// in order to only report the newest track listened to
|
||||
// If not throttled, just search past the next current first message as it is being sent
|
||||
|
||||
const std::size_t offset {_state == State::Throttled ? std::size_t {0} : std::size_t {1}};
|
||||
if (_sendQueue.size() > offset)
|
||||
{
|
||||
_sendQueue.erase(std::remove_if(std::next(std::begin(_sendQueue), offset), std::end(_sendQueue),
|
||||
[&](const QueuedListen& queuedListen) { return queuedListen.listen.userId == listen.userId && !queuedListen.timePoint.isValid(); }), std::end(_sendQueue));
|
||||
}
|
||||
}
|
||||
|
||||
_sendQueue.emplace_back(QueuedListen {listen, timePoint});
|
||||
|
||||
if (_state == State::Idle)
|
||||
sendNextQueuedListen();
|
||||
}
|
||||
|
||||
void
|
||||
ListenBrainzScrobbler::sendNextQueuedListen()
|
||||
{
|
||||
assert(_state == State::Idle);
|
||||
if (_sendQueue.empty())
|
||||
return;
|
||||
|
||||
sendListen(_sendQueue.front().listen, _sendQueue.front().timePoint);
|
||||
_state = State::Sending;
|
||||
}
|
||||
|
||||
void
|
||||
ListenBrainzScrobbler::sendListen(const Listen& listen, const Wt::WDateTime& timePoint)
|
||||
{
|
||||
Database::Session& session {_db.getTLSSession()};
|
||||
|
||||
const std::optional<UUID> listenBrainzToken {getListenBrainzToken(session, listen.userId)};
|
||||
if (!listenBrainzToken)
|
||||
return;
|
||||
|
||||
std::string payload {listenToJsonString(session, listen, timePoint, timePoint.isValid() ? "single" : "playing_now")};
|
||||
if (payload.empty())
|
||||
{
|
||||
LOG(DEBUG) << "Cannot convert listen to json: skipping";
|
||||
return;
|
||||
}
|
||||
|
||||
// now send this
|
||||
Wt::Http::Message message;
|
||||
message.addHeader("Authorization", "Token " + std::string {listenBrainzToken->getAsString()});
|
||||
message.addBodyText(payload);
|
||||
|
||||
const std::string endPoint {_apiEndpoint + "submit-listens"};
|
||||
if (!_client.post(endPoint, message))
|
||||
LOG(ERROR) << "Cannot post to '" << endPoint << "': invalid scheme or URL?";
|
||||
|
||||
LOG(DEBUG) << "POST done to '" << endPoint << "'";
|
||||
}
|
||||
|
||||
void
|
||||
ListenBrainzScrobbler::onClientDone(Wt::AsioWrapper::error_code ec, const Wt::Http::Message& msg)
|
||||
{
|
||||
assert(!_sendQueue.empty());
|
||||
QueuedListen& queuedListen {_sendQueue.front()};
|
||||
|
||||
_state = State::Idle;
|
||||
|
||||
LOG(DEBUG) << "POST done. status = " << msg.status() << ", msg = '" << msg.body() << "'";
|
||||
if (ec)
|
||||
{
|
||||
LOG(ERROR) << "Client error: " << ec.message();
|
||||
// may be a network error, try again later
|
||||
if (++queuedListen.retryCount > _maxRetryCount)
|
||||
_sendQueue.pop_front();
|
||||
|
||||
throttle(_defaultRetryWaitDuration);
|
||||
return;
|
||||
}
|
||||
|
||||
bool mustThrottle{};
|
||||
|
||||
switch (msg.status())
|
||||
{
|
||||
case 429:
|
||||
mustThrottle = true;
|
||||
break;
|
||||
|
||||
case 200:
|
||||
if (queuedListen.timePoint.isValid())
|
||||
cacheListen(queuedListen.listen, queuedListen.timePoint);
|
||||
_sendQueue.pop_front();
|
||||
break;
|
||||
|
||||
default:
|
||||
LOG(ERROR) << "Submit error: '" << msg.body() << "'";
|
||||
_sendQueue.pop_front();
|
||||
break;
|
||||
}
|
||||
|
||||
const auto remainingCount {headerReadAs<std::size_t>(msg, "X-RateLimit-Remaining")};
|
||||
LOG(DEBUG) << "Remaining messages = " << (remainingCount ? *remainingCount : 0);
|
||||
if (mustThrottle || (remainingCount && *remainingCount == 0))
|
||||
{
|
||||
const auto waitDuration {headerReadAs<std::chrono::seconds>(msg, "X-RateLimit-Reset-In")};
|
||||
throttle(waitDuration.value_or(_defaultRetryWaitDuration));
|
||||
}
|
||||
else
|
||||
{
|
||||
sendNextQueuedListen();
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
ListenBrainzScrobbler::throttle(std::chrono::seconds requestedDuration)
|
||||
{
|
||||
assert(_state == State::Idle);
|
||||
|
||||
const std::chrono::seconds duration {requestedDuration.count() > 0 ? requestedDuration : std::chrono::seconds {1}};
|
||||
LOG(DEBUG) << "Throttling for " << duration.count() << " seconds";
|
||||
|
||||
_ioService.schedule(duration, [this]
|
||||
{
|
||||
_state = State::Idle;
|
||||
sendNextQueuedListen();
|
||||
});
|
||||
_state = State::Throttled;
|
||||
}
|
||||
|
||||
void
|
||||
ListenBrainzScrobbler::cacheListen(const Listen& listen, const Wt::WDateTime& timePoint)
|
||||
{
|
||||
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::TrackList::pointer tracklist {getListensTrackList(session, user)};
|
||||
if (!tracklist)
|
||||
tracklist = Database::TrackList::create(session, historyTracklistName, Database::TrackList::Type::Internal, false, user);
|
||||
|
||||
Database::TrackListEntry::create(session, track, getListensTrackList(session, user), timePoint);
|
||||
}
|
||||
|
||||
} // Scrobbling
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* 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 <deque>
|
||||
#include <mutex>
|
||||
|
||||
#include <Wt/Http/Client.h>
|
||||
#include <Wt/WIOService.h>
|
||||
|
||||
#include "IScrobbler.hpp"
|
||||
|
||||
namespace Database
|
||||
{
|
||||
class Db;
|
||||
class Session;
|
||||
class TrackList;
|
||||
}
|
||||
|
||||
namespace Scrobbling
|
||||
{
|
||||
class ListenBrainzScrobbler final : public IScrobbler
|
||||
{
|
||||
public:
|
||||
ListenBrainzScrobbler(Database::Db& db);
|
||||
~ListenBrainzScrobbler();
|
||||
|
||||
ListenBrainzScrobbler(const ListenBrainzScrobbler&) = delete;
|
||||
ListenBrainzScrobbler(const ListenBrainzScrobbler&&) = delete;
|
||||
ListenBrainzScrobbler& operator=(const ListenBrainzScrobbler&) = delete;
|
||||
ListenBrainzScrobbler& operator=(const ListenBrainzScrobbler&&) = delete;
|
||||
|
||||
private:
|
||||
void listenStarted(const Listen& listen) override;
|
||||
void listenFinished(const Listen& listen, std::chrono::seconds duration) override;
|
||||
void addListen(const Listen& listen, const Wt::WDateTime& timePoint) override;
|
||||
|
||||
Wt::Dbo::ptr<Database::TrackList> getListensTrackList(Database::Session& session, Wt::Dbo::ptr<Database::User> user) override;
|
||||
|
||||
void enqueListen(const Listen& listen, const Wt::WDateTime& timePoint);
|
||||
void sendNextQueuedListen();
|
||||
void sendListen(const Listen& listen, const Wt::WDateTime& timePoint);
|
||||
void onClientDone(Wt::AsioWrapper::error_code ec, const Wt::Http::Message& msg);
|
||||
void throttle(std::chrono::seconds duration);
|
||||
|
||||
void cacheListen(const Listen& listen, const Wt::WDateTime& timePoint);
|
||||
|
||||
enum class State
|
||||
{
|
||||
Idle,
|
||||
Throttled,
|
||||
Sending,
|
||||
};
|
||||
State _state {State::Idle};
|
||||
|
||||
const std::string _apiEndpoint;
|
||||
const std::size_t _maxRetryCount {2};
|
||||
const std::chrono::seconds _defaultRetryWaitDuration {30};
|
||||
|
||||
Database::Db& _db;
|
||||
Wt::WIOService _ioService;
|
||||
Wt::Http::Client _client {_ioService};
|
||||
|
||||
struct QueuedListen
|
||||
{
|
||||
Listen listen;
|
||||
Wt::WDateTime timePoint;
|
||||
std::size_t retryCount {};
|
||||
};
|
||||
std::deque<QueuedListen> _sendQueue;
|
||||
};
|
||||
} // Scrobbling
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* 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 <vector>
|
||||
#include <set>
|
||||
#include <string_view>
|
||||
|
||||
#include <Wt/WDateTime.h>
|
||||
|
||||
#include "scrobbling/Listen.hpp"
|
||||
|
||||
namespace Database
|
||||
{
|
||||
class Artist;
|
||||
class Db;
|
||||
class Release;
|
||||
class Session;
|
||||
class Track;
|
||||
class User;
|
||||
}
|
||||
|
||||
namespace Scrobbling
|
||||
{
|
||||
|
||||
class IScrobbling
|
||||
{
|
||||
public:
|
||||
virtual ~IScrobbling() = default;
|
||||
|
||||
// Scrobbling
|
||||
virtual void listenStarted(const Listen& listen) = 0;
|
||||
virtual void listenFinished(const Listen& listen, std::chrono::seconds duration) = 0;
|
||||
|
||||
virtual void addListen(const Listen& listen, Wt::WDateTime timePoint) = 0;
|
||||
|
||||
// Stats
|
||||
// From most recent to oldest
|
||||
virtual std::vector<Wt::Dbo::ptr<Database::Artist>> getRecentArtists(Database::Session& session,
|
||||
Wt::Dbo::ptr<Database::User> user,
|
||||
const std::set<Database::IdType>& clusterIds,
|
||||
std::optional<Database::TrackArtistLinkType> linkType,
|
||||
std::optional<Database::Range> range,
|
||||
bool& moreResults) = 0;
|
||||
|
||||
virtual std::vector<Wt::Dbo::ptr<Database::Release>> getRecentReleases(Database::Session& session,
|
||||
Wt::Dbo::ptr<Database::User> user,
|
||||
const std::set<Database::IdType>& clusterIds,
|
||||
std::optional<Database::Range> range,
|
||||
bool& moreResults) = 0;
|
||||
|
||||
virtual std::vector<Wt::Dbo::ptr<Database::Track>> getRecentTracks(Database::Session& session,
|
||||
Wt::Dbo::ptr<Database::User> user,
|
||||
const std::set<Database::IdType>& clusterIds,
|
||||
std::optional<Database::Range> range,
|
||||
bool& moreResults) = 0;
|
||||
|
||||
// Top
|
||||
virtual std::vector<Wt::Dbo::ptr<Database::Artist>> getTopArtists(Database::Session& session,
|
||||
Wt::Dbo::ptr<Database::User> user,
|
||||
const std::set<Database::IdType>& clusterIds,
|
||||
std::optional<Database::TrackArtistLinkType> linkType,
|
||||
std::optional<Database::Range> range,
|
||||
bool& moreResults) = 0;
|
||||
|
||||
virtual std::vector<Wt::Dbo::ptr<Database::Release>> getTopReleases(Database::Session& session,
|
||||
Wt::Dbo::ptr<Database::User> user,
|
||||
const std::set<Database::IdType>& clusterIds,
|
||||
std::optional<Database::Range> range,
|
||||
bool& moreResults) = 0;
|
||||
|
||||
virtual std::vector<Wt::Dbo::ptr<Database::Track>> getTopTracks(Database::Session& session,
|
||||
Wt::Dbo::ptr<Database::User> user,
|
||||
const std::set<Database::IdType>& clusterIds,
|
||||
std::optional<Database::Range> range,
|
||||
bool& moreResults) = 0;
|
||||
};
|
||||
|
||||
std::unique_ptr<IScrobbling> createScrobbling(Database::Db& db);
|
||||
|
||||
} // ns Scrobbling
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* 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 "database/Types.hpp"
|
||||
|
||||
namespace Scrobbling
|
||||
{
|
||||
struct Listen
|
||||
{
|
||||
Database::IdType userId {};
|
||||
Database::IdType trackId {};
|
||||
};
|
||||
} // ns Scrobbling
|
||||
|
||||
Reference in New Issue
Block a user