-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: api support for playlist bookmarks
- Loading branch information
Showing
9 changed files
with
395 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
src/main/java/me/kavin/piped/server/handlers/auth/PlaylistBookmarkHandlers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package me.kavin.piped.server.handlers.auth; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import it.unimi.dsi.fastutil.objects.ObjectArrayList; | ||
import me.kavin.piped.utils.DatabaseHelper; | ||
import me.kavin.piped.utils.DatabaseSessionFactory; | ||
import me.kavin.piped.utils.ExceptionHandler; | ||
import me.kavin.piped.utils.obj.db.PlaylistBookmark; | ||
import me.kavin.piped.utils.obj.db.User; | ||
import me.kavin.piped.utils.resp.AcceptedResponse; | ||
import me.kavin.piped.utils.resp.AuthenticationFailureResponse; | ||
import me.kavin.piped.utils.resp.BookmarkedStatusResponse; | ||
import me.kavin.piped.utils.resp.InvalidRequestResponse; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.hibernate.Session; | ||
import org.schabi.newpipe.extractor.exceptions.ExtractionException; | ||
import org.schabi.newpipe.extractor.playlist.PlaylistInfo; | ||
|
||
import java.io.IOException; | ||
|
||
import static me.kavin.piped.consts.Constants.mapper; | ||
import static me.kavin.piped.utils.URLUtils.*; | ||
|
||
public class PlaylistBookmarkHandlers { | ||
public static byte[] createPlaylistBookmarkResponse(String session, String playlistId) throws IOException, ExtractionException { | ||
|
||
if (StringUtils.isBlank(session) || StringUtils.isBlank(playlistId)) | ||
ExceptionHandler.throwErrorResponse(new InvalidRequestResponse("session and name are required parameters")); | ||
|
||
User user = DatabaseHelper.getUserFromSession(session); | ||
|
||
if (user == null) ExceptionHandler.throwErrorResponse(new AuthenticationFailureResponse()); | ||
|
||
try (Session s = DatabaseSessionFactory.createSession()) { | ||
if (DatabaseHelper.isBookmarked(s, user, playlistId)) { | ||
var bookmark = DatabaseHelper.getPlaylistBookmarkFromPlaylistId(s, user, playlistId); | ||
return mapper.writeValueAsBytes(createPlaylistBookmarkResponseItem(bookmark)); | ||
} | ||
|
||
final PlaylistInfo info = PlaylistInfo.getInfo("https://www.youtube.com/playlist?list=" + playlistId); | ||
|
||
var playlistBookmark = new PlaylistBookmark(playlistId, info.getName(), info.getDescription().getContent(), getLastThumbnail(info.getThumbnails()), info.getUploaderName(), substringYouTube(info.getUploaderUrl()), getLastThumbnail(info.getUploaderAvatars()), info.getStreamCount(), user); | ||
|
||
var tr = s.beginTransaction(); | ||
s.persist(playlistBookmark); | ||
tr.commit(); | ||
|
||
ObjectNode response = createPlaylistBookmarkResponseItem(playlistBookmark); | ||
|
||
return mapper.writeValueAsBytes(response); | ||
} | ||
} | ||
|
||
public static byte[] deletePlaylistBookmarkResponse(String session, String playlistId) throws IOException { | ||
|
||
if (StringUtils.isBlank(session) || StringUtils.isBlank(playlistId)) | ||
ExceptionHandler.throwErrorResponse(new InvalidRequestResponse("session and playlistId are required parameters")); | ||
|
||
User user = DatabaseHelper.getUserFromSession(session); | ||
|
||
if (user == null) ExceptionHandler.throwErrorResponse(new AuthenticationFailureResponse()); | ||
|
||
try (Session s = DatabaseSessionFactory.createSession()) { | ||
|
||
DatabaseHelper.deletePlaylistBookmark(s, user, playlistId); | ||
|
||
return mapper.writeValueAsBytes(new AcceptedResponse()); | ||
} | ||
} | ||
|
||
public static byte[] playlistBookmarksResponse(String session) throws IOException { | ||
|
||
if (StringUtils.isBlank(session)) | ||
ExceptionHandler.throwErrorResponse(new InvalidRequestResponse("session is a required parameter")); | ||
|
||
User user = DatabaseHelper.getUserFromSession(session); | ||
|
||
if (user == null) ExceptionHandler.throwErrorResponse(new AuthenticationFailureResponse()); | ||
|
||
try (Session s = DatabaseSessionFactory.createSession()) { | ||
|
||
var responseArray = new ObjectArrayList<>(); | ||
var playlistBookmarks = DatabaseHelper.getPlaylistBookmarks(s, user); | ||
|
||
for (PlaylistBookmark bookmark : playlistBookmarks) { | ||
responseArray.add(createPlaylistBookmarkResponseItem(bookmark)); | ||
} | ||
|
||
return mapper.writeValueAsBytes(responseArray); | ||
} | ||
} | ||
|
||
public static byte[] isBookmarkedResponse(String session, String playlistId) throws IOException { | ||
|
||
if (StringUtils.isBlank(session) || StringUtils.isBlank(playlistId)) | ||
ExceptionHandler.throwErrorResponse(new InvalidRequestResponse("session and playlistId are required parameters")); | ||
|
||
User user = DatabaseHelper.getUserFromSession(session); | ||
|
||
if (user == null) ExceptionHandler.throwErrorResponse(new AuthenticationFailureResponse()); | ||
|
||
try (Session s = DatabaseSessionFactory.createSession()) { | ||
boolean isBookmarked = DatabaseHelper.isBookmarked(s, user, playlistId); | ||
|
||
return mapper.writeValueAsBytes(new BookmarkedStatusResponse(isBookmarked)); | ||
} | ||
} | ||
|
||
private static ObjectNode createPlaylistBookmarkResponseItem(PlaylistBookmark bookmark) { | ||
ObjectNode node = mapper.createObjectNode(); | ||
node.put("playlistId", String.valueOf(bookmark.getPlaylistId())); | ||
node.put("name", bookmark.getName()); | ||
node.put("shortDescription", bookmark.getShortDescription()); | ||
node.put("thumbnailUrl", rewriteURL(bookmark.getThumbnailUrl())); | ||
node.put("uploader", bookmark.getUploader()); | ||
node.put("uploaderUrl", bookmark.getUploaderUrl()); | ||
node.put("uploaderAvatar", rewriteURL(bookmark.getUploaderAvatar())); | ||
node.put("videos", bookmark.getVideoCount()); | ||
return node; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
src/main/java/me/kavin/piped/utils/obj/db/PlaylistBookmark.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package me.kavin.piped.utils.obj.db; | ||
|
||
import jakarta.persistence.*; | ||
|
||
@Entity | ||
@Table(name = "playlist_bookmarks", indexes = {@Index(columnList = "playlist_id", name = "playlist_bookmarks_playlist_id_idx"), @Index(columnList = "owner", name = "playlist_bookmarks_owner_idx")}) | ||
public class PlaylistBookmark { | ||
|
||
public PlaylistBookmark() { | ||
} | ||
|
||
public PlaylistBookmark(String playlist_id, String name, String short_description, String thumbnail_url, String uploader, String uploader_url, String uploader_avatar, long video_count, User owner) { | ||
this.playlist_id = playlist_id; | ||
this.name = name; | ||
this.short_description = short_description; | ||
this.thumbnail_url = thumbnail_url; | ||
this.uploader = uploader; | ||
this.uploader_url = uploader_url; | ||
this.uploader_avatar = uploader_avatar; | ||
this.video_count = video_count; | ||
this.owner = owner; | ||
} | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private long id; | ||
|
||
@Column(name = "playlist_id", nullable = false) | ||
private String playlist_id; | ||
|
||
@Column(name = "name", length = 200) | ||
private String name; | ||
|
||
@Column(name = "short_description", length = 300) | ||
private String short_description; | ||
|
||
@Column(name = "thumbnail_url", length = 300) | ||
private String thumbnail_url; | ||
|
||
@Column(name = "uploader", length = 100) | ||
private String uploader; | ||
|
||
@Column(name = "uploader_url", length = 100) | ||
private String uploader_url; | ||
|
||
@Column(name = "uploader_avatar", length = 150) | ||
private String uploader_avatar; | ||
|
||
@Column(name = "video_count") | ||
private long video_count; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "owner", nullable = false) | ||
private User owner; | ||
|
||
public long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getPlaylistId() { | ||
return playlist_id; | ||
} | ||
|
||
public void setPlaylistId(String playlist_id) { | ||
this.playlist_id = playlist_id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getShortDescription() { | ||
return short_description; | ||
} | ||
|
||
public void setShortDescription(String short_description) { | ||
this.short_description = short_description; | ||
} | ||
|
||
public String getThumbnailUrl() { | ||
return thumbnail_url; | ||
} | ||
|
||
public void setThumbnailUrl(String thumbnailUrl) { | ||
this.thumbnail_url = thumbnailUrl; | ||
} | ||
|
||
public String getUploader() { | ||
return uploader; | ||
} | ||
|
||
public void setUploader(String uploader) { | ||
this.uploader = uploader; | ||
} | ||
|
||
public String getUploaderUrl() { | ||
return uploader_url; | ||
} | ||
|
||
public void setUploaderUrl(String uploaderUrl) { | ||
this.uploader_url = uploaderUrl; | ||
} | ||
|
||
public String getUploaderAvatar() { | ||
return uploader_avatar; | ||
} | ||
|
||
public void setUploaderAvatar(String uploaderAvatar) { | ||
this.uploader_avatar = uploaderAvatar; | ||
} | ||
|
||
public long getVideoCount() { | ||
return video_count; | ||
} | ||
|
||
public void setVideoCount(long videoCount) { | ||
this.video_count = videoCount; | ||
} | ||
|
||
public User getOwner() { | ||
return owner; | ||
} | ||
|
||
public void setOwner(User owner) { | ||
this.owner = owner; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/me/kavin/piped/utils/resp/BookmarkedStatusResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package me.kavin.piped.utils.resp; | ||
|
||
public class BookmarkedStatusResponse { | ||
public boolean bookmarked; | ||
|
||
public BookmarkedStatusResponse(boolean bookmarked) { | ||
this.bookmarked = bookmarked; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.