|
| 1 | +/* |
| 2 | + * This file is part of nzbget. See <https://nzbget.com>. |
| 3 | + * |
| 4 | + * Copyright (C) 2026 Denis <denis@nzbget.com> |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation; either version 2 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | + |
| 21 | +#include "nzbget.h" |
| 22 | +#include "CollectionAnalyzer.h" |
| 23 | +#include "FileTypes.h" |
| 24 | + |
| 25 | +namespace PostDownloadRenamer |
| 26 | +{ |
| 27 | + |
| 28 | +std::vector<CollectionAnalyzer::FileGroup> |
| 29 | +CollectionAnalyzer::BuildGroups(const std::vector<Candidate>& candidates) |
| 30 | +{ |
| 31 | + std::vector<FileGroup> groups; |
| 32 | + for (const Candidate& candidate : candidates) |
| 33 | + { |
| 34 | + if (FileTypes::IsSubtitleExt(candidate.ext) || FileTypes::IsSampleStem(candidate.stem)) continue; |
| 35 | + |
| 36 | + FileGroup* targetGroup = nullptr; |
| 37 | + for (FileGroup& group : groups) |
| 38 | + { |
| 39 | + if (group.parentDir == candidate.parentDir && group.extKey == candidate.extLower) |
| 40 | + { |
| 41 | + targetGroup = &group; |
| 42 | + break; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + if (!targetGroup) |
| 47 | + { |
| 48 | + groups.push_back({candidate.parentDir, candidate.extLower, 0, 0, 0, false}); |
| 49 | + targetGroup = &groups.back(); |
| 50 | + } |
| 51 | + |
| 52 | + targetGroup->count++; |
| 53 | + uintmax_t size = candidate.size; |
| 54 | + if (size > targetGroup->largest) |
| 55 | + { |
| 56 | + targetGroup->second = targetGroup->largest; |
| 57 | + targetGroup->largest = size; |
| 58 | + } |
| 59 | + else if (size > targetGroup->second) |
| 60 | + { |
| 61 | + targetGroup->second = size; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + for (FileGroup& group : groups) |
| 66 | + { |
| 67 | + bool isAmbiguousCollection = (group.count >= 2 && group.largest <= group.second * 3); |
| 68 | + bool isAudio = FileTypes::IsAudioExt(group.extKey); |
| 69 | + group.skip = isAmbiguousCollection && !isAudio; |
| 70 | + } |
| 71 | + |
| 72 | + return groups; |
| 73 | +} |
| 74 | + |
| 75 | +CollectionAnalyzer::CollectionAnalyzer(const std::vector<Candidate>& candidates) |
| 76 | + : m_groups(BuildGroups(candidates)) |
| 77 | +{ |
| 78 | +} |
| 79 | + |
| 80 | +bool CollectionAnalyzer::ShouldSkip(const Candidate& candidate) const |
| 81 | +{ |
| 82 | + bool isSub = FileTypes::IsSubtitleExt(candidate.ext); |
| 83 | + bool isSample = FileTypes::IsSampleStem(candidate.stem); |
| 84 | + |
| 85 | + if (isSub || isSample) |
| 86 | + { |
| 87 | + bool hasNonAudioGroup = false; |
| 88 | + bool allNonAudioSkipped = true; |
| 89 | + |
| 90 | + for (const FileGroup& group : m_groups) |
| 91 | + { |
| 92 | + if (group.parentDir == candidate.parentDir && !FileTypes::IsAudioExt(group.extKey)) |
| 93 | + { |
| 94 | + hasNonAudioGroup = true; |
| 95 | + if (!group.skip) |
| 96 | + { |
| 97 | + allNonAudioSkipped = false; |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return hasNonAudioGroup && allNonAudioSkipped; |
| 103 | + } |
| 104 | + |
| 105 | + for (const FileGroup& group : m_groups) |
| 106 | + { |
| 107 | + if (group.parentDir == candidate.parentDir && group.extKey == candidate.extLower) |
| 108 | + { |
| 109 | + return group.skip; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return false; |
| 114 | +} |
| 115 | + |
| 116 | +} |
0 commit comments