mirror of
https://github.com/LuanRT/YouTube.js.git
synced 2026-06-17 19:42:14 +00:00
* feat: add core comments section classes * chore: update type declarations * chore: fix linter warnings * style: fix linter * chore: update tests * chore(tests): fix typo * chore(tests): fix typo x2 * fix(tests): `getReplies()` method is only present in `CommentThread` and not `Comment` * chore(tests): fix comment id path * chore(tests): remove outdated code * chore(tests): fix results path * chore: enforce code style * chore: update type declarations * docs: add examples and documentation * chore(docs): fix paths * chore(docs): fix more paths * chore(docs): fix `Comments.js` path * chore(docs): fix typo * chore(docs): mention example file * chore(examples): fix imports * chore(examples): fix typo
118 lines
2.8 KiB
JavaScript
118 lines
2.8 KiB
JavaScript
'use strict';
|
|
|
|
const Utils = require('../utils/Utils');
|
|
|
|
/** @namespace */
|
|
class PlaylistManager {
|
|
#actions;
|
|
|
|
/**
|
|
* @param {import('../Actions')} actions
|
|
*/
|
|
constructor (actions) {
|
|
this.#actions = actions;
|
|
}
|
|
|
|
/**
|
|
* API
|
|
*
|
|
* @typedef {{ success: boolean, status_code: number, playlist_id: string, data: object }} Response
|
|
*/
|
|
|
|
/**
|
|
* Creates a playlist.
|
|
*
|
|
* @param {string} title
|
|
* @param {string[]} video_ids
|
|
* @returns {Promise.<Response>}
|
|
*/
|
|
async create(title, video_ids) {
|
|
Utils.throwIfMissing({ title, video_ids });
|
|
|
|
const response = await this.#actions.playlist('playlist/create', { title, ids: video_ids });
|
|
|
|
return {
|
|
success: response.success,
|
|
status_code: response.status_code,
|
|
playlist_id: response.data.playlistId,
|
|
data: response.data
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Deletes a given playlist.
|
|
*
|
|
* @param {string} playlist_id
|
|
* @returns {Promise.<Response>}
|
|
*/
|
|
async delete(playlist_id) {
|
|
Utils.throwIfMissing({ playlist_id });
|
|
|
|
const response = await this.#actions.playlist('playlist/delete', { playlist_id });
|
|
|
|
return {
|
|
playlist_id,
|
|
success: response.success,
|
|
status_code: response.status_code,
|
|
data: response.data
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Adds videos to a given playlist.
|
|
*
|
|
* @param {string} playlist_id
|
|
* @param {Array.<string>} video_ids
|
|
* @returns {Promise.<Response>}
|
|
*/
|
|
async addVideos(playlist_id, video_ids) {
|
|
Utils.throwIfMissing({ playlist_id, video_ids });
|
|
|
|
const response = await this.#actions.playlist('browse/edit_playlist', {
|
|
ids: video_ids,
|
|
action: 'ACTION_ADD_VIDEO',
|
|
playlist_id
|
|
});
|
|
|
|
return {
|
|
playlist_id,
|
|
success: response.success,
|
|
status_code: response.status_code,
|
|
data: response.data
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Removes videos from a given playlist.
|
|
*
|
|
* @param {string} playlist_id
|
|
* @param {Array.<string>} video_ids
|
|
* @returns {Promise.<Response>}
|
|
*/
|
|
async removeVideos(playlist_id, video_ids) {
|
|
Utils.throwIfMissing({ playlist_id, video_ids });
|
|
|
|
const plinfo = await this.#actions.browse(`VL${playlist_id}`);
|
|
|
|
const list = Utils.findNode(plinfo.data, 'contents', 'contents', 13, false);
|
|
if (!list.isEditable) throw new Utils.InnertubeError('This playlist cannot be edited.', playlist_id);
|
|
|
|
const videos = list.contents.filter((item) => video_ids.includes(item.playlistVideoRenderer.videoId));
|
|
const set_video_ids = videos.map((video) => video.playlistVideoRenderer.setVideoId);
|
|
|
|
const response = await this.#actions.playlist('browse/edit_playlist', {
|
|
ids: set_video_ids,
|
|
action: 'ACTION_REMOVE_VIDEO',
|
|
playlist_id
|
|
});
|
|
|
|
return {
|
|
success: response.success,
|
|
status_code: response.status_code,
|
|
playlist_id,
|
|
data: response.data
|
|
};
|
|
}
|
|
}
|
|
|
|
module.exports = PlaylistManager; |