Added podcast support, only from subsonic API for now, ref #726

This commit is contained in:
emeric
2025-09-13 15:04:13 +02:00
parent 1d584ebcc6
commit 932e7715f5
111 changed files with 4717 additions and 188 deletions
+1
View File
@@ -1,6 +1,7 @@
add_subdirectory(artwork)
add_subdirectory(auth)
add_subdirectory(feedback)
add_subdirectory(podcast)
add_subdirectory(recommendation)
add_subdirectory(scanner)
add_subdirectory(scrobbling)
@@ -63,7 +63,7 @@ namespace lms::artwork
ArtworkService::~ArtworkService() = default;
std::unique_ptr<image::IEncodedImage> ArtworkService::getFromImageFile(const std::filesystem::path& p, std::optional<image::ImageSize> width) const
std::unique_ptr<image::IEncodedImage> ArtworkService::getFromImageFile(const std::filesystem::path& p, std::string_view mimeType, std::optional<image::ImageSize> width) const
{
std::unique_ptr<image::IEncodedImage> image;
@@ -71,7 +71,7 @@ namespace lms::artwork
{
if (!width)
{
image = image::readImage(p);
image = image::readImage(p, mimeType);
}
else
{
@@ -177,8 +177,7 @@ namespace lms::artwork
if (image)
return image;
db::TrackEmbeddedImageId trackEmbeddedImageId;
db::ImageId imageId;
db::Artwork::UnderlyingId underlyingArtworkId;
{
db::Session& session{ _db.getTLSSession() };
@@ -186,16 +185,13 @@ namespace lms::artwork
db::Artwork::pointer artwork{ db::Artwork::find(session, artworkId) };
if (artwork)
{
trackEmbeddedImageId = artwork->getTrackEmbeddedImageId();
imageId = artwork->getImageId();
}
underlyingArtworkId = artwork->getUnderlyingId();
}
if (trackEmbeddedImageId.isValid())
image = getTrackEmbeddedImage(trackEmbeddedImageId, width);
else if (imageId.isValid())
image = getImage(imageId, width);
if (const auto* trackEmbeddedImageId = std::get_if<db::TrackEmbeddedImageId>(&underlyingArtworkId))
image = getTrackEmbeddedImage(*trackEmbeddedImageId, width);
else if (const auto* imageId = std::get_if<db::ImageId>(&underlyingArtworkId))
image = getImage(*imageId, width);
if (image)
_cache.addImage(cacheEntryDesc, image);
@@ -206,6 +202,7 @@ namespace lms::artwork
std::shared_ptr<image::IEncodedImage> ArtworkService::getImage(db::ImageId imageId, std::optional<image::ImageSize> width)
{
std::filesystem::path imageFile;
std::string mimeType;
{
db::Session& session{ _db.getTLSSession() };
auto transaction{ session.createReadTransaction() };
@@ -215,9 +212,10 @@ namespace lms::artwork
return nullptr;
imageFile = image->getAbsoluteFilePath();
mimeType = image->getMimeType();
}
return getFromImageFile(imageFile, width);
return getFromImageFile(imageFile, mimeType, width);
}
std::shared_ptr<image::IEncodedImage> ArtworkService::getTrackEmbeddedImage(db::TrackEmbeddedImageId trackEmbeddedImageId, std::optional<image::ImageSize> width)
@@ -62,7 +62,7 @@ namespace lms::artwork
std::shared_ptr<image::IEncodedImage> getImage(db::ImageId imageId, std::optional<image::ImageSize> width);
std::shared_ptr<image::IEncodedImage> getTrackEmbeddedImage(db::TrackEmbeddedImageId trackEmbeddedImageId, std::optional<image::ImageSize> width);
std::unique_ptr<image::IEncodedImage> getFromImageFile(const std::filesystem::path& p, std::optional<image::ImageSize> width) const;
std::unique_ptr<image::IEncodedImage> getFromImageFile(const std::filesystem::path& p, std::string_view mimeType, std::optional<image::ImageSize> width) const;
std::unique_ptr<image::IEncodedImage> getTrackImage(const std::filesystem::path& path, std::size_t index, std::optional<image::ImageSize> width) const;
db::IDb& _db;
@@ -133,7 +133,7 @@ namespace lms::feedback::listenBrainz
request.message.addBodyText(Wt::Json::serialize(root));
request.message.addHeader("Content-Type", "application/json");
request.onSuccessFunc = [this, type, starredTrackId](std::string_view /*msgBody*/) {
request.onSuccessFunc = [this, type, starredTrackId](const Wt::Http::Message&) {
boost::asio::post(boost::asio::bind_executor(_strand, [this, type, starredTrackId] {
onFeedbackSent(type, starredTrackId);
}));
@@ -321,8 +321,8 @@ namespace lms::feedback::listenBrainz
request.priority = core::http::ClientRequestParameters::Priority::Low;
request.relativeUrl = "/1/validate-token";
request.headers = { { "Authorization", "Token " + std::string{ listenBrainzToken->getAsString() } } };
request.onSuccessFunc = [this, &context](std::string_view msgBody) {
context.listenBrainzUserName = utils::parseValidateToken(msgBody);
request.onSuccessFunc = [this, &context](const Wt::Http::Message& msg) {
context.listenBrainzUserName = utils::parseValidateToken(msg.body());
if (context.listenBrainzUserName.empty())
{
onSyncEnded(context);
@@ -344,8 +344,8 @@ namespace lms::feedback::listenBrainz
core::http::ClientGETRequestParameters request;
request.relativeUrl = "/1/feedback/user/" + std::string{ context.listenBrainzUserName } + "/get-feedback?score=1&count=0";
request.priority = core::http::ClientRequestParameters::Priority::Low;
request.onSuccessFunc = [this, &context](std::string_view msgBody) {
std::string msgBodyCopy{ msgBody };
request.onSuccessFunc = [this, &context](const Wt::Http::Message& msg) {
std::string msgBodyCopy{ msg.body() };
boost::asio::post(boost::asio::bind_executor(_strand, [this, msgBodyCopy, &context] {
LOG(DEBUG, "Current feedback count = " << (context.feedbackCount ? *context.feedbackCount : 0) << " for user '" << context.listenBrainzUserName << "'");
@@ -376,8 +376,8 @@ namespace lms::feedback::listenBrainz
core::http::ClientGETRequestParameters request;
request.relativeUrl = "/1/feedback/user/" + context.listenBrainzUserName + "/get-feedback?offset=" + std::to_string(context.fetchedFeedbackCount);
request.priority = core::http::ClientRequestParameters::Priority::Low;
request.onSuccessFunc = [this, &context](std::string_view msgBody) {
std::string msgBodyCopy{ msgBody };
request.onSuccessFunc = [this, &context](const Wt::Http::Message& msg) {
std::string msgBodyCopy{ msg.body() };
boost::asio::post(boost::asio::bind_executor(_strand, [this, msgBodyCopy, &context] {
const std::size_t fetchedFeedbackCount{ processGetFeedbacks(msgBodyCopy, context) };
if (fetchedFeedbackCount == 0 // no more thing available on server
+40
View File
@@ -0,0 +1,40 @@
pkg_check_modules(PUGIXML REQUIRED IMPORTED_TARGET pugixml)
add_library(lmspodcast STATIC
impl/steps/CheckForMissingFilesStep.cpp
impl/steps/ClearTmpDirectoryStep.cpp
impl/steps/DownloadEpisodeArtworksStep.cpp
impl/steps/DownloadEpisodesStep.cpp
impl/steps/DownloadPodcastArtworksStep.cpp
impl/steps/RefreshPodcastsStep.cpp
impl/steps/RemoveEpisodesStep.cpp
impl/steps/RemovePodcastsStep.cpp
impl/steps/Utils.cpp
impl/Executor.cpp
impl/PodcastParsing.cpp
impl/PodcastService.cpp
)
target_include_directories(lmspodcast INTERFACE
include
)
target_include_directories(lmspodcast PRIVATE
include
impl
${PUGIXML_INCLUDE_DIRS}
)
target_link_libraries(lmspodcast PRIVATE
lmscore
lmsimage
PkgConfig::PUGIXML
)
target_link_libraries(lmspodcast PUBLIC
lmsdatabase
)
if(BUILD_TESTING)
add_subdirectory(test)
endif()
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2025 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 "core/Exception.hpp"
namespace lms::podcast
{
class Exception : public core::LmsException
{
public:
using LmsException::LmsException;
};
} // namespace lms::podcast
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2025 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 "Executor.hpp"
#include <boost/asio/io_context.hpp>
#include <boost/asio/bind_executor.hpp>
#include <boost/asio/post.hpp>
namespace lms::podcast
{
Executor::Executor(boost::asio::io_context& ioContext)
: _strand{ ioContext }
{
}
void Executor::post(std::function<void()> callback)
{
boost::asio::post(boost::asio::bind_executor(_strand, std::move(callback)));
}
} // namespace lms::podcast
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2025 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/strand.hpp>
namespace lms::podcast
{
class Executor
{
public:
Executor(boost::asio::io_context& ioContext);
void post(std::function<void()> callback);
private:
boost::asio::io_context::strand _strand;
};
} // namespace lms::podcast
@@ -0,0 +1,202 @@
/*
* Copyright (C) 2025 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 "PodcastParsing.hpp"
#include <charconv>
#include <optional>
#include <pugixml.hpp>
#include "core/ILogger.hpp"
#include "core/String.hpp"
namespace lms::podcast
{
namespace
{
std::optional<std::chrono::seconds> parseDuration(std::string_view str)
{
auto parse_int{ [](std::string_view sv) -> std::optional<int> {
int value{};
const auto [ptr, ec]{ std::from_chars(sv.data(), sv.data() + sv.size(), value) };
return (ec == std::errc()) ? std::optional{ value } : std::nullopt;
} };
std::array<int, 3> parts{ 0, 0, 0 };
int index{ 3 };
while (!str.empty() && --index >= 0)
{
const std::size_t pos{ str.rfind(':') };
const std::string_view token{ (pos == std::string_view::npos) ? str : str.substr(pos + 1) };
const auto val{ parse_int(token) };
if (!val)
return std::nullopt;
parts[index] = *val;
if (pos == std::string_view::npos)
break;
str.remove_suffix(str.size() - pos);
}
return std::chrono::hours{ parts[0] } + std::chrono::minutes{ parts[1] } + std::chrono::seconds{ parts[2] };
}
std::optional<std::chrono::seconds> getDuration(const pugi::xml_node& node, const char* tag)
{
std::optional<std::chrono::seconds> res;
if (const pugi::xml_node child{ node.child(tag) })
{
std::string_view value{ child.child_value() };
res = parseDuration(value);
}
return res;
}
std::optional<bool> getBool(const pugi::xml_node& node, const char* tag)
{
std::optional<bool> res;
if (const pugi::xml_node child{ node.child(tag) })
{
std::string_view value{ child.child_value() };
if (value == "true" || value == "1" || value == "on" || value == "yes")
res = true;
else if (value == "false" || value == "0" || value == "off" || value == "no")
res = false;
}
return res;
}
std::string_view getText(const pugi::xml_node& node, const char* tag)
{
std::string_view res;
if (const pugi::xml_node child{ node.child(tag) })
res = child.child_value();
return res;
}
std::string getRawText(const pugi::xml_node& node, const char* tag)
{
std::string res;
if (const pugi::xml_node child{ node.child(tag) })
{
std::ostringstream oss;
for (const pugi::xml_node& n : child.children())
n.print(oss, "", pugi::format_raw);
res = oss.str();
}
return res;
}
std::string_view getAttribute(const pugi::xml_node& node, const char* tag, const char* attribute)
{
std::string_view res;
if (const pugi::xml_node child{ node.child(tag) })
res = child.attribute(attribute).value();
return res;
}
} // namespace
Podcast parsePodcastRssFeed(std::string_view rssXml)
{
Podcast podcast;
pugi::xml_document doc;
pugi::xml_parse_result result{ doc.load_buffer(rssXml.data(), rssXml.size()) };
if (!result)
{
LMS_LOG(METADATA, ERROR, "Cannot read xml: " << result.description());
throw ParseException{ result.description() };
}
const pugi::xml_node channel{ doc.child("rss").child("channel") };
if (!channel)
throw ParseException{ "No <channel> element found in podcast XML" };
podcast.title = getText(channel, "title");
podcast.link = getText(channel, "link");
podcast.description = getRawText(channel, "description");
podcast.language = getText(channel, "language");
podcast.copyright = getText(channel, "copyright");
podcast.lastBuildDate = core::stringUtils::fromRFC822String(getText(channel, "lastBuildDate"));
// itunes fields
podcast.newUrl = getText(channel, "itunes:new-feed-url");
podcast.author = getText(channel, "itunes:author");
podcast.category = getAttribute(channel, "itunes:category", "text");
podcast.imageUrl = getText(channel, "itunes:image");
if (podcast.imageUrl.empty())
{
if (const pugi::xml_node image{ channel.child("image") })
podcast.imageUrl = getText(image, "url");
}
if (const pugi::xml_node owner{ channel.child("itunes:owner") })
{
podcast.ownerEmail = getText(owner, "itunes:email");
podcast.ownerName = getText(owner, "itunes:name");
}
podcast.subtitle = getText(channel, "itunes:subtitle");
podcast.summary = getRawText(channel, "itunes:summary");
podcast.explicitContent = getBool(channel, "itunes:explicit");
// parse nested episodes
for (pugi::xml_node episode{ channel.child("item") }; episode; episode = episode.next_sibling("item"))
{
PodcastEpisode e;
e.title = getText(episode, "title");
// <enclosure url="https://proxycast.radiofrance.fr/e2a713a6-aba0-4d2a-a4a1-d135d98f1f8a/13940-09.08.2025-ITEMA_24214125-2021F22805S0364-NET_MFI_F7191B05-DD5B-4AFE-BB96-3BD8ADB3240A-22.mp3" length="51842568" type="audio/mpeg"/>
if (const pugi::xml_node enclosure{ episode.child("enclosure") })
e.url = getText(enclosure, "url");
e.pubDate = core::stringUtils::fromRFC822String(getText(episode, "pubDate"));
e.description = getRawText(episode, "description");
e.link = getText(episode, "link");
e.author = getText(episode, "itunes:author");
if (e.author.empty())
e.author = getText(episode, "author");
e.enclosureUrl.url = getAttribute(episode, "enclosure", "url");
e.enclosureUrl.length = core::stringUtils::readAs<std::size_t>(getAttribute(episode, "enclosure", "length")).value_or(0);
e.enclosureUrl.type = getAttribute(episode, "enclosure", "type");
e.category = getAttribute(episode, "itunes:category", "text");
e.duration = getDuration(episode, "itunes:duration").value_or(std::chrono::seconds::zero());
e.guid = getText(episode, "guid");
e.imageUrl = getAttribute(episode, "itunes:image", "href");
e.explicitContent = getBool(episode, "itunes:explicit");
podcast.episodes.push_back(std::move(e));
}
return podcast;
}
} // namespace lms::podcast
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2025 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 <string_view>
#include "Exception.hpp"
#include "PodcastTypes.hpp"
namespace lms::podcast
{
class ParseException : public Exception
{
public:
using Exception::Exception;
};
Podcast parsePodcastRssFeed(std::string_view rssXml);
} // namespace lms::podcast
@@ -0,0 +1,330 @@
/*
* Copyright (C) 2025 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 "PodcastService.hpp"
#include <filesystem>
#include "core/IConfig.hpp"
#include "core/ILogger.hpp"
#include "core/ITraceLogger.hpp"
#include "core/Service.hpp"
#include "core/http/IClient.hpp"
#include "database/IDb.hpp"
#include "database/Session.hpp"
#include "database/objects/Podcast.hpp"
#include "database/objects/PodcastEpisode.hpp"
#include "steps/CheckForMissingFilesStep.hpp"
#include "steps/ClearTmpDirectoryStep.hpp"
#include "steps/DownloadEpisodeArtworksStep.hpp"
#include "steps/DownloadEpisodesStep.hpp"
#include "steps/DownloadPodcastArtworksStep.hpp"
#include "steps/RefreshPodcastsStep.hpp"
#include "steps/RemoveEpisodesStep.hpp"
#include "steps/RemovePodcastsStep.hpp"
#include "Exception.hpp"
namespace lms::podcast
{
std::unique_ptr<IPodcastService> createPodcastService(boost::asio::io_context& ioContext, db::IDb& db, const std::filesystem::path& cachePath)
{
return std::make_unique<PodcastService>(ioContext, db, cachePath);
}
PodcastService::PodcastService(boost::asio::io_context& ioContext, db::IDb& db, const std::filesystem::path& cachePath)
: _executor{ ioContext }
, _refreshTimer(ioContext)
, _httpClient{ core::http::createClient(ioContext, "") }
, _refreshContext{ _executor, db, *_httpClient, cachePath }
, _refreshPeriod{ core::Service<core::IConfig>::get()->getULong("podcast-refresh-period-hours", 2) }
, _refreshInProgress{ false }
, _abortRequested{ false }
, _refreshStepIndex{ 0 }
{
if (_refreshPeriod.count() < 1)
{
LMS_LOG(PODCAST, ERROR, "Podcast refresh period must be at least 1 hour");
throw Exception{ "Podcast refresh period must be at least 1 hour" };
}
setupSteps();
std::filesystem::create_directories(_refreshContext.cachePath);
std::filesystem::create_directories(_refreshContext.tmpCachePath);
LMS_LOG(PODCAST, INFO, "Starting service...");
scheduleRefresh(std::chrono::seconds{ 1 });
LMS_LOG(PODCAST, INFO, "Service started!");
}
PodcastService::~PodcastService()
{
std::unique_lock lock{ _controlMutex };
abortCurrentRefresh(lock);
LMS_LOG(PODCAST, INFO, "Service stopped!");
}
std::filesystem::path PodcastService::getCachePath() const
{
return _refreshContext.cachePath;
}
db::PodcastId PodcastService::addPodcast(std::string_view url)
{
std::unique_lock lock{ _controlMutex };
abortCurrentRefresh(lock);
db::PodcastId podcastId;
{
db::Session& session{ _refreshContext.db.getTLSSession() };
auto transaction{ session.createWriteTransaction() };
db::Podcast::pointer podcast{ db::Podcast::find(session, url) };
if (!podcast)
podcast = session.create<db::Podcast>(url);
podcastId = podcast->getId();
}
allowRefresh();
scheduleRefresh();
return podcastId;
}
bool PodcastService::removePodcast(db::PodcastId podcastId)
{
bool res{};
std::unique_lock lock{ _controlMutex };
abortCurrentRefresh(lock);
{
db::Session& session{ _refreshContext.db.getTLSSession() };
auto transaction{ session.createWriteTransaction() };
db::Podcast::pointer podcast{ db::Podcast::find(session, podcastId) };
if (podcast)
{
podcast.modify()->setDeleteRequested(true);
res = true;
}
}
allowRefresh();
scheduleRefresh();
return res;
}
void PodcastService::refreshPodcasts()
{
std::unique_lock lock{ _controlMutex };
abortCurrentRefresh(lock);
allowRefresh();
scheduleRefresh();
}
bool PodcastService::downloadPodcastEpisode(db::PodcastEpisodeId episodeId)
{
bool res{};
std::unique_lock lock{ _controlMutex };
abortCurrentRefresh(lock);
{
db::Session& session{ _refreshContext.db.getTLSSession() };
auto transaction{ session.createWriteTransaction() };
db::PodcastEpisode::pointer episode{ db::PodcastEpisode::find(session, episodeId) };
if (episode)
{
episode.modify()->setManualDownloadState(db::PodcastEpisode::ManualDownloadState::DownloadRequested);
res = true;
}
}
allowRefresh();
scheduleRefresh();
return res;
}
bool PodcastService::deletePodcastEpisode(db::PodcastEpisodeId episodeId)
{
bool res{};
std::unique_lock lock{ _controlMutex };
abortCurrentRefresh(lock);
{
db::Session& session{ _refreshContext.db.getTLSSession() };
auto transaction{ session.createWriteTransaction() };
db::PodcastEpisode::pointer episode{ db::PodcastEpisode::find(session, episodeId) };
if (episode)
{
episode.modify()->setManualDownloadState(db::PodcastEpisode::ManualDownloadState::DeleteRequested);
res = true;
}
}
allowRefresh();
scheduleRefresh();
return res;
}
bool PodcastService::hasPodcasts() const
{
db::Session& session{ _refreshContext.db.getTLSSession() };
auto transaction{ session.createReadTransaction() };
return db::Podcast::getCount(session) > 0;
}
void PodcastService::abortCurrentRefresh(std::unique_lock<std::mutex>& lock)
{
LMS_LOG(PODCAST, DEBUG, "Aborting current refresh...");
_abortRequested = true;
for (auto& step : _refreshSteps)
step->requestAbort(true);
_httpClient->abortAllRequests();
_refreshTimer.cancel();
_controlCv.wait(lock, [this] {
return !_refreshInProgress;
});
LMS_LOG(PODCAST, DEBUG, "Current refresh aborted!");
}
void PodcastService::allowRefresh()
{
assert(!_refreshInProgress);
assert(_abortRequested);
_abortRequested = false;
for (auto& step : _refreshSteps)
step->requestAbort(false);
}
void PodcastService::scheduleRefresh(std::chrono::seconds fromNow)
{
if (!hasPodcasts())
{
LMS_LOG(PODCAST, DEBUG, "No podcast: not scheduling refresh");
return;
}
LMS_LOG(PODCAST, DEBUG, "Scheduled podcast refresh in " << fromNow.count() << " seconds...");
_refreshTimer.expires_after(fromNow);
_refreshTimer.async_wait([this](const boost::system::error_code& ec) {
if (ec == boost::asio::error::operation_aborted)
return;
if (ec)
throw Exception{ "Steady timer failure: " + std::string{ ec.message() } };
_executor.post([this] { startRefresh(); });
});
}
void PodcastService::startRefresh()
{
LMS_LOG(PODCAST, DEBUG, "Starting podcast refresh");
_refreshInProgress = true;
_refreshStepIndex = 0;
runStep(_refreshStepIndex);
}
void PodcastService::setupSteps()
{
auto onDoneCallback{ [this](bool success) {
onCurrentStepDone(success);
} };
_refreshSteps.clear();
// order is important, each step is done only when the previous one is done
_refreshSteps.emplace_back(std::make_unique<ClearTmpDirectoryStep>(_refreshContext, onDoneCallback));
_refreshSteps.emplace_back(std::make_unique<CheckForMissingFilesStep>(_refreshContext, onDoneCallback));
_refreshSteps.emplace_back(std::make_unique<RefreshPodcastsStep>(_refreshContext, onDoneCallback));
_refreshSteps.emplace_back(std::make_unique<RemovePodcastsStep>(_refreshContext, onDoneCallback));
_refreshSteps.emplace_back(std::make_unique<RemoveEpisodesStep>(_refreshContext, onDoneCallback));
_refreshSteps.emplace_back(std::make_unique<DownloadPodcastArtworksStep>(_refreshContext, onDoneCallback));
_refreshSteps.emplace_back(std::make_unique<DownloadEpisodeArtworksStep>(_refreshContext, onDoneCallback));
_refreshSteps.emplace_back(std::make_unique<DownloadEpisodesStep>(_refreshContext, onDoneCallback));
}
void PodcastService::onCurrentStepDone(bool success)
{
LMS_LOG(PODCAST, DEBUG, "Step '" << _refreshSteps[_refreshStepIndex]->getName() << "' done: " << (success ? "success" : _abortRequested ? "aborted" :
"failure"));
if (success && !_abortRequested)
runNextStep();
else
onRefreshDone();
}
void PodcastService::runNextStep()
{
if (++_refreshStepIndex < _refreshSteps.size())
runStep(_refreshStepIndex);
else
onRefreshDone();
}
void PodcastService::runStep(std::size_t stepIndex)
{
_refreshContext.executor.post([stepIndex, this] {
assert(stepIndex < _refreshSteps.size());
RefreshStep& step{ *_refreshSteps[stepIndex] };
LMS_LOG(PODCAST, DEBUG, "Running step '" << step.getName() << "'");
{
LMS_SCOPED_TRACE_OVERVIEW("Podcast", step.getName());
step.run();
}
});
}
void PodcastService::onRefreshDone()
{
LMS_LOG(PODCAST, DEBUG, "Refresh done" << (_abortRequested ? " (aborted)" : ""));
const bool rescheduleRefresh{ !_abortRequested };
_refreshInProgress = false;
_controlCv.notify_all();
if (rescheduleRefresh)
scheduleRefresh(_refreshPeriod);
}
} // namespace lms::podcast
@@ -0,0 +1,91 @@
/*
* Copyright (C) 2025 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 <atomic>
#include <chrono>
#include <condition_variable>
#include <memory>
#include <mutex>
#include <boost/asio/steady_timer.hpp>
#include "database/objects/PodcastId.hpp"
#include "services/podcast/IPodcastService.hpp"
#include "Executor.hpp"
#include "RefreshContext.hpp"
namespace lms::core::http
{
class IClient;
}
namespace lms::podcast
{
class RefreshStep;
class PodcastService : public IPodcastService
{
public:
PodcastService(boost::asio::io_context& ioContext, db::IDb& db, const std::filesystem::path& cachePath);
~PodcastService() override;
PodcastService(const PodcastService&) = delete;
PodcastService& operator=(const PodcastService&) = delete;
private:
std::filesystem::path getCachePath() const override;
db::PodcastId addPodcast(std::string_view url) override;
bool removePodcast(db::PodcastId podcast) override;
void refreshPodcasts() override;
bool downloadPodcastEpisode(db::PodcastEpisodeId episode) override;
bool deletePodcastEpisode(db::PodcastEpisodeId episode) override;
bool hasPodcasts() const;
void abortCurrentRefresh(std::unique_lock<std::mutex>& lock);
void allowRefresh();
void scheduleRefresh(std::chrono::seconds fromNow = std::chrono::seconds::zero());
void startRefresh();
void onRefreshDone();
void setupSteps();
void onCurrentStepDone(bool success);
void runNextStep();
void runStep(std::size_t stepIndex);
Executor _executor;
boost::asio::steady_timer _refreshTimer;
std::unique_ptr<core::http::IClient> _httpClient;
RefreshContext _refreshContext;
const std::chrono::hours _refreshPeriod;
std::mutex _controlMutex;
std::condition_variable _controlCv;
std::atomic<bool> _refreshInProgress;
std::atomic<bool> _abortRequested;
std::vector<std::unique_ptr<RefreshStep>> _refreshSteps;
std::size_t _refreshStepIndex;
};
} // namespace lms::podcast
@@ -0,0 +1,78 @@
/*
* Copyright (C) 2025 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 <optional>
#include <string>
#include <vector>
#include <Wt/WDateTime.h>
namespace lms::podcast
{
struct EnclosureUrl
{
std::string url;
std::size_t length;
std::string type;
};
struct PodcastEpisode
{
std::string url;
std::string title;
std::string link;
std::string description;
Wt::WDateTime pubDate;
std::string author;
std::string category;
std::optional<bool> explicitContent;
std::string imageUrl;
std::string ownerEmail;
std::string guid;
EnclosureUrl enclosureUrl;
std::chrono::milliseconds duration{ 0 };
};
struct Podcast
{
std::string title;
std::string link;
std::string description;
std::string language;
std::string copyright;
Wt::WDateTime lastBuildDate;
// itunes fields
std::string newUrl;
std::string author;
std::string category;
std::optional<bool> explicitContent;
std::string imageUrl;
std::string ownerEmail;
std::string ownerName;
std::string subtitle;
std::string summary;
std::vector<PodcastEpisode> episodes; // List of episodes in the podcast
};
} // namespace lms::podcast
@@ -0,0 +1,60 @@
/*
* Copyright (C) 2025 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 <filesystem>
namespace lms
{
namespace db
{
class IDb;
}
namespace core::http
{
class IClient;
}
} // namespace lms
namespace lms::podcast
{
class Executor;
struct RefreshContext
{
RefreshContext(Executor& executor, db::IDb& db, core::http::IClient& client, const std::filesystem::path& cachePath)
: executor{ executor }
, client{ client }
, db{ db }
, cachePath{ cachePath }
, tmpCachePath{ cachePath / "tmp" }
{
}
~RefreshContext() = default;
RefreshContext(const RefreshContext&) = delete;
RefreshContext& operator=(const RefreshContext&) = delete;
Executor& executor;
core::http::IClient& client;
db::IDb& db;
const std::filesystem::path cachePath;
const std::filesystem::path tmpCachePath;
};
} // namespace lms::podcast
@@ -0,0 +1,144 @@
/*
* Copyright (C) 2025 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 "CheckForMissingFilesStep.hpp"
#include <vector>
#include "core/ILogger.hpp"
#include "database/IDb.hpp"
#include "database/Session.hpp"
#include "database/objects/Artwork.hpp"
#include "database/objects/Image.hpp"
#include "database/objects/Podcast.hpp"
#include "database/objects/PodcastEpisode.hpp"
namespace lms::podcast
{
namespace
{
bool fileExists(const std::string& path)
{
std::error_code ec;
bool res{ std::filesystem::exists(path, ec) };
if (ec)
{
LMS_LOG(PODCAST, ERROR, "Error checking file existence for path " << path << ": " << ec.message());
return false;
}
return res;
}
bool checkArtworkFile(const db::Artwork::pointer& artwork)
{
assert(std::holds_alternative<db::ImageId>(artwork->getUnderlyingId())); // these artworks can only be an image
const std::filesystem::path filePath{ artwork->getAbsoluteFilePath() };
if (!fileExists(filePath.string()))
{
LMS_LOG(PODCAST, DEBUG, "Artwork file is missing: " << filePath);
return false;
}
return true;
}
} // namespace
core::LiteralString CheckForMissingFilesStep::getName() const
{
return "Check for missing files";
}
void CheckForMissingFilesStep::run()
{
checkMissingImages();
checkMissingEpisodes();
onDone();
}
void CheckForMissingFilesStep::checkMissingImages()
{
std::vector<db::ImageId> missingImages;
{
auto& session{ getDb().getTLSSession() };
auto transaction{ session.createReadTransaction() };
db::Podcast::find(session, [&](const db::Podcast::pointer& podcast) {
if (const db::Artwork::pointer artwork{ podcast->getArtwork() })
{
if (!checkArtworkFile(artwork))
missingImages.push_back(artwork->getImageId());
}
});
db::PodcastEpisode::find(session, db::PodcastEpisode::FindParameters{}, [&](const db::PodcastEpisode::pointer& episode) {
if (const db::Artwork::pointer artwork{ episode->getArtwork() })
{
if (!checkArtworkFile(artwork))
missingImages.push_back(artwork->getImageId());
}
});
}
if (!missingImages.empty())
{
auto& session{ getDb().getTLSSession() };
auto transaction{ session.createWriteTransaction() };
session.destroy<db::Image>(missingImages); // will propagate to artworks and podcasts/episodes
}
}
void CheckForMissingFilesStep::checkMissingEpisodes()
{
std::vector<db::PodcastEpisodeId> missingEpisodes;
{
auto& session{ getDb().getTLSSession() };
auto transaction{ session.createReadTransaction() };
db::PodcastEpisode::find(session, db::PodcastEpisode::FindParameters{}, [&](const db::PodcastEpisode::pointer& episode) {
if (episode->getAudioRelativeFilePath().empty())
return;
const std::filesystem::path filePath{ getCachePath() / episode->getAudioRelativeFilePath() };
if (!fileExists(filePath))
{
LMS_LOG(PODCAST, INFO, "Episode file " << filePath << " is missing for episode '" << episode->getTitle() << "'");
missingEpisodes.push_back(episode->getId());
}
});
}
if (!missingEpisodes.empty())
{
auto& session{ getDb().getTLSSession() };
auto transaction{ session.createWriteTransaction() };
for (const auto& episodeId : missingEpisodes)
{
db::PodcastEpisode::pointer episode{ db::PodcastEpisode::find(session, episodeId) };
episode.modify()->setAudioRelativeFilePath({});
}
}
}
} // namespace lms::podcast
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2025 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 "RefreshStep.hpp"
namespace lms::podcast
{
class CheckForMissingFilesStep : public RefreshStep
{
using RefreshStep::RefreshStep;
private:
core::LiteralString getName() const override;
void run() override;
void checkMissingImages();
void checkMissingEpisodes();
};
} // namespace lms::podcast
@@ -0,0 +1,62 @@
/*
* Copyright (C) 2025 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 "ClearTmpDirectoryStep.hpp"
#include "core/ILogger.hpp"
namespace lms::podcast
{
namespace
{
bool clearDirectory(const std::filesystem::path& _rootPath)
{
for (const auto& entry : std::filesystem::directory_iterator{ _rootPath })
{
std::error_code ec;
std::filesystem::remove_all(entry, ec);
if (ec)
{
LMS_LOG(PODCAST, ERROR, "Failed to remove " << entry << ": " << ec.message());
return false;
}
}
return true;
}
} // namespace
core::LiteralString ClearTmpDirectoryStep::getName() const
{
return "Clear tmp Directory";
}
void ClearTmpDirectoryStep::run()
{
if (!clearDirectory(getTmpCachePath()))
{
LMS_LOG(PODCAST, ERROR, "Failed to delete tmp directory " << getTmpCachePath() << ": aborting refresh");
onAbort();
return;
}
onDone();
}
} // namespace lms::podcast
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2025 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 "RefreshStep.hpp"
namespace lms::podcast
{
class ClearTmpDirectoryStep : public RefreshStep
{
public:
using RefreshStep::RefreshStep;
private:
core::LiteralString getName() const override;
void run() override;
};
} // namespace lms::podcast
@@ -0,0 +1,154 @@
/*
* Copyright (C) 2025 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 "DownloadEpisodeArtworksStep.hpp"
#include <filesystem>
#include <fstream>
#include <system_error>
#include "core/ILogger.hpp"
#include "core/http/IClient.hpp"
#include "database/IDb.hpp"
#include "database/Session.hpp"
#include "database/objects/Artwork.hpp"
#include "database/objects/Image.hpp"
#include "database/objects/PodcastEpisode.hpp"
#include "Executor.hpp"
#include "Utils.hpp"
namespace lms::podcast
{
namespace
{
void createEpisodeArtwork(db::Session& session, db::PodcastEpisodeId episodeId, const std::filesystem::path& filePath, std::string_view contentType)
{
auto transaction{ session.createWriteTransaction() };
db::PodcastEpisode::pointer episode{ db::PodcastEpisode::find(session, episodeId) };
if (!episode)
return;
if (db::Artwork::pointer artwork{ utils::createArtworkFromImage(session, filePath, contentType) })
episode.modify()->setArtwork(artwork);
}
} // namespace
core::LiteralString DownloadEpisodeArtworksStep::getName() const
{
return "Download episode artworks";
}
void DownloadEpisodeArtworksStep::run()
{
auto& session{ getDb().getTLSSession() };
auto transaction{ session.createReadTransaction() };
_episodeArtworksToDownload.clear();
db::PodcastEpisode::find(session, db::PodcastEpisode::FindParameters{}, [&](const db::PodcastEpisode::pointer& episode) {
if (episode->getImageUrl().empty())
return;
if (episode->getArtworkId().isValid())
return;
_episodeArtworksToDownload.push_back(episode->getId());
});
processNext();
}
void DownloadEpisodeArtworksStep::processNext()
{
if (abortRequested())
{
onAbort();
return;
}
getExecutor().post([this] {
if (_episodeArtworksToDownload.empty())
{
onDone();
return;
}
const db::PodcastEpisodeId podcastEpisodeId{ _episodeArtworksToDownload.front() };
_episodeArtworksToDownload.pop_front();
process(podcastEpisodeId);
});
}
void DownloadEpisodeArtworksStep::process(db::PodcastEpisodeId episodeId)
{
auto& session{ getDb().getTLSSession() };
auto transaction{ session.createReadTransaction() };
const auto episode{ db::PodcastEpisode::find(getDb().getTLSSession(), episodeId) };
if (!episode)
{
LMS_LOG(PODCAST, DEBUG, "Cannot find episode: removed?");
processNext();
return;
}
const std::string url{ episode->getImageUrl() };
const std::filesystem::path finalFilePath{ getCachePath() / utils::generateRandomFileName() };
core::http::ClientGETRequestParameters params;
params.relativeUrl = episode->getImageUrl();
params.onFailureFunc = [this, episode] {
LMS_LOG(PODCAST, ERROR, "Failed to download episode image from '" << episode->getImageUrl() << "'");
processNext();
};
params.onSuccessFunc = [=, this](const Wt::Http::Message& msg) {
const std::string body{ msg.body() }; // API enforces a copy here
std::ofstream file{ finalFilePath, std::ios::binary | std::ios::trunc };
if (!file)
{
std::error_code ec{ errno, std::generic_category() };
LMS_LOG(PODCAST, ERROR, "Failed to open file " << finalFilePath << " for writing: " << ec.message());
processNext();
return;
}
file.write(body.data(), body.size());
if (!file)
{
std::error_code ec{ errno, std::generic_category() };
LMS_LOG(PODCAST, ERROR, "Failed to write to file " << finalFilePath << ": " << ec.message());
processNext();
return;
}
const std::string* contentType{ msg.getHeader("Content-Type") };
LMS_LOG(PODCAST, INFO, "Downloaded episode artwork for episode '" << episode->getTitle() << "' to " << finalFilePath << " with content type '" << (contentType ? *contentType : "unknown") << "', size = " << body.size() << " bytes");
createEpisodeArtwork(getDb().getTLSSession(), episodeId, finalFilePath, contentType ? *contentType : "application/octet-stream");
processNext();
};
params.onAbortFunc = [this] {
onAbort();
};
getClient().sendGETRequest(std::move(params));
}
} // namespace lms::podcast
@@ -0,0 +1,43 @@
/*
* Copyright (C) 2025 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 <deque>
#include "database/objects/PodcastEpisodeId.hpp"
#include "RefreshStep.hpp"
namespace lms::podcast
{
class DownloadEpisodeArtworksStep : public RefreshStep
{
using RefreshStep::RefreshStep;
private:
core::LiteralString getName() const override;
void run() override;
void processNext();
void process(db::PodcastEpisodeId episodeId);
std::deque<db::PodcastEpisodeId> _episodeArtworksToDownload;
};
} // namespace lms::podcast
@@ -0,0 +1,196 @@
/*
* Copyright (C) 2025 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 "DownloadEpisodesStep.hpp"
#include <fstream>
#include "core/IConfig.hpp"
#include "core/ILogger.hpp"
#include "core/Service.hpp"
#include "core/http/IClient.hpp"
#include "database/IDb.hpp"
#include "database/Session.hpp"
#include "database/objects/Podcast.hpp"
#include "database/objects/PodcastEpisode.hpp"
#include "Executor.hpp"
#include "Utils.hpp"
namespace lms::podcast
{
namespace
{
void updateEpisode(db::Session& session, db::PodcastEpisodeId episodeId, const std::filesystem::path& relativeFilePath)
{
auto transaction{ session.createWriteTransaction() };
db::PodcastEpisode::pointer dbEpisode{ db::PodcastEpisode::find(session, episodeId) };
if (!dbEpisode)
return; // may have been deleted by admin
dbEpisode.modify()->setAudioRelativeFilePath(relativeFilePath);
}
} // namespace
DownloadEpisodesStep::DownloadEpisodesStep(RefreshContext& context, OnDoneCallback callback)
: RefreshStep{ context, std::move(callback) }
, _autoDownloadEpisodes{ core::Service<core::IConfig>::get()->getBool("podcast-auto-download-episodes", true) }
, _autoDownloadEpisodesMaxAge{ core::Service<core::IConfig>::get()->getULong("podcast-auto-download-episodes-max-age-days", 30) }
{
}
core::LiteralString DownloadEpisodesStep::getName() const
{
return "Download episodes";
}
void DownloadEpisodesStep::run()
{
collectEpisodes();
processNext();
}
void DownloadEpisodesStep::collectEpisodes()
{
const Wt::WDateTime now{ Wt::WDateTime::currentDateTime() };
auto& session{ getDb().getTLSSession() };
auto transaction{ session.createReadTransaction() };
db::PodcastEpisode::FindParameters params;
_episodesToDownload.clear();
db::PodcastEpisode::find(session, params, [&](const db::PodcastEpisode::pointer& episode) {
if (!episode->getAudioRelativeFilePath().empty())
return; // already downloaded
switch (episode->getManualDownloadState())
{
case db::PodcastEpisode::ManualDownloadState::DownloadRequested:
LMS_LOG(PODCAST, DEBUG, "Adding episode '" << episode->getTitle() << "' from podcast '" << episode->getPodcast()->getTitle() << "' to download queue (manually requested)");
_episodesToDownload.push_back(episode->getId());
break;
case db::PodcastEpisode::ManualDownloadState::None:
if (_autoDownloadEpisodes && now < episode->getPubDate().addDays(_autoDownloadEpisodesMaxAge.count()))
{
LMS_LOG(PODCAST, DEBUG, "Adding episode '" << episode->getTitle() << "' from podcast '" << episode->getPodcast()->getTitle() << "' to download queue (auto-download enabled)");
_episodesToDownload.push_back(episode->getId());
}
break;
case db::PodcastEpisode::ManualDownloadState::DeleteRequested:
break;
}
});
}
void DownloadEpisodesStep::processNext()
{
getExecutor().post([this] {
if (_episodesToDownload.empty())
{
LMS_LOG(PODCAST, DEBUG, "All pending episodes downloaded");
onDone();
return;
}
const db::PodcastEpisodeId podcastEpisodeId{ _episodesToDownload.front() };
_episodesToDownload.pop_front();
process(podcastEpisodeId);
});
}
void DownloadEpisodesStep::process(db::PodcastEpisodeId episodeId)
{
auto& session{ getDb().getTLSSession() };
auto transaction{ session.createReadTransaction() };
const db::PodcastEpisode::pointer episode{ db::PodcastEpisode::find(session, episodeId) };
if (!episode)
{
LMS_LOG(PODCAST, DEBUG, "Cannot find episode: removed?"); // TODO if removed, need to keep it in the db to check for new episodes...
processNext();
return;
}
const std::string randomName{ utils::generateRandomFileName() };
const std::filesystem::path tmpFilePath{ getTmpCachePath() / randomName };
const std::filesystem::path finalFilePath{ getCachePath() / randomName };
LMS_LOG(PODCAST, DEBUG, "Downloading episode '" << episode->getTitle() << "' from '" << episode->getEnclosureUrl() << "' in tmp file '" << tmpFilePath << "'");
core::http::ClientGETRequestParameters params;
const std::string url{ episode->getEnclosureUrl() };
params.relativeUrl = url;
params.onFailureFunc = [this, episode] {
LMS_LOG(PODCAST, ERROR, "Failed to download podcast episode from '" << episode->getEnclosureUrl() << "'");
processNext();
};
params.onChunkReceived = [url, tmpFilePath](std::span<const std::byte> chunk) {
std::ofstream file{ tmpFilePath, std::ios::binary | std::ios::app };
if (!file)
{
std::error_code ec{ errno, std::generic_category() };
LMS_LOG(PODCAST, ERROR, "Failed to open file '" << tmpFilePath << "' for writing: " << ec.message());
return core::http::ClientGETRequestParameters::ChunckReceivedResult::Abort;
}
// check write status
file.write(reinterpret_cast<const char*>(chunk.data()), chunk.size());
if (!file)
{
std::error_code ec{ errno, std::generic_category() };
LMS_LOG(PODCAST, ERROR, "Failed to write to file '" << tmpFilePath << "': " << ec.message());
return core::http::ClientGETRequestParameters::ChunckReceivedResult::Abort;
}
return core::http::ClientGETRequestParameters::ChunckReceivedResult::Continue;
};
params.onSuccessFunc = [=, this]([[maybe_unused]] const Wt::Http::Message& msg) {
assert(msg.body().empty());
getExecutor().post([=, this] {
LMS_LOG(PODCAST, DEBUG, "Download episode from '" << url << "' complete");
LMS_LOG(PODCAST, DEBUG, "Renaming temp file " << tmpFilePath << " to " << finalFilePath);
std::error_code ec;
std::filesystem::rename(tmpFilePath, finalFilePath, ec);
if (ec)
LMS_LOG(PODCAST, ERROR, "Failed to rename temp file " << tmpFilePath << " to " << finalFilePath << ": " << ec.message());
else
updateEpisode(getDb().getTLSSession(), episodeId, randomName);
// TODO: now the file is complete, should we attempt to read it and get the real information like duration and size?
LMS_LOG(PODCAST, INFO, "Successfully downloaded episode '" << episode->getTitle() << "'");
processNext();
});
};
params.onAbortFunc = [this] {
onAbort();
};
LMS_LOG(PODCAST, DEBUG, "Downloading episode from '" << url << "'...");
getClient().sendGETRequest(std::move(params));
}
} // namespace lms::podcast
@@ -0,0 +1,50 @@
/*
* Copyright (C) 2025 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 <deque>
#include "database/objects/PodcastEpisodeId.hpp"
#include "RefreshStep.hpp"
namespace lms::podcast
{
class DownloadEpisodesStep : public RefreshStep
{
public:
DownloadEpisodesStep(RefreshContext& context, OnDoneCallback callback);
private:
core::LiteralString getName() const override;
void run() override;
void collectEpisodes();
void processNext();
void process(db::PodcastEpisodeId episodeId);
const bool _autoDownloadEpisodes;
const std::chrono::days _autoDownloadEpisodesMaxAge;
std::deque<db::PodcastEpisodeId> _episodesToDownload;
};
} // namespace lms::podcast
@@ -0,0 +1,153 @@
/*
* Copyright (C) 2025 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 "DownloadPodcastArtworksStep.hpp"
#include <filesystem>
#include <fstream>
#include <system_error>
#include "core/ILogger.hpp"
#include "core/http/IClient.hpp"
#include "database/IDb.hpp"
#include "database/Session.hpp"
#include "database/objects/Artwork.hpp"
#include "database/objects/Podcast.hpp"
#include "Executor.hpp"
#include "Utils.hpp"
namespace lms::podcast
{
namespace
{
void createPodcastArtwork(db::Session& session, db::PodcastId podcastId, const std::filesystem::path& filePath, std::string_view contentType)
{
auto transaction{ session.createWriteTransaction() };
db::Podcast::pointer dbPodcast{ db::Podcast::find(session, podcastId) };
if (!dbPodcast)
return; // may have been deleted by admin
if (db::Artwork::pointer artwork{ utils::createArtworkFromImage(session, filePath, contentType) })
dbPodcast.modify()->setArtwork(artwork);
}
} // namespace
core::LiteralString DownloadPodcastArtworksStep::getName() const
{
return "Download podcast artworks";
}
void DownloadPodcastArtworksStep::run()
{
auto& session{ getDb().getTLSSession() };
auto transaction{ session.createReadTransaction() };
_podcastArtworksToDownload.clear();
db::Podcast::find(session, [&](const db::Podcast::pointer& podcast) {
if (podcast->getImageUrl().empty() || podcast->getTitle().empty())
return;
if (podcast->getArtworkId().isValid())
return;
_podcastArtworksToDownload.push_back(podcast->getId());
});
processNext();
}
void DownloadPodcastArtworksStep::processNext()
{
if (abortRequested())
{
onAbort();
return;
}
getExecutor().post([this] {
if (_podcastArtworksToDownload.empty())
{
onDone();
return;
}
const db::PodcastId podcastId{ _podcastArtworksToDownload.front() };
_podcastArtworksToDownload.pop_front();
process(podcastId);
});
}
void DownloadPodcastArtworksStep::process(db::PodcastId podcastId)
{
auto& session{ getDb().getTLSSession() };
auto transaction{ session.createReadTransaction() };
const auto podcast{ db::Podcast::find(getDb().getTLSSession(), podcastId) };
if (!podcast)
{
LMS_LOG(PODCAST, DEBUG, "Cannot find podcast: removed?");
processNext();
return;
}
const std::string url{ podcast->getImageUrl() };
const std::filesystem::path finalFilePath{ getCachePath() / utils::generateRandomFileName() };
core::http::ClientGETRequestParameters params;
params.relativeUrl = podcast->getImageUrl();
params.onFailureFunc = [this, podcast] {
LMS_LOG(PODCAST, ERROR, "Failed to download podcast image from '" << podcast->getImageUrl() << "'");
processNext();
};
params.onSuccessFunc = [=, this](const Wt::Http::Message& msg) {
const std::string body{ msg.body() }; // API enforces a copy here
std::ofstream file{ finalFilePath, std::ios::binary | std::ios::app };
if (!file)
{
std::error_code ec{ errno, std::generic_category() };
LMS_LOG(PODCAST, ERROR, "Failed to open file " << finalFilePath << " for writing: " << ec.message());
processNext();
return;
}
file.write(body.data(), body.size());
if (!file)
{
std::error_code ec{ errno, std::generic_category() };
LMS_LOG(PODCAST, ERROR, "Failed to write to file " << finalFilePath << ": " << ec.message());
processNext();
return;
}
const std::string* contentType{ msg.getHeader("Content-Type") };
LMS_LOG(PODCAST, INFO, "Downloaded podcast artwork for podcast '" << podcast->getTitle() << "' to " << finalFilePath << " with content type '" << (contentType ? *contentType : "unknown") << "', size = " << body.size());
createPodcastArtwork(getDb().getTLSSession(), podcastId, finalFilePath, contentType ? *contentType : "application/octet-stream");
processNext();
};
params.onAbortFunc = [this] {
onAbort();
};
getClient().sendGETRequest(std::move(params));
}
} // namespace lms::podcast
@@ -0,0 +1,43 @@
/*
* Copyright (C) 2025 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 <deque>
#include "database/objects/PodcastId.hpp"
#include "RefreshStep.hpp"
namespace lms::podcast
{
class DownloadPodcastArtworksStep : public RefreshStep
{
using RefreshStep::RefreshStep;
private:
core::LiteralString getName() const override;
void run() override;
void processNext();
void process(db::PodcastId podcastId);
std::deque<db::PodcastId> _podcastArtworksToDownload;
};
} // namespace lms::podcast
@@ -0,0 +1,204 @@
/*
* Copyright (C) 2025 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 "RefreshPodcastsStep.hpp"
#include "core/ILogger.hpp"
#include "core/http/IClient.hpp"
#include "database/IDb.hpp"
#include "database/Session.hpp"
#include "database/objects/Artwork.hpp"
#include "database/objects/Image.hpp"
#include "database/objects/Podcast.hpp"
#include "database/objects/PodcastEpisode.hpp"
#include "Executor.hpp"
#include "PodcastParsing.hpp"
#include "PodcastTypes.hpp"
namespace lms::podcast
{
namespace
{
void removeArtwork(db::Session& session, const db::Artwork::pointer& artwork)
{
const auto underlyingImageId{ artwork->getUnderlyingId() };
const auto* imageId{ std::get_if<db::ImageId>(&underlyingImageId) };
assert(imageId); // these artworks can only be an image
std::error_code ec;
std::filesystem::remove(artwork->getAbsoluteFilePath(), ec);
if (ec)
LMS_LOG(PODCAST, WARNING, "Failed to remove old podcast artwork file '" << artwork->getAbsoluteFilePath() << "': " << ec.message());
session.destroy<db::Image>(*imageId);
}
void updatePodcast(db::Session& session, db::PodcastId podcastId, const Podcast& podcast)
{
auto transaction{ session.createWriteTransaction() };
db::Podcast::pointer dbPodcast{ db::Podcast::find(session, podcastId) };
if (!dbPodcast)
return; // may have been deleted by admin
LMS_LOG(PODCAST, DEBUG, "Refreshing podcast '" << podcast.title << "' received from '" << dbPodcast->getUrl() << "'");
// force update the podcast data
if (!podcast.newUrl.empty() && podcast.newUrl != dbPodcast->getUrl())
{
LMS_LOG(PODCAST, INFO, "Podcast '" << podcast.title << "' : URL changed from '" << dbPodcast->getUrl() << "' to '" << podcast.newUrl << "'");
dbPodcast.modify()->setUrl(podcast.newUrl);
}
dbPodcast.modify()->setAuthor(podcast.author);
dbPodcast.modify()->setCategory(podcast.category);
dbPodcast.modify()->setCopyright(podcast.copyright);
dbPodcast.modify()->setDescription(podcast.description);
dbPodcast.modify()->setExplicit(podcast.explicitContent ? *podcast.explicitContent : false);
dbPodcast.modify()->setLanguage(podcast.language);
dbPodcast.modify()->setLastBuildDate(podcast.lastBuildDate);
dbPodcast.modify()->setLink(podcast.link);
dbPodcast.modify()->setOwnerEmail(podcast.ownerEmail);
dbPodcast.modify()->setOwnerName(podcast.ownerName);
dbPodcast.modify()->setSubtitle(podcast.subtitle);
dbPodcast.modify()->setSummary(podcast.summary);
dbPodcast.modify()->setTitle(podcast.title);
if (dbPodcast->getImageUrl() != podcast.imageUrl)
{
LMS_LOG(PODCAST, INFO, "Podcast '" << podcast.title << "' : image url changed from '" << dbPodcast->getImageUrl() << "' to '" << podcast.imageUrl << "'");
if (db::Artwork::pointer currentArtwork{ dbPodcast->getArtwork() })
removeArtwork(session, currentArtwork);
dbPodcast.modify()->setImageUrl(podcast.imageUrl);
}
// Only create episodes if they are new, do not modify/update existing entries for now
// TODO: update existing episodes, remove artwork if url changed
Wt::WDateTime previousNewestEpisodeDateTime{};
if (db::PodcastEpisode::pointer dbEpisode{ db::PodcastEpisode::findNewtestEpisode(session, podcastId) })
previousNewestEpisodeDateTime = dbEpisode->getPubDate();
// TODO: mark for deletion old episodes that are no longer referenced!!
for (const auto& episode : podcast.episodes)
{
if (previousNewestEpisodeDateTime.isValid() && episode.pubDate <= previousNewestEpisodeDateTime)
continue; // consider already in db
LMS_LOG(PODCAST, DEBUG, "Adding episode '" << episode.title << "' to podcast '" << podcast.title << "'");
auto dbEpisode{ session.create<db::PodcastEpisode>(dbPodcast) };
dbEpisode.modify()->setAuthor(episode.author);
dbEpisode.modify()->setCategory(episode.category);
dbEpisode.modify()->setDescription(episode.description);
dbEpisode.modify()->setEnclosureUrl(episode.enclosureUrl.url);
dbEpisode.modify()->setEnclosureContentType(episode.enclosureUrl.type);
dbEpisode.modify()->setEnclosureLength(episode.enclosureUrl.length);
dbEpisode.modify()->setExplicit(episode.explicitContent ? *episode.explicitContent : false);
dbEpisode.modify()->setLink(episode.link);
dbEpisode.modify()->setPubDate(episode.pubDate);
dbEpisode.modify()->setTitle(episode.title);
dbEpisode.modify()->setImageUrl(episode.imageUrl);
dbEpisode.modify()->setDuration(episode.duration);
}
}
} // namespace
core::LiteralString RefreshPodcastsStep::getName() const
{
return "Refresh podcasts";
}
void RefreshPodcastsStep::run()
{
auto& session{ getDb().getTLSSession() };
auto transaction{ session.createReadTransaction() };
db::Podcast::find(session, [this](const db::Podcast::pointer& podcast) {
LMS_LOG(PODCAST, DEBUG, "Found podcast to refresh at '" << podcast->getUrl() << "'");
podcastsToRefresh.push(podcast->getId());
});
refreshNextPodcast();
}
void RefreshPodcastsStep::refreshNextPodcast()
{
if (abortRequested())
{
onAbort();
return;
}
getExecutor().post([this] {
if (podcastsToRefresh.empty())
{
LMS_LOG(PODCAST, DEBUG, "All podcasts refreshed");
onDone();
return;
}
const db::PodcastId podcastId{ podcastsToRefresh.front() };
podcastsToRefresh.pop();
refreshPodcast(podcastId);
});
}
void RefreshPodcastsStep::refreshPodcast(db::PodcastId podcastId)
{
auto& session{ getDb().getTLSSession() };
auto transaction{ session.createReadTransaction() };
const db::Podcast::pointer podcast{ db::Podcast::find(session, podcastId) };
if (!podcast)
{
refreshNextPodcast(); // maybe removed in the meantime by admin
return;
}
LMS_LOG(PODCAST, DEBUG, "Syncing podcast from '" << podcast->getUrl() << "'");
const std::string url{ podcast->getUrl() };
core::http::ClientGETRequestParameters params;
params.relativeUrl = podcast->getUrl();
params.onFailureFunc = [this, podcast] {
LMS_LOG(PODCAST, ERROR, "Failed to sync podcast from '" << podcast->getUrl() << "'");
refreshNextPodcast();
};
params.onSuccessFunc = [this, podcast, podcastId](const Wt::Http::Message& msg) {
getExecutor().post([this, podcast, podcastId, msgBody = msg.body()] {
try
{
const auto podcast{ parsePodcastRssFeed(msgBody) };
updatePodcast(getDb().getTLSSession(), podcastId, podcast);
}
catch (const ParseException& e)
{
LMS_LOG(PODCAST, ERROR, "Failed to parse rss feed from '" << podcast->getUrl() << "': " << e.what());
}
refreshNextPodcast();
});
};
params.onAbortFunc = [this] {
onAbort();
};
getClient().sendGETRequest(std::move(params));
}
} // namespace lms::podcast
@@ -0,0 +1,44 @@
/*
* Copyright (C) 2025 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 <queue>
#include "database/objects/PodcastId.hpp"
#include "RefreshStep.hpp"
namespace lms::podcast
{
class RefreshPodcastsStep : public RefreshStep
{
using RefreshStep::RefreshStep;
private:
core::LiteralString getName() const override;
void run() override;
void refreshNextPodcast();
void refreshPodcast(db::PodcastId podcastId);
std::queue<db::PodcastId> podcastsToRefresh;
};
} // namespace lms::podcast
@@ -0,0 +1,99 @@
/*
* Copyright (C) 2025 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 <atomic>
#include <filesystem>
#include <functional>
#include "core/LiteralString.hpp"
#include "RefreshContext.hpp"
namespace lms::podcast
{
class RefreshStep
{
public:
using OnDoneCallback = std::function<void(bool success)>;
RefreshStep(RefreshContext& context, OnDoneCallback callback)
: _context{ context }
, _onDoneCallback{ std::move(callback) } {}
virtual ~RefreshStep() = default;
virtual core::LiteralString getName() const = 0;
virtual void run() = 0;
void requestAbort(bool value)
{
_abortRequested = value;
}
protected:
bool abortRequested() const
{
return _abortRequested;
}
// Called by the step implementation when done
void onDone()
{
_onDoneCallback(true);
}
// Called by the step implementation when it wants to abort the whole refresh process
void onAbort()
{
_onDoneCallback(false);
}
Executor& getExecutor()
{
return _context.executor;
}
db::IDb& getDb()
{
return _context.db;
}
const std::filesystem::path& getCachePath() const
{
return _context.cachePath;
}
const std::filesystem::path& getTmpCachePath() const
{
return _context.tmpCachePath;
}
core::http::IClient& getClient()
{
return _context.client;
}
private:
RefreshContext& _context;
OnDoneCallback _onDoneCallback;
std::atomic<bool> _abortRequested;
};
} // namespace lms::podcast
@@ -0,0 +1,108 @@
/*
* Copyright (C) 2025 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 "RemoveEpisodesStep.hpp"
#include <filesystem>
#include <vector>
#include "core/IConfig.hpp"
#include "core/ILogger.hpp"
#include "core/Service.hpp"
#include "database/IDb.hpp"
#include "database/Session.hpp"
#include "database/objects/Artwork.hpp"
#include "database/objects/PodcastEpisode.hpp"
#include "Utils.hpp"
namespace lms::podcast
{
core::LiteralString RemoveEpisodesStep::getName() const
{
return "Remove podcast episodes";
}
RemoveEpisodesStep::RemoveEpisodesStep(RefreshContext& context, OnDoneCallback callback)
: RefreshStep{ context, std::move(callback) }
, _autoDownloadEpisodesMaxAge{ core::Service<core::IConfig>::get()->getULong("podcast-auto-download-episodes-max-age-days", 30) }
{
}
void RemoveEpisodesStep::run()
{
std::vector<db::PodcastEpisodeId> episodesToRemove;
std::vector<db::ImageId> imagesToRemove;
// Step 1 collect the episodes to remove
{
auto removePodcastFile{ [&](const db::PodcastEpisode::pointer& episode) {
// We keep the artwork of the episode (TODO, not if the episode is no longer referenced by the podcast?)
assert(!episode->getAudioRelativeFilePath().empty());
utils::removeFile(getCachePath() / episode->getAudioRelativeFilePath());
episodesToRemove.emplace_back(episode->getId());
} };
const Wt::WDateTime now{ Wt::WDateTime::currentDateTime() };
auto& session{ getDb().getTLSSession() };
auto transaction{ session.createReadTransaction() };
db::PodcastEpisode::find(session, db::PodcastEpisode::FindParameters{}, [&](const db::PodcastEpisode::pointer& episode) {
switch (episode->getManualDownloadState())
{
case db::PodcastEpisode::ManualDownloadState::None:
if (!episode->getAudioRelativeFilePath().empty() && now > episode->getPubDate().addDays(_autoDownloadEpisodesMaxAge.count())) // TODO make this configurable per podcast
{
LMS_LOG(PODCAST, INFO, "Removing episode '" << episode->getTitle() << "' because it is older than " << _autoDownloadEpisodesMaxAge.count() << " days");
removePodcastFile(episode);
}
break;
case db::PodcastEpisode::ManualDownloadState::DownloadRequested:
// always keep the manually downloaded episodes
break;
case db::PodcastEpisode::ManualDownloadState::DeleteRequested:
if (!episode->getAudioRelativeFilePath().empty())
{
LMS_LOG(PODCAST, DEBUG, "Removing episode '" << episode->getTitle() << "' because it was manually deleted");
removePodcastFile(episode);
}
break;
}
});
}
// second step, remove the database entries (must be consistent with first step!)
if (!episodesToRemove.empty())
{
auto& session{ getDb().getTLSSession() };
auto transaction{ session.createWriteTransaction() };
for (db::PodcastEpisodeId episodeId : episodesToRemove)
{
if (db::PodcastEpisode::pointer episode{ db::PodcastEpisode::find(session, episodeId) })
episode.modify()->setAudioRelativeFilePath(std::filesystem::path{});
}
}
onDone();
}
} // namespace lms::podcast
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2025 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 "RefreshStep.hpp"
namespace lms::podcast
{
class RemoveEpisodesStep : public RefreshStep
{
public:
RemoveEpisodesStep(RefreshContext& context, OnDoneCallback callback);
private:
core::LiteralString getName() const override;
void run() override;
const std::chrono::days _autoDownloadEpisodesMaxAge;
};
} // namespace lms::podcast
@@ -0,0 +1,95 @@
/*
* Copyright (C) 2025 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 "RemovePodcastsStep.hpp"
#include <filesystem>
#include <vector>
#include "core/ILogger.hpp"
#include "database/IDb.hpp"
#include "database/Session.hpp"
#include "database/objects/Artwork.hpp"
#include "database/objects/Image.hpp"
#include "database/objects/Podcast.hpp"
#include "database/objects/PodcastEpisode.hpp"
#include "Utils.hpp"
namespace lms::podcast
{
core::LiteralString RemovePodcastsStep::getName() const
{
return "Remove podcasts";
}
void RemovePodcastsStep::run()
{
std::vector<db::PodcastId> podcastsToRemove;
std::vector<db::ImageId> imagesToRemove;
{
auto& session{ getDb().getTLSSession() };
auto transaction{ session.createReadTransaction() };
db::Podcast::find(session, [&](const db::Podcast::pointer& podcast) {
if (!podcast->isDeleteRequested())
return;
LMS_LOG(PODCAST, DEBUG, "Removing podcast '" << podcast->getUrl() << "'. Title: '" << podcast->getTitle() << "'");
// remove podcast artwork
if (const db::Artwork::pointer artwork{ podcast->getArtwork() })
{
utils::removeFile(artwork->getAbsoluteFilePath());
imagesToRemove.emplace_back(std::get<db::ImageId>(artwork->getUnderlyingId()));
}
db::PodcastEpisode::FindParameters params;
params.setPodcast(podcast->getId());
db::PodcastEpisode::find(session, params, [&](const db::PodcastEpisode::pointer& episode) {
if (const db::Artwork::pointer artwork{ episode->getArtwork() })
{
utils::removeFile(artwork->getAbsoluteFilePath());
imagesToRemove.emplace_back(std::get<db::ImageId>(artwork->getUnderlyingId()));
}
if (!episode->getAudioRelativeFilePath().empty())
utils::removeFile(getCachePath() / episode->getAudioRelativeFilePath());
});
podcastsToRemove.emplace_back(podcast->getId());
});
}
// second step, remove the database entries (must be consistent with first step!)
if (!podcastsToRemove.empty() || !imagesToRemove.empty())
{
auto& session{ getDb().getTLSSession() };
auto transaction{ session.createWriteTransaction() };
session.destroy<db::Podcast>(podcastsToRemove); // will propagate to episodes
session.destroy<db::Image>(imagesToRemove); // will propagate to artworks
}
onDone();
}
} // namespace lms::podcast
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2025 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 "RefreshStep.hpp"
namespace lms::podcast
{
class RemovePodcastsStep : public RefreshStep
{
using RefreshStep::RefreshStep;
private:
core::LiteralString getName() const override;
void run() override;
};
} // namespace lms::podcast
@@ -0,0 +1,93 @@
/*
* Copyright (C) 2025 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 <cassert>
#include <system_error>
#include "core/ILogger.hpp"
#include "core/UUID.hpp"
#include "database/Session.hpp"
#include "database/objects/Artwork.hpp"
#include "database/objects/Image.hpp"
#include "database/objects/Podcast.hpp"
#include "image/Exception.hpp"
#include "image/Image.hpp"
#include "image/Types.hpp"
namespace lms::podcast::utils
{
std::filesystem::path getPodcastRelativePath(const db::Podcast::pointer& podcast)
{
assert(podcast);
return podcast->getId().toString();
}
static std::optional<image::ImageProperties> probeImage(const std::filesystem::path& path)
{
try
{
return image::probeImage(path);
}
catch (const image::Exception& e)
{
LMS_LOG(PODCAST, WARNING, "Failed to probe artwork image " << path << ": " << e.what());
return std::nullopt;
}
}
db::Artwork::pointer createArtworkFromImage(db::Session& session, const std::filesystem::path& filePath, std::string_view mimeType)
{
std::error_code ec;
const auto fileSize{ std::filesystem::file_size(filePath, ec) };
if (ec)
{
LMS_LOG(PODCAST, ERROR, "Failed to get file size of " << filePath << ": " << ec.message());
return db::Artwork::pointer{};
}
db::Image::pointer image{ session.create<db::Image>(filePath) };
image.modify()->setFileSize(static_cast<std::size_t>(fileSize));
if (const std::optional<image::ImageProperties> imageProperties{ probeImage(filePath) })
{
image.modify()->setWidth(imageProperties->width);
image.modify()->setHeight(imageProperties->height);
}
image.modify()->setLastWriteTime(Wt::WDateTime::currentDateTime());
image.modify()->setMimeType(mimeType);
return session.create<db::Artwork>(image);
}
std::string generateRandomFileName()
{
return std::string{ core::UUID::generate().getAsString() };
}
void removeFile(const std::filesystem::path& filePath)
{
std::error_code ec;
std::filesystem::remove(filePath, ec);
if (ec)
LMS_LOG(PODCAST, WARNING, "Failed to remove file " << filePath << ": " << ec.message());
else
LMS_LOG(PODCAST, DEBUG, "Removed file " << filePath);
}
} // namespace lms::podcast::utils
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2025 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 <filesystem>
#include <string>
#include "database/Object.hpp"
namespace lms::db
{
class Artwork;
class Session;
} // namespace lms::db
namespace lms::podcast::utils
{
db::ObjectPtr<db::Artwork> createArtworkFromImage(db::Session& session, const std::filesystem::path& filePath, std::string_view mimeType);
std::string generateRandomFileName();
void removeFile(const std::filesystem::path& filePath);
} // namespace lms::podcast::utils
@@ -0,0 +1,53 @@
/*
* Copyright (C) 2025 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 <filesystem>
#include <memory>
#include <boost/asio/io_context.hpp>
#include "database/objects/PodcastEpisodeId.hpp"
#include "database/objects/PodcastId.hpp"
namespace lms::db
{
class IDb;
}
namespace lms::podcast
{
class IPodcastService
{
public:
virtual ~IPodcastService() = default;
virtual std::filesystem::path getCachePath() const = 0;
virtual db::PodcastId addPodcast(std::string_view url) = 0;
virtual bool removePodcast(db::PodcastId podcast) = 0;
virtual void refreshPodcasts() = 0;
virtual bool downloadPodcastEpisode(db::PodcastEpisodeId episode) = 0;
virtual bool deletePodcastEpisode(db::PodcastEpisodeId episode) = 0;
};
std::unique_ptr<IPodcastService> createPodcastService(boost::asio::io_context& ioContext, db::IDb& db, const std::filesystem::path& cachePath);
} // namespace lms::podcast
@@ -0,0 +1,19 @@
add_executable(test-podcast
PodcastParser.cpp
PodcastService.cpp
)
target_link_libraries(test-podcast PRIVATE
lmscore
lmspodcast
GTest::GTest
)
target_include_directories(test-podcast PRIVATE
../impl
)
if (NOT CMAKE_CROSSCOMPILING)
gtest_discover_tests(test-podcast)
endif()
@@ -0,0 +1,151 @@
/*
* Copyright (C) 2025 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 <chrono>
#include <Wt/WDate.h>
#include <Wt/WTime.h>
#include <gtest/gtest.h>
#include "PodcastParsing.hpp"
namespace lms::podcast::tests
{
TEST(Podcast, PodcastParsing)
{
constexpr std::string_view xmlData{ R"(<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:pa="http://podcastaddict.com" xmlns:podcastRF="http://radiofrance.fr/Lancelot/Podcast#" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0" version="2.0">
<channel>
<title>Affaires sensibles</title>
<link>https://www.franceinter.fr/emission-affaires-sensibles</link>
<description>Les grandes affaires, les aventures et les procès qui ont marqué les cinquante dernières années. Vous aimez ce podcast ? Pour écouter tous les épisodes sans limite, rendez-vous sur <a href="https://www.franceinter.fr/emission-affaires-sensibles?at_campaign=desc_podcast&at_medium=lien_RSS">Radio France</a>.</description>
<language>fr</language>
<copyright>Radio France</copyright>
<lastBuildDate>Sat, 09 Aug 2025 21:34:32 +0200</lastBuildDate>
<generator>Radio France</generator>
<image>
<url>https://www.radiofrance.fr/s3/cruiser-production/2023/05/5f197ac5-b950-4149-8578-ae12aa6695b0/1400x1400_sc_rf_omm_0000038645_ite.jpg</url>
<title>Affaires sensibles</title>
<link>https://www.franceinter.fr/emission-affaires-sensibles</link>
</image>
<itunes:author>France Inter</itunes:author>
<itunes:category text="Society & Culture"/>
<itunes:explicit>no</itunes:explicit>
<itunes:image href="https://www.radiofrance.fr/s3/cruiser-production/2023/05/5f197ac5-b950-4149-8578-ae12aa6695b0/1400x1400_sc_rf_omm_0000038645_ite.jpg"/>
<itunes:owner>
<itunes:email>podcast@radiofrance.com</itunes:email>
<itunes:name>Radio France</itunes:name>
</itunes:owner>
<itunes:subtitle>Affaires sensibles</itunes:subtitle>
<itunes:summary>Les grandes affaires, les aventures et les procès qui ont marqué les cinquante dernières années. Vous aimez ce podcast ? Pour écouter tous les épisodes sans limite, rendez-vous sur <a href="https://www.franceinter.fr/emission-affaires-sensibles?at_campaign=desc_podcast&at_medium=lien_RSS">Radio France</a>.</itunes:summary>
<itunes:new-feed-url>https://radiofrance-podcast.net/podcast09/35099478-7c72-4f9e-a6de-1b928400e9e5/rss_13940.xml</itunes:new-feed-url>
<pa:new-feed-url>https://radiofrance-podcast.net/podcast09/d4463877-caa3-4507-9399-f5eb00fde027/rss_13940.xml</pa:new-feed-url>
<podcastRF:originStation>1</podcastRF:originStation>
<googleplay:block>yes</googleplay:block>
<item>
<title>Apollo 13 ou les naufragés de lespace</title>
<link>https://www.radiofrance.fr/franceinter/podcasts/affaires-sensibles/affaires-sensibles-du-jeudi-30-decembre-2021-4186094</link>
<description>durée : 00:53:58 - Affaires sensibles - par : Christophe Barreyre, Fabrice Drouelle - C'est une épopée qui réunit héroïsme, génie technologique, audace diplomatique, sens du tragique. Lodyssée d'Apollo 13 cest un vaisseau spatial en perdition, -haut, flirtant avec la lune mais qui au dernier moment se dérobe pour une vulgaire panne électrique dans la soute du vaisseau. - réalisé par : Flora BERNARD, Marion Le Lay, Stéphane COSME Vous aimez ce podcast ? Pour écouter tous les autres épisodes sans limite, rendez-vous sur <a href="https://www.franceinter.fr/emission-affaires-sensibles?at_campaign=desc_episode&at_medium=lien_RSS">Radio France</a>.</description>
<author>podcast@radiofrance.com (Radio France)</author>
<category>Society & Culture</category>
<enclosure url="https://proxycast.radiofrance.fr/e2a713a6-aba0-4d2a-a4a1-d135d98f1f8a/13940-09.08.2025-ITEMA_24214125-2021F22805S0364-NET_MFI_F7191B05-DD5B-4AFE-BB96-3BD8ADB3240A-22.mp3" length="51842568" type="audio/mpeg"/>
<guid isPermaLink="false">9f587824-01f5-443d-aa72-a56519d25857-NET_MFI_F7191B05-DD5B-4AFE-BB96-3BD8ADB3240A</guid>
<pubDate>Sat, 09 Aug 2025 15:59:59 +0200</pubDate>
<podcastRF:businessReference>22805</podcastRF:businessReference>
<podcastRF:magnetothequeID>2021F22805S0364</podcastRF:magnetothequeID>
<itunes:title>Apollo 13 ou les naufragés de lespace</itunes:title>
<itunes:image href="https://www.radiofrance.fr/s3/cruiser-production/2021/04/ec0f1c5d-ecfa-4ec4-a5a5-30f446d25aea/1400x1400_affaires_sensibles.jpg"/>
<itunes:author>Christophe Barreyre, Fabrice Drouelle</itunes:author>
<itunes:explicit>no</itunes:explicit>
<itunes:keywords>Apollo,13,ou,les,naufragés,de,lespace</itunes:keywords>
<itunes:subtitle>Apollo 13 ou les naufragés de lespace</itunes:subtitle>
<itunes:summary>durée : 00:53:58 - Affaires sensibles - par : Christophe Barreyre, Fabrice Drouelle - C'est une épopée qui réunit héroïsme, génie technologique, audace diplomatique, sens du tragique. Lodyssée d'Apollo 13 cest un vaisseau spatial en perdition, -haut, flirtant avec la lune mais qui au dernier moment se dérobe pour une vulgaire panne électrique dans la soute du vaisseau. - réalisé par : Flora BERNARD, Marion Le Lay, Stéphane COSME Vous aimez ce podcast ? Pour écouter tous les autres épisodes sans limite, rendez-vous sur <a href="https://www.franceinter.fr/emission-affaires-sensibles?at_campaign=desc_episode&at_medium=lien_RSS">Radio France</a>.</itunes:summary>
<itunes:duration>00:53:58</itunes:duration>
<googleplay:block>yes</googleplay:block>
</item>
<item>
<title>Stéphane Breitwieser, le pilleur de musées</title>
<link>https://www.radiofrance.fr/franceinter/podcasts/affaires-sensibles/affaires-sensibles-du-mercredi-02-avril-2025-9048848</link>
<description>durée : 00:47:45 - Affaires sensibles - par : Fabrice Drouelle, Franck COGNARD - Aujourdhui dans Affaires Sensibles : laffaire Stéphane Breitwieser, le pilleur de musées - réalisé par : Etienne BERTIN Vous aimez ce podcast ? Pour écouter tous les autres épisodes sans limite, rendez-vous sur <a href="https://www.franceinter.fr/emission-affaires-sensibles?at_campaign=desc_episode&at_medium=lien_RSS">Radio France</a>.</description>
<author>podcast@radiofrance.com (Radio France)</author>
<category>Society & Culture</category>
<enclosure url="https://proxycast.radiofrance.fr/d0895b0b-a99c-4e9d-9d99-13a029960d04/13940-08.08.2025-ITEMA_24213067-2025F22805S0092-NET_MFI_FF964E00-DFB6-45AC-A9D2-2D1211F37586-22.mp3" length="45869054" type="audio/mpeg"/>
<guid isPermaLink="false">719c21be-3832-4b67-8d65-9d07b3715d7b-NET_MFI_FF964E00-DFB6-45AC-A9D2-2D1211F37586</guid>
<pubDate>Fri, 08 Aug 2025 20:59:59 +0200</pubDate>
<podcastRF:businessReference>22805</podcastRF:businessReference>
<podcastRF:magnetothequeID>2025F22805S0092</podcastRF:magnetothequeID>
<itunes:title>Stéphane Breitwieser, le pilleur de musées</itunes:title>
<itunes:image href="https://www.radiofrance.fr/s3/cruiser-production/2023/04/7b50cf5f-f5bd-4dc4-8b1d-b08666768dcf/1400x1400_sc_affaires-sensibles.jpg"/>
<itunes:author>Fabrice Drouelle, Franck COGNARD</itunes:author>
<itunes:explicit>no</itunes:explicit>
<itunes:keywords>Stéphane,Breitwieser,,le,pilleur,de,musées</itunes:keywords>
<itunes:subtitle>Stéphane Breitwieser, le pilleur de musées</itunes:subtitle>
<itunes:summary>durée : 00:47:45 - Affaires sensibles - par : Fabrice Drouelle, Franck COGNARD - Aujourdhui dans Affaires Sensibles : laffaire Stéphane Breitwieser, le pilleur de musées - réalisé par : Etienne BERTIN Vous aimez ce podcast ? Pour écouter tous les autres épisodes sans limite, rendez-vous sur <a href="https://www.franceinter.fr/emission-affaires-sensibles?at_campaign=desc_episode&at_medium=lien_RSS">Radio France</a>.</itunes:summary>
<itunes:duration>00:47:45</itunes:duration>
<googleplay:block>yes</googleplay:block>
</item>
</channel>
</rss>)" };
const Podcast podcast{ parsePodcastRssFeed(xmlData) };
EXPECT_EQ(podcast.title, "Affaires sensibles");
EXPECT_EQ(podcast.description, R"(Les grandes affaires, les aventures et les procès qui ont marqué les cinquante dernières années. Vous aimez ce podcast ? Pour écouter tous les épisodes sans limite, rendez-vous sur <a href="https://www.franceinter.fr/emission-affaires-sensibles?at_campaign=desc_podcast&amp;at_medium=lien_RSS">Radio France</a>.)");
EXPECT_EQ(podcast.author, "France Inter");
EXPECT_EQ(podcast.link, "https://www.franceinter.fr/emission-affaires-sensibles");
EXPECT_EQ(podcast.language, "fr");
EXPECT_EQ(podcast.copyright, "Radio France");
EXPECT_EQ(podcast.lastBuildDate, (Wt::WDateTime{ Wt::WDate{ 2025, 8, 9 }, Wt::WTime{ 23, 34, 32 } }));
EXPECT_EQ(podcast.imageUrl, "https://www.radiofrance.fr/s3/cruiser-production/2023/05/5f197ac5-b950-4149-8578-ae12aa6695b0/1400x1400_sc_rf_omm_0000038645_ite.jpg");
// itunes
EXPECT_EQ(podcast.copyright, "Radio France");
EXPECT_EQ(podcast.author, "France Inter");
EXPECT_EQ(podcast.category, "Society & Culture");
EXPECT_EQ(podcast.explicitContent, false);
EXPECT_EQ(podcast.ownerEmail, "podcast@radiofrance.com");
EXPECT_EQ(podcast.ownerName, "Radio France");
EXPECT_EQ(podcast.subtitle, "Affaires sensibles");
EXPECT_EQ(podcast.summary, R"(Les grandes affaires, les aventures et les procès qui ont marqué les cinquante dernières années. Vous aimez ce podcast ? Pour écouter tous les épisodes sans limite, rendez-vous sur <a href="https://www.franceinter.fr/emission-affaires-sensibles?at_campaign=desc_podcast&amp;at_medium=lien_RSS">Radio France</a>.)");
ASSERT_EQ(podcast.episodes.size(), 2);
EXPECT_EQ(podcast.episodes[0].title, R"(Apollo 13 ou les naufragés de lespace)");
EXPECT_EQ(podcast.episodes[0].description, R"(durée : 00:53:58 - Affaires sensibles - par : Christophe Barreyre, Fabrice Drouelle - C'est une épopée qui réunit héroïsme, génie technologique, audace diplomatique, sens du tragique. Lodyssée d'Apollo 13 cest un vaisseau spatial en perdition, là-haut, flirtant avec la lune mais qui au dernier moment se dérobe pour une vulgaire panne électrique dans la soute du vaisseau. - réalisé par : Flora BERNARD, Marion Le Lay, Stéphane COSME Vous aimez ce podcast ? Pour écouter tous les autres épisodes sans limite, rendez-vous sur <a href="https://www.franceinter.fr/emission-affaires-sensibles?at_campaign=desc_episode&amp;at_medium=lien_RSS">Radio France</a>.)");
EXPECT_EQ(podcast.episodes[0].author, "Christophe Barreyre, Fabrice Drouelle");
EXPECT_EQ(podcast.episodes[0].explicitContent, false);
EXPECT_EQ(podcast.episodes[0].imageUrl, "https://www.radiofrance.fr/s3/cruiser-production/2021/04/ec0f1c5d-ecfa-4ec4-a5a5-30f446d25aea/1400x1400_affaires_sensibles.jpg");
EXPECT_EQ(podcast.episodes[0].link, "https://www.radiofrance.fr/franceinter/podcasts/affaires-sensibles/affaires-sensibles-du-jeudi-30-decembre-2021-4186094");
EXPECT_EQ(podcast.episodes[0].pubDate, (Wt::WDateTime{ Wt::WDate{ 2025, 8, 9 }, Wt::WTime{ 17, 59, 59 } }));
EXPECT_EQ(podcast.episodes[0].guid, "9f587824-01f5-443d-aa72-a56519d25857-NET_MFI_F7191B05-DD5B-4AFE-BB96-3BD8ADB3240A");
EXPECT_EQ(podcast.episodes[0].enclosureUrl.url, "https://proxycast.radiofrance.fr/e2a713a6-aba0-4d2a-a4a1-d135d98f1f8a/13940-09.08.2025-ITEMA_24214125-2021F22805S0364-NET_MFI_F7191B05-DD5B-4AFE-BB96-3BD8ADB3240A-22.mp3");
EXPECT_EQ(podcast.episodes[0].enclosureUrl.length, 51842568);
EXPECT_EQ(podcast.episodes[0].enclosureUrl.type, "audio/mpeg");
EXPECT_EQ(podcast.episodes[0].duration, std::chrono::minutes{ 53 } + std::chrono::seconds{ 58 });
EXPECT_EQ(podcast.episodes[1].title, R"(Stéphane Breitwieser, le pilleur de musées)");
EXPECT_EQ(podcast.episodes[1].description, R"(durée : 00:47:45 - Affaires sensibles - par : Fabrice Drouelle, Franck COGNARD - Aujourdhui dans Affaires Sensibles : laffaire Stéphane Breitwieser, le pilleur de musées - réalisé par : Etienne BERTIN Vous aimez ce podcast ? Pour écouter tous les autres épisodes sans limite, rendez-vous sur <a href="https://www.franceinter.fr/emission-affaires-sensibles?at_campaign=desc_episode&amp;at_medium=lien_RSS">Radio France</a>.)");
EXPECT_EQ(podcast.episodes[1].author, "Fabrice Drouelle, Franck COGNARD");
EXPECT_EQ(podcast.episodes[1].explicitContent, false);
EXPECT_EQ(podcast.episodes[1].imageUrl, "https://www.radiofrance.fr/s3/cruiser-production/2023/04/7b50cf5f-f5bd-4dc4-8b1d-b08666768dcf/1400x1400_sc_affaires-sensibles.jpg");
EXPECT_EQ(podcast.episodes[1].link, "https://www.radiofrance.fr/franceinter/podcasts/affaires-sensibles/affaires-sensibles-du-mercredi-02-avril-2025-9048848");
EXPECT_EQ(podcast.episodes[1].pubDate, (Wt::WDateTime{ Wt::WDate{ 2025, 8, 8 }, Wt::WTime{ 22, 59, 59 } }));
EXPECT_EQ(podcast.episodes[1].guid, "719c21be-3832-4b67-8d65-9d07b3715d7b-NET_MFI_FF964E00-DFB6-45AC-A9D2-2D1211F37586");
EXPECT_EQ(podcast.episodes[1].enclosureUrl.url, "https://proxycast.radiofrance.fr/d0895b0b-a99c-4e9d-9d99-13a029960d04/13940-08.08.2025-ITEMA_24213067-2025F22805S0092-NET_MFI_FF964E00-DFB6-45AC-A9D2-2D1211F37586-22.mp3");
EXPECT_EQ(podcast.episodes[1].enclosureUrl.length, 45869054);
EXPECT_EQ(podcast.episodes[1].enclosureUrl.type, "audio/mpeg");
EXPECT_EQ(podcast.episodes[1].duration, std::chrono::minutes{ 47 } + std::chrono::seconds{ 45 });
}
} // namespace lms::podcast::tests
@@ -0,0 +1,32 @@
/*
* Copyright (C) 2025 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 <gtest/gtest.h>
#include "core/ILogger.hpp"
#include "core/Service.hpp"
int main(int argc, char** argv)
{
using namespace lms;
core::Service<core::logging::ILogger> logger{ core::logging::createLogger(core::logging::Severity::ERROR) };
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
@@ -153,14 +153,15 @@ namespace lms::scanner
} // namespace
std::unique_ptr<IScannerService> createScannerService(db::IDb& db)
std::unique_ptr<IScannerService> createScannerService(db::IDb& db, const std::filesystem::path& cachePath)
{
return std::make_unique<ScannerService>(db);
return std::make_unique<ScannerService>(db, cachePath);
}
ScannerService::ScannerService(db::IDb& db)
ScannerService::ScannerService(db::IDb& db, const std::filesystem::path& cachePath)
: _db{ db }
, _jobScheduler{ core::createJobScheduler("Scanner", getScannerThreadCount()) }
, _cachePath{ cachePath }
{
_ioService.setThreadCount(1);
@@ -496,6 +497,7 @@ namespace lms::scanner
.abortScan = _abortScan,
.db = _db,
.fileScanners = _fileScanners,
.cachePath = _cachePath
};
// Order is important: steps are sequential
@@ -31,11 +31,12 @@
#include <Wt/WIOService.h>
#include <Wt/WSignal.h>
#include "FileScanners.hpp"
#include "ScannerSettings.hpp"
#include "database/IDb.hpp"
#include "database/Session.hpp"
#include "services/scanner/IScannerService.hpp"
#include "FileScanners.hpp"
#include "ScannerSettings.hpp"
#include "steps/IScanStep.hpp"
namespace lms::core
@@ -53,7 +54,7 @@ namespace lms::scanner
class ScannerService : public IScannerService
{
public:
ScannerService(db::IDb& db);
ScannerService(db::IDb& db, const std::filesystem::path& cachePath);
~ScannerService() override;
ScannerService(const ScannerService&) = delete;
ScannerService& operator=(const ScannerService&) = delete;
@@ -89,6 +90,7 @@ namespace lms::scanner
db::IDb& _db;
std::unique_ptr<core::IJobScheduler> _jobScheduler;
const std::filesystem::path _cachePath;
FileScanners _fileScanners;
std::vector<std::unique_ptr<IScanStep>> _scanSteps;
@@ -30,6 +30,7 @@ namespace lms::scanner
, _db{ initParams.db }
, _jobScheduler{ initParams.jobScheduler }
, _fileScanners(initParams.fileScanners)
, _cachePath{ initParams.cachePath }
, _lastScanSettings{ initParams.lastScanSettings }
{
}
@@ -55,6 +55,7 @@ namespace lms::scanner
bool& abortScan;
db::IDb& db;
const FileScanners& fileScanners;
const std::filesystem::path& cachePath;
};
ScanStepBase(InitParams& initParams);
~ScanStepBase() override;
@@ -65,6 +66,7 @@ namespace lms::scanner
core::IJobScheduler& getJobScheduler() { return _jobScheduler; };
const ScannerSettings* getLastScanSettings() const { return _lastScanSettings; }
const FileScanners& getFileScanners() const { return _fileScanners; }
const std::filesystem::path& getCachePath() const { return _cachePath; }
void addError(ScanContext& context, std::shared_ptr<ScanError> error);
@@ -83,6 +85,7 @@ namespace lms::scanner
private:
core::IJobScheduler& _jobScheduler;
const FileScanners& _fileScanners;
const std::filesystem::path& _cachePath;
const ScannerSettings* _lastScanSettings{};
ScanErrorLogger _scanErrorLogger;
@@ -63,6 +63,9 @@ namespace lms::scanner
{
}
CheckForRemovedFilesJob(const CheckForRemovedFilesJob&) = delete;
CheckForRemovedFilesJob& operator=(const CheckForRemovedFilesJob&) = delete;
std::size_t getProcessedCount() const { return _processedCount; }
std::span<const IdType> getObjectsToRemove() const { return _objectsToRemove; }
@@ -90,8 +93,7 @@ namespace lms::scanner
return false;
}
// For each track, make sure the the file still exists
// and still belongs to a media directory
// For file, make sure the the file still exists, is a regular file, is in a media directory and is of a supported format
if (!fileEntry.exists() || !fileEntry.is_regular_file())
{
LMS_LOG(DBUPDATER, DEBUG, "Removing " << p << ": missing");
@@ -151,7 +153,7 @@ namespace lms::scanner
}
template<typename Object>
bool fetchNextFilesToCheck(db::Session& session, typename Object::IdType& lastCheckedId, std::vector<FileToCheck<typename Object::IdType>>& filesToCheck)
bool fetchNextFilesToCheck(db::Session& session, typename Object::IdType& lastCheckedId, const std::filesystem::path& cachepath, std::vector<FileToCheck<typename Object::IdType>>& filesToCheck)
{
constexpr std::size_t batchSize{ 200 };
@@ -164,6 +166,10 @@ namespace lms::scanner
{
const typename Object::IdType previousLastCheckedId{ lastCheckedId };
Object::findAbsoluteFilePath(session, lastCheckedId, batchSize, [&](Object::IdType objectId, const std::filesystem::path& filePath) {
// Do not consider files in the cache directory as they are not managed by the scanner itself
if (core::pathUtils::isPathInRootPath(filePath, cachepath))
return;
// special case for track lyrics, only check external lyrics
if constexpr (std::is_same_v<Object, db::TrackLyrics>)
{
@@ -242,12 +248,11 @@ namespace lms::scanner
ObjectIdType lastCheckedId;
std::vector<FileToCheck<ObjectIdType>> filesToCheck;
while (fetchNextFilesToCheck<Object>(session, lastCheckedId, filesToCheck))
while (fetchNextFilesToCheck<Object>(session, lastCheckedId, getCachePath(), filesToCheck))
queue.push(std::make_unique<CheckForRemovedFilesJob<ObjectIdType>>(_settings, getFileScanners(), filesToCheck));
}
// process all remaining objects
context.stats.deletions += removeObjects<Object>(session, objectIdsToRemove, false);
}
} // namespace lms::scanner
@@ -19,6 +19,7 @@
#pragma once
#include <filesystem>
#include <optional>
#include "ScannerEvents.hpp"
@@ -61,5 +62,5 @@ namespace lms::scanner
virtual Events& getEvents() = 0;
};
std::unique_ptr<IScannerService> createScannerService(db::IDb& db);
std::unique_ptr<IScannerService> createScannerService(db::IDb& db, const std::filesystem::path& cachePath);
} // namespace lms::scanner
@@ -27,7 +27,6 @@
#include <boost/asio/bind_executor.hpp>
#include <boost/asio/post.hpp>
#include "ListensParser.hpp"
#include "core/IConfig.hpp"
#include "core/Service.hpp"
#include "core/http/IClient.hpp"
@@ -40,6 +39,7 @@
#include "database/objects/User.hpp"
#include "services/scrobbling/Exception.hpp"
#include "ListensParser.hpp"
#include "Utils.hpp"
namespace lms::scrobbling::listenBrainz
@@ -246,7 +246,7 @@ namespace lms::scrobbling::listenBrainz
saveListen(timedListen, db::SyncState::PendingAdd);
request.priority = core::http::ClientRequestParameters::Priority::Normal;
request.onSuccessFunc = [this, timedListen](std::string_view) {
request.onSuccessFunc = [this, timedListen](const Wt::Http::Message&) {
boost::asio::post(boost::asio::bind_executor(_strand, [this, timedListen] {
if (saveListen(timedListen, db::SyncState::Synchronized))
{
@@ -456,8 +456,8 @@ namespace lms::scrobbling::listenBrainz
request.priority = core::http::ClientRequestParameters::Priority::Low;
request.relativeUrl = "/1/validate-token";
request.headers = { { "Authorization", "Token " + std::string{ listenBrainzToken->getAsString() } } };
request.onSuccessFunc = [this, &context](std::string_view msgBody) {
context.listenBrainzUserName = utils::parseValidateToken(msgBody);
request.onSuccessFunc = [this, &context](const Wt::Http::Message& msg) {
context.listenBrainzUserName = utils::parseValidateToken(msg.body());
if (context.listenBrainzUserName.empty())
{
onSyncEnded(context);
@@ -479,8 +479,8 @@ namespace lms::scrobbling::listenBrainz
core::http::ClientGETRequestParameters request;
request.relativeUrl = "/1/user/" + std::string{ context.listenBrainzUserName } + "/listen-count";
request.priority = core::http::ClientRequestParameters::Priority::Low;
request.onSuccessFunc = [this, &context](std::string_view msgBody) {
const auto listenCount{ parseListenCount(msgBody) };
request.onSuccessFunc = [this, &context](const Wt::Http::Message& msg) {
const auto listenCount{ parseListenCount(msg.body()) };
boost::asio::post(boost::asio::bind_executor(_strand, [this, listenCount, &context] {
if (listenCount)
LOG(DEBUG, "Listen count for listenbrainz user '" << context.listenBrainzUserName << "' = " << *listenCount);
@@ -512,8 +512,8 @@ namespace lms::scrobbling::listenBrainz
core::http::ClientGETRequestParameters request;
request.relativeUrl = "/1/user/" + context.listenBrainzUserName + "/listens?max_ts=" + std::to_string(context.maxDateTime.toTime_t());
request.priority = core::http::ClientRequestParameters::Priority::Low;
request.onSuccessFunc = [this, &context](std::string_view msgBody) {
processGetListensResponse(msgBody, context);
request.onSuccessFunc = [this, &context](const Wt::Http::Message& msg) {
processGetListensResponse(msg.body(), context);
if (context.fetchedListenCount >= _maxSyncListenCount || !context.maxDateTime.isValid())
{
onSyncEnded(context);
@@ -23,7 +23,6 @@
#include "core/ILogger.hpp"
#include "database/IDb.hpp"
#include "database/Session.hpp"
#include "database/objects/Track.hpp"
#include "TranscodingResourceHandler.hpp"
@@ -64,20 +63,17 @@ namespace lms::transcoding
{
av::InputParameters avInputParams;
std::optional<std::size_t> estimatedContentLength;
avInputParams.file = inputParameters.filePath;
avInputParams.offset = inputParameters.offset;
avInputParams.streamIndex = inputParameters.streamIndex;
if (estimateContentLength)
{
auto& session{ _db.getTLSSession() };
auto transaction{ session.createReadTransaction() };
db::Track::pointer track{ db::Track::find(session, inputParameters.trackId) };
if (!track)
return nullptr;
avInputParams.file = track->getAbsoluteFilePath();
avInputParams.offset = inputParameters.offset;
avInputParams.streamIndex = inputParameters.streamIndex;
if (estimateContentLength)
estimatedContentLength = doEstimateContentLength(outputParameters.bitrate, track->getDuration());
if (inputParameters.offset < inputParameters.duration)
estimatedContentLength = doEstimateContentLength(outputParameters.bitrate, inputParameters.duration - inputParameters.offset);
else
LMS_LOG(TRANSCODING, WARNING, "Offset " << inputParameters.offset << " is greater than audio file duration " << inputParameters.duration << ": not estimating content length");
}
return std::make_unique<TranscodingResourceHandler>(avInputParams, toAv(outputParameters), estimatedContentLength);
@@ -19,12 +19,11 @@
#pragma once
#include <chrono>
#include <filesystem>
#include <memory>
#include <optional>
#include "database/objects/TrackId.hpp"
namespace lms
{
namespace core
@@ -43,9 +42,10 @@ namespace lms::transcoding
{
struct InputParameters
{
db::TrackId trackId;
std::chrono::milliseconds offset{}; // Offset in the track file to start transcoding from
std::optional<std::size_t> streamIndex; // Index of the stream to be transcoded (select "best" audio stream if not set)
std::filesystem::path filePath;
std::chrono::milliseconds duration{}; // Duration of the audio file
std::chrono::milliseconds offset{}; // Offset in the audio file to start transcoding from
std::optional<std::size_t> streamIndex; // Index of the stream to be transcoded (select the "best" audio stream if not set)
};
enum class OutputFormat