playlist_service #2
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,6 +2,7 @@ HELP.md
|
||||
target/
|
||||
.mvn/wrapper/maven-wrapper.jar
|
||||
!**/src/main/**/target/
|
||||
downloads/
|
||||
!**/src/test/**/target/
|
||||
|
||||
### STS ###
|
||||
|
||||
@@ -65,7 +65,7 @@ public class VideoController {
|
||||
if(trigger.input) {
|
||||
ytdlpService.processPlaylist();
|
||||
ytdlpService.validateVideos();
|
||||
System.out.println("Hello!");
|
||||
ytdlpService.downloadVideos();
|
||||
}
|
||||
return statusCode;
|
||||
|
||||
|
||||
@@ -39,7 +39,4 @@ public class Video {
|
||||
@JoinColumn(name = "creator_id")
|
||||
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;
|
||||
|
||||
import com.example.video_downloader.entity.Creator;
|
||||
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.CreatorRepository;
|
||||
import com.example.video_downloader.repositories.PlaylistRepository;
|
||||
import com.example.video_downloader.repositories.VideoRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -10,8 +12,10 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.HttpClientErrorException;
|
||||
|
||||
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.Date;
|
||||
|
||||
@Service
|
||||
public class YTDLPService {
|
||||
@@ -28,6 +32,9 @@ public class YTDLPService {
|
||||
@Autowired
|
||||
private ProcessService processService;
|
||||
|
||||
@Autowired
|
||||
private CreatorRepository creatorRepository;
|
||||
|
||||
|
||||
public void processPlaylist() throws IOException, InterruptedException {
|
||||
List<Playlist> playlists = playlistRepository.findByStatus(StatusEnum.NEW);
|
||||
@@ -53,29 +60,51 @@ public class YTDLPService {
|
||||
String data = processVideo(video.getUrl());
|
||||
String[] parts = data.trim().split(", ");
|
||||
video.setName(parts[0]);
|
||||
video.setCreator(parts[1]);
|
||||
video.setUploadDate(parts[2]);
|
||||
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);
|
||||
}
|
||||
}
|
||||
// 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("Download failed.");
|
||||
// video.setStatus(StatusEnum.FAILED);
|
||||
// }
|
||||
|
||||
public void downloadVideos() throws IOException, InterruptedException {
|
||||
List<Video> response = videoRepository.findByStatus(StatusEnum.NEW);
|
||||
|
||||
for(Video video : response){
|
||||
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("Download failed.");
|
||||
video.setStatus(StatusEnum.FAILED);
|
||||
}
|
||||
videoRepository.save(video);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
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 result;
|
||||
return processService.execute("yt-dlp", "--force-ipv4", "--skip-download", "--print", "%(title)s, %(uploader)s, %(upload_date)s, %(duration_string)s, %(uploader_url)s", url);
|
||||
}
|
||||
public void downloadVideo(String url, Long id) throws HttpClientErrorException, InterruptedException, IOException {
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user