From 374d067a9523e59de3c879d7090ff14a10976641 Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Mon, 27 Jun 2022 17:11:22 +0100 Subject: [PATCH] Implement RYD-Proxy API implementation. (#293) --- config.properties | 4 ++++ .../java/me/kavin/piped/consts/Constants.java | 6 +++++ .../me/kavin/piped/utils/ResponseHelper.java | 23 +++++++++++++++++++ .../java/me/kavin/piped/utils/RydHelper.java | 18 +++++++++++++++ 4 files changed, 51 insertions(+) create mode 100644 src/main/java/me/kavin/piped/utils/RydHelper.java diff --git a/config.properties b/config.properties index 0feee2d..9e85cf3 100644 --- a/config.properties +++ b/config.properties @@ -21,6 +21,10 @@ DISABLE_REGISTRATION:false FEED_RETENTION:30 # Disable CPU expensive timers (for nodes with low CPU, at least one node should have this disabled) DISABLE_TIMERS:false +# RYD Proxy URL (see https://github.com/TeamPiped/RYD-Proxy) +RYD_PROXY_URL:https://ryd-proxy.kavin.rocks +# Disable the usage of RYD +DISABLE_RYD:false # Hibernate properties hibernate.connection.url:jdbc:postgresql://postgres:5432/piped hibernate.connection.driver_class:org.postgresql.Driver diff --git a/src/main/java/me/kavin/piped/consts/Constants.java b/src/main/java/me/kavin/piped/consts/Constants.java index 3e00ef5..ddef6e1 100644 --- a/src/main/java/me/kavin/piped/consts/Constants.java +++ b/src/main/java/me/kavin/piped/consts/Constants.java @@ -51,6 +51,10 @@ public class Constants { public static final boolean DISABLE_TIMERS; + public static final String RYD_PROXY_URL; + + public static final boolean DISABLE_RYD; + public static final String VERSION; public static final ObjectMapper mapper = new ObjectMapper().addMixIn(Page.class, PageMixin.class); @@ -78,6 +82,8 @@ public class Constants { DISABLE_REGISTRATION = Boolean.parseBoolean(getProperty(prop, "DISABLE_REGISTRATION", "false")); FEED_RETENTION = Integer.parseInt(getProperty(prop, "FEED_RETENTION", "30")); DISABLE_TIMERS = Boolean.parseBoolean(getProperty(prop, "DISABLE_TIMERS", "false")); + RYD_PROXY_URL = getProperty(prop, "RYD_PROXY_URL", "https://ryd-proxy.kavin.rocks"); + DISABLE_RYD = Boolean.parseBoolean(getProperty(prop, "DISABLE_RYD", "false")); System.getenv().forEach((key, value) -> { if (key.startsWith("hibernate")) hibernateProperties.put(key, value); diff --git a/src/main/java/me/kavin/piped/utils/ResponseHelper.java b/src/main/java/me/kavin/piped/utils/ResponseHelper.java index dccc246..297365d 100644 --- a/src/main/java/me/kavin/piped/utils/ResponseHelper.java +++ b/src/main/java/me/kavin/piped/utils/ResponseHelper.java @@ -98,6 +98,15 @@ public class ResponseHelper { return null; }); + final var futureDislikeRating = Multithreading.supplyAsync(() -> { + try { + return RydHelper.getDislikeRating(videoId); + } catch (Exception e) { + ExceptionHandler.handle(e); + } + return null; + }); + final List subtitles = new ObjectArrayList<>(); final List chapters = new ObjectArrayList<>(); @@ -165,6 +174,20 @@ public class ResponseHelper { lbryId = null; } + // Attempt to get dislikes calculating with the RYD API rating + if (info.getDislikeCount() < 0 && info.getLikeCount() >= 0) { + double rating; + try { + rating = futureDislikeRating.get(2, TimeUnit.SECONDS); + } catch (Exception e) { + rating = -1; + } + + if (rating > 1 && rating <= 5) { + info.setDislikeCount(Math.round(info.getLikeCount() * ((5 - rating) / (rating - 1)))); + } + } + final Streams streams = new Streams(info.getName(), info.getDescription().getContent(), info.getTextualUploadDate(), info.getUploaderName(), substringYouTube(info.getUploaderUrl()), rewriteURL(info.getUploaderAvatarUrl()), rewriteURL(info.getThumbnailUrl()), info.getDuration(), diff --git a/src/main/java/me/kavin/piped/utils/RydHelper.java b/src/main/java/me/kavin/piped/utils/RydHelper.java new file mode 100644 index 0000000..e10ec98 --- /dev/null +++ b/src/main/java/me/kavin/piped/utils/RydHelper.java @@ -0,0 +1,18 @@ +package me.kavin.piped.utils; + + +import me.kavin.piped.consts.Constants; + +import java.io.IOException; + +public class RydHelper { + public static double getDislikeRating(String videoId) throws IOException { + + if (Constants.DISABLE_RYD) + return -1; + + return Constants.mapper.readTree(RequestUtils.sendGet(Constants.RYD_PROXY_URL + "/votes/" + videoId)) + .get("rating") + .asDouble(-1); + } +}