From d59940c02856758bf02cfbad9a243fb89d6fa317 Mon Sep 17 00:00:00 2001 From: Xavier Fontanet Date: Sun, 19 May 2024 15:29:32 +0200 Subject: [PATCH] start looking for files --- .../tv/mangrana/worker/FailedImportFixer.java | 38 +++++++++++++++---- .../java/tv/mangrana/worker/QueueFixer.java | 21 ++++++---- 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/src/main/java/tv/mangrana/worker/FailedImportFixer.java b/src/main/java/tv/mangrana/worker/FailedImportFixer.java index 335b409..dba9bd6 100644 --- a/src/main/java/tv/mangrana/worker/FailedImportFixer.java +++ b/src/main/java/tv/mangrana/worker/FailedImportFixer.java @@ -3,19 +3,43 @@ package tv.mangrana.worker; import tv.mangrana.sonarr.api.schema.queue.Record; import tv.mangrana.sonarr.api.schema.series.SonarrSerie; -public class FailedImportFixer { - private final Record queueRecord; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import java.util.stream.Collectors; - private FailedImportFixer(Record queueRecord) { +public class FailedImportFixer { + public static final int MINIMUM_FILE_SIZE_TO_BE_CONSIDERED_A_VIDEO = 300000; + private final Record queueRecord; + private final SonarrSerie serie; + + private FailedImportFixer(Record queueRecord, SonarrSerie serie) { this.queueRecord = queueRecord; + this.serie = serie; } static FailedImportFixer of(Record queueRecord, SonarrSerie serie) { - return new FailedImportFixer(queueRecord); + return new FailedImportFixer(queueRecord, serie); } - void fix() { - System.out.printf("fixing: %s%n" ,queueRecord.getTitle()); - System.out.printf(">> located in: %s%n%n", queueRecord.getOutputPath()); + void fix() throws IOException { + System.out.printf("%nfixing: %s%n" ,queueRecord.getTitle()); + System.out.printf(">> located in: %s%n", queueRecord.getOutputPath()); + + var torrentPath = Path.of(queueRecord.getOutputPath()); + List torrentFiles = getVideoFilesFrom(torrentPath); + + var sonarPath = Path.of(serie.getPath()); + List sonarFiles = getVideoFilesFrom(sonarPath); + } + + private List getVideoFilesFrom(Path torrentPath) throws IOException { + try (var pathWalk = Files.walk(torrentPath, 3)) { + return pathWalk + .filter(p -> p.toFile().isFile()) + .filter(p -> p.toFile().length() > MINIMUM_FILE_SIZE_TO_BE_CONSIDERED_A_VIDEO) + .collect(Collectors.toList()); + } } } diff --git a/src/main/java/tv/mangrana/worker/QueueFixer.java b/src/main/java/tv/mangrana/worker/QueueFixer.java index f36fc57..da0498f 100644 --- a/src/main/java/tv/mangrana/worker/QueueFixer.java +++ b/src/main/java/tv/mangrana/worker/QueueFixer.java @@ -5,6 +5,7 @@ import tv.mangrana.sonarr.api.client.gateway.SonarrApiGateway; import tv.mangrana.sonarr.api.schema.queue.Record; import tv.mangrana.sonarr.api.schema.series.SonarrSerie; +import java.io.IOException; import java.util.Collection; import java.util.HashMap; import java.util.List; @@ -23,7 +24,7 @@ public class QueueFixer { List sonarQueue = retrieveQueueRecordsFromSonarr(); Collection records = deduplicate(sonarQueue); List recordsToFix = filterFailedImportsOfIdProblem(records); - recordsToFix.forEach(this::fixFailedImport); + recordsToFix.forEach(this::try2FixFailedImport); } private List retrieveQueueRecordsFromSonarr() { @@ -49,12 +50,18 @@ public class QueueFixer { .anyMatch(IMPORT_FAILURE_BECAUSE_MATCHED_BY_ID::equals); } - private void fixFailedImport(Record record) { - var seriesId = record.getSeriesId(); - SonarrSerie serie = getSerieFromSonarr(seriesId); - FailedImportFixer - .of(record, serie) - .fix(); + private void try2FixFailedImport(Record record) { + try { + var seriesId = record.getSeriesId(); + SonarrSerie serie = getSerieFromSonarr(seriesId); + if (serie == null) return; + + FailedImportFixer + .of(record, serie) + .fix(); + } catch (IOException e) { + System.out.printf("!! could not fix the import %s%n", record.getTitle()); + } } private SonarrSerie getSerieFromSonarr(Integer seriesId) {