IT WORKS.
This commit is contained in:
@@ -6,11 +6,17 @@ import com.example.video_downloader.repositories.VideoRepository;
|
|||||||
import jakarta.annotation.PostConstruct;
|
import jakarta.annotation.PostConstruct;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import static java.nio.file.Path.*;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class ProcessService {
|
public class ProcessService {
|
||||||
|
|
||||||
@@ -18,27 +24,29 @@ public class ProcessService {
|
|||||||
private VideoRepository videoRepository;
|
private VideoRepository videoRepository;
|
||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void validateVideos() throws IOException, InterruptedException {
|
public void validateVideos() throws HttpClientErrorException, IOException, InterruptedException {
|
||||||
List<Video> response = videoRepository.findUrlByStatus(StatusEnum.NEW);
|
List<Video> response = videoRepository.findUrlByStatus(StatusEnum.NEW);
|
||||||
for(Video video : response) {
|
for(Video video : response) {
|
||||||
if(video.getStatus().equals(StatusEnum.NEW)){
|
if(video.getStatus().equals(StatusEnum.NEW)){
|
||||||
downloadVideo(video.getUrl());
|
downloadVideo(video.getUrl(), video.getId());
|
||||||
video.setStatus(StatusEnum.SAVED);
|
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);
|
videoRepository.save(video);
|
||||||
} else {
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//@PostConstruct
|
//@PostConstruct
|
||||||
public void downloadVideo(String url) throws InterruptedException, IOException {
|
public void downloadVideo(String url, Long id) throws HttpClientErrorException, InterruptedException, IOException {
|
||||||
try {
|
String outputPath = "downloads/" + id + ".%(ext)s";
|
||||||
execute("yt-dlp", "youtube:player_client=android", url);
|
execute("yt-dlp", "-o", outputPath, url);
|
||||||
} catch (Exception e) {
|
|
||||||
System.out.println("Something went awry.");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void execute(String... command) throws InterruptedException, IOException {
|
public void execute(String... command) throws InterruptedException, IOException {
|
||||||
@@ -46,7 +54,9 @@ public class ProcessService {
|
|||||||
builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
|
builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
|
||||||
builder.redirectError(ProcessBuilder.Redirect.INHERIT);
|
builder.redirectError(ProcessBuilder.Redirect.INHERIT);
|
||||||
Process process = builder.start();
|
Process process = builder.start();
|
||||||
process.waitFor();
|
int exitCode = process.waitFor();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user