From 76291ea03858ad7cc28ccdb712481b8291a3b138 Mon Sep 17 00:00:00 2001 From: Robert Vokac Date: Mon, 22 Jul 2024 18:47:48 +0200 Subject: [PATCH] Refactoring --- .../org/nanoboot/youtubedlfrontend/Arg.java | 34 +++++ .../nanoboot/youtubedlfrontend/ArgType.java | 44 ++++++ .../org/nanoboot/youtubedlfrontend/Args.java | 89 ++++++++++++ .../org/nanoboot/youtubedlfrontend/Main.java | 132 +++--------------- .../youtubedlfrontend/YoutubeComment.java | 22 ++- .../youtubedlfrontend/YoutubeVideo.java | 9 +- 6 files changed, 208 insertions(+), 122 deletions(-) create mode 100644 src/main/java/org/nanoboot/youtubedlfrontend/Arg.java create mode 100644 src/main/java/org/nanoboot/youtubedlfrontend/ArgType.java create mode 100644 src/main/java/org/nanoboot/youtubedlfrontend/Args.java diff --git a/src/main/java/org/nanoboot/youtubedlfrontend/Arg.java b/src/main/java/org/nanoboot/youtubedlfrontend/Arg.java new file mode 100644 index 0000000..bb7443f --- /dev/null +++ b/src/main/java/org/nanoboot/youtubedlfrontend/Arg.java @@ -0,0 +1,34 @@ +/////////////////////////////////////////////////////////////////////////////////////////////// +// youtubedl-frontend: Tool generating html pages for Archive Box. +// Copyright (C) 2024 the original author or authors. +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; version 2 +// of the License only. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +/////////////////////////////////////////////////////////////////////////////////////////////// +package org.nanoboot.youtubedlfrontend; + +import lombok.AllArgsConstructor; +import lombok.Data; + +/** + * + * @author robertvokac + */ + +@Data +@AllArgsConstructor +public class Arg { + private ArgType argType; + private String value; +} diff --git a/src/main/java/org/nanoboot/youtubedlfrontend/ArgType.java b/src/main/java/org/nanoboot/youtubedlfrontend/ArgType.java new file mode 100644 index 0000000..0f92cb5 --- /dev/null +++ b/src/main/java/org/nanoboot/youtubedlfrontend/ArgType.java @@ -0,0 +1,44 @@ +/////////////////////////////////////////////////////////////////////////////////////////////// +// youtubedl-frontend: Tool generating html pages for Archive Box. +// Copyright (C) 2024 the original author or authors. +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; version 2 +// of the License only. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +/////////////////////////////////////////////////////////////////////////////////////////////// +package org.nanoboot.youtubedlfrontend; + +import lombok.Getter; + +/** + * + * @author robertvokac + */ +public enum ArgType { + VIDEO("video", null), + CHANNEL("channel", null), + VIDEOS_PER_ROW("videos-per-row", "4"), + ALWAYS_GENERATE_METADATA("always-generate-metadata", "true"), + ALWAYS_GENERATE_HTML_FILES("always-generate-html-files", "true"), + THUMBNAIL_AS_BASE64("thumbnail-as-base64", "false"), + THUMBNAIL_LINKS_TO_YOUTUBE("thumbnail-links-to-youtube", "false"); + @Getter + private String name; + @Getter + private String defaultValue; + ArgType(String name, String defaultValue) { + this.name = name; + this.defaultValue = defaultValue; + } + +} diff --git a/src/main/java/org/nanoboot/youtubedlfrontend/Args.java b/src/main/java/org/nanoboot/youtubedlfrontend/Args.java new file mode 100644 index 0000000..5d84434 --- /dev/null +++ b/src/main/java/org/nanoboot/youtubedlfrontend/Args.java @@ -0,0 +1,89 @@ +/////////////////////////////////////////////////////////////////////////////////////////////// +// youtubedl-frontend: Tool generating html pages for Archive Box. +// Copyright (C) 2024 the original author or authors. +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; version 2 +// of the License only. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +/////////////////////////////////////////////////////////////////////////////////////////////// +package org.nanoboot.youtubedlfrontend; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import java.util.stream.Stream; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.ToString; + +/** + * + * @author robertvokac + */ +@Data +@AllArgsConstructor +@ToString +public class Args { + + public static final String TWO_DASHES = "--"; + private final Map map = new HashMap<>(); + + public Args(String[] args) { + if (args.length > 0) { + for (int i = 0; i < args.length; i++) { + String arg = args[i]; + if (i == 0 && !arg.startsWith(TWO_DASHES)) { + continue; + } + Optional argType = Stream.of((ArgType.values())) + .filter(a -> arg.equals(TWO_DASHES + a.getName())).findFirst(); + + if (argType.isPresent()) { + i++; + final ArgType argTypeGet = argType.get(); + if (i >= args.length) { + throw new YoutubedlFrontendException("Fatal error: missing value for " + TWO_DASHES + argTypeGet.getName()); + } + String value = args[i]; + if (argTypeGet == ArgType.VIDEOS_PER_ROW) { + int argVideosPerRow = Integer.parseInt(args[i]); + if (argVideosPerRow < 2) { + value = "0"; + } + } + map.put(argTypeGet, new Arg(argTypeGet, value)); + } + + } + } + } + + public Optional getString(ArgType argType) { + if (!map.containsKey(argType)) { + + return (argType.getDefaultValue() == null || argType.getDefaultValue().isEmpty()) ? Optional.empty() : Optional.of(argType.getDefaultValue()); + } + return Optional.of(map.get(argType).getValue()); + + } + + public Optional getBoolean(ArgType argType) { + Optional o = getString(argType); + return o.isPresent() ? Optional.of(Utils.convertStringToBoolean(o.get())) : Optional.empty(); + } + + public Optional getInteger(ArgType argType) { + Optional o = getString(argType); + return o.isPresent() ? Optional.of(Integer.valueOf(o.get())) : Optional.empty(); + } +} diff --git a/src/main/java/org/nanoboot/youtubedlfrontend/Main.java b/src/main/java/org/nanoboot/youtubedlfrontend/Main.java index fbaf5da..4e772e1 100755 --- a/src/main/java/org/nanoboot/youtubedlfrontend/Main.java +++ b/src/main/java/org/nanoboot/youtubedlfrontend/Main.java @@ -33,6 +33,7 @@ import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; +import static org.nanoboot.youtubedlfrontend.Args.TWO_DASHES; /** * @author Robert Vokac @@ -42,118 +43,23 @@ public class Main { private static int iii = 0; private static int internalStaticVariableVideoNumberPerRow = 0; - public static boolean argAlwaysGenerateMetadata = true; - public static boolean argAlwaysGenerateHtmlFiles = true; - public static boolean thumbnailAsBase64 = false; - public static boolean thumbnailLinksToYoutube = false; - public static int argVideosPerRow = 4; + public static int THUMBNAIL_WIDTH = 250; - public static String argVideo; - public static String argChannel; + public static void main(String[] args) throws IOException, InterruptedException { System.out.println("youtubedlfrontend - HTML generator\n"); - //args = "/rv/databig/youtube --_video UDpsz1yIwiw --always-generate-metadata 1 --always-generate-html-files 1 --videos-per-row 4".split(" "); if (args.length < 1) { //System.err.println("At least one argument is expected, but the count of arguments is: " + args.length + "."); String argsS = "/rv/blupi/archivebox --_video UDpsz1yIwiw --always-generate-metadata 0 " - + " --always-generate-html-files 1 --videos-per-row 4 --thumbnail-links-to-youtube 1" + + " --always-generate-html-files 0 --videos-per-row 4 --thumbnail-links-to-youtube 1" + " --thumbnail-as-base64 1"; args = argsS.split(" "); //System.exit(1); } - argVideo = ""; - argChannel = ""; - if (args.length > 0) { - for (int i = 0; i < args.length; i++) { - String arg = args[i]; - if (i == 0 && !arg.startsWith(TWO_DASHES)) { - continue; - } - if (arg.equals("--video")) { - i++; - if (i >= args.length) { - throw new YoutubedlFrontendException("Fatal error: missing value for --video"); - } - argVideo = args[i]; - } - if (arg.equals("--channel")) { - i++; - if (i >= args.length) { - throw new YoutubedlFrontendException("Fatal error: missing value for --channel"); - } - argChannel = args[i]; - } - - if (arg.equals("--videos-per-row")) { - i++; - if (i >= args.length) { - throw new YoutubedlFrontendException("Fatal error: missing value for --videos-per-row"); - } - argVideosPerRow = Integer.parseInt(args[i]); - if (argVideosPerRow < 2) { - argVideosPerRow = 0; - } - } - - if (arg.equals("--always-generate-metadata")) { - i++; - if (i >= args.length) { - throw new YoutubedlFrontendException("Fatal error: missing value for --always-generate-metadata"); - } - String s = args[i]; - - try { - argAlwaysGenerateMetadata = Utils.convertStringToBoolean(s); - } catch (Exception e) { - throw new YoutubedlFrontendException("Invalid value for --always-generate-metadata"); - } - } - - if (arg.equals("--always-generate-html-files")) { - i++; - if (i >= args.length) { - throw new YoutubedlFrontendException("Fatal error: missing value for --always-generate-html-files"); - } - String s = args[i]; - try {argAlwaysGenerateHtmlFiles = Utils.convertStringToBoolean(s);} catch(Exception e) { - throw new YoutubedlFrontendException("Invalid value for --always-generate-html-files"); - } - - } - - if (arg.equals("--thumbnail-as-base64")) { - i++; - if (i >= args.length) { - throw new YoutubedlFrontendException("Fatal error: missing value for --thumbnail-as-base64"); - } - String s = args[i]; - try {thumbnailAsBase64 = Utils.convertStringToBoolean(s);} catch(Exception e) { - throw new YoutubedlFrontendException("Invalid value for --thumbnail-as-base64"); - } - - - } - - if (arg.equals("--thumbnail-links-to-youtube")) { - i++; - if (i >= args.length) { - throw new YoutubedlFrontendException("Fatal error: missing value for --thumbnail-links-to-youtube"); - } - String s = args[i]; - try { - thumbnailLinksToYoutube = Utils.convertStringToBoolean(s); - } catch (Exception e) { - throw new YoutubedlFrontendException("Invalid value for --thumbnail-links-to-youtube"); - } - - } - - - - } - } + Args argsInstance = new Args(args); + System.out.println(argsInstance.toString()); String workingDirectory = args.length > 0 && !args[0].startsWith(TWO_DASHES) ? args[0] : new File(".").getAbsolutePath(); File archiveBoxRootDirectory = new File(workingDirectory); @@ -167,16 +73,14 @@ public class Main { //nothing to do continue; } - YoutubeVideo youtubeVideo = new YoutubeVideo(mediaDirectory); - if (!Main.argVideo.isBlank() && !youtubeVideo.getId().equals(Main.argVideo)) { + 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())) { continue; } - if (!argVideo.isBlank() && !youtubeVideo.getId().equals(argVideo)) { - continue; - } - if (!argChannel.isBlank() && !youtubeVideo.getChannelId().equals(argChannel)) { + if (argsInstance.getString(ArgType.CHANNEL).isPresent() && !argsInstance.getString(ArgType.CHANNEL).equals(youtubeVideo.getChannelId())) { continue; } + i++; System.out.println("\n\nFound video #" + i); @@ -244,6 +148,9 @@ public class Main { Youtube videos + + +