code review and refactor
This commit is contained in:
parent
dc73030822
commit
22a0f8bef6
|
@ -3,20 +3,18 @@ package tv.mangrana.worker;
|
||||||
import tv.mangrana.sonarr.api.schema.queue.Record;
|
import tv.mangrana.sonarr.api.schema.queue.Record;
|
||||||
|
|
||||||
public class FailedImportFixer {
|
public class FailedImportFixer {
|
||||||
private final String elementTitle;
|
|
||||||
private final Record queueRecord;
|
private final Record queueRecord;
|
||||||
|
|
||||||
private FailedImportFixer(String elementTitle, Record queueRecord) {
|
private FailedImportFixer(Record queueRecord) {
|
||||||
this.elementTitle = elementTitle;
|
|
||||||
this.queueRecord = queueRecord;
|
this.queueRecord = queueRecord;
|
||||||
}
|
}
|
||||||
|
|
||||||
static FailedImportFixer of(String elementTitle, Record queueRecord) {
|
static FailedImportFixer of(Record queueRecord) {
|
||||||
return new FailedImportFixer(elementTitle, queueRecord);
|
return new FailedImportFixer(queueRecord);
|
||||||
}
|
}
|
||||||
|
|
||||||
void fix() {
|
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());
|
System.out.printf(">> located in: %s%n%n", queueRecord.getOutputPath());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,11 @@ public class MainWorker {
|
||||||
|
|
||||||
private final QueueFixer queueFixer;
|
private final QueueFixer queueFixer;
|
||||||
|
|
||||||
|
public static void main(String[] args) throws IncorrectWorkingReferencesException {
|
||||||
|
var worker = new MainWorker();
|
||||||
|
worker.work();
|
||||||
|
}
|
||||||
|
|
||||||
public MainWorker() throws IncorrectWorkingReferencesException {
|
public MainWorker() throws IncorrectWorkingReferencesException {
|
||||||
ConfigFileLoader configFileLoader = new ConfigFileLoader();
|
ConfigFileLoader configFileLoader = new ConfigFileLoader();
|
||||||
queueFixer = new QueueFixer(configFileLoader);
|
queueFixer = new QueueFixer(configFileLoader);
|
||||||
|
@ -15,9 +20,4 @@ public class MainWorker {
|
||||||
private void work() {
|
private void work() {
|
||||||
queueFixer.fix();
|
queueFixer.fix();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) throws IncorrectWorkingReferencesException {
|
|
||||||
var worker = new MainWorker();
|
|
||||||
worker.work();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,13 +4,14 @@ import tv.mangrana.config.ConfigFileLoader;
|
||||||
import tv.mangrana.sonarr.api.client.gateway.SonarrApiGateway;
|
import tv.mangrana.sonarr.api.client.gateway.SonarrApiGateway;
|
||||||
import tv.mangrana.sonarr.api.schema.queue.Record;
|
import tv.mangrana.sonarr.api.schema.queue.Record;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class QueueFixer {
|
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;
|
private final SonarrApiGateway sonarrApiGateway;
|
||||||
|
|
||||||
QueueFixer(ConfigFileLoader configFileLoader) {
|
QueueFixer(ConfigFileLoader configFileLoader) {
|
||||||
|
@ -18,30 +19,38 @@ public class QueueFixer {
|
||||||
}
|
}
|
||||||
|
|
||||||
void fix() {
|
void fix() {
|
||||||
var queue = sonarrApiGateway.getFullQueue();
|
List<Record> sonarQueue = retrieveQueueRecordsFromSonarr();
|
||||||
List<Record> recordsWithImportFailure = queue.getRecords().stream()
|
Collection<Record> records = deduplicate(sonarQueue);
|
||||||
.filter(this::recordsWithImportFailure)
|
List<Record> recordsToFix = filterFailedImportsOfIdProblem(records);
|
||||||
.collect(Collectors.toList());
|
recordsToFix.forEach(this::fixFailedImport);
|
||||||
fixFailedImports(recordsWithImportFailure);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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()
|
return record.getStatusMessages().stream()
|
||||||
.flatMap(status -> status.getMessages().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) {
|
private void fixFailedImport(Record record) {
|
||||||
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) {
|
|
||||||
FailedImportFixer
|
FailedImportFixer
|
||||||
.of(recordEntry.getKey(), recordEntry.getValue())
|
.of(record)
|
||||||
.fix();
|
.fix();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue