From 43d4bcf927600ce0f17254a0a9533fc8b354c664 Mon Sep 17 00:00:00 2001 From: Robert Vokac Date: Mon, 1 Jul 2024 21:27:19 +0200 Subject: [PATCH] Small improvements II --- .../archiveboxyoutubehelper/Main.java | 62 ++++++++++++++++++- .../archiveboxyoutubehelper/YoutubeVideo.java | 38 +++++++++--- 2 files changed, 89 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/nanoboot/archiveboxyoutubehelper/Main.java b/src/main/java/org/nanoboot/archiveboxyoutubehelper/Main.java index b77db6e..cd943f0 100755 --- a/src/main/java/org/nanoboot/archiveboxyoutubehelper/Main.java +++ b/src/main/java/org/nanoboot/archiveboxyoutubehelper/Main.java @@ -46,12 +46,36 @@ public class Main { System.out.println("archiveboxyoutubehelper - HTML generator\n"); if (args.length == 0) { - args = new String[]{"/rv/blupi/archivebox"}; + args = new String[]{"/rv/blupi/archivebox" + //, "--video", "eVqgt6s4h30" + }; } - if (args.length != 1) { - System.err.println("One argument is expected, but the count of arguments is: " + args.length + "."); + if (args.length < 1) { + System.err.println("At least one argument is expected, but the count of arguments is: " + args.length + "."); System.exit(1); } + String argVideo = ""; + String argChannel = ""; + if(args.length > 1) { + for(int i = 1;i= args.length) { + throw new ArchiveBoxYoutubeHelperException("Fatal error: missing value for --video"); + } + argVideo = args[i]; + } + if(s.equals("--channel")) { + i++; + if(i >= args.length) { + throw new ArchiveBoxYoutubeHelperException("Fatal error: missing value for --channel"); + } + argChannel = args[i]; + } + } + } + File archiveBoxRootDirectory = new File(args[0]); File archiveBoxArchiveDirectory = new File(archiveBoxRootDirectory, "archive"); int i = 0; @@ -64,6 +88,13 @@ public class Main { continue; } YoutubeVideo youtubeVideo = new YoutubeVideo(mediaDirectory); + + if(!argVideo.isBlank() && !youtubeVideo.getId().equals(argVideo)) { + continue; + } + if(!argChannel.isBlank() && !youtubeVideo.getChannelId().equals(argChannel)) { + continue; + } i++; System.out.println("\n\nFound video #" + i); @@ -83,6 +114,18 @@ public class Main { youtubeVideos.add(youtubeVideo); } Collections.sort(youtubeVideos); + YoutubeVideo previousVideo = null; + YoutubeVideo nextVideo = null; + YoutubeVideo currentVideo = null; + for(int j = 0; j channelUrls = new HashMap<>(); List channels = new ArrayList<>(); youtubeVideos.stream().forEach(c -> { @@ -150,6 +193,7 @@ public class Main { .append(" •︎ ") .append("#").append(iii) .append("\n"); + z.setNumber(iii); sb.append("\n"); if (videoNumberPerRow == VIDEOS_PER_ROW) { sb.append(""); @@ -195,6 +239,16 @@ public class Main { .append("/media/thumbnail.jpg\">
"); sb2.append("").append(z.getTitle()).append(""); sb2.append("

"); + sb2.append("#").append(z.getNumber()).append("   "); + if(z.getPreviousVideoId() != null) sb2.append(""); + sb2.append("Back"); + if(z.getPreviousVideoId() != null) sb2.append(""); + sb2.append("   "); + if(z.getNextVideoId()!= null) sb2.append(""); + sb2.append("Next"); + if(z.getNextVideoId() != null) sb2.append(""); + sb2.append(" "); + sb2.append("
"); sb2.append("
");
                     sb2.append(z.getDescription().isBlank() ? "No description" : z.getDescription());
                     sb2.append("
"); @@ -253,6 +307,8 @@ sb2.append("
System.out.println(s)); } } diff --git a/src/main/java/org/nanoboot/archiveboxyoutubehelper/YoutubeVideo.java b/src/main/java/org/nanoboot/archiveboxyoutubehelper/YoutubeVideo.java index 2937ddc..506dca5 100644 --- a/src/main/java/org/nanoboot/archiveboxyoutubehelper/YoutubeVideo.java +++ b/src/main/java/org/nanoboot/archiveboxyoutubehelper/YoutubeVideo.java @@ -42,10 +42,16 @@ public class YoutubeVideo implements Comparable { private String channelUrl; private String channelId; private String uploadDate; + private long timestamp; private String description; private String thumbnail; private List comments = new ArrayList<>(); - + private String previousVideoId = null; + private String nextVideoId = null; + private String ext = null; + private int number; + public static final List missingYoutubeVideos = new ArrayList<>(); + YoutubeVideo(File mediaDirectory) throws InterruptedException, IOException { File metadataFile = new File(mediaDirectory, "metadata"); if (!Main.ALWAYS_COMPUTE_METADATA && metadataFile.exists()) { @@ -91,19 +97,29 @@ public class YoutubeVideo implements Comparable { // Optional descriptionFile = files.stream().filter(f -> f.getName().endsWith(".description")).findFirst(); + ext = jsonObject.getString("ext"); + Optional videoFile = files .stream() - .filter( - f -> !f.getName().endsWith(".description") - && !f.getName().endsWith(".json") - && !f.getName().equals("metadata") - && !f.getName().endsWith(thumbnail) + .filter(f -> + (f.getName().endsWith("." + ext)) || + (f.getName().endsWith(".mp4")) || + (f.getName().endsWith(".mkv")) ) +// .filter( +// f -> !f.getName().endsWith(".description") +// && !f.getName().endsWith(".json") +// && !f.getName().equals("metadata") +// && !f.getName().endsWith(thumbnail) +// ) .findFirst(); + + snapshot = mediaDirectory.getParentFile().getName(); + id = jsonObject.getString("id"); + if(videoFile.isEmpty())missingYoutubeVideos.add(id); this.description = descriptionFile.isPresent() ? Utils.readTextFromFile(descriptionFile.get()) : ""; - id = jsonObject.getString("id"); - snapshot = mediaDirectory.getParentFile().getName(); + title = jsonObject.getString("title"); if (videoFile.isPresent() && !videoFile.get().getName().endsWith(".part")) { final File videoFileGet = videoFile.get(); @@ -116,6 +132,8 @@ public class YoutubeVideo implements Comparable { channelUrl = jsonObject.getString("channel_url"); channelId = jsonObject.getString("channel_id"); uploadDate = jsonObject.getString("upload_date"); + timestamp = jsonObject.getLong("timestamp"); + if (jsonObject.has("comments")) { final JSONArray jsonArray = jsonObject.getJSONArray("comments"); @@ -171,7 +189,11 @@ public class YoutubeVideo implements Comparable { @Override public int compareTo(YoutubeVideo o) { if (this.channelName != null && o.channelName != null && this.channelName.contentEquals(o.channelName)) { + if(this.uploadDate.equals(o.uploadDate)) { + return Long.valueOf(timestamp).compareTo(o.timestamp); + } else { return this.uploadDate.compareTo(o.uploadDate); + } } else { if (this.channelName != null && o.channelName != null) { return this.channelName.compareTo(o.channelName);