Separate function for downloading and file validation. This is now a rudimentary working version.
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,6 +2,7 @@ HELP.md
|
|||||||
target/
|
target/
|
||||||
.mvn/wrapper/maven-wrapper.jar
|
.mvn/wrapper/maven-wrapper.jar
|
||||||
!**/src/main/**/target/
|
!**/src/main/**/target/
|
||||||
|
downloads/
|
||||||
!**/src/test/**/target/
|
!**/src/test/**/target/
|
||||||
|
|
||||||
### STS ###
|
### STS ###
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ public class VideoController {
|
|||||||
if(trigger.input) {
|
if(trigger.input) {
|
||||||
ytdlpService.processPlaylist();
|
ytdlpService.processPlaylist();
|
||||||
ytdlpService.validateVideos();
|
ytdlpService.validateVideos();
|
||||||
System.out.println("Hello!");
|
ytdlpService.downloadVideos();
|
||||||
}
|
}
|
||||||
return statusCode;
|
return statusCode;
|
||||||
|
|
||||||
|
|||||||
@@ -39,7 +39,4 @@ public class Video {
|
|||||||
@JoinColumn(name = "creator_id")
|
@JoinColumn(name = "creator_id")
|
||||||
private Creator creator;
|
private Creator creator;
|
||||||
|
|
||||||
|
|
||||||
public void setCreator(String part) {
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.example.video_downloader.repositories;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import com.example.video_downloader.entity.Creator;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public interface CreatorRepository extends JpaRepository<Creator, Long> {
|
||||||
|
Optional<Creator> findByName(String name);
|
||||||
|
}
|
||||||
@@ -1,8 +1,10 @@
|
|||||||
package com.example.video_downloader.services;
|
package com.example.video_downloader.services;
|
||||||
|
|
||||||
|
import com.example.video_downloader.entity.Creator;
|
||||||
import com.example.video_downloader.entity.Playlist;
|
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.CreatorRepository;
|
||||||
import com.example.video_downloader.repositories.PlaylistRepository;
|
import com.example.video_downloader.repositories.PlaylistRepository;
|
||||||
import com.example.video_downloader.repositories.VideoRepository;
|
import com.example.video_downloader.repositories.VideoRepository;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -10,8 +12,10 @@ import org.springframework.stereotype.Service;
|
|||||||
import org.springframework.web.client.HttpClientErrorException;
|
import org.springframework.web.client.HttpClientErrorException;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class YTDLPService {
|
public class YTDLPService {
|
||||||
@@ -28,6 +32,9 @@ public class YTDLPService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private ProcessService processService;
|
private ProcessService processService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private CreatorRepository creatorRepository;
|
||||||
|
|
||||||
|
|
||||||
public void processPlaylist() throws IOException, InterruptedException {
|
public void processPlaylist() throws IOException, InterruptedException {
|
||||||
List<Playlist> playlists = playlistRepository.findByStatus(StatusEnum.NEW);
|
List<Playlist> playlists = playlistRepository.findByStatus(StatusEnum.NEW);
|
||||||
@@ -53,29 +60,51 @@ public class YTDLPService {
|
|||||||
String data = processVideo(video.getUrl());
|
String data = processVideo(video.getUrl());
|
||||||
String[] parts = data.trim().split(", ");
|
String[] parts = data.trim().split(", ");
|
||||||
video.setName(parts[0]);
|
video.setName(parts[0]);
|
||||||
video.setCreator(parts[1]);
|
|
||||||
video.setUploadDate(parts[2]);
|
video.setUploadDate(parts[2]);
|
||||||
video.setDuration(parts[3]);
|
video.setDuration(parts[3]);
|
||||||
|
|
||||||
|
String creatorName = parts[1];
|
||||||
|
String creatorUrl = parts[4];
|
||||||
|
|
||||||
|
Creator creator = creatorRepository
|
||||||
|
.findByName(creatorName)
|
||||||
|
.orElseGet(() -> {
|
||||||
|
Creator newCreator = new Creator();
|
||||||
|
newCreator.setName(creatorName);
|
||||||
|
newCreator.setUrl(creatorUrl);
|
||||||
|
return creatorRepository.save(newCreator);
|
||||||
|
});
|
||||||
|
video.setCreator(creator);
|
||||||
videoRepository.save(video);
|
videoRepository.save(video);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Path filePath = Paths.get("downloads/" + video.getId() + ".webm");
|
|
||||||
// if(Files.exists(filePath) && Files.isRegularFile(filePath)) {
|
public void downloadVideos() throws IOException, InterruptedException {
|
||||||
// System.out.println("Apparently it exists mate.");
|
List<Video> response = videoRepository.findByStatus(StatusEnum.NEW);
|
||||||
// video.setFullPath(String.valueOf(filePath));
|
|
||||||
// video.setStatus(StatusEnum.SAVED);
|
for(Video video : response){
|
||||||
// } else {
|
downloadVideo(video.getUrl(), video.getId());
|
||||||
// System.out.println("Download failed.");
|
Path filePath = Paths.get("downloads/" + video.getId() + ".webm");
|
||||||
// video.setStatus(StatusEnum.FAILED);
|
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("Download failed.");
|
||||||
|
video.setStatus(StatusEnum.FAILED);
|
||||||
|
}
|
||||||
|
videoRepository.save(video);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public String processVideo(String url) throws HttpClientErrorException, InterruptedException, IOException {
|
public String processVideo(String url) throws HttpClientErrorException, InterruptedException, IOException {
|
||||||
String result = processService.execute("yt-dlp", "--force-ipv4", "--skip-download", "--print", "%(title)s, %(uploader)s, %(upload_date)s, %(duration_string)s", url);
|
return processService.execute("yt-dlp", "--force-ipv4", "--skip-download", "--print", "%(title)s, %(uploader)s, %(upload_date)s, %(duration_string)s, %(uploader_url)s", url);
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
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";
|
||||||
String result = processService.execute("yt-dlp", "--force-ipv4", "--print", "%(title)s, %(uploader)s, %(upload_date)s, %(duration_string)s", outputPath, url);
|
String result = processService.execute("yt-dlp", "--force-ipv4", "-o", outputPath, url);
|
||||||
System.out.println(result);
|
System.out.println(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user