Decoupling ProcessService from Video Validation and Validation Execution
This commit is contained in:
@@ -1,54 +1,12 @@
|
|||||||
package com.example.video_downloader.services;
|
package com.example.video_downloader.services;
|
||||||
|
|
||||||
import com.example.video_downloader.entity.StatusEnum;
|
|
||||||
import com.example.video_downloader.entity.Video;
|
|
||||||
import com.example.video_downloader.repositories.VideoRepository;
|
|
||||||
import jakarta.annotation.PostConstruct;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.client.HttpClientErrorException;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static java.nio.file.Path.*;
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class ProcessService {
|
public class ProcessService {
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private VideoRepository videoRepository;
|
|
||||||
|
|
||||||
@PostConstruct
|
|
||||||
public void validateVideos() throws HttpClientErrorException, IOException, InterruptedException {
|
|
||||||
List<Video> response = videoRepository.findUrlByStatus(StatusEnum.NEW);
|
|
||||||
for(Video video : response) {
|
|
||||||
if(video.getStatus().equals(StatusEnum.NEW)){
|
|
||||||
downloadVideo(video.getUrl(), video.getId());
|
|
||||||
Path filePath = Paths.get("downloads/" + video.getId() + ".webm");
|
|
||||||
|
|
||||||
if(Files.exists(filePath) && Files.isRegularFile(filePath)) {
|
|
||||||
System.out.println("Apparently it exists mate.");
|
|
||||||
video.setStatus(StatusEnum.SAVED);
|
|
||||||
} else {
|
|
||||||
System.out.println("Nah it does not exist mate.");
|
|
||||||
video.setStatus(StatusEnum.FAILED);
|
|
||||||
}
|
|
||||||
videoRepository.save(video);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//@PostConstruct
|
|
||||||
public void downloadVideo(String url, Long id) throws HttpClientErrorException, InterruptedException, IOException {
|
|
||||||
String outputPath = "downloads/" + id + ".%(ext)s";
|
|
||||||
execute("yt-dlp", "-o", outputPath, url);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void execute(String... command) throws InterruptedException, IOException {
|
public void execute(String... command) throws InterruptedException, IOException {
|
||||||
ProcessBuilder builder = new ProcessBuilder(command);
|
ProcessBuilder builder = new ProcessBuilder(command);
|
||||||
builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
|
builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
|
||||||
|
|||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package com.example.video_downloader.services;
|
||||||
|
|
||||||
|
import com.example.video_downloader.entity.StatusEnum;
|
||||||
|
import com.example.video_downloader.entity.Video;
|
||||||
|
import com.example.video_downloader.repositories.VideoRepository;
|
||||||
|
import jakarta.annotation.PostConstruct;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.client.HttpClientErrorException;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class YtdlpService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private VideoRepository videoRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ProcessService processService;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void validateVideos() throws HttpClientErrorException, IOException, InterruptedException {
|
||||||
|
List<Video> response = videoRepository.findUrlByStatus(StatusEnum.NEW);
|
||||||
|
for(Video video : response) {
|
||||||
|
if(video.getStatus().equals(StatusEnum.NEW)){
|
||||||
|
downloadVideo(video.getUrl(), video.getId());
|
||||||
|
Path filePath = Paths.get("downloads/" + video.getId() + ".webm");
|
||||||
|
|
||||||
|
if(Files.exists(filePath) && Files.isRegularFile(filePath)) {
|
||||||
|
System.out.println("Apparently it exists mate.");
|
||||||
|
video.setStatus(StatusEnum.SAVED);
|
||||||
|
} else {
|
||||||
|
System.out.println("Nah it does not exist mate.");
|
||||||
|
video.setStatus(StatusEnum.FAILED);
|
||||||
|
}
|
||||||
|
videoRepository.save(video);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void downloadVideo(String url, Long id) throws HttpClientErrorException, InterruptedException, IOException {
|
||||||
|
String outputPath = "downloads/" + id + ".%(ext)s";
|
||||||
|
processService.execute("yt-dlp", "-o", outputPath, url);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user