UNPKG

polyv-live-cli

Version:

CLI tool for managing PolyV live streaming services.

105 lines 4.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PromotionHandler = void 0; const base_handler_1 = require("./base.handler"); const errors_1 = require("../utils/errors"); const api_command_1 = require("../utils/api-command"); const promotion_service_1 = require("../services/promotion-service"); class PromotionHandler extends base_handler_1.BaseHandler { constructor(authConfig, serviceConfig) { super(); this.promotionService = new promotion_service_1.PromotionServiceSdk(authConfig, serviceConfig); } async listPromotions(options) { const format = options.output || 'table'; this.validateListOptions(options); const promotions = await this.promotionService.listPopularizations(options.channelId); if (format === 'json') { this.displayData(promotions, 'json'); } else { this.displayPromotionsTable(promotions); } } async createPromotions(options) { const format = options.output || 'table'; this.validateCreateOptions(options); await (0, api_command_1.confirmWrite)(options.force, `Create ${options.names.length} promotion channel(s) for channel ${options.channelId}?`); const createdPromotions = await this.promotionService.batchCreatePopularizations({ channelId: options.channelId, names: options.names, }); if (format === 'json') { this.displayData(createdPromotions, 'json'); } else { this.displayCreatedPromotionsTable(createdPromotions); } } validateListOptions(options) { if (!options.channelId || options.channelId.trim() === '') { throw new errors_1.PolyVValidationError('channelId is required (频道ID是必需的)', 'channelId', options.channelId, 'required'); } if (options.output && options.output !== 'table' && options.output !== 'json') { throw new errors_1.PolyVValidationError('output format must be table or json (输出格式必须是 table 或 json)', 'output', options.output, 'invalid_format'); } } validateCreateOptions(options) { if (!options.channelId || options.channelId.trim() === '') { throw new errors_1.PolyVValidationError('channelId is required (频道ID是必需的)', 'channelId', options.channelId, 'required'); } if (!options.names || options.names.length === 0) { throw new errors_1.PolyVValidationError('names is required and cannot be empty (推广渠道名称是必需的且不能为空)', 'names', options.names, 'required'); } for (const name of options.names) { if (!name || name.trim() === '') { throw new errors_1.PolyVValidationError('names cannot contain empty strings (推广渠道名称不能包含空字符串)', 'names', options.names, 'invalid_value'); } } for (const name of options.names) { if (name.length > 20) { throw new errors_1.PolyVValidationError('each name cannot exceed 20 characters (每个名称不能超过20个字符)', 'names', options.names, 'invalid_length'); } } } displayPromotionsTable(promotions) { if (promotions.length === 0) { console.log('No promotions found (未找到推广渠道)'); return; } const tableData = promotions.map((promotion) => { return { '推广ID': promotion.promoteId, '推广名称': promotion.popularizationName, '访问次数': promotion.visitsNum, '观看人数': promotion.viewerNum, '创建时间': this.formatTimestamp(promotion.createdTime), }; }); this.displayAsTable(tableData); } displayCreatedPromotionsTable(promotions) { if (promotions.length === 0) { console.log('No promotions created (未创建推广渠道)'); return; } const tableData = promotions.map((promotion) => { return { '推广ID': promotion.promoteId, '推广名称': promotion.popularizationName, }; }); this.displayAsTable(tableData); } formatTimestamp(timestamp) { const date = new Date(timestamp); const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, '0'); const day = String(date.getDate()).padStart(2, '0'); const hours = String(date.getHours()).padStart(2, '0'); const minutes = String(date.getMinutes()).padStart(2, '0'); return `${year}-${month}-${day} ${hours}:${minutes}`; } } exports.PromotionHandler = PromotionHandler; //# sourceMappingURL=promotion.handler.js.map