From e5556f38e58673a0b90b1a70eed76974b8232e72 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sun, 1 Jan 2023 20:18:50 +0100 Subject: [PATCH 1/2] Don't error out when a video of many is private when adding to playlist --- .../handlers/auth/AuthPlaylistHandlers.java | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/main/java/me/kavin/piped/server/handlers/auth/AuthPlaylistHandlers.java b/src/main/java/me/kavin/piped/server/handlers/auth/AuthPlaylistHandlers.java index 042df97..dafcacb 100644 --- a/src/main/java/me/kavin/piped/server/handlers/auth/AuthPlaylistHandlers.java +++ b/src/main/java/me/kavin/piped/server/handlers/auth/AuthPlaylistHandlers.java @@ -240,19 +240,27 @@ public class AuthPlaylistHandlers { .orElse(null); if (video == null) { - StreamInfo info = StreamInfo.getInfo("https://www.youtube.com/watch?v=" + videoId); + try { + StreamInfo info = StreamInfo.getInfo("https://www.youtube.com/watch?v=" + videoId); - String channelId = StringUtils.substringAfter(info.getUploaderUrl(), "/channel/"); + String channelId = StringUtils.substringAfter(info.getUploaderUrl(), "/channel/"); - var channel = DatabaseHelper.getChannelFromId(s, channelId); + var channel = DatabaseHelper.getChannelFromId(s, channelId); - if (channel == null) { - channel = DatabaseHelper.saveChannel(channelId); + if (channel == null) { + channel = DatabaseHelper.saveChannel(channelId); + } + + video = new PlaylistVideo(videoId, info.getName(), info.getThumbnailUrl(), info.getDuration(), channel); + + s.persist(video); + } catch (Exception e) { + // only return an error if the unavailable video is the only one in the request + if (videoIds.size() == 1) { + return mapper.writeValueAsBytes(mapper.createObjectNode() + .put("error", "The video is either private or got deleted")); + } } - - video = new PlaylistVideo(videoId, info.getName(), info.getThumbnailUrl(), info.getDuration(), channel); - - s.persist(video); } if (playlist.getVideos().isEmpty()) playlist.setThumbnail(video.getThumbnail()); From 48ee0bba859b457b88a995ae97ae1d5c570c74e8 Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Mon, 2 Jan 2023 20:59:57 +0000 Subject: [PATCH 2/2] Clean code and throw error if no video was added. --- .../handlers/auth/AuthPlaylistHandlers.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main/java/me/kavin/piped/server/handlers/auth/AuthPlaylistHandlers.java b/src/main/java/me/kavin/piped/server/handlers/auth/AuthPlaylistHandlers.java index dafcacb..253598d 100644 --- a/src/main/java/me/kavin/piped/server/handlers/auth/AuthPlaylistHandlers.java +++ b/src/main/java/me/kavin/piped/server/handlers/auth/AuthPlaylistHandlers.java @@ -232,6 +232,8 @@ public class AuthPlaylistHandlers { var videos = playlist.getVideos(); + boolean added = false; + for (String videoId : videoIds) { if (StringUtils.isEmpty(videoId)) continue; @@ -255,19 +257,24 @@ public class AuthPlaylistHandlers { s.persist(video); } catch (Exception e) { - // only return an error if the unavailable video is the only one in the request - if (videoIds.size() == 1) { - return mapper.writeValueAsBytes(mapper.createObjectNode() - .put("error", "The video is either private or got deleted")); - } + ExceptionHandler.handle(e); + continue; } } if (playlist.getVideos().isEmpty()) playlist.setThumbnail(video.getThumbnail()); + added = true; + videos.add(video); } + if (!added) { + // only return an error if no videos were added + return mapper.writeValueAsBytes(mapper.createObjectNode() + .put("error", "Unable to add any videos, since they were unable to be fetched")); + } + var tr = s.beginTransaction(); s.merge(playlist); tr.commit();