From 712c876c71c973f156c01b29f68e4667ed49f5f4 Mon Sep 17 00:00:00 2001 From: Robert Vokac Date: Fri, 27 Dec 2024 17:27:03 +0100 Subject: [PATCH] Formatting code --- .../utils/youtubedlfrontend/Main.java | 165 +++++++++--------- .../utils/youtubedlfrontend/Utils.java | 1 + .../utils/youtubedlfrontend/YoutubeVideo.java | 4 +- .../youtubedlfrontend/YoutubeVideoHtml.java | 62 +++---- 4 files changed, 119 insertions(+), 113 deletions(-) diff --git a/src/main/java/com/openeggbert/utils/youtubedlfrontend/Main.java b/src/main/java/com/openeggbert/utils/youtubedlfrontend/Main.java index 88f6246..0677ed2 100755 --- a/src/main/java/com/openeggbert/utils/youtubedlfrontend/Main.java +++ b/src/main/java/com/openeggbert/utils/youtubedlfrontend/Main.java @@ -84,21 +84,24 @@ public class Main { channelsDirectory.mkdir(); } channels.stream().forEach(c -> { - StringBuilder oneChannelStringBuilder = createChannelHtml(c, channels, argsInstance, channelUrls, youtubeVideos, archiveBoxRootDirectory, videosDirectory, archiveBoxArchiveDirectory); - Utils.writeTextToFile(oneChannelStringBuilder.toString(), new File(channelsDirectory, channelUrls.get(c).split("/channel/")[1] + ".html")); + StringBuilder oneChannelStringBuilder = createChannelHtml(c, channels, argsInstance, channelUrls, youtubeVideos, archiveBoxRootDirectory, videosDirectory, archiveBoxArchiveDirectory); + Utils.writeTextToFile(oneChannelStringBuilder.toString(), new File(channelsDirectory, channelUrls.get(c).split("/channel/")[1] + ".html")); }); StringBuilder oneChannelStringBuilder = createChannelHtml(null, channels, argsInstance, channelUrls, youtubeVideos, archiveBoxRootDirectory, videosDirectory, archiveBoxArchiveDirectory); Utils.writeTextToFile(oneChannelStringBuilder.toString(), videosHtmlFile); - 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"); + System.out.println("Total duration: " + ((int) ((((double) YoutubeVideo.totalDurationInMilliseconds) / 1000d / 60d / 60d))) + " hours"); 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());}); + .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.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());}); + .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) { @@ -131,87 +134,87 @@ public class Main { channels.stream().filter(c -> wantedChannelName == null ? true : c.equals(wantedChannelName)).forEach(channel -> { oneChannelStringBuilder.append("

").append(channel).append("

\n"); oneChannelStringBuilder.append("
"); - + oneChannelStringBuilder.append("").append("Videos").append(""); oneChannelStringBuilder.append("   ( ").append(channelUrls.get(channel)).append(" )"); - - if(wantedChannelName != null) { - oneChannelStringBuilder.append("
"); - iii = 0; - internalStaticVariableVideoNumberPerRow = 0; - oneChannelStringBuilder.append("\n"); - long countOfVideosInChannel = youtubeVideos.stream().filter(v -> channel.equals(v.getChannelName())).count(); - youtubeVideos.stream().filter(v -> channel.equals(v.getChannelName())).forEach(youtubeVideo -> { - iii++; - if (internalStaticVariableVideoNumberPerRow == 0) { - oneChannelStringBuilder.append(""); - } - internalStaticVariableVideoNumberPerRow++; - oneChannelStringBuilder.append("
\n\n"); + if (internalStaticVariableVideoNumberPerRow == argsInstance.getInteger(ArgType.VIDEOS_PER_ROW).get()) { + oneChannelStringBuilder.append(""); + internalStaticVariableVideoNumberPerRow = 0; + } + File videoHtmlFile = new File(videosDirectory, youtubeVideo.getId() + ".html"); + if (!videoHtmlFile.exists() || argsInstance.getBoolean(ArgType.ALWAYS_GENERATE_HTML_FILES).get()) { + + { + String singleVideo = new YoutubeVideoHtml(youtubeVideo, archiveBoxRootDirectory, archiveBoxArchiveDirectory, countOfVideosInChannel).toString(); + Utils.writeTextToFile(singleVideo, videoHtmlFile); + processedVideos++; + System.out.println("Processed " + processedVideos + " from " + archiveBoxArchiveDirectory.list().length); + } + } + }); + if (internalStaticVariableVideoNumberPerRow < argsInstance.getInteger(ArgType.VIDEOS_PER_ROW).get()) { oneChannelStringBuilder.append(""); - internalStaticVariableVideoNumberPerRow = 0; } - File videoHtmlFile = new File(videosDirectory, youtubeVideo.getId() + ".html"); - if (!videoHtmlFile.exists() || argsInstance.getBoolean(ArgType.ALWAYS_GENERATE_HTML_FILES).get()) { - - { - String singleVideo = new YoutubeVideoHtml(youtubeVideo, archiveBoxRootDirectory, archiveBoxArchiveDirectory, countOfVideosInChannel).toString(); - Utils.writeTextToFile(singleVideo, videoHtmlFile); - processedVideos++; - System.out.println("Processed " + processedVideos + " from " + archiveBoxArchiveDirectory.list().length); - } - } - }); - if (internalStaticVariableVideoNumberPerRow < argsInstance.getInteger(ArgType.VIDEOS_PER_ROW).get()) { - oneChannelStringBuilder.append(""); - } - oneChannelStringBuilder.append("
"); + iii = 0; + internalStaticVariableVideoNumberPerRow = 0; + oneChannelStringBuilder.append("\n"); + long countOfVideosInChannel = youtubeVideos.stream().filter(v -> channel.equals(v.getChannelName())).count(); + youtubeVideos.stream().filter(v -> channel.equals(v.getChannelName())).forEach(youtubeVideo -> { + iii++; + if (internalStaticVariableVideoNumberPerRow == 0) { + oneChannelStringBuilder.append(""); + } + internalStaticVariableVideoNumberPerRow++; + oneChannelStringBuilder.append("\n"); - if (internalStaticVariableVideoNumberPerRow == argsInstance.getInteger(ArgType.VIDEOS_PER_ROW).get()) { + oneChannelStringBuilder.append("\" width=\"") + .append(THUMBNAIL_WIDTH) + .append("\">\n"); + oneChannelStringBuilder.append("\n"); + String uploadDate = youtubeVideo.getUploadDate(); + uploadDate = uploadDate.substring(0, 4) + "-" + uploadDate.substring(4, 6) + "-" + uploadDate.substring(6, 8); + oneChannelStringBuilder.append("\n"); + youtubeVideo.setNumber(iii); + oneChannelStringBuilder.append("
\n\n"); - oneChannelStringBuilder.append("\n"); - String uploadDate = youtubeVideo.getUploadDate(); - uploadDate = uploadDate.substring(0, 4) + "-" + uploadDate.substring(4, 6) + "-" + uploadDate.substring(6, 8); - oneChannelStringBuilder.append("\n"); - youtubeVideo.setNumber(iii); - oneChannelStringBuilder.append("
").append(youtubeVideo.getTitle()).append("
").append(uploadDate).append(" •︎ ").append(youtubeVideo.getVideoDuration()) - .append(" •︎ ") - .append("#").append(iii) - .append("
").append(youtubeVideo.getTitle()).append("
").append(uploadDate).append(" •︎ ").append(youtubeVideo.getVideoDuration()) + .append(" •︎ ") + .append("#").append(iii) + .append("
\n"); - oneChannelStringBuilder.append("
"); + oneChannelStringBuilder.append("
\n"); + oneChannelStringBuilder.append("
"); } oneChannelStringBuilder.append("
"); }); @@ -222,6 +225,6 @@ public class Main { return oneChannelStringBuilder; } - private static int processedVideos = 0; + private static int processedVideos = 0; } diff --git a/src/main/java/com/openeggbert/utils/youtubedlfrontend/Utils.java b/src/main/java/com/openeggbert/utils/youtubedlfrontend/Utils.java index c407c95..6105d84 100644 --- a/src/main/java/com/openeggbert/utils/youtubedlfrontend/Utils.java +++ b/src/main/java/com/openeggbert/utils/youtubedlfrontend/Utils.java @@ -51,6 +51,7 @@ public class Utils { public static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); public static final NumberFormat TWO_DECIMAL_POINTS_FORMATTER = new DecimalFormat("#0.00"); + private Utils() { //Not meant to be instantiated. } diff --git a/src/main/java/com/openeggbert/utils/youtubedlfrontend/YoutubeVideo.java b/src/main/java/com/openeggbert/utils/youtubedlfrontend/YoutubeVideo.java index 07d21e6..8caa430 100644 --- a/src/main/java/com/openeggbert/utils/youtubedlfrontend/YoutubeVideo.java +++ b/src/main/java/com/openeggbert/utils/youtubedlfrontend/YoutubeVideo.java @@ -328,6 +328,7 @@ public class YoutubeVideo implements Comparable { } } public static long totalDurationInMilliseconds = 0l; + public static List loadYoutubeVideos(File archiveBoxArchiveDirectory, Args argsInstance) throws IOException, InterruptedException { int i = 0; List youtubeVideos = new ArrayList<>(); @@ -394,10 +395,11 @@ public class YoutubeVideo implements Comparable { } return youtubeVideos; } + public long getVideoDurationInMilliseconds() { String duration = videoDuration; String[] array = duration.split(":"); - long ms = Long.valueOf(array[0]) * 60l *60l * 1000l; + long ms = Long.valueOf(array[0]) * 60l * 60l * 1000l; ms = ms + Long.valueOf(array[1]) * 60l * 1000l; String[] array2 = array[2].split("\\."); ms = ms + Long.valueOf(array2[0]) * 1000l; diff --git a/src/main/java/com/openeggbert/utils/youtubedlfrontend/YoutubeVideoHtml.java b/src/main/java/com/openeggbert/utils/youtubedlfrontend/YoutubeVideoHtml.java index 698e96e..b99c0c6 100644 --- a/src/main/java/com/openeggbert/utils/youtubedlfrontend/YoutubeVideoHtml.java +++ b/src/main/java/com/openeggbert/utils/youtubedlfrontend/YoutubeVideoHtml.java @@ -36,7 +36,7 @@ public class YoutubeVideoHtml { private final String singleVideo; public YoutubeVideoHtml(YoutubeVideo youtubeVideo, File archiveBoxRootDirectory, File archiveBoxArchiveDirectory, long countOfVideosInChannel) { - + StringBuilder videoHtml = new StringBuilder(""" @@ -65,14 +65,14 @@ public class YoutubeVideoHtml { String videoLocalFileEncoded = null; String videoLocalUrl = ""; try { - videoLocalFileEncoded = URLEncoder.encode(youtubeVideo.getVideoFileName(), StandardCharsets.UTF_8.displayName()).replace("+", "%20").replace("#", "%23"); + videoLocalFileEncoded = URLEncoder.encode(youtubeVideo.getVideoFileName(), StandardCharsets.UTF_8.displayName()).replace("+", "%20").replace("#", "%23"); videoLocalUrl = "file:///" + archiveBoxRootDirectory.getAbsolutePath() + "/archive/" + youtubeVideo.getSnapshot() + "/media/" + videoLocalFileEncoded; } catch (UnsupportedEncodingException ex) { throw new YoutubedlFrontendException(ex.getMessage()); } if (!youtubeVideo.getVideoFileName().endsWith(".mkv")) { videoHtml.append("