1
0
Fork 0

some refactors, ready to copy the files

This commit is contained in:
Xavier Fontanet 2024-05-19 21:29:56 +02:00
parent 7cc6596f01
commit d8814d2e75
5 changed files with 66 additions and 35 deletions

2
push.sh Normal file
View File

@ -0,0 +1,2 @@
docker build -t xeviff/sonarr_queue_fix:latest .
docker push xeviff/sonarr_queue_fix:latest

View File

@ -14,33 +14,46 @@ import java.util.stream.Collectors;
public class FailedImportFixer { public class FailedImportFixer {
public static final int MINIMUM_FILE_SIZE_TO_BE_CONSIDERED_A_VIDEO = 300000; public static final int MINIMUM_FILE_SIZE_TO_BE_CONSIDERED_A_VIDEO = 300000;
private final Record queueRecord; private final Record queueRecord;
private final SonarrSerie serie;
private final MissingFilesDetector missingFilesDetector = new MissingFilesDetector(); private final MissingFilesDetector missingFilesDetector = new MissingFilesDetector();
private final Path torrentPath;
private final Path seriePath;
private final FileCopier fileCopier;
private Path seasonPath;
static Factory factory() {
return new Factory();
}
public static class Factory {
FailedImportFixer newFixerFor(Record queueRecord, SonarrSerie serie){
return new FailedImportFixer(queueRecord, serie);
}
}
private FailedImportFixer(Record queueRecord, SonarrSerie serie) { private FailedImportFixer(Record queueRecord, SonarrSerie serie) {
this.queueRecord = queueRecord; this.queueRecord = queueRecord;
this.serie = serie; this.torrentPath = Path.of(queueRecord.getOutputPath());
} this.seriePath = Path.of(serie.getPath());
fileCopier = new FileCopier();
static FailedImportFixer of(Record queueRecord, SonarrSerie serie) {
return new FailedImportFixer(queueRecord, serie);
} }
void fix() throws IOException { void fix() throws IOException {
System.out.printf("%nfixing: %s%n" ,queueRecord.getTitle()); System.out.printf("%nfixing: %s%n" ,queueRecord.getTitle());
System.out.printf(">> located in: %s%n", queueRecord.getOutputPath()); List<Path> torrentFiles = resolveTorrentFiles();
List<Path> sonarFiles = resolveSonarrFiles();
var torrentPath = Path.of(queueRecord.getOutputPath()); List<Path> filesToCopy = missingFilesDetector.getMissingFilesAtDestination(torrentFiles, sonarFiles);
List<Path> torrentFiles = getVideoFilesFrom(torrentPath); filesToCopy.forEach(this::copy);
var seasonFolderName = calculateSeasonPathFrom(torrentPath);
var sonarPath = Path.of(serie.getPath());
List<Path> sonarFiles = getVideoFilesFrom(sonarPath.resolve(seasonFolderName));
missingFilesDetector.printDifferencesBetween(torrentFiles, sonarFiles);
} }
private String calculateSeasonPathFrom(Path torrentPath) { private List<Path> resolveTorrentFiles() throws IOException {
return getVideoFilesFrom(Path.of(queueRecord.getOutputPath()));
}
private List<Path> resolveSonarrFiles() throws IOException {
seasonPath = seriePath.resolve(getSeasonFolder());
return getVideoFilesFrom(seasonPath);
}
private String getSeasonFolder() {
try { try {
return StringCaptor.getSeasonFolderNameFromSeason(torrentPath.getFileName().toString()); return StringCaptor.getSeasonFolderNameFromSeason(torrentPath.getFileName().toString());
} catch (IncorrectWorkingReferencesException e) { } catch (IncorrectWorkingReferencesException e) {
@ -58,4 +71,9 @@ public class FailedImportFixer {
} }
} }
private void copy(Path fileToCopy) {
Path target = seasonPath.resolve(fileToCopy.getFileName());
System.out.println("** going to hardlink file to "+target);
fileCopier.hardLink(fileToCopy, target);
}
} }

View File

@ -5,8 +5,7 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
public class FileCopier { public class FileCopier {
void hardLink(Path source, Path destination) {
private void hardLink(Path source, Path destination) {
try { try {
Files.createLink(destination, source); Files.createLink(destination, source);
} catch (IOException e) { } catch (IOException e) {
@ -16,9 +15,10 @@ public class FileCopier {
} }
} }
} }
class ProjectPath { class ProjectPath {
static Path of(String path) { static Path of(String path) {
String projectPath = System.getProperty("user.dir"); String projectPath = System.getProperty("user.dir");
return Path.of(projectPath+path); return Path.of(projectPath + path);
} }
} }

View File

@ -7,20 +7,19 @@ import java.util.stream.Collectors;
public class MissingFilesDetector { public class MissingFilesDetector {
void printDifferencesBetween(List<Path> torrentFiles, List<Path> sonarrFiles) { private Map<Long, List<Path>> sonarrFilesByLength;
List<Path> getMissingFilesAtDestination(List<Path> torrentFiles, List<Path> sonarrFiles) {
System.out.printf("going to compare %d torrent files with %d sonar files%n", System.out.printf("going to compare %d torrent files with %d sonar files%n",
torrentFiles.size(), sonarrFiles.size()); 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 torrentFiles;
Map<Long, List<Path>> sonarrFileLengths = getFileLengthsMapFrom(sonarrFiles); sonarrFilesByLength = getFileLengthsMapFrom(sonarrFiles);
if (hasFilesWithSameSize(sonarrFileLengths)) if (hasFilesWithSameSize(sonarrFilesByLength))
return; return torrentFiles;
torrentFileLengths.keySet().stream()
.filter(fileSize -> missingAtDestination(fileSize, sonarrFileLengths))
.collect(Collectors.toSet());
return resolveMissingFiles(torrentFileLengths);
} }
Map<Long, List<Path>> getFileLengthsMapFrom(List<Path> files) { Map<Long, List<Path>> getFileLengthsMapFrom(List<Path> files) {
@ -38,14 +37,24 @@ 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 %d. 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;
} }
boolean missingAtDestination(Long fileSize, Map<Long, List<Path>> torrentFileLengths) { private List<Path> resolveMissingFiles(Map<Long, List<Path>> torrentFilesByLength) {
return !torrentFileLengths.containsKey(fileSize); return torrentFilesByLength.entrySet()
.stream()
.filter(this::missingAtDestination)
.map(Map.Entry::getValue)
.flatMap(List::stream)
.toList();
} }
private boolean missingAtDestination(Map.Entry<Long, List<Path>> torrentFileEntry) {
return !sonarrFilesByLength.containsKey(torrentFileEntry.getKey());
}
} }

View File

@ -15,10 +15,11 @@ 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; private final FailedImportFixer.Factory failedImportFixerFactory;
QueueFixer() { QueueFixer() {
sonarrApiGateway = Sonarr.api(); sonarrApiGateway = Sonarr.api();
failedImportFixerFactory = FailedImportFixer.factory();
} }
void fix() { void fix() {
@ -57,8 +58,9 @@ public class QueueFixer {
SonarrSerie serie = getSerieFromSonarr(seriesId); SonarrSerie serie = getSerieFromSonarr(seriesId);
if (serie == null) return; if (serie == null) return;
failedImportFixer = FailedImportFixer.of(record, serie); failedImportFixerFactory
failedImportFixer.fix(); .newFixerFor(record, serie)
.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();