some refactors, ready to copy the files
This commit is contained in:
parent
7cc6596f01
commit
d8814d2e75
|
@ -0,0 +1,2 @@
|
|||
docker build -t xeviff/sonarr_queue_fix:latest .
|
||||
docker push xeviff/sonarr_queue_fix:latest
|
|
@ -14,33 +14,46 @@ import java.util.stream.Collectors;
|
|||
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 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) {
|
||||
this.queueRecord = queueRecord;
|
||||
this.serie = serie;
|
||||
}
|
||||
|
||||
static FailedImportFixer of(Record queueRecord, SonarrSerie serie) {
|
||||
return new FailedImportFixer(queueRecord, serie);
|
||||
this.torrentPath = Path.of(queueRecord.getOutputPath());
|
||||
this.seriePath = Path.of(serie.getPath());
|
||||
fileCopier = new FileCopier();
|
||||
}
|
||||
|
||||
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<Path> torrentFiles = getVideoFilesFrom(torrentPath);
|
||||
|
||||
var seasonFolderName = calculateSeasonPathFrom(torrentPath);
|
||||
var sonarPath = Path.of(serie.getPath());
|
||||
List<Path> sonarFiles = getVideoFilesFrom(sonarPath.resolve(seasonFolderName));
|
||||
|
||||
missingFilesDetector.printDifferencesBetween(torrentFiles, sonarFiles);
|
||||
List<Path> torrentFiles = resolveTorrentFiles();
|
||||
List<Path> sonarFiles = resolveSonarrFiles();
|
||||
List<Path> filesToCopy = missingFilesDetector.getMissingFilesAtDestination(torrentFiles, sonarFiles);
|
||||
filesToCopy.forEach(this::copy);
|
||||
}
|
||||
|
||||
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 {
|
||||
return StringCaptor.getSeasonFolderNameFromSeason(torrentPath.getFileName().toString());
|
||||
} 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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,8 +5,7 @@ import java.nio.file.Files;
|
|||
import java.nio.file.Path;
|
||||
|
||||
public class FileCopier {
|
||||
|
||||
private void hardLink(Path source, Path destination) {
|
||||
void hardLink(Path source, Path destination) {
|
||||
try {
|
||||
Files.createLink(destination, source);
|
||||
} catch (IOException e) {
|
||||
|
@ -16,6 +15,7 @@ public class FileCopier {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ProjectPath {
|
||||
static Path of(String path) {
|
||||
String projectPath = System.getProperty("user.dir");
|
||||
|
|
|
@ -7,20 +7,19 @@ import java.util.stream.Collectors;
|
|||
|
||||
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",
|
||||
torrentFiles.size(), sonarrFiles.size());
|
||||
Map<Long, List<Path>> torrentFileLengths = getFileLengthsMapFrom(torrentFiles);
|
||||
if (hasFilesWithSameSize(torrentFileLengths))
|
||||
return;
|
||||
Map<Long, List<Path>> sonarrFileLengths = getFileLengthsMapFrom(sonarrFiles);
|
||||
if (hasFilesWithSameSize(sonarrFileLengths))
|
||||
return;
|
||||
|
||||
torrentFileLengths.keySet().stream()
|
||||
.filter(fileSize -> missingAtDestination(fileSize, sonarrFileLengths))
|
||||
.collect(Collectors.toSet());
|
||||
return torrentFiles;
|
||||
sonarrFilesByLength = getFileLengthsMapFrom(sonarrFiles);
|
||||
if (hasFilesWithSameSize(sonarrFilesByLength))
|
||||
return torrentFiles;
|
||||
|
||||
return resolveMissingFiles(torrentFileLengths);
|
||||
}
|
||||
|
||||
Map<Long, List<Path>> getFileLengthsMapFrom(List<Path> files) {
|
||||
|
@ -38,14 +37,24 @@ public class MissingFilesDetector {
|
|||
boolean hasMultipleElements(List<Path> 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 %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());
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean missingAtDestination(Long fileSize, Map<Long, List<Path>> torrentFileLengths) {
|
||||
return !torrentFileLengths.containsKey(fileSize);
|
||||
private List<Path> resolveMissingFiles(Map<Long, List<Path>> torrentFilesByLength) {
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
|
@ -15,10 +15,11 @@ 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;
|
||||
private final FailedImportFixer.Factory failedImportFixerFactory;
|
||||
|
||||
QueueFixer() {
|
||||
sonarrApiGateway = Sonarr.api();
|
||||
failedImportFixerFactory = FailedImportFixer.factory();
|
||||
}
|
||||
|
||||
void fix() {
|
||||
|
@ -57,8 +58,9 @@ public class QueueFixer {
|
|||
SonarrSerie serie = getSerieFromSonarr(seriesId);
|
||||
if (serie == null) return;
|
||||
|
||||
failedImportFixer = FailedImportFixer.of(record, serie);
|
||||
failedImportFixer.fix();
|
||||
failedImportFixerFactory
|
||||
.newFixerFor(record, serie)
|
||||
.fix();
|
||||
} catch (IOException e) {
|
||||
System.out.printf("!! could not fix the import %s%n", record.getTitle());
|
||||
e.printStackTrace();
|
||||
|
|
Loading…
Reference in New Issue