addPlaylistItems method
Add songs to an existing playlist.
playlistId
Playlist id.videoIds
List of Video ids.sourcePlaylist
Playlist id of a playlist to add to the current playlist (no duplicate check).duplicates
Iftrue
, duplicates will be added. Iffalse
, an error will be returned if there are duplicates (no items are added to the playlist).
Returns status String and a Map containing the new setVideoId
for each videoId
or full response.
Implementation
Future<dynamic> addPlaylistItems(
String playlistId, {
List<String>? videoIds,
String? sourcePlaylist,
bool duplicates = false,
}) async {
checkAuth();
final body = {'playlistId': validatePlaylistId(playlistId), 'actions': []};
if ((videoIds == null || videoIds.isEmpty) && sourcePlaylist == null) {
throw YTMusicUserError(
'You must provide either videoIds or a source_playlist to add to the playlist',
);
}
if (videoIds != null) {
for (final videoId in videoIds) {
final action = {'action': 'ACTION_ADD_VIDEO', 'addedVideoId': videoId};
if (duplicates) action['dedupeOption'] = 'DEDUPE_OPTION_SKIP';
(body['actions']! as List).add(action);
}
}
if (sourcePlaylist != null) {
(body['actions']! as List).add({
'action': 'ACTION_ADD_PLAYLIST',
'addedFullListId': sourcePlaylist,
});
// add an empty ACTION_ADD_VIDEO because otherwise
// YTM doesn't return the Map that maps videoIds to their new setVideoIds
if (videoIds == null) {
(body['actions']! as List).add({
'action': 'ACTION_ADD_VIDEO',
'addedVideoId': null,
});
}
}
const endpoint = 'browse/edit_playlist';
final response = await sendRequest(endpoint, body);
if (response.containsKey('status') &&
(response['status'] as JsonMap).containsKey('SUCCEEDED')) {
final resultMap =
(response['playlistEditResults'] as List<JsonMap>)
.map((r) => r['playlistEditVideoAddedResultData'])
.toList();
return {'status': response['status'], 'playlistEditResults': resultMap};
} else {
return response;
}
}