From 3be67c18ff2f0b023caf2ac4ef1526a1beb18659 Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Sun, 11 Dec 2022 02:45:18 +0000 Subject: [PATCH] Detect shorts content properly in feed. --- .../me/kavin/piped/utils/VideoHelpers.java | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/main/java/me/kavin/piped/utils/VideoHelpers.java b/src/main/java/me/kavin/piped/utils/VideoHelpers.java index 42d696e..0242e85 100644 --- a/src/main/java/me/kavin/piped/utils/VideoHelpers.java +++ b/src/main/java/me/kavin/piped/utils/VideoHelpers.java @@ -1,5 +1,7 @@ package me.kavin.piped.utils; +import com.grack.nanojson.JsonObject; +import com.grack.nanojson.JsonWriter; import me.kavin.piped.consts.Constants; import me.kavin.piped.utils.obj.db.Video; import org.apache.commons.lang3.StringUtils; @@ -9,6 +11,12 @@ import org.schabi.newpipe.extractor.stream.StreamInfoItem; import java.util.concurrent.TimeUnit; +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.schabi.newpipe.extractor.NewPipe.getPreferredContentCountry; +import static org.schabi.newpipe.extractor.NewPipe.getPreferredLocalization; +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getJsonPostResponse; +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.prepareDesktopJsonBuilder; + public class VideoHelpers { public static void handleNewVideo(String url, long time, me.kavin.piped.utils.obj.db.Channel channel) { try { @@ -18,7 +26,7 @@ public class VideoHelpers { } } - public static void handleNewVideo(StreamInfo info, long time, me.kavin.piped.utils.obj.db.Channel channel) { + public static void handleNewVideo(StreamInfo info, long time, me.kavin.piped.utils.obj.db.Channel channel) throws Exception { if (channel == null) channel = DatabaseHelper.getChannelFromId( @@ -29,6 +37,9 @@ public class VideoHelpers { if (channel != null && (System.currentTimeMillis() - infoTime) < TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION)) { + + info.setShortFormContent(isShort(info.getId())); + try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) { if (!DatabaseHelper.doesVideoExist(s, info.getId())) { @@ -52,6 +63,20 @@ public class VideoHelpers { } } + public static boolean isShort(String videoId) throws Exception { + + final byte[] body = JsonWriter.string(prepareDesktopJsonBuilder( + getPreferredLocalization(), getPreferredContentCountry()) + .value("url", "https://www.youtube.com/shorts/" + videoId) + .done()) + .getBytes(UTF_8); + + final JsonObject jsonResponse = getJsonPostResponse("navigation/resolve_url", + body, getPreferredLocalization()); + + return jsonResponse.getObject("endpoint").has("reelWatchEndpoint"); + } + public static void updateVideo(String id, StreamInfo info, long time) { Multithreading.runAsync(() -> { try {