IT WORKS.
This commit is contained in:
@@ -6,11 +6,17 @@ 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.web.client.HttpClientErrorException;
|
||||
|
||||
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
|
||||
public class ProcessService {
|
||||
|
||||
@@ -18,27 +24,29 @@ public class ProcessService {
|
||||
private VideoRepository videoRepository;
|
||||
|
||||
@PostConstruct
|
||||
public void validateVideos() throws IOException, InterruptedException {
|
||||
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());
|
||||
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);
|
||||
videoRepository.save(video);
|
||||
} else {
|
||||
return;
|
||||
System.out.println("Nah it does not exist mate.");
|
||||
video.setStatus(StatusEnum.FAILED);
|
||||
}
|
||||
videoRepository.save(video);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//@PostConstruct
|
||||
public void downloadVideo(String url) throws InterruptedException, IOException {
|
||||
try {
|
||||
execute("yt-dlp", "youtube:player_client=android", url);
|
||||
} catch (Exception e) {
|
||||
System.out.println("Something went awry.");
|
||||
}
|
||||
|
||||
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 {
|
||||
@@ -46,7 +54,9 @@ public class ProcessService {
|
||||
builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
|
||||
builder.redirectError(ProcessBuilder.Redirect.INHERIT);
|
||||
Process process = builder.start();
|
||||
process.waitFor();
|
||||
int exitCode = process.waitFor();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user