Better event send handling.

This commit is contained in:
Kavin 2022-11-23 21:52:01 +00:00
parent de7d006c4d
commit abd19aa94d
No known key found for this signature in database
GPG key ID: 49451E4482CC5BCD
2 changed files with 30 additions and 30 deletions

View file

@ -18,18 +18,22 @@ import me.kavin.piped.server.handlers.auth.AuthPlaylistHandlers;
import me.kavin.piped.server.handlers.auth.FeedHandlers; import me.kavin.piped.server.handlers.auth.FeedHandlers;
import me.kavin.piped.server.handlers.auth.UserHandlers; import me.kavin.piped.server.handlers.auth.UserHandlers;
import me.kavin.piped.utils.*; import me.kavin.piped.utils.*;
import me.kavin.piped.utils.obj.MatrixHelper;
import me.kavin.piped.utils.obj.federation.FederatedVideoInfo;
import me.kavin.piped.utils.resp.*; import me.kavin.piped.utils.resp.*;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils; import org.apache.commons.lang3.exception.ExceptionUtils;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.StatelessSession; import org.hibernate.StatelessSession;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.schabi.newpipe.extractor.stream.StreamInfo;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import static io.activej.config.converter.ConfigConverters.ofInetSocketAddress; import static io.activej.config.converter.ConfigConverters.ofInetSocketAddress;
import static io.activej.http.HttpHeaders.*; import static io.activej.http.HttpHeaders.*;
@ -77,7 +81,29 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher {
if (DatabaseHelper.doesVideoExist(s, StringUtils.substring(url, -11))) if (DatabaseHelper.doesVideoExist(s, StringUtils.substring(url, -11)))
continue; continue;
} }
VideoHelpers.handleNewVideo(url, entry.getPublishedDate().getTime(), null); Multithreading.runAsync(() -> {
try {
StreamInfo info = StreamInfo.getInfo(url);
Multithreading.runAsync(() -> {
if (info.getUploadDate() != null && System.currentTimeMillis() - info.getUploadDate().offsetDateTime().toInstant().toEpochMilli() < TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION)) {
try {
MatrixHelper.sendEvent("video.piped.stream.info", new FederatedVideoInfo(
StringUtils.substring(info.getUrl(), -11), StringUtils.substring(info.getUploaderUrl(), -24),
info.getName(),
info.getDuration(), info.getViewCount())
);
} catch (Exception e) {
ExceptionHandler.handle(e);
}
}
});
VideoHelpers.handleNewVideo(info, entry.getPublishedDate().getTime(), null);
} catch (Exception e) {
ExceptionHandler.handle(e);
}
});
} }
}); });

View file

@ -1,9 +1,7 @@
package me.kavin.piped.utils; package me.kavin.piped.utils;
import me.kavin.piped.consts.Constants; import me.kavin.piped.consts.Constants;
import me.kavin.piped.utils.obj.MatrixHelper;
import me.kavin.piped.utils.obj.db.Video; import me.kavin.piped.utils.obj.db.Video;
import me.kavin.piped.utils.obj.federation.FederatedVideoInfo;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.hibernate.StatelessSession; import org.hibernate.StatelessSession;
import org.schabi.newpipe.extractor.stream.StreamInfo; import org.schabi.newpipe.extractor.stream.StreamInfo;
@ -22,20 +20,6 @@ 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) {
Multithreading.runAsync(() -> {
if (info.getUploadDate() != null && System.currentTimeMillis() - info.getUploadDate().offsetDateTime().toInstant().toEpochMilli() < TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION)) {
try {
MatrixHelper.sendEvent("video.piped.stream.info", new FederatedVideoInfo(
StringUtils.substring(info.getUrl(), -11), StringUtils.substring(info.getUploaderUrl(), -24),
info.getName(),
info.getDuration(), info.getViewCount())
);
} catch (Exception e) {
ExceptionHandler.handle(e);
}
}
});
if (channel == null) if (channel == null)
channel = DatabaseHelper.getChannelFromId( channel = DatabaseHelper.getChannelFromId(
info.getUploaderUrl().substring("https://www.youtube.com/channel/".length())); info.getUploaderUrl().substring("https://www.youtube.com/channel/".length()));
@ -68,23 +52,13 @@ public class VideoHelpers {
} }
} }
public static void updateVideo(String id, StreamInfoItem item, long time) {
Multithreading.runAsync(() -> {
try {
if (!updateVideo(id, item.getViewCount(), item.getDuration(), item.getName())) {
handleNewVideo(item.getUrl(), time, null);
}
} catch (Exception e) {
ExceptionHandler.handle(e);
}
});
}
public static void updateVideo(String id, StreamInfo info, long time) { public static void updateVideo(String id, StreamInfo info, long time) {
Multithreading.runAsync(() -> { Multithreading.runAsync(() -> {
try { try {
if (!updateVideo(id, info.getViewCount(), info.getDuration(), info.getName())) { if (!updateVideo(id, info.getViewCount(), info.getDuration(), info.getName())) {
handleNewVideo(info, time, null); var channel = DatabaseHelper.getChannelFromId(StringUtils.substring(info.getUploaderUrl(), -24));
if (channel != null)
handleNewVideo(info, time, channel);
} }
} catch (Exception e) { } catch (Exception e) {
ExceptionHandler.handle(e); ExceptionHandler.handle(e);