mirror of
				https://github.com/TeamPiped/Piped-Backend.git
				synced 2024-08-14 23:51:41 +00:00 
			
		
		
		
	Merge pull request #463 from Bnyro/playlists
Allow adding multiple videos to a playlist with one request
This commit is contained in:
		
						commit
						a654d22635
					
				
					 2 changed files with 47 additions and 22 deletions
				
			
		| 
						 | 
				
			
			@ -1,6 +1,7 @@
 | 
			
		|||
package me.kavin.piped.server;
 | 
			
		||||
 | 
			
		||||
import com.fasterxml.jackson.core.JsonProcessingException;
 | 
			
		||||
import com.fasterxml.jackson.databind.JsonNode;
 | 
			
		||||
import com.rometools.rome.feed.synd.SyndFeed;
 | 
			
		||||
import com.rometools.rome.io.SyndFeedInput;
 | 
			
		||||
import io.activej.config.Config;
 | 
			
		||||
| 
						 | 
				
			
			@ -12,6 +13,7 @@ import io.activej.inject.annotation.Provides;
 | 
			
		|||
import io.activej.inject.module.AbstractModule;
 | 
			
		||||
import io.activej.inject.module.Module;
 | 
			
		||||
import io.activej.launchers.http.MultithreadedHttpServerLauncher;
 | 
			
		||||
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
 | 
			
		||||
import me.kavin.piped.consts.Constants;
 | 
			
		||||
import me.kavin.piped.server.handlers.*;
 | 
			
		||||
import me.kavin.piped.server.handlers.auth.AuthPlaylistHandlers;
 | 
			
		||||
| 
						 | 
				
			
			@ -377,8 +379,20 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher {
 | 
			
		|||
                    try {
 | 
			
		||||
                        var json = Constants.mapper.readTree(request.loadBody().getResult().asArray());
 | 
			
		||||
                        var playlistId = json.get("playlistId").textValue();
 | 
			
		||||
                        var videoId = json.get("videoId").textValue();
 | 
			
		||||
                        return getJsonResponse(AuthPlaylistHandlers.addToPlaylistResponse(request.getHeader(AUTHORIZATION), playlistId, videoId), "private");
 | 
			
		||||
                        var videoIds = new ObjectArrayList<String>();
 | 
			
		||||
                        // backwards compatibility
 | 
			
		||||
                        var videoIdField = json.get("videoId");
 | 
			
		||||
                        if (videoIdField != null) {
 | 
			
		||||
                            videoIds.add(videoIdField.textValue());
 | 
			
		||||
                        }
 | 
			
		||||
                        var videoIdsField = json.get("videoIds");
 | 
			
		||||
                        if (videoIdsField != null) {
 | 
			
		||||
                            for (JsonNode node : videoIdsField) {
 | 
			
		||||
                                videoIds.add(node.textValue());
 | 
			
		||||
                            }
 | 
			
		||||
                        }
 | 
			
		||||
 | 
			
		||||
                        return getJsonResponse(AuthPlaylistHandlers.addToPlaylistResponse(request.getHeader(AUTHORIZATION), playlistId, videoIds), "private");
 | 
			
		||||
                    } catch (Exception e) {
 | 
			
		||||
                        return getErrorResponse(e, request.getPath());
 | 
			
		||||
                    }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -8,6 +8,7 @@ import com.rometools.rome.feed.synd.SyndFeedImpl;
 | 
			
		|||
import com.rometools.rome.io.SyndFeedOutput;
 | 
			
		||||
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
 | 
			
		||||
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
 | 
			
		||||
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
 | 
			
		||||
import jakarta.persistence.criteria.JoinType;
 | 
			
		||||
import me.kavin.piped.consts.Constants;
 | 
			
		||||
import me.kavin.piped.utils.*;
 | 
			
		||||
| 
						 | 
				
			
			@ -198,10 +199,14 @@ public class AuthPlaylistHandlers {
 | 
			
		|||
        return mapper.writeValueAsBytes(new AcceptedResponse());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static byte[] addToPlaylistResponse(String session, String playlistId, String videoId) throws IOException, ExtractionException {
 | 
			
		||||
    public static byte[] addToPlaylistResponse(String session, String playlistId, List<String> videoIds) throws IOException, ExtractionException {
 | 
			
		||||
 | 
			
		||||
        if (StringUtils.isBlank(session) || StringUtils.isBlank(playlistId) || StringUtils.isBlank(videoId))
 | 
			
		||||
            ExceptionHandler.throwErrorResponse(new InvalidRequestResponse("session, playlistId and videoId are required parameters"));
 | 
			
		||||
        videoIds = videoIds.stream()
 | 
			
		||||
                .filter(StringUtils::isNotBlank)
 | 
			
		||||
                .collect(Collectors.toList());
 | 
			
		||||
 | 
			
		||||
        if (StringUtils.isBlank(session) || StringUtils.isBlank(playlistId) || videoIds.isEmpty())
 | 
			
		||||
            ExceptionHandler.throwErrorResponse(new InvalidRequestResponse("session, playlistId and videoId(s) are required parameters"));
 | 
			
		||||
 | 
			
		||||
        var user = DatabaseHelper.getUserFromSession(session);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -223,32 +228,38 @@ public class AuthPlaylistHandlers {
 | 
			
		|||
                return mapper.writeValueAsBytes(mapper.createObjectNode()
 | 
			
		||||
                        .put("error", "You are not the owner this playlist"));
 | 
			
		||||
 | 
			
		||||
            var video = DatabaseHelper.getPlaylistVideoFromId(s, videoId);
 | 
			
		||||
            var playlistVideos = DatabaseHelper.getPlaylistVideosFromIds(s, new ObjectOpenHashSet<>(videoIds));
 | 
			
		||||
 | 
			
		||||
            if (video == null) {
 | 
			
		||||
                StreamInfo info = StreamInfo.getInfo("https://www.youtube.com/watch?v=" + videoId);
 | 
			
		||||
            var videos = playlist.getVideos();
 | 
			
		||||
 | 
			
		||||
                String channelId = StringUtils.substringAfter(info.getUploaderUrl(), "/channel/");
 | 
			
		||||
            for (String videoId : videoIds) {
 | 
			
		||||
                if (StringUtils.isEmpty(videoId)) continue;
 | 
			
		||||
 | 
			
		||||
                var channel = DatabaseHelper.getChannelFromId(s, channelId);
 | 
			
		||||
                var video = playlistVideos.stream().filter(v -> v.getId().equals(videoId))
 | 
			
		||||
                        .findFirst()
 | 
			
		||||
                        .orElse(null);
 | 
			
		||||
 | 
			
		||||
                if (channel == null) {
 | 
			
		||||
                    channel = DatabaseHelper.saveChannel(channelId);
 | 
			
		||||
                if (video == null) {
 | 
			
		||||
                    StreamInfo info = StreamInfo.getInfo("https://www.youtube.com/watch?v=" + videoId);
 | 
			
		||||
 | 
			
		||||
                    String channelId = StringUtils.substringAfter(info.getUploaderUrl(), "/channel/");
 | 
			
		||||
 | 
			
		||||
                    var channel = DatabaseHelper.getChannelFromId(s, channelId);
 | 
			
		||||
 | 
			
		||||
                    if (channel == null) {
 | 
			
		||||
                        channel = DatabaseHelper.saveChannel(channelId);
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    video = new PlaylistVideo(videoId, info.getName(), info.getThumbnailUrl(), info.getDuration(), channel);
 | 
			
		||||
 | 
			
		||||
                    s.persist(video);
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                video = new PlaylistVideo(videoId, info.getName(), info.getThumbnailUrl(), info.getDuration(), channel);
 | 
			
		||||
 | 
			
		||||
                var tr = s.beginTransaction();
 | 
			
		||||
                s.persist(video);
 | 
			
		||||
                tr.commit();
 | 
			
		||||
                if (playlist.getVideos().isEmpty()) playlist.setThumbnail(video.getThumbnail());
 | 
			
		||||
 | 
			
		||||
                videos.add(video);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (playlist.getVideos().isEmpty())
 | 
			
		||||
                playlist.setThumbnail(video.getThumbnail());
 | 
			
		||||
 | 
			
		||||
            playlist.getVideos().add(video);
 | 
			
		||||
 | 
			
		||||
            var tr = s.beginTransaction();
 | 
			
		||||
            s.merge(playlist);
 | 
			
		||||
            tr.commit();
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue