From 7cc6596f014e8ea5167d1729c959ca677241f783 Mon Sep 17 00:00:00 2001 From: Xavier Fontanet Date: Sun, 19 May 2024 20:35:12 +0200 Subject: [PATCH] first bugfix --- .../tv/mangrana/worker/FailedImportFixer.java | 21 +++++++++++++++---- .../mangrana/worker/MissingFilesDetector.java | 16 +++++++------- .../java/tv/mangrana/worker/QueueFixer.java | 6 +++--- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/main/java/tv/mangrana/worker/FailedImportFixer.java b/src/main/java/tv/mangrana/worker/FailedImportFixer.java index 63c6a2d..e0fd9ee 100644 --- a/src/main/java/tv/mangrana/worker/FailedImportFixer.java +++ b/src/main/java/tv/mangrana/worker/FailedImportFixer.java @@ -1,7 +1,9 @@ package tv.mangrana.worker; +import tv.mangrana.exception.IncorrectWorkingReferencesException; import tv.mangrana.sonarr.api.schema.queue.Record; import tv.mangrana.sonarr.api.schema.series.SonarrSerie; +import tv.mangrana.utils.StringCaptor; import java.io.IOException; import java.nio.file.Files; @@ -29,15 +31,26 @@ public class FailedImportFixer { System.out.printf(">> located in: %s%n", queueRecord.getOutputPath()); var torrentPath = Path.of(queueRecord.getOutputPath()); - var sonarPath = Path.of(serie.getPath()); List torrentFiles = getVideoFilesFrom(torrentPath); - List sonarFiles = getVideoFilesFrom(sonarPath); + + var seasonFolderName = calculateSeasonPathFrom(torrentPath); + var sonarPath = Path.of(serie.getPath()); + List sonarFiles = getVideoFilesFrom(sonarPath.resolve(seasonFolderName)); missingFilesDetector.printDifferencesBetween(torrentFiles, sonarFiles); } - private List getVideoFilesFrom(Path torrentPath) throws IOException { - try (var pathWalk = Files.walk(torrentPath, 3)) { + private String calculateSeasonPathFrom(Path torrentPath) { + try { + return StringCaptor.getSeasonFolderNameFromSeason(torrentPath.getFileName().toString()); + } catch (IncorrectWorkingReferencesException e) { + throw new RuntimeException(e); + } + } + + private List getVideoFilesFrom(Path path) throws IOException { + System.out.println("going to explore "+path); + try (var pathWalk = Files.walk(path, 3)) { return pathWalk .filter(p -> p.toFile().isFile()) .filter(p -> p.toFile().length() > MINIMUM_FILE_SIZE_TO_BE_CONSIDERED_A_VIDEO) diff --git a/src/main/java/tv/mangrana/worker/MissingFilesDetector.java b/src/main/java/tv/mangrana/worker/MissingFilesDetector.java index 9ecab03..8848d22 100644 --- a/src/main/java/tv/mangrana/worker/MissingFilesDetector.java +++ b/src/main/java/tv/mangrana/worker/MissingFilesDetector.java @@ -8,6 +8,8 @@ import java.util.stream.Collectors; public class MissingFilesDetector { void printDifferencesBetween(List torrentFiles, List sonarrFiles) { + System.out.printf("going to compare %d torrent files with %d sonar files%n", + torrentFiles.size(), sonarrFiles.size()); Map> torrentFileLengths = getFileLengthsMapFrom(torrentFiles); if (hasFilesWithSameSize(torrentFileLengths)) return; @@ -15,8 +17,9 @@ public class MissingFilesDetector { if (hasFilesWithSameSize(sonarrFileLengths)) return; - torrentFileLengths.keySet().forEach(fileSize -> - takeDecisionForMismatch(fileSize, torrentFileLengths)); + torrentFileLengths.keySet().stream() + .filter(fileSize -> missingAtDestination(fileSize, sonarrFileLengths)) + .collect(Collectors.toSet()); } @@ -35,17 +38,14 @@ public class MissingFilesDetector { boolean hasMultipleElements(List paths) { if (paths.size() > 1) { var sampleFile = paths.get(0).toFile(); - System.out.printf("There is more than one file with the same size of %s. Name: %s %n", + System.out.printf("There is more than one file with the same size of %d. Name: %s %n", sampleFile.length(), sampleFile.getName()); return true; } return false; } - void takeDecisionForMismatch(Long fileSize, Map> torrentFileLengths) { - var alreadyExists = torrentFileLengths.containsKey(fileSize); - if (!alreadyExists) { - System.out.printf("- needs to copy %s%n", torrentFileLengths.get(fileSize)); - } + boolean missingAtDestination(Long fileSize, Map> torrentFileLengths) { + return !torrentFileLengths.containsKey(fileSize); } } \ No newline at end of file diff --git a/src/main/java/tv/mangrana/worker/QueueFixer.java b/src/main/java/tv/mangrana/worker/QueueFixer.java index a64cc96..6eef005 100644 --- a/src/main/java/tv/mangrana/worker/QueueFixer.java +++ b/src/main/java/tv/mangrana/worker/QueueFixer.java @@ -15,6 +15,7 @@ import java.util.stream.Collectors; public class QueueFixer { 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 FailedImportFixer failedImportFixer; QueueFixer() { sonarrApiGateway = Sonarr.api(); @@ -56,9 +57,8 @@ public class QueueFixer { SonarrSerie serie = getSerieFromSonarr(seriesId); if (serie == null) return; - FailedImportFixer - .of(record, serie) - .fix(); + failedImportFixer = FailedImportFixer.of(record, serie); + failedImportFixer.fix(); } catch (IOException e) { System.out.printf("!! could not fix the import %s%n", record.getTitle()); e.printStackTrace();