-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from RomanMager/issue-18
Add ability to set Playlist visibility
- Loading branch information
Showing
16 changed files
with
394 additions
and
280 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
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,10 @@ | ||
package com.ypm.constant; | ||
|
||
public enum Part { | ||
SNIPPET, STATUS; | ||
|
||
@Override | ||
public String toString() { | ||
return this.name().toLowerCase(); | ||
} | ||
} |
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,10 @@ | ||
package com.ypm.constant; | ||
|
||
public enum PrivacyStatus { | ||
PRIVATE, PUBLIC, UNLISTED; | ||
|
||
@Override | ||
public String toString() { | ||
return this.name().toLowerCase(); | ||
} | ||
} |
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
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,8 @@ | ||
package com.ypm.dto; | ||
|
||
import java.util.Optional; | ||
|
||
public record PlaylistDto(String title, | ||
Optional<String> description, | ||
String status) { | ||
} |
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 |
---|---|---|
@@ -1,7 +1,10 @@ | ||
package com.ypm.dto.request; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.ypm.dto.PlaylistDto; | ||
|
||
import java.util.List; | ||
|
||
public record MergePlayListsRequest(String mergedPlayListTitle, | ||
public record MergePlayListsRequest(@JsonProperty(value = "playlistDetails") PlaylistDto playlistDto, | ||
List<String> playListsIds, | ||
boolean deleteAfterMerge) { } |
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 |
---|---|---|
@@ -1,101 +1,25 @@ | ||
package com.ypm.service; | ||
|
||
import com.google.api.services.youtube.YouTube; | ||
import com.google.api.services.youtube.model.Playlist; | ||
import com.google.api.services.youtube.model.PlaylistItem; | ||
import com.google.api.services.youtube.model.PlaylistSnippet; | ||
import com.ypm.exception.PlayListNotFoundException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import com.ypm.dto.PlaylistDto; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class PlayListService { | ||
public interface PlayListService { | ||
|
||
private final YouTube youTubeClient; | ||
private final VideoService videoService; | ||
List<Playlist> getPlayLists(String accessToken) throws IOException; | ||
|
||
public List<Playlist> getPlayLists(String accessToken) throws IOException { | ||
return youTubeClient | ||
.playlists() | ||
.list(List.of("snippet")) | ||
.setAccessToken(accessToken) | ||
.setMine(true) | ||
.execute() | ||
.getItems(); | ||
} | ||
Playlist createPlayList(String accessToken, PlaylistDto playlistDto) throws IOException; | ||
|
||
public Playlist mergePlayLists(String accessToken, String mergedPlayListName, | ||
List<String> playListsIds, | ||
boolean deleteAfterMerge) throws IOException { | ||
Playlist getPlayListById(String accessToken, String playListId) throws IOException; | ||
|
||
Playlist mergedPlayList = createPlayList(accessToken, mergedPlayListName); | ||
void deletePlayList(String accessToken, String playListId) throws IOException; | ||
|
||
for (String playListId : playListsIds) { | ||
List<PlaylistItem> videos = videoService.getPlayListVideos(accessToken, playListId); | ||
List<String> videosIds = videos.stream().map(PlaylistItem::getId).toList(); | ||
videoService.moveVideos(accessToken, playListId, mergedPlayList.getId(), videosIds); | ||
if (deleteAfterMerge) deletePlayList(accessToken, playListId); | ||
} | ||
Playlist mergePlayLists(String accessToken, PlaylistDto playlistDto, | ||
List<String> playListsIds, | ||
boolean deleteAfterMerge) throws IOException; | ||
|
||
return mergedPlayList; | ||
} | ||
|
||
public Playlist createPlayList(String accessToken, | ||
String title) throws IOException { | ||
PlaylistSnippet playList = new PlaylistSnippet(); | ||
playList.setTitle(title); | ||
return createPlayList(accessToken, playList); | ||
} | ||
|
||
public Playlist createPlayList(String accessToken, | ||
PlaylistSnippet playlistSnippet) throws IOException { | ||
Playlist playlist = new Playlist(); | ||
playlist.setSnippet(playlistSnippet); | ||
|
||
return youTubeClient | ||
.playlists() | ||
.insert(List.of("snippet"), playlist) | ||
.setAccessToken(accessToken) | ||
.execute(); | ||
} | ||
|
||
public Playlist updatePlayListTitle(String accessToken, String playListId, | ||
String newTitle) throws IOException { | ||
|
||
Playlist playlistToEdit = getPlayListById(accessToken, playListId); | ||
playlistToEdit.getSnippet().setTitle(newTitle); | ||
|
||
return youTubeClient | ||
.playlists() | ||
.update(List.of("snippet"), playlistToEdit) | ||
.setAccessToken(accessToken) | ||
.execute(); | ||
} | ||
|
||
public Playlist getPlayListById(String accessToken, String playListId) throws IOException { | ||
return youTubeClient | ||
.playlists() | ||
.list(List.of("snippet")) | ||
.setId(List.of(playListId)) | ||
.setAccessToken(accessToken) | ||
.execute() | ||
.getItems() | ||
.stream() | ||
.findFirst() | ||
.orElseThrow(() -> | ||
new PlayListNotFoundException(playListId, | ||
"At some stage we were not able to find one of the request-related playlists.")); | ||
} | ||
|
||
public void deletePlayList(String accessToken, String playListId) throws IOException { | ||
youTubeClient | ||
.playlists() | ||
.delete(playListId) | ||
.setAccessToken(accessToken) | ||
.execute(); | ||
} | ||
Playlist updatePlayListTitle(String accessToken, String playListId, | ||
String newTitle) throws IOException; | ||
} |
Oops, something went wrong.