From 66c89e1c7288659a813080ba78404f477ad3e8a4 Mon Sep 17 00:00:00 2001 From: Robert Vokac Date: Fri, 27 Dec 2024 17:26:22 +0100 Subject: [PATCH] Cleanup --- .../utils/youtubedlfrontend/ArgType.java | 4 +-- .../utils/youtubedlfrontend/Main.java | 28 ++++++------------- .../youtubedlfrontend/YoutubeComment.java | 7 ----- .../utils/youtubedlfrontend/YoutubeVideo.java | 24 ++++------------ .../youtubedlfrontend/YoutubeVideoHtml.java | 25 +++++------------ 5 files changed, 23 insertions(+), 65 deletions(-) diff --git a/src/main/java/com/openeggbert/utils/youtubedlfrontend/ArgType.java b/src/main/java/com/openeggbert/utils/youtubedlfrontend/ArgType.java index 64fc91c..d026f1f 100644 --- a/src/main/java/com/openeggbert/utils/youtubedlfrontend/ArgType.java +++ b/src/main/java/com/openeggbert/utils/youtubedlfrontend/ArgType.java @@ -34,9 +34,9 @@ public enum ArgType { THUMBNAIL_AS_BASE64("thumbnail-as-base64", "false"), THUMBNAIL_LINKS_TO_YOUTUBE("thumbnail-links-to-youtube", "false"); @Getter - private String name; + private final String name; @Getter - private String defaultValue; + private final String defaultValue; ArgType(String name, String defaultValue) { this.name = name; diff --git a/src/main/java/com/openeggbert/utils/youtubedlfrontend/Main.java b/src/main/java/com/openeggbert/utils/youtubedlfrontend/Main.java index 13f1dc2..88f6246 100755 --- a/src/main/java/com/openeggbert/utils/youtubedlfrontend/Main.java +++ b/src/main/java/com/openeggbert/utils/youtubedlfrontend/Main.java @@ -64,12 +64,12 @@ public class Main { List youtubeVideos = YoutubeVideo.loadYoutubeVideos(archiveBoxArchiveDirectory, argsInstance); Map channelUrls = new HashMap<>(); List channels = new ArrayList<>(); - youtubeVideos.stream().forEach(c -> { - final String channelName_ = c.getChannelName(); + youtubeVideos.stream().forEach(v -> { + final String channelName = v.getChannelName(); - if (channelName_ != null && !channelUrls.containsKey(c.getChannelName())) { - channelUrls.put(channelName_, c.getChannelUrl()); - channels.add(channelName_); + if (channelName != null && !channelUrls.containsKey(v.getChannelName())) { + channelUrls.put(channelName, v.getChannelUrl()); + channels.add(channelName); } }); Collections.sort(channels, (String o1, String o2) -> o1.toLowerCase().compareTo(o2.toLowerCase())); @@ -94,21 +94,11 @@ public class Main { System.out.println("[Warning] Snapshots without videos:"); YoutubeVideo.missingYoutubeVideos.forEach(s -> System.out.println(s)); System.out.println("Total duration: " + ((int)((((double)YoutubeVideo.totalDurationInMilliseconds) / 1000d / 60d / 60d))) + " hours"); - youtubeVideos.sort(new Comparator() { - @Override - public int compare(YoutubeVideo o1, YoutubeVideo o2) { - return Long.valueOf(o1.getVideoDurationInMilliseconds()).compareTo(o2.getVideoDurationInMilliseconds()); - } - }); - youtubeVideos.forEach(y-> {System.out.println(y.getVideoDurationInMinutes() + " = minutes \t" + "https://youtube.com/watch?v=" + y.getId() + "\t" + y.getTitle());}); + youtubeVideos.stream().sorted((YoutubeVideo o1, YoutubeVideo o2) -> Long.valueOf(o1.getVideoDurationInMilliseconds()).compareTo(o2.getVideoDurationInMilliseconds())) + .forEach(y-> {System.out.println(y.getVideoDurationInMinutes() + " = minutes \t" + "https://youtube.com/watch?v=" + y.getId() + "\t" + y.getTitle());}); System.out.println("\n\n\n\n"); - youtubeVideos.sort(new Comparator() { - @Override - public int compare(YoutubeVideo o1, YoutubeVideo o2) { - return Long.valueOf(o1.getVideoFileSizeInBytes()).compareTo(o2.getVideoFileSizeInBytes()); - } - }); - youtubeVideos.forEach(y-> {System.out.println(y.getVideoFileSizeInMegaBytes()+ " MB \t" + "https://youtube.com/watch?v=" + y.getId() + "\t" + y.getTitle());}); + youtubeVideos.stream().sorted((YoutubeVideo o1, YoutubeVideo o2) -> Long.valueOf(o1.getVideoFileSizeInBytes()).compareTo(o2.getVideoFileSizeInBytes())) + .forEach(y-> {System.out.println(y.getVideoFileSizeInMegaBytes()+ " MB \t" + "https://youtube.com/watch?v=" + y.getId() + "\t" + y.getTitle());}); } private static StringBuilder createChannelHtml(String wantedChannelName, List channels, Args argsInstance, Map channelUrls, List youtubeVideos, File archiveBoxRootDirectory, File videosDirectory, File archiveBoxArchiveDirectory) { diff --git a/src/main/java/com/openeggbert/utils/youtubedlfrontend/YoutubeComment.java b/src/main/java/com/openeggbert/utils/youtubedlfrontend/YoutubeComment.java index 65352d4..da82dca 100644 --- a/src/main/java/com/openeggbert/utils/youtubedlfrontend/YoutubeComment.java +++ b/src/main/java/com/openeggbert/utils/youtubedlfrontend/YoutubeComment.java @@ -50,7 +50,6 @@ public class YoutubeComment implements Comparable { List root = getChildren(list, "root"); Collections.sort(root); return list; - //return sort(list, new ArrayList<>(), "root"); } private static List getChildren(List all, String parentId) { @@ -65,13 +64,7 @@ public class YoutubeComment implements Comparable { @Override public int compareTo(YoutubeComment o) { - //if(this.timestamp != o.timestamp) { - // return this.id.compareTo(o.id); return Long.valueOf(this.timestamp).compareTo(o.timestamp); -//} -// else { -// return this.id.compareTo(o.id); -// } } public int dotCount() { diff --git a/src/main/java/com/openeggbert/utils/youtubedlfrontend/YoutubeVideo.java b/src/main/java/com/openeggbert/utils/youtubedlfrontend/YoutubeVideo.java index 6f4aa5a..07d21e6 100644 --- a/src/main/java/com/openeggbert/utils/youtubedlfrontend/YoutubeVideo.java +++ b/src/main/java/com/openeggbert/utils/youtubedlfrontend/YoutubeVideo.java @@ -81,9 +81,6 @@ public class YoutubeVideo implements Comparable { File metadataFile = new File(mediaDirectory, "metadata"); if (!argAlwaysGenerateMetadata && metadataFile.exists()) { - YoutubeVideo yv = new YoutubeVideo(); - //new ObjectMapper().readValue(Utils.readTextFromFile(metadataFile), YoutubeVideo.class); - Properties properties = new Properties(); var input = new FileInputStream(metadataFile); properties.load(new InputStreamReader(input, Charset.forName("UTF-8"))); @@ -96,7 +93,7 @@ public class YoutubeVideo implements Comparable { snapshot = properties.getProperty("snapshot"); title = properties.getProperty("title"); videoFileName = properties.getProperty("videoFileName"); - videoFileSizeInBytes = Long.valueOf(properties.getProperty("videoFileSizeInBytes")); + videoFileSizeInBytes = Long.parseLong(properties.getProperty("videoFileSizeInBytes")); videoFileSha512HashSum = properties.getProperty("videoFileSha512HashSum"); videoDuration = properties.getProperty("videoDuration"); channelName = properties.getProperty("channelName"); @@ -110,7 +107,6 @@ public class YoutubeVideo implements Comparable { comments = new ArrayList<>(); JSONArray ja = new JSONArray(properties.getProperty("comments")); ja.forEach(o -> { - JSONObject jo = (JSONObject) o; try { final String toString = o.toString(); System.out.println(toString); @@ -124,7 +120,7 @@ public class YoutubeVideo implements Comparable { previousVideoId = properties.getProperty("previousVideoId"); nextVideoId = properties.getProperty("nextVideoId"); ext = properties.getProperty("ext"); - number = Integer.valueOf(properties.getProperty("number")); + number = Integer.parseInt(properties.getProperty("number")); return; } List files = Arrays.asList(mediaDirectory.listFiles()); @@ -132,9 +128,6 @@ public class YoutubeVideo implements Comparable { String json = jsonFile.isPresent() ? Utils.readTextFromFile(jsonFile.get()) : ""; JSONObject jsonObject = new JSONObject(json); id = jsonObject.getString("id"); -// if(!Main.argVideo.isBlank() && !id.equals(Main.argVideo)) { -// return; -// } thumbnail = jsonObject.getString("thumbnail"); if (thumbnail == null) { @@ -143,9 +136,7 @@ public class YoutubeVideo implements Comparable { JSONArray thumbnails = jsonObject.getJSONArray("thumbnails"); for (int i = 0; i < thumbnails.length(); i++) { JSONObject o = (JSONObject) thumbnails.get(i); - if (!o.has("width")) { - continue; - } else { + if (o.has("width")) { int width = o.getInt("width"); if (width < (((double) Main.THUMBNAIL_WIDTH) * 0.8d)) { continue; @@ -159,10 +150,6 @@ public class YoutubeVideo implements Comparable { File thumbnailFile = new File(mediaDirectory, "thumbnail." + getThumbnailFormat()); File miniThumbnailFile = new File(mediaDirectory, "mini-thumbnail." + getMiniThumbnailFormat()); -// new File(mediaDirectory, "thumbnail.jpg").delete(); -// new File(mediaDirectory, "mini-thumbnail.jpg").delete(); -// new File(mediaDirectory, "thumbnail.webp").delete(); -// new File(mediaDirectory, "mini-thumbnail.webp").delete(); if (thumbnail != null) { if (!thumbnailFile.exists()) { try (BufferedInputStream in = new BufferedInputStream(new URL(thumbnail).openStream()); FileOutputStream fileOutputStream = new FileOutputStream(thumbnailFile.getAbsolutePath())) { @@ -200,7 +187,6 @@ public class YoutubeVideo implements Comparable { || (f.getName().endsWith(".mp4")) || (f.getName().endsWith(".mkv")) || (f.getName().endsWith(".webm")) ) - .findFirst(); snapshot = mediaDirectory.getParentFile().getName(); @@ -353,10 +339,10 @@ public class YoutubeVideo implements Comparable { continue; } YoutubeVideo youtubeVideo = new YoutubeVideo(mediaDirectory, argsInstance.getBoolean(ArgType.ALWAYS_GENERATE_METADATA).get(), argsInstance.getString(ArgType.VIDEO).orElse("")); - if (argsInstance.getString(ArgType.VIDEO).isPresent() && !argsInstance.getString(ArgType.VIDEO).equals(youtubeVideo.getId())) { + if (argsInstance.getString(ArgType.VIDEO).isPresent() && !argsInstance.getString(ArgType.VIDEO).get().equals(youtubeVideo.getId())) { continue; } - if (argsInstance.getString(ArgType.CHANNEL).isPresent() && !argsInstance.getString(ArgType.CHANNEL).equals(youtubeVideo.getChannelId())) { + if (argsInstance.getString(ArgType.CHANNEL).isPresent() && !argsInstance.getString(ArgType.CHANNEL).get().equals(youtubeVideo.getChannelId())) { continue; } diff --git a/src/main/java/com/openeggbert/utils/youtubedlfrontend/YoutubeVideoHtml.java b/src/main/java/com/openeggbert/utils/youtubedlfrontend/YoutubeVideoHtml.java index d643039..698e96e 100644 --- a/src/main/java/com/openeggbert/utils/youtubedlfrontend/YoutubeVideoHtml.java +++ b/src/main/java/com/openeggbert/utils/youtubedlfrontend/YoutubeVideoHtml.java @@ -21,8 +21,6 @@ package com.openeggbert.utils.youtubedlfrontend; import java.io.File; import java.io.UnsupportedEncodingException; -import java.net.URI; -import java.net.URISyntaxException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.Date; @@ -35,7 +33,7 @@ import lombok.Getter; public class YoutubeVideoHtml { @Getter - private String singleVideo; + private final String singleVideo; public YoutubeVideoHtml(YoutubeVideo youtubeVideo, File archiveBoxRootDirectory, File archiveBoxArchiveDirectory, long countOfVideosInChannel) { @@ -61,7 +59,7 @@ public class YoutubeVideoHtml { """ ); String finalUrl = "https://www.youtube.com/watch?v=" + youtubeVideo.getId(); - videoHtml.append("
\n
\n"); + videoHtml.append("
\n
\n"); videoHtml.append(""); videoHtml.append(finalUrl).append("").append("
\n"); String videoLocalFileEncoded = null; @@ -73,12 +71,10 @@ public class YoutubeVideoHtml { throw new YoutubedlFrontendException(ex.getMessage()); } if (!youtubeVideo.getVideoFileName().endsWith(".mkv")) { - //try { videoHtml.append("