playlist_service #2
@@ -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)){
|
||||
|
zoli
commented
Az adatbazisbol mar csak azokat a videokat akarjuk lekerdezni ahol a status new szoval itt folosleges megegyszer ellenorizni. Amennyiben olyan videokat is vissza ad amiben a status nem new, ott akkor a lekerdezesd kell javitani nem pedig ujra ellenorizni. Valszeg findByStatus(StatusEnum.NEW) az mukodik, a findUrlByStatus ugy hangzik mint ha a tablabol csak az URL oszlopot akarnad lekerdezni, de az egesz videot kered vissza. Szoval itt felre vezeto a methodus elnevezese es nem is biztos hogy azt csinalja amit kene. Az adatbazisbol mar csak azokat a videokat akarjuk lekerdezni ahol a status new szoval itt folosleges megegyszer ellenorizni.
Amennyiben olyan videokat is vissza ad amiben a status nem new, ott akkor a lekerdezesd kell javitani nem pedig ujra ellenorizni.
Valszeg findByStatus(StatusEnum.NEW) az mukodik, a findUrlByStatus ugy hangzik mint ha a tablabol csak az URL oszlopot akarnad lekerdezni, de az egesz videot kered vissza. Szoval itt felre vezeto a methodus elnevezese es nem is biztos hogy azt csinalja amit kene.
|
||||
downloadVideo(video.getUrl());
|
||||
video.setStatus(StatusEnum.SAVED);
|
||||
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);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//@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
Hozz letre egy YtDlpService-t a VideoService mintajara es oda keruljon at a validateVideos() methodus.
Es nevezd at downloadNewVideos-ra