1
0
Fork 0

code review and refactor

This commit is contained in:
Xavier Fontanet 2024-05-19 14:54:51 +02:00
parent dc73030822
commit 22a0f8bef6
3 changed files with 37 additions and 30 deletions

View File

@ -3,20 +3,18 @@ package tv.mangrana.worker;
import tv.mangrana.sonarr.api.schema.queue.Record;
public class FailedImportFixer {
private final String elementTitle;
private final Record queueRecord;
private FailedImportFixer(String elementTitle, Record queueRecord) {
this.elementTitle = elementTitle;
private FailedImportFixer(Record queueRecord) {
this.queueRecord = queueRecord;
}
static FailedImportFixer of(String elementTitle, Record queueRecord) {
return new FailedImportFixer(elementTitle, queueRecord);
static FailedImportFixer of(Record queueRecord) {
return new FailedImportFixer(queueRecord);
}
void fix() {
System.out.printf("fixing: %s%n",elementTitle);
System.out.printf("fixing: %s%n" ,queueRecord.getTitle());
System.out.printf(">> located in: %s%n%n", queueRecord.getOutputPath());
}
}

View File

@ -7,6 +7,11 @@ public class MainWorker {
private final QueueFixer queueFixer;
public static void main(String[] args) throws IncorrectWorkingReferencesException {
var worker = new MainWorker();
worker.work();
}
public MainWorker() throws IncorrectWorkingReferencesException {
ConfigFileLoader configFileLoader = new ConfigFileLoader();
queueFixer = new QueueFixer(configFileLoader);
@ -15,9 +20,4 @@ public class MainWorker {
private void work() {
queueFixer.fix();
}
public static void main(String[] args) throws IncorrectWorkingReferencesException {
var worker = new MainWorker();
worker.work();
}
}

View File

@ -4,13 +4,14 @@ import tv.mangrana.config.ConfigFileLoader;
import tv.mangrana.sonarr.api.client.gateway.SonarrApiGateway;
import tv.mangrana.sonarr.api.schema.queue.Record;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class QueueFixer {
final static String ID_IMPORT_FAILURE = "Found matching series via grab history, but release was matched to series by ID. Automatic import is not possible. See the FAQ for details.";
final static String IMPORT_FAILURE_BECAUSE_MATCHED_BY_ID = "Found matching series via grab history, but release was matched to series by ID. Automatic import is not possible. See the FAQ for details.";
private final SonarrApiGateway sonarrApiGateway;
QueueFixer(ConfigFileLoader configFileLoader) {
@ -18,30 +19,38 @@ public class QueueFixer {
}
void fix() {
var queue = sonarrApiGateway.getFullQueue();
List<Record> recordsWithImportFailure = queue.getRecords().stream()
.filter(this::recordsWithImportFailure)
.collect(Collectors.toList());
fixFailedImports(recordsWithImportFailure);
List<Record> sonarQueue = retrieveQueueRecordsFromSonarr();
Collection<Record> records = deduplicate(sonarQueue);
List<Record> recordsToFix = filterFailedImportsOfIdProblem(records);
recordsToFix.forEach(this::fixFailedImport);
}
private boolean recordsWithImportFailure(Record record) {
private List<Record> retrieveQueueRecordsFromSonarr() {
return sonarrApiGateway.getFullQueue().getRecords();
}
private Collection<Record> deduplicate(List<Record> repetitiveRecords) {
Map<String, Record> recordsByTitle = new HashMap<>();
repetitiveRecords.forEach(record ->
recordsByTitle.putIfAbsent(record.getTitle(), record));
return recordsByTitle.values();
}
private List<Record> filterFailedImportsOfIdProblem(Collection<Record> records) {
return records.stream()
.filter(this::recordsWithImportFailureBecauseIdMatching)
.collect(Collectors.toList());
}
private boolean recordsWithImportFailureBecauseIdMatching(Record record) {
return record.getStatusMessages().stream()
.flatMap(status -> status.getMessages().stream())
.anyMatch(ID_IMPORT_FAILURE::equals);
.anyMatch(IMPORT_FAILURE_BECAUSE_MATCHED_BY_ID::equals);
}
private void fixFailedImports(List<Record> recordsWithImportFailure) {
Map<String, Record> recordsByTitle = new HashMap<>();
recordsWithImportFailure.forEach(record ->
recordsByTitle.putIfAbsent(record.getTitle(), record));
recordsByTitle.entrySet().forEach(this::fixFailedImport);
}
private void fixFailedImport(Map.Entry<String, Record> recordEntry) {
private void fixFailedImport(Record record) {
FailedImportFixer
.of(recordEntry.getKey(), recordEntry.getValue())
.of(record)
.fix();
}