polyv-live-cli
Version:
CLI tool for managing PolyV live streaming services.
201 lines • 9.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PlaybackServiceSdk = void 0;
const errors_1 = require("../utils/errors");
const sdk_1 = require("../sdk");
class PlaybackServiceSdk {
constructor(authConfig, serviceConfig) {
this.authConfig = authConfig;
this.config = serviceConfig;
}
async getPlaybackList(options) {
this.validateListOptions(options);
const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl);
const params = {};
if (options.page !== undefined) {
params.page = options.page;
}
if (options.pageSize !== undefined) {
params.pageSize = options.pageSize;
}
if (options.listType !== undefined) {
params.listType = options.listType;
}
const result = await client.channel.getPlaybackList(options.channelId, params);
return {
contents: (result.contents || []).map((item) => {
const displayItem = {
videoId: item.videoId,
channelId: String(item.channelId),
title: item.title || '',
duration: this.formatDuration(item.duration),
};
if (item.videoPoolId !== undefined)
displayItem.videoPoolId = item.videoPoolId;
if (item.userId !== undefined)
displayItem.userId = item.userId;
if (item.firstImage !== undefined)
displayItem.firstImage = item.firstImage;
if (item.myBr !== undefined)
displayItem.myBr = item.myBr;
if (item.seed !== undefined)
displayItem.seed = item.seed;
if (item.createdTime !== undefined)
displayItem.createdTime = item.createdTime;
if (item.lastModified !== undefined)
displayItem.lastModified = item.lastModified;
if (item.asDefault !== undefined)
displayItem.asDefault = item.asDefault;
if (item.status !== undefined)
displayItem.status = item.status;
if (item.watchUrl !== undefined)
displayItem.watchUrl = item.watchUrl;
if (item.liveType !== undefined)
displayItem.liveType = item.liveType;
if (item.origin !== undefined)
displayItem.origin = item.origin;
return displayItem;
}),
pageNumber: result.pageNumber,
pageSize: result.pageSize,
totalItems: result.totalItems || result.total || 0,
totalPages: result.totalPages || 0,
};
}
async listPlaybackSettings(channelIds) {
const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl);
return client.v4Channel.listPlaybackSettings({ channelIds });
}
async getPlaybackVideoInfo(channelIds) {
const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl);
return client.v4Channel.getPlaybackVideoInfo({ channelIds });
}
async updateChannelSubtitles(channelId, body) {
const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl);
await client.v4Channel.updateChannelSubtitleBatch({ channelId, body });
}
async getPlaybackEnabled(channelId) {
const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl);
return client.channel.getPlaybackEnabled(channelId);
}
async setPlaybackEnabled(params) {
const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl);
return client.channel.setUserPlaybackEnabled(params);
}
async addVodPlayback(params) {
const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl);
return client.channel.addVodPlaybackToLibrary(params);
}
async updatePlaybackTitle(channelId, videoId, title) {
const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl);
return client.channel.updatePlaybackTitle(channelId, videoId, title);
}
async movePlaybackVideo(params) {
const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl);
await client.channel.movePlaybackVideo(params);
}
async sortPlaybackVideos(params) {
const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl);
await client.channel.sortPlaybackVideos(params);
}
formatDuration(seconds) {
if (seconds === undefined || seconds === null) {
return '00:00:00';
}
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
const secs = seconds % 60;
return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
}
validateListOptions(options) {
const errors = [];
if (!options.channelId || typeof options.channelId !== 'string' || options.channelId.trim().length === 0) {
errors.push('channelId is required and must be a non-empty string');
}
if (options.page !== undefined && (!Number.isInteger(options.page) || options.page < 1)) {
errors.push('page must be a positive integer');
}
if (options.pageSize !== undefined && (!Number.isInteger(options.pageSize) || options.pageSize < 1)) {
errors.push('pageSize must be a positive integer');
}
if (options.listType !== undefined && !['playback', 'vod'].includes(options.listType)) {
errors.push('listType must be either "playback" or "vod"');
}
if (errors.length > 0) {
throw new errors_1.PolyVValidationError(`Playback list options validation failed: ${errors.join(', ')}`, 'options', options, 'validation_failed');
}
}
async deletePlayback(channelId, videoId, listType) {
this.validateDeleteParams(channelId, videoId, listType);
const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl);
const result = await client.channel.deletePlayback(channelId, videoId, listType);
return result;
}
validateDeleteParams(channelId, videoId, listType) {
const errors = [];
if (!channelId || typeof channelId !== 'string' || channelId.trim().length === 0) {
errors.push('channelId is required and must be a non-empty string');
}
if (!videoId || typeof videoId !== 'string' || videoId.trim().length === 0) {
errors.push('videoId is required and must be a non-empty string');
}
if (listType !== undefined && !['playback', 'vod'].includes(listType)) {
errors.push('listType must be either "playback" or "vod"');
}
if (errors.length > 0) {
throw new errors_1.PolyVValidationError(`Playback delete parameters validation failed: ${errors.join(', ')}`, 'params', { channelId, videoId, listType }, 'validation_failed');
}
}
async mergePlayback(channelId, fileIds, fileName) {
this.validateMergeParams(channelId, fileIds);
const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl);
const params = { fileIds };
if (fileName !== undefined) {
params.fileName = fileName;
}
const result = await client.channel.recordFileMerge(channelId, params);
return result;
}
async mergePlaybackAsync(channelId, fileIds, options) {
this.validateMergeParams(channelId, fileIds);
const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl);
const params = { fileIds };
if (options?.fileName !== undefined) {
params.fileName = options.fileName;
}
if (options?.callbackUrl !== undefined) {
params.callbackUrl = options.callbackUrl;
}
if (options?.autoConvert !== undefined) {
params.autoConvert = options.autoConvert ? 'Y' : 'N';
}
if (options?.mergeMp4 !== undefined) {
params.mergeMp4 = options.mergeMp4 ? 'Y' : 'N';
}
if (options?.orderByCustom !== undefined) {
params.orderByCustom = options.orderByCustom ? 'Y' : 'N';
}
await client.channel.recordFileMerge(channelId, params);
return true;
}
validateMergeParams(channelId, fileIds) {
const errors = [];
if (!channelId || typeof channelId !== 'string' || channelId.trim().length === 0) {
errors.push('channelId is required and must be a non-empty string');
}
if (!Array.isArray(fileIds) || fileIds.length === 0) {
errors.push('fileIds is required and must be a non-empty array');
}
else {
const invalidFileIds = fileIds.filter(id => !id || typeof id !== 'string' || id.trim().length === 0);
if (invalidFileIds.length > 0) {
errors.push('all fileIds must be non-empty strings');
}
}
if (errors.length > 0) {
throw new errors_1.PolyVValidationError(`Playback merge parameters validation failed: ${errors.join(', ')}`, 'params', { channelId, fileIds }, 'validation_failed');
}
}
}
exports.PlaybackServiceSdk = PlaybackServiceSdk;
//# sourceMappingURL=playback.service.sdk.js.map