Added basic support for artist.nfo file parsing, ref #640
This commit is contained in:
@@ -29,6 +29,7 @@
|
||||
#include "database/MediaLibrary.hpp"
|
||||
#include "database/ScanSettings.hpp"
|
||||
|
||||
#include "scanners/ArtistInfoFileScanner.hpp"
|
||||
#include "scanners/AudioFileScanner.hpp"
|
||||
#include "scanners/ImageFileScanner.hpp"
|
||||
#include "scanners/LyricsFileScanner.hpp"
|
||||
@@ -347,6 +348,7 @@ namespace lms::scanner
|
||||
} };
|
||||
|
||||
_fileScanners.clear();
|
||||
_fileScanners.emplace_back(std::make_unique<ArtistInfoFileScanner>(_db));
|
||||
_fileScanners.emplace_back(std::make_unique<AudioFileScanner>(_db, _settings));
|
||||
_fileScanners.emplace_back(std::make_unique<ImageFileScanner>(_db));
|
||||
_fileScanners.emplace_back(std::make_unique<LyricsFileScanner>(_db));
|
||||
|
||||
@@ -0,0 +1,195 @@
|
||||
/*
|
||||
* 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 "ArtistInfoFileScanner.hpp"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
#include "core/ILogger.hpp"
|
||||
#include "database/Artist.hpp"
|
||||
#include "database/ArtistInfo.hpp"
|
||||
#include "database/Db.hpp"
|
||||
#include "database/MediaLibrary.hpp"
|
||||
#include "database/Session.hpp"
|
||||
#include "metadata/ArtistInfo.hpp"
|
||||
|
||||
#include "IFileScanOperation.hpp"
|
||||
#include "ScanContext.hpp"
|
||||
#include "Utils.hpp"
|
||||
|
||||
namespace lms::scanner
|
||||
{
|
||||
namespace
|
||||
{
|
||||
class ArtistInfoFileScanOperation : public IFileScanOperation
|
||||
{
|
||||
public:
|
||||
ArtistInfoFileScanOperation(const FileToScan& file, db::Db& db)
|
||||
: _file{ file.file }
|
||||
, _mediaLibrary{ file.mediaLibrary }
|
||||
, _db{ db } {}
|
||||
~ArtistInfoFileScanOperation() override = default;
|
||||
ArtistInfoFileScanOperation(const ArtistInfoFileScanOperation&) = delete;
|
||||
ArtistInfoFileScanOperation& operator=(const ArtistInfoFileScanOperation&) = delete;
|
||||
|
||||
private:
|
||||
const std::filesystem::path& getFile() const override { return _file; };
|
||||
core::LiteralString getName() const override { return "ScanArtistInfoFile"; }
|
||||
void scan() override;
|
||||
void processResult(ScanContext& context) override;
|
||||
|
||||
std::string getArtistNameFromArtistInfoFilePath();
|
||||
|
||||
const std::filesystem::path _file;
|
||||
const MediaLibraryInfo _mediaLibrary;
|
||||
db::Db& _db;
|
||||
|
||||
std::optional<metadata::ArtistInfo> _parsedArtistInfo;
|
||||
};
|
||||
|
||||
void ArtistInfoFileScanOperation::scan()
|
||||
{
|
||||
try
|
||||
{
|
||||
std::ifstream ifs{ _file };
|
||||
if (!ifs)
|
||||
{
|
||||
LMS_LOG(DBUPDATER, ERROR, "Cannot open file " << _file);
|
||||
return;
|
||||
}
|
||||
|
||||
_parsedArtistInfo = metadata::parseArtistInfo(ifs);
|
||||
if (!_parsedArtistInfo->mbid.has_value())
|
||||
{
|
||||
LMS_LOG(DBUPDATER, DEBUG, "Discarding artist info in file " << _file << ": no mbid set");
|
||||
_parsedArtistInfo.reset();
|
||||
}
|
||||
else if (_parsedArtistInfo->name.empty())
|
||||
{
|
||||
LMS_LOG(DBUPDATER, DEBUG, "Discarding artist info in file " << _file << ": no name set");
|
||||
_parsedArtistInfo.reset();
|
||||
}
|
||||
}
|
||||
catch (const metadata::ArtistInfoParseException& e)
|
||||
{
|
||||
LMS_LOG(DBUPDATER, ERROR, "Cannot read artist info in file " << _file << ": " << e.what());
|
||||
}
|
||||
}
|
||||
|
||||
void ArtistInfoFileScanOperation::processResult(ScanContext& context)
|
||||
{
|
||||
ScanStats& stats{ context.stats };
|
||||
|
||||
const std::optional<FileInfo> fileInfo{ utils::retrieveFileInfo(_file, _mediaLibrary.rootDirectory) };
|
||||
if (!fileInfo)
|
||||
{
|
||||
stats.skips++;
|
||||
return;
|
||||
}
|
||||
|
||||
db::Session& dbSession{ _db.getTLSSession() };
|
||||
db::ArtistInfo::pointer artistInfo{ db::ArtistInfo::find(dbSession, _file) };
|
||||
if (!_parsedArtistInfo)
|
||||
{
|
||||
if (artistInfo)
|
||||
{
|
||||
artistInfo.remove();
|
||||
stats.deletions++;
|
||||
LMS_LOG(DBUPDATER, DEBUG, "Removed artist info file " << _file);
|
||||
}
|
||||
context.stats.errors.emplace_back(_file, ScanErrorType::CannotReadArtistInfoFile);
|
||||
return;
|
||||
}
|
||||
|
||||
const bool added{ !artistInfo };
|
||||
if (!artistInfo)
|
||||
{
|
||||
artistInfo = dbSession.create<db::ArtistInfo>();
|
||||
artistInfo.modify()->setAbsoluteFilePath(_file);
|
||||
}
|
||||
|
||||
artistInfo.modify()->setLastWriteTime(fileInfo->lastWriteTime);
|
||||
artistInfo.modify()->setType(_parsedArtistInfo->type);
|
||||
artistInfo.modify()->setGender(_parsedArtistInfo->gender);
|
||||
artistInfo.modify()->setDisambiguation(_parsedArtistInfo->disambiguation);
|
||||
artistInfo.modify()->setBiography(_parsedArtistInfo->biography);
|
||||
|
||||
db::MediaLibrary::pointer mediaLibrary{ db::MediaLibrary::find(dbSession, _mediaLibrary.id) }; // may be null if settings are updated in // => next scan will correct this
|
||||
artistInfo.modify()->setDirectory(utils::getOrCreateDirectory(dbSession, _file.parent_path(), mediaLibrary));
|
||||
|
||||
db::Artist::pointer artist{ db::Artist::find(dbSession, *_parsedArtistInfo->mbid) };
|
||||
if (!artist)
|
||||
artist = dbSession.create<db::Artist>(_parsedArtistInfo->name, _parsedArtistInfo->mbid);
|
||||
|
||||
artist.modify()->setName(_parsedArtistInfo->name);
|
||||
artist.modify()->setSortName(_parsedArtistInfo->sortName);
|
||||
artistInfo.modify()->setArtist(artist);
|
||||
|
||||
if (added)
|
||||
{
|
||||
LMS_LOG(DBUPDATER, DEBUG, "Added artist info file " << _file);
|
||||
stats.additions++;
|
||||
}
|
||||
else
|
||||
{
|
||||
LMS_LOG(DBUPDATER, DEBUG, "Updated artist info file '" << _file);
|
||||
stats.updates++;
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
ArtistInfoFileScanner::ArtistInfoFileScanner(db::Db& db)
|
||||
: _db{ db }
|
||||
{
|
||||
}
|
||||
|
||||
core::LiteralString ArtistInfoFileScanner::getName() const
|
||||
{
|
||||
return "Artist info scanner ";
|
||||
}
|
||||
|
||||
std::span<const std::filesystem::path> ArtistInfoFileScanner::getSupportedExtensions() const
|
||||
{
|
||||
return metadata::getSupportedInfoFileExtensions();
|
||||
}
|
||||
|
||||
bool ArtistInfoFileScanner::needsScan(ScanContext& context, const FileToScan& file) const
|
||||
{
|
||||
const Wt::WDateTime lastWriteTime{ utils::retrieveFileGetLastWrite(file.file) };
|
||||
// Should rarely fail as we are currently iterating it
|
||||
if (!lastWriteTime.isValid())
|
||||
{
|
||||
context.stats.skips++;
|
||||
return false;
|
||||
}
|
||||
|
||||
db::Session& dbSession{ _db.getTLSSession() };
|
||||
auto transaction{ dbSession.createReadTransaction() };
|
||||
db::ArtistInfo::pointer artistInfo{ db::ArtistInfo::find(dbSession, file.file) };
|
||||
if (!artistInfo)
|
||||
return true;
|
||||
|
||||
return artistInfo->getLastWriteTime() != lastWriteTime;
|
||||
}
|
||||
|
||||
std::unique_ptr<IFileScanOperation> ArtistInfoFileScanner::createScanOperation(const FileToScan& fileToScan) const
|
||||
{
|
||||
return std::make_unique<ArtistInfoFileScanOperation>(fileToScan, _db);
|
||||
}
|
||||
} // namespace lms::scanner
|
||||
@@ -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 "IFileScanner.hpp"
|
||||
|
||||
namespace lms
|
||||
{
|
||||
namespace db
|
||||
{
|
||||
class Db;
|
||||
}
|
||||
} // namespace lms
|
||||
|
||||
namespace lms::scanner
|
||||
{
|
||||
class ArtistInfoFileScanner : public IFileScanner
|
||||
{
|
||||
public:
|
||||
ArtistInfoFileScanner(db::Db& db);
|
||||
~ArtistInfoFileScanner() override = default;
|
||||
ArtistInfoFileScanner(const ArtistInfoFileScanner&) = delete;
|
||||
ArtistInfoFileScanner& operator=(const ArtistInfoFileScanner&) = delete;
|
||||
|
||||
private:
|
||||
core::LiteralString getName() const override;
|
||||
std::span<const std::filesystem::path> getSupportedExtensions() const override;
|
||||
bool needsScan(ScanContext& context, const FileToScan& file) const override;
|
||||
std::unique_ptr<IFileScanOperation> createScanOperation(const FileToScan& fileToScan) const override;
|
||||
|
||||
db::Db& _db;
|
||||
};
|
||||
} // namespace lms::scanner
|
||||
@@ -94,6 +94,7 @@ namespace lms::scanner
|
||||
{
|
||||
trackLyrics.remove();
|
||||
stats.deletions++;
|
||||
LMS_LOG(DBUPDATER, DEBUG, "Removed lyrics file " << _file);
|
||||
}
|
||||
context.stats.errors.emplace_back(_file, ScanErrorType::CannotReadLyricsFile);
|
||||
return;
|
||||
|
||||
@@ -98,9 +98,9 @@ namespace lms::scanner
|
||||
{
|
||||
playList.remove();
|
||||
stats.deletions++;
|
||||
LMS_LOG(DBUPDATER, DEBUG, "Removed playlist file " << _file);
|
||||
}
|
||||
context.stats.errors.emplace_back(_file, ScanErrorType::CannotReadPlayListFile);
|
||||
LMS_LOG(DBUPDATER, DEBUG, "Removed playlist file " << _file);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -28,7 +28,9 @@
|
||||
#include "core/IConfig.hpp"
|
||||
#include "core/ILogger.hpp"
|
||||
#include "core/Path.hpp"
|
||||
#include "core/String.hpp"
|
||||
#include "database/Artist.hpp"
|
||||
#include "database/ArtistInfo.hpp"
|
||||
#include "database/Db.hpp"
|
||||
#include "database/Directory.hpp"
|
||||
#include "database/Image.hpp"
|
||||
@@ -57,14 +59,14 @@ namespace lms::scanner
|
||||
std::span<const std::string> artistFileNames;
|
||||
};
|
||||
|
||||
db::Image::pointer findImageInDirectory(SearchImageContext& searchContext, const std::filesystem::path& directoryPath)
|
||||
db::Image::pointer findImageInDirectory(SearchImageContext& searchContext, const std::filesystem::path& directoryPath, std::span<const std::string> fileStemsToSearch)
|
||||
{
|
||||
db::Image::pointer image;
|
||||
|
||||
const db::Directory::pointer directory{ db::Directory::find(searchContext.session, directoryPath) };
|
||||
if (directory) // may not exist for artists that are split on different media libraries
|
||||
{
|
||||
for (std::string_view fileStem : searchContext.artistFileNames)
|
||||
for (std::string_view fileStem : fileStemsToSearch)
|
||||
{
|
||||
db::Image::FindParameters params;
|
||||
params.setDirectory(directory->getId());
|
||||
@@ -96,6 +98,24 @@ namespace lms::scanner
|
||||
return image;
|
||||
}
|
||||
|
||||
db::Image::pointer searchImageInArtistInfoDirectory(SearchImageContext& searchContext, db::ArtistId artistId)
|
||||
{
|
||||
db::Image::pointer image;
|
||||
|
||||
std::vector<std::string> fileInfoPaths;
|
||||
db::ArtistInfo::find(searchContext.session, artistId, [&](const db::ArtistInfo::pointer& artistInfo) {
|
||||
fileInfoPaths.push_back(artistInfo->getAbsoluteFilePath());
|
||||
|
||||
if (!image)
|
||||
image = findImageInDirectory(searchContext, artistInfo->getDirectory()->getAbsolutePath(), std::array<std::string, 2>{ "thumb", "folder" });
|
||||
});
|
||||
|
||||
if (fileInfoPaths.size() > 1)
|
||||
LMS_LOG(DBUPDATER, DEBUG, "Found " << fileInfoPaths.size() << " artist info files for same artist: " << core::stringUtils::joinStrings(fileInfoPaths, ", "));
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
db::Image::pointer searchImageInDirectories(SearchImageContext& searchContext, db::ArtistId artistId)
|
||||
{
|
||||
db::Image::pointer image;
|
||||
@@ -123,7 +143,7 @@ namespace lms::scanner
|
||||
std::filesystem::path directoryToInspect{ core::pathUtils::getLongestCommonPath(std::cbegin(releasePaths), std::cend(releasePaths)) };
|
||||
while (true)
|
||||
{
|
||||
image = findImageInDirectory(searchContext, directoryToInspect);
|
||||
image = findImageInDirectory(searchContext, directoryToInspect, searchContext.artistFileNames);
|
||||
if (image)
|
||||
return image;
|
||||
|
||||
@@ -140,7 +160,7 @@ namespace lms::scanner
|
||||
// /someOtherUserConfiguredArtistFile.jpg
|
||||
for (const std::filesystem::path& releasePath : releasePaths)
|
||||
{
|
||||
image = findImageInDirectory(searchContext, releasePath);
|
||||
image = findImageInDirectory(searchContext, releasePath, searchContext.artistFileNames);
|
||||
if (image)
|
||||
return image;
|
||||
}
|
||||
@@ -156,6 +176,9 @@ namespace lms::scanner
|
||||
if (const auto mbid{ artist->getMBID() })
|
||||
image = getImageFromMbid(searchContext, *mbid);
|
||||
|
||||
if (!image)
|
||||
image = searchImageInArtistInfoDirectory(searchContext, artist->getId());
|
||||
|
||||
if (!image)
|
||||
image = searchImageInDirectories(searchContext, artist->getId());
|
||||
|
||||
|
||||
@@ -21,9 +21,9 @@
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "ScannerSettings.hpp"
|
||||
#include "core/ILogger.hpp"
|
||||
#include "core/Path.hpp"
|
||||
#include "database/ArtistInfo.hpp"
|
||||
#include "database/Db.hpp"
|
||||
#include "database/Image.hpp"
|
||||
#include "database/PlayListFile.hpp"
|
||||
@@ -32,6 +32,8 @@
|
||||
#include "database/TrackLyrics.hpp"
|
||||
#include "scanners/IFileScanner.hpp"
|
||||
|
||||
#include "ScannerSettings.hpp"
|
||||
|
||||
namespace lms::scanner
|
||||
{
|
||||
namespace
|
||||
@@ -53,6 +55,7 @@ namespace lms::scanner
|
||||
context.currentStepStats.totalElems += db::Image::getCount(session);
|
||||
context.currentStepStats.totalElems += db::TrackLyrics::getExternalLyricsCount(session);
|
||||
context.currentStepStats.totalElems += db::PlayListFile::getCount(session);
|
||||
context.currentStepStats.totalElems += db::ArtistInfo::getCount(session);
|
||||
}
|
||||
LMS_LOG(DBUPDATER, DEBUG, context.currentStepStats.totalElems << " files to be checked...");
|
||||
|
||||
@@ -67,6 +70,7 @@ namespace lms::scanner
|
||||
checkForRemovedFiles<db::Image>(context, supportedFileExtensions);
|
||||
checkForRemovedFiles<db::TrackLyrics>(context, supportedFileExtensions);
|
||||
checkForRemovedFiles<db::PlayListFile>(context, supportedFileExtensions);
|
||||
checkForRemovedFiles<db::ArtistInfo>(context, supportedFileExtensions);
|
||||
}
|
||||
|
||||
template<typename Object>
|
||||
|
||||
Reference in New Issue
Block a user