mirror of
				https://github.com/TeamPiped/Piped-Backend.git
				synced 2024-08-14 23:51:41 +00:00 
			
		
		
		
	Merge pull request #467 from TeamPiped/federate-new
Better event send handling, and avoid fetching StreamInfo unnecessarily
This commit is contained in:
		
						commit
						26a4ebbabf
					
				
					 2 changed files with 30 additions and 30 deletions
				
			
		| 
						 | 
				
			
			@ -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.UserHandlers;
 | 
			
		||||
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 org.apache.commons.lang3.StringUtils;
 | 
			
		||||
import org.apache.commons.lang3.exception.ExceptionUtils;
 | 
			
		||||
import org.hibernate.Session;
 | 
			
		||||
import org.hibernate.StatelessSession;
 | 
			
		||||
import org.jetbrains.annotations.NotNull;
 | 
			
		||||
import org.schabi.newpipe.extractor.stream.StreamInfo;
 | 
			
		||||
import org.xml.sax.InputSource;
 | 
			
		||||
 | 
			
		||||
import java.io.ByteArrayInputStream;
 | 
			
		||||
import java.net.InetSocketAddress;
 | 
			
		||||
import java.util.Objects;
 | 
			
		||||
import java.util.concurrent.Executor;
 | 
			
		||||
import java.util.concurrent.TimeUnit;
 | 
			
		||||
 | 
			
		||||
import static io.activej.config.converter.ConfigConverters.ofInetSocketAddress;
 | 
			
		||||
import static io.activej.http.HttpHeaders.*;
 | 
			
		||||
| 
						 | 
				
			
			@ -77,7 +81,29 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher {
 | 
			
		|||
                                    if (DatabaseHelper.doesVideoExist(s, StringUtils.substring(url, -11)))
 | 
			
		||||
                                        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);
 | 
			
		||||
                                    }
 | 
			
		||||
                                });
 | 
			
		||||
                            }
 | 
			
		||||
                        });
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,9 +1,7 @@
 | 
			
		|||
package me.kavin.piped.utils;
 | 
			
		||||
 | 
			
		||||
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.federation.FederatedVideoInfo;
 | 
			
		||||
import org.apache.commons.lang3.StringUtils;
 | 
			
		||||
import org.hibernate.StatelessSession;
 | 
			
		||||
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) {
 | 
			
		||||
 | 
			
		||||
        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)
 | 
			
		||||
            channel = DatabaseHelper.getChannelFromId(
 | 
			
		||||
                    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) {
 | 
			
		||||
        Multithreading.runAsync(() -> {
 | 
			
		||||
            try {
 | 
			
		||||
                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) {
 | 
			
		||||
                ExceptionHandler.handle(e);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue