/*
* Copyright (C) 2016 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 .
*/
#include "TagLibParser.hpp"
#include
#include
#include
#include
#include
#include "utils/Logger.hpp"
#include "utils/Utils.hpp"
namespace MetaData
{
std::vector
getPropertyValuesFirstMatch(const TagLib::PropertyMap& properties, const std::set& keys)
{
std::vector res;
for (const std::string& key : keys)
{
const TagLib::StringList& values {properties[key]};
if (values.isEmpty())
continue;
res.reserve(values.size());
std::transform(std::cbegin(values), std::cend(values), std::back_inserter(res), [](const auto& value) { return stringTrim(value.to8Bit(true)); });
break;
}
return res;
}
std::vector
getPropertyValues(const TagLib::PropertyMap& properties, const std::string& key)
{
return getPropertyValuesFirstMatch(properties, {std::move(key)});
}
static
std::vector
splitAndTrimString(const std::string& str, const std::string& delimiters)
{
std::vector res;
std::vector strings {splitString(str, delimiters)};
for (const std::string& s : strings)
res.emplace_back(stringTrim(s));
return res;
}
static
std::vector
getArtists(const TagLib::PropertyMap& properties)
{
std::vector res;
std::vector artistNames {getPropertyValues(properties, "ARTISTS")};
if (artistNames.empty())
artistNames = getPropertyValues(properties, "ARTIST");
if (artistNames.empty())
return res;
const std::vector artistsMBID {getPropertyValuesFirstMatch(properties, {"MUSICBRAINZ_ARTISTID", "MUSICBRAINZ ARTIST ID"})};
if (artistNames.size() == artistsMBID.size())
{
std::transform(std::cbegin(artistNames), std::cend(artistNames), std::cbegin(artistsMBID), std::back_inserter(res),
[&](const std::string& name, const std::string& mbid) { return Artist{name, mbid}; });
}
else
{
std::transform(std::cbegin(artistNames), std::cend(artistNames), std::back_inserter(res),
[&](const std::string& name) { return Artist{name, ""}; });
}
return res;
}
static
std::vector
getAlbumArtists(const TagLib::PropertyMap& properties)
{
std::vector res;
std::vector artistNames {getPropertyValues(properties, "ALBUMARTIST")};
if (artistNames.empty())
return res;
const std::vector artistsMBID {getPropertyValuesFirstMatch(properties, {"MUSICBRAINZ_ALBUMARTISTID", "MUSICBRAINZ ALBUM ARTIST ID"})};
if (artistNames.size() == artistsMBID.size())
{
std::transform(std::cbegin(artistNames), std::cend(artistNames), std::cbegin(artistsMBID), std::back_inserter(res),
[&](const std::string& name, const std::string& mbid) { return Artist{name, mbid}; });
}
else
{
std::transform(std::cbegin(artistNames), std::cend(artistNames), std::back_inserter(res),
[&](const std::string& name) { return Artist{name, ""}; });
}
return res;
}
static
boost::optional
getAlbum(const TagLib::PropertyMap& properties)
{
boost::optional res;
std::vector albumName {getPropertyValues(properties, "ALBUM")};
if (albumName.empty())
return res;
std::vector albumMBID {getPropertyValuesFirstMatch(properties, {"MUSICBRAINZ_ALBUMID", "MUSICBRAINZ ALBUM ID"})};
res = Album{std::move(albumName.front()), ""};
if (!albumMBID.empty())
res->musicBrainzAlbumID = std::move(albumMBID.front());
return res;
}
boost::optional