1
0
mirror of https://github.com/openeggbert/youtubedl-frontend.git synced 2025-03-14 21:23:27 +01:00

Formatting code

This commit is contained in:
Robert Vokac 2024-12-27 17:27:03 +01:00
parent 66c89e1c72
commit 712c876c71
Signed by: robertvokac
GPG Key ID: FB9CE8E20AADA55F
4 changed files with 119 additions and 113 deletions

View File

@ -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<String> channels, Args argsInstance, Map<String, String> channelUrls, List<YoutubeVideo> 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("<h1>").append(channel).append("</h1>\n");
oneChannelStringBuilder.append("<div style=\"max-width:").append((Main.THUMBNAIL_WIDTH + 20) * argsInstance.getInteger(ArgType.VIDEOS_PER_ROW).get()).append("px\">");
oneChannelStringBuilder.append("<a target=\"_blank\" href =\"channels/").append(channelUrls.get(channel).split("/channel/")[1]).append(".html").append("\">").append("Videos").append("</a>");
oneChannelStringBuilder.append("&nbsp;&nbsp;&nbsp;( <a href =\"").append(channelUrls.get(channel)).append("\">").append(channelUrls.get(channel)).append("</a> )");
if(wantedChannelName != null) {
oneChannelStringBuilder.append("<div class=\"videos\">");
iii = 0;
internalStaticVariableVideoNumberPerRow = 0;
oneChannelStringBuilder.append("<table>\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("<tr>");
}
internalStaticVariableVideoNumberPerRow++;
oneChannelStringBuilder.append("<td><div class=\"box\"><table style=\"margin:5px;max-width:")
.append(THUMBNAIL_WIDTH)
.append("px;\">\n<tr><td><a href=\"");
if (argsInstance.getBoolean(ArgType.THUMBNAIL_LINKS_TO_YOUTUBE).get()) {
oneChannelStringBuilder.append("https://www.youtube.com/watch?v=").append(youtubeVideo.getId());
} else {
oneChannelStringBuilder.append("../videos/" + youtubeVideo.getId() + ".html");
}
oneChannelStringBuilder.append("\" target=\"_blank\"><img src=\"");
String thumbnailPath = new StringBuilder()
.append("archive/")
.append(youtubeVideo.getSnapshot())
.append("/media/mini-thumbnail.")
.append(youtubeVideo.getMiniThumbnailFormat()).toString();
if (argsInstance.getBoolean(ArgType.THUMBNAIL_AS_BASE64).get()) {
try {
byte[] bytes = Files.readAllBytes(new File(archiveBoxRootDirectory + "/" + thumbnailPath).toPath());
System.out.println("###=" + archiveBoxRootDirectory + "/" + thumbnailPath);
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
if (wantedChannelName != null) {
oneChannelStringBuilder.append("<div class=\"videos\">");
iii = 0;
internalStaticVariableVideoNumberPerRow = 0;
oneChannelStringBuilder.append("<table>\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("<tr>");
}
internalStaticVariableVideoNumberPerRow++;
oneChannelStringBuilder.append("<td><div class=\"box\"><table style=\"margin:5px;max-width:")
.append(THUMBNAIL_WIDTH)
.append("px;\">\n<tr><td><a href=\"");
if (argsInstance.getBoolean(ArgType.THUMBNAIL_LINKS_TO_YOUTUBE).get()) {
oneChannelStringBuilder.append("https://www.youtube.com/watch?v=").append(youtubeVideo.getId());
} else {
oneChannelStringBuilder.append("../videos/" + youtubeVideo.getId() + ".html");
}
oneChannelStringBuilder.append("\" target=\"_blank\"><img src=\"");
String thumbnailPath = new StringBuilder()
.append("archive/")
.append(youtubeVideo.getSnapshot())
.append("/media/mini-thumbnail.")
.append(youtubeVideo.getMiniThumbnailFormat()).toString();
if (argsInstance.getBoolean(ArgType.THUMBNAIL_AS_BASE64).get()) {
try {
bytes = Utils.resizeImage(bais, 25, (int) (9d / 16d * 25d), youtubeVideo.getThumbnailFormat());
} catch (Exception e) {
//bytes = Utils.resizeImage(bais, 125, (int) (9d / 16d * 125d), "webp");
byte[] bytes = Files.readAllBytes(new File(archiveBoxRootDirectory + "/" + thumbnailPath).toPath());
System.out.println("###=" + archiveBoxRootDirectory + "/" + thumbnailPath);
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
try {
bytes = Utils.resizeImage(bais, 25, (int) (9d / 16d * 25d), youtubeVideo.getThumbnailFormat());
} catch (Exception e) {
//bytes = Utils.resizeImage(bais, 125, (int) (9d / 16d * 125d), "webp");
}
String bytesS = "data:image/jpg;base64, " + com.robertvokac.powerframework.io.bit.base64.Base64Coder.encode(bytes);
oneChannelStringBuilder.append(bytesS);
} catch (IOException ex) {
throw new YoutubedlFrontendException(ex.getMessage());
}
String bytesS = "data:image/jpg;base64, " + com.robertvokac.powerframework.io.bit.base64.Base64Coder.encode(bytes);
oneChannelStringBuilder.append(bytesS);
} catch (IOException ex) {
throw new YoutubedlFrontendException(ex.getMessage());
} else {
oneChannelStringBuilder.append("../" + thumbnailPath);
}
} else {
oneChannelStringBuilder.append("../" + thumbnailPath);
}
oneChannelStringBuilder.append("\" width=\"")
.append(THUMBNAIL_WIDTH)
.append("\"></a></td></tr>\n");
oneChannelStringBuilder.append("<tr><td><b style=\"font-size:90%;\">").append(youtubeVideo.getTitle()).append("</b></td></tr>\n");
String uploadDate = youtubeVideo.getUploadDate();
uploadDate = uploadDate.substring(0, 4) + "-" + uploadDate.substring(4, 6) + "-" + uploadDate.substring(6, 8);
oneChannelStringBuilder.append("<tr><td style=\"font-size:80%;color:grey;\">").append(uploadDate).append(" •︎ ").append(youtubeVideo.getVideoDuration())
.append(" •︎ ")
.append("#").append(iii)
.append("</td></tr>\n");
youtubeVideo.setNumber(iii);
oneChannelStringBuilder.append("</table></div></td>\n");
if (internalStaticVariableVideoNumberPerRow == argsInstance.getInteger(ArgType.VIDEOS_PER_ROW).get()) {
oneChannelStringBuilder.append("\" width=\"")
.append(THUMBNAIL_WIDTH)
.append("\"></a></td></tr>\n");
oneChannelStringBuilder.append("<tr><td><b style=\"font-size:90%;\">").append(youtubeVideo.getTitle()).append("</b></td></tr>\n");
String uploadDate = youtubeVideo.getUploadDate();
uploadDate = uploadDate.substring(0, 4) + "-" + uploadDate.substring(4, 6) + "-" + uploadDate.substring(6, 8);
oneChannelStringBuilder.append("<tr><td style=\"font-size:80%;color:grey;\">").append(uploadDate).append(" •︎ ").append(youtubeVideo.getVideoDuration())
.append(" •︎ ")
.append("#").append(iii)
.append("</td></tr>\n");
youtubeVideo.setNumber(iii);
oneChannelStringBuilder.append("</table></div></td>\n");
if (internalStaticVariableVideoNumberPerRow == argsInstance.getInteger(ArgType.VIDEOS_PER_ROW).get()) {
oneChannelStringBuilder.append("<tr>");
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("<tr>");
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("<tr>");
}
oneChannelStringBuilder.append("</table>\n");
oneChannelStringBuilder.append("</div>");
oneChannelStringBuilder.append("</table>\n");
oneChannelStringBuilder.append("</div>");
}
oneChannelStringBuilder.append("</div>");
});
@ -222,6 +225,6 @@ public class Main {
return oneChannelStringBuilder;
}
private static int processedVideos = 0;
private static int processedVideos = 0;
}

View File

@ -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.
}

View File

@ -328,6 +328,7 @@ public class YoutubeVideo implements Comparable<YoutubeVideo> {
}
}
public static long totalDurationInMilliseconds = 0l;
public static List<YoutubeVideo> loadYoutubeVideos(File archiveBoxArchiveDirectory, Args argsInstance) throws IOException, InterruptedException {
int i = 0;
List<YoutubeVideo> youtubeVideos = new ArrayList<>();
@ -394,10 +395,11 @@ public class YoutubeVideo implements Comparable<YoutubeVideo> {
}
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;

View File

@ -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("""
<!DOCTYPE html>
<html>
@ -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("<video src=\"");
String videoFileName = youtubeVideo.getVideoFileName();
{
@ -94,7 +94,7 @@ public class YoutubeVideoHtml {
} else {
videoHtml.append("<a target=\"_blank\" href=\"").append(videoLocalUrl).append("\">");
videoHtml.append("<img style=\"margin:10px;height:500px;\" src=\"../archive/")
.append(youtubeVideo.getSnapshot())
.append("/media/thumbnail.")
@ -105,25 +105,24 @@ public class YoutubeVideoHtml {
videoHtml.append("<br>\n<br>\n");
videoHtml.append("#").append(youtubeVideo.getNumber()).append("&nbsp;&nbsp;&nbsp;");
boolean backEnabled = youtubeVideo.getNumber() > 1 && youtubeVideo.getPreviousVideoId() != null;
{
videoHtml.append("<button ").append(backEnabled ? "" : "disabled").append(" style=\"").append(backEnabled ? "" : "visibility:hidden;").append("font-size:200%;\" onclick=\"window.location ='").append("./").append(youtubeVideo.getPreviousVideoId()).append(".html'\">");
videoHtml.append("<button ").append(backEnabled ? "" : "disabled").append(" style=\"").append(backEnabled ? "" : "visibility:hidden;").append("font-size:200%;\" onclick=\"window.location ='").append("./").append(youtubeVideo.getPreviousVideoId()).append(".html'\">");
videoHtml.append("Back");
videoHtml.append("Back");
videoHtml.append("</button>");
}
videoHtml.append("&nbsp;&nbsp;&nbsp;");
boolean nextEnabled = youtubeVideo.getNumber() < countOfVideosInChannel && youtubeVideo.getNextVideoId() != null;
{
videoHtml.append("<button ").append(nextEnabled ? "" : "disabled").append(" style=\"").append(nextEnabled ? "" : "visibility:hidden;").append("font-size:200%;\" onclick=\"window.location ='").append("./").append(youtubeVideo.getNextVideoId()).append(".html'\">");
videoHtml.append("Next");
videoHtml.append("Next");
videoHtml.append("</button>");
}
videoHtml.append(" ");
videoHtml
@ -134,22 +133,22 @@ videoHtml.append("<button ").append(backEnabled ? "" : "disabled").append(" styl
.append("\">Download</a> ");
videoHtml.append(Utils.TWO_DECIMAL_POINTS_FORMATTER.format(((double) youtubeVideo.getVideoFileSizeInBytes()) / 1024d / 1024d)).append(" MB ");
if (youtubeVideo.getVideoFileName().endsWith(".mkv")) {
String v = youtubeVideo.getVideoFileName().replaceAll(" ", "\\\\ ");
v = v.replace("(", "\\(");
v = v.replace(")", "\\)");
var vWebm = v.substring(0, v.length() - 3) + "webm";
videoHtml.append("<input type=\"text\" id=\"archiveBoxArchiveDirectory\" name=\"archiveBoxArchiveDirectory\" size=\"100\" width=\"100\" style=\"margin-bottom:20px;margin-right:10px;font-size:110%;padding:5px;\" value=\"");
videoHtml.append("cd ").append(archiveBoxArchiveDirectory).append("/").append(youtubeVideo.getSnapshot()).append("/media/");
videoHtml.append(" && ffmpeg -i ").append(v).append(" -preset slow -crf 18 ").append(vWebm);
videoHtml.append("\"><br>");
} else {
videoHtml.append("<input type=\"text\" id=\"archiveBoxArchiveDirectory\" name=\"archiveBoxArchiveDirectory\" size=\"100\" width=\"100\" style=\"margin-bottom:20px;margin-right:10px;font-size:110%;padding:5px;\" value=\"");
videoHtml.append(archiveBoxArchiveDirectory).append("/").append(youtubeVideo.getSnapshot()).append("/media/");
videoHtml.append("\"><br>");
}
@ -160,26 +159,27 @@ videoHtml.append("<button ").append(backEnabled ? "" : "disabled").append(" styl
videoHtml.append("</pre>");
videoHtml.append("<h2>Comments</h2>");
youtubeVideo.getComments().forEach(co -> {
videoHtml.append("<div style=\"margin-left:")
.append(co.dotCount() * 50)
.append("px;\">");
videoHtml.append("<h3>").append(co.getAuthor()).append("</h3>");
videoHtml.append("<span style=\"color:grey;font-size:80%;\">")
.append(Utils.DATE_FORMAT.format(new Date(co.getTimestamp() * 1000))).append("</span><br>\n");
videoHtml.append("<span style=\"color:grey;font-size:80%;\">").append(co.getId()).append(" ")
.append(co.getParentId()).append("</span><br>\n");
videoHtml.append("<pre style=\"white-space: pre-wrap;border:1px solid black;max-width:600px;padding:10px;min-height:50px;\">").append(co.getText()).append("</pre>");
videoHtml.append("</div>");
videoHtml.append("<div style=\"margin-left:")
.append(co.dotCount() * 50)
.append("px;\">");
videoHtml.append("<h3>").append(co.getAuthor()).append("</h3>");
videoHtml.append("<span style=\"color:grey;font-size:80%;\">")
.append(Utils.DATE_FORMAT.format(new Date(co.getTimestamp() * 1000))).append("</span><br>\n");
videoHtml.append("<span style=\"color:grey;font-size:80%;\">").append(co.getId()).append(" ")
.append(co.getParentId()).append("</span><br>\n");
videoHtml.append("<pre style=\"white-space: pre-wrap;border:1px solid black;max-width:600px;padding:10px;min-height:50px;\">").append(co.getText()).append("</pre>");
videoHtml.append("</div>");
});
videoHtml.append("</body></html>");
singleVideo = videoHtml.toString();
}
@Override
public String toString() {
return this.singleVideo;
}
}