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