1
0
Fork 0

first bugfix

This commit is contained in:
Xavier Fontanet 2024-05-19 20:35:12 +02:00
parent 81c288725e
commit 7cc6596f01
3 changed files with 28 additions and 15 deletions

View File

@ -1,7 +1,9 @@
package tv.mangrana.worker; package tv.mangrana.worker;
import tv.mangrana.exception.IncorrectWorkingReferencesException;
import tv.mangrana.sonarr.api.schema.queue.Record; import tv.mangrana.sonarr.api.schema.queue.Record;
import tv.mangrana.sonarr.api.schema.series.SonarrSerie; import tv.mangrana.sonarr.api.schema.series.SonarrSerie;
import tv.mangrana.utils.StringCaptor;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
@ -29,15 +31,26 @@ public class FailedImportFixer {
System.out.printf(">> located in: %s%n", queueRecord.getOutputPath()); System.out.printf(">> located in: %s%n", queueRecord.getOutputPath());
var torrentPath = Path.of(queueRecord.getOutputPath()); var torrentPath = Path.of(queueRecord.getOutputPath());
var sonarPath = Path.of(serie.getPath());
List<Path> torrentFiles = getVideoFilesFrom(torrentPath); List<Path> torrentFiles = getVideoFilesFrom(torrentPath);
List<Path> sonarFiles = getVideoFilesFrom(sonarPath);
var seasonFolderName = calculateSeasonPathFrom(torrentPath);
var sonarPath = Path.of(serie.getPath());
List<Path> sonarFiles = getVideoFilesFrom(sonarPath.resolve(seasonFolderName));
missingFilesDetector.printDifferencesBetween(torrentFiles, sonarFiles); missingFilesDetector.printDifferencesBetween(torrentFiles, sonarFiles);
} }
private List<Path> getVideoFilesFrom(Path torrentPath) throws IOException { private String calculateSeasonPathFrom(Path torrentPath) {
try (var pathWalk = Files.walk(torrentPath, 3)) { try {
return StringCaptor.getSeasonFolderNameFromSeason(torrentPath.getFileName().toString());
} catch (IncorrectWorkingReferencesException e) {
throw new RuntimeException(e);
}
}
private List<Path> getVideoFilesFrom(Path path) throws IOException {
System.out.println("going to explore "+path);
try (var pathWalk = Files.walk(path, 3)) {
return pathWalk return pathWalk
.filter(p -> p.toFile().isFile()) .filter(p -> p.toFile().isFile())
.filter(p -> p.toFile().length() > MINIMUM_FILE_SIZE_TO_BE_CONSIDERED_A_VIDEO) .filter(p -> p.toFile().length() > MINIMUM_FILE_SIZE_TO_BE_CONSIDERED_A_VIDEO)

View File

@ -8,6 +8,8 @@ import java.util.stream.Collectors;
public class MissingFilesDetector { public class MissingFilesDetector {
void printDifferencesBetween(List<Path> torrentFiles, List<Path> sonarrFiles) { void printDifferencesBetween(List<Path> torrentFiles, List<Path> sonarrFiles) {
System.out.printf("going to compare %d torrent files with %d sonar files%n",
torrentFiles.size(), sonarrFiles.size());
Map<Long, List<Path>> torrentFileLengths = getFileLengthsMapFrom(torrentFiles); Map<Long, List<Path>> torrentFileLengths = getFileLengthsMapFrom(torrentFiles);
if (hasFilesWithSameSize(torrentFileLengths)) if (hasFilesWithSameSize(torrentFileLengths))
return; return;
@ -15,8 +17,9 @@ public class MissingFilesDetector {
if (hasFilesWithSameSize(sonarrFileLengths)) if (hasFilesWithSameSize(sonarrFileLengths))
return; return;
torrentFileLengths.keySet().forEach(fileSize -> torrentFileLengths.keySet().stream()
takeDecisionForMismatch(fileSize, torrentFileLengths)); .filter(fileSize -> missingAtDestination(fileSize, sonarrFileLengths))
.collect(Collectors.toSet());
} }
@ -35,17 +38,14 @@ public class MissingFilesDetector {
boolean hasMultipleElements(List<Path> paths) { boolean hasMultipleElements(List<Path> paths) {
if (paths.size() > 1) { if (paths.size() > 1) {
var sampleFile = paths.get(0).toFile(); 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()); sampleFile.length(), sampleFile.getName());
return true; return true;
} }
return false; return false;
} }
void takeDecisionForMismatch(Long fileSize, Map<Long, List<Path>> torrentFileLengths) { boolean missingAtDestination(Long fileSize, Map<Long, List<Path>> torrentFileLengths) {
var alreadyExists = torrentFileLengths.containsKey(fileSize); return !torrentFileLengths.containsKey(fileSize);
if (!alreadyExists) {
System.out.printf("- needs to copy %s%n", torrentFileLengths.get(fileSize));
}
} }
} }

View File

@ -15,6 +15,7 @@ import java.util.stream.Collectors;
public class QueueFixer { 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."; 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;
private FailedImportFixer failedImportFixer;
QueueFixer() { QueueFixer() {
sonarrApiGateway = Sonarr.api(); sonarrApiGateway = Sonarr.api();
@ -56,9 +57,8 @@ public class QueueFixer {
SonarrSerie serie = getSerieFromSonarr(seriesId); SonarrSerie serie = getSerieFromSonarr(seriesId);
if (serie == null) return; if (serie == null) return;
FailedImportFixer failedImportFixer = FailedImportFixer.of(record, serie);
.of(record, serie) failedImportFixer.fix();
.fix();
} catch (IOException e) { } catch (IOException e) {
System.out.printf("!! could not fix the import %s%n", record.getTitle()); System.out.printf("!! could not fix the import %s%n", record.getTitle());
e.printStackTrace(); e.printStackTrace();