Print is finally working.
This commit is contained in:
@@ -29,10 +29,6 @@ public class VideoController {
|
||||
@Autowired
|
||||
private YTDLPService ytdlpService;
|
||||
|
||||
@Data
|
||||
private static class Response {
|
||||
private Long id;
|
||||
}
|
||||
|
||||
@Data
|
||||
private static class Trigger {
|
||||
@@ -40,14 +36,8 @@ public class VideoController {
|
||||
}
|
||||
|
||||
@PostMapping(path = "/videos")
|
||||
public Response saveNewVideo(@RequestBody SaveNewVideoRequest request){
|
||||
|
||||
Response response = new Response();
|
||||
Video video = videoService.saveNewVideo(request);
|
||||
response.setId(video.getId());
|
||||
|
||||
return response;
|
||||
|
||||
public Video saveNewVideo(@RequestBody SaveNewVideoRequest request){
|
||||
return videoService.saveNewVideo(request);
|
||||
}
|
||||
|
||||
@PostMapping(path = "/playlists")
|
||||
@@ -71,6 +61,7 @@ public class VideoController {
|
||||
public int getJobDone(@RequestBody Trigger trigger) throws IOException, InterruptedException {
|
||||
int statusCode = 200;
|
||||
if(trigger.input) {
|
||||
ytdlpService.processPlaylist();
|
||||
ytdlpService.validateVideos();
|
||||
}
|
||||
return statusCode;
|
||||
|
||||
@@ -17,9 +17,13 @@ public class Playlist {
|
||||
@GeneratedValue(strategy = GenerationType.SEQUENCE)
|
||||
private Long id;
|
||||
|
||||
@Column
|
||||
private String playlistName;
|
||||
|
||||
|
||||
@Column
|
||||
private String url;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
private StatusEnum status;
|
||||
|
||||
@ManyToMany
|
||||
|
||||
@@ -4,6 +4,9 @@ import jakarta.persistence.*;
|
||||
import lombok.Data;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@ToString
|
||||
@Entity
|
||||
@@ -22,9 +25,13 @@ public class Video {
|
||||
|
||||
private String fullPath;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "playlist_id")
|
||||
private Playlist playlist;
|
||||
@ManyToMany
|
||||
@JoinTable(
|
||||
name = "video_playlist",
|
||||
joinColumns = @JoinColumn(name = "video_id"),
|
||||
inverseJoinColumns = @JoinColumn(name = "playlist_id")
|
||||
)
|
||||
private List<Playlist> playlist = new ArrayList<>();
|
||||
|
||||
@OneToOne
|
||||
@JoinColumn(name = "creator_id")
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
package com.example.video_downloader.repositories;
|
||||
|
||||
import com.example.video_downloader.entity.Playlist;
|
||||
import com.example.video_downloader.entity.StatusEnum;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface PlaylistRepository extends JpaRepository<Playlist, Long> {
|
||||
List<Playlist> findByStatus(StatusEnum status);
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import com.example.video_downloader.repositories.PlaylistRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.security.InvalidParameterException;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@@ -16,14 +17,13 @@ public class PlaylistService {
|
||||
private PlaylistRepository playlistRepository;
|
||||
|
||||
public Playlist saveNewPlaylist(SaveNewPlaylistRequest request){
|
||||
if(!request.getUrl().contains("list")) throw new InvalidParameterException();
|
||||
Playlist playlist = new Playlist();
|
||||
|
||||
playlist.setStatus(StatusEnum.NEW);
|
||||
playlist.setPlaylistName(request.getPlaylistName());
|
||||
playlist.setUrl(request.getUrl());
|
||||
playlistRepository.save(playlist);
|
||||
return playlist;
|
||||
|
||||
}
|
||||
|
||||
public List<Playlist> getAllPlaylists(){
|
||||
|
||||
@@ -2,17 +2,23 @@ package com.example.video_downloader.services;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@Service
|
||||
public class ProcessService {
|
||||
|
||||
public void execute(String... command) throws InterruptedException, IOException {
|
||||
public String execute(String... command) throws InterruptedException, IOException {
|
||||
ProcessBuilder builder = new ProcessBuilder(command);
|
||||
builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
|
||||
builder.redirectError(ProcessBuilder.Redirect.INHERIT);
|
||||
// builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
|
||||
// builder.redirectError(ProcessBuilder.Redirect.INHERIT);
|
||||
Process process = builder.start();
|
||||
String res = new String(process.getInputStream().readAllBytes());
|
||||
|
||||
int exitCode = process.waitFor();
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -7,8 +7,8 @@ import com.example.video_downloader.repositories.VideoRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.security.InvalidParameterException;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class VideoService {
|
||||
@@ -16,7 +16,8 @@ public class VideoService {
|
||||
@Autowired
|
||||
private VideoRepository videoRepository;
|
||||
|
||||
public Video saveNewVideo(SaveNewVideoRequest request){
|
||||
public Video saveNewVideo(SaveNewVideoRequest request) {
|
||||
if (request.getUrl().contains("list")) throw new InvalidParameterException("");
|
||||
Video video = new Video();
|
||||
video.setStatus(StatusEnum.NEW);
|
||||
video.setUrl(request.getUrl());
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
package com.example.video_downloader.services;
|
||||
|
||||
import com.example.video_downloader.entity.Playlist;
|
||||
import com.example.video_downloader.entity.StatusEnum;
|
||||
import com.example.video_downloader.entity.Video;
|
||||
import com.example.video_downloader.repositories.PlaylistRepository;
|
||||
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;
|
||||
@@ -23,31 +24,54 @@ public class YTDLPService {
|
||||
@Autowired
|
||||
private PlaylistService playlistService;
|
||||
|
||||
@Autowired
|
||||
private PlaylistRepository playlistRepository;
|
||||
|
||||
@Autowired
|
||||
private ProcessService processService;
|
||||
|
||||
|
||||
public void processPlaylist() throws IOException, InterruptedException {
|
||||
List<Playlist> playlists = playlistRepository.findByStatus(StatusEnum.NEW);
|
||||
|
||||
for(Playlist playlist : playlists) {
|
||||
String urls = processService.execute("yt-dlp", "--flat-playlist", "--print", "webpage_url", playlist.getUrl());
|
||||
for(String url : urls.split("\\n")){
|
||||
Video video = new Video();
|
||||
video.setUrl(url);
|
||||
video.setStatus(StatusEnum.NEW);
|
||||
video.getPlaylist().add(playlist);
|
||||
videoRepository.save(video);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void validateVideos() throws HttpClientErrorException, IOException, InterruptedException {
|
||||
List<Video> response = videoRepository.findByStatus(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.setFullPath(String.valueOf(filePath));
|
||||
video.setStatus(StatusEnum.SAVED);
|
||||
} else {
|
||||
System.out.println("Nah it does not exist mate.");
|
||||
System.out.println("Download failed.");
|
||||
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);
|
||||
String result = processService.execute("yt-dlp", "--print", "%(title)s, %(uploader)s, %(upload_date)s, %(duration_string)s", outputPath, url);
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user