Skip to content

Commit

Permalink
fix: dup artists
Browse files Browse the repository at this point in the history
  • Loading branch information
pnck committed May 13, 2024
1 parent 5c2f57b commit 51458bd
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/input_ncm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ void input_ncm::get_info(file_info &p_info, abort_callback &p_abort) {
reader->get_info(/*sub song*/ 0, p_info, p_abort);

auto mp = meta_processor(p_info);
p_info.meta_remove_all();
mp.update(ncm_file_->meta_info());
mp.apply(p_info);
}
Expand Down
38 changes: 16 additions & 22 deletions src/meta_process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,36 +117,30 @@ void meta_processor::update_by_json(const nlohmann::json &json, bool overwriting
if (val.size() != 2) {
continue;
}
if (!val[0].is_string() || !val[1].is_number_integer()) {
if (!val[0].is_string() /*|| !val[1].is_number_integer()*/) { // ARTIST ID can be str or num
continue;
}
artist->emplace(val[0].get<std::string>(), val[1].get<uint64_t>());
artist->emplace(val[0].get<std::string>(), weak_typed_id(val[1]));
}
};

// NOTE: I found an abnormal case that albumPicId is a number instead of string.
// So I deside to test every possible numeric type and try to convert them.

#define reflect_single(field, TYPE) \
[&](const json_t &j) { \
if (j.is_null()) { \
field.reset(); \
return; \
} \
try { \
update_v(field, j.get<TYPE>()); \
} catch (const json_t::type_error &) { \
try { \
if constexpr (std::is_same_v<TYPE, std::string>) { \
update_v(field, std::to_string(j.get<uint64_t>())); \
} else if constexpr (std::is_same_v<TYPE, uint64_t>) { \
\
update_v(field, std::stoull(j.get<std::string>())); \
} \
} catch (const json_t::type_error &) { \
} catch (const std::invalid_argument &) { \
} \
} \
#define reflect_single(field, TYPE) \
[&](const json_t &j) { \
if (j.is_null()) { \
field.reset(); \
return; \
} \
try { \
update_v(field, j.get<TYPE>()); \
} catch (const json_t::type_error &) { \
try { \
update_v(field, weak_typed_id(j)); \
} catch (const json_t::type_error &) { \
} \
} \
}
#define reflect_multi_string(field) \
[&](const json_t &j) { \
Expand Down
25 changes: 25 additions & 0 deletions src/meta_process.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,31 @@ namespace fb2k_ncm
}
} // namespace

namespace
{
class weak_typed_id {
uint64_t n_ = 0;

public:
explicit weak_typed_id(const nlohmann::json &json) {
if (json.is_number_integer()) {
n_ = json.get<uint64_t>();
} else if (json.is_string()) {
n_ = std::stoull(json.get<std::string>());
}
}
explicit weak_typed_id(const std::string &s) : n_(0) {
try {
n_ = std::stoull(s);
} catch (std::invalid_argument &e) {
}
}
explicit weak_typed_id(std::integral auto n) : n_(n) {}
operator uint64_t() const noexcept { return n_; }
operator std::string() const { return std::to_string(n_); }
};
} // namespace

class meta_processor : public uniform_meta_st {
public:
void update(const file_info &info); // FB2K
Expand Down
3 changes: 3 additions & 0 deletions test/sample/dup_extra_meta_mp3_win_ncm3.0.0beta-159431.ncm
Git LFS file not shown

0 comments on commit 51458bd

Please sign in to comment.