umbot
Version:
Universal bot(vk, telegram, viber) or skills for Yandex.Alisa, Маруся and sber
124 lines (123 loc) • 5 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.YandexImageRequest = void 0;
const YandexRequest_1 = require("./YandexRequest");
const mmApp_1 = require("../mmApp");
const Request_1 = require("./request/Request");
class YandexImageRequest extends YandexRequest_1.YandexRequest {
STANDARD_URL = 'https://dialogs.yandex.net/api/v1/';
skillId;
constructor(oauth = null, skillId = null) {
super(oauth);
this.skillId = skillId || mmApp_1.mmApp.params.app_id || null;
this._request.url = this.STANDARD_URL;
}
_getImagesUrl() {
return this.STANDARD_URL + `skills/${this.skillId}/images`;
}
async checkOutPlace() {
this._request.url = this.STANDARD_URL + 'status';
const query = await this.call();
if (query && typeof query.images.quota !== 'undefined') {
return query.images.quota;
}
this._log('YandexImageRequest.checkOutPlace() Error: Не удалось проверить занятое место!');
return null;
}
async downloadImageUrl(imageUrl) {
if (this.skillId) {
this._request.url = this._getImagesUrl();
this._request.header = Request_1.Request.HEADER_AP_JSON;
this._request.post = { url: imageUrl };
const query = await this.call();
if (query && typeof query.image.id !== 'undefined') {
return query.image;
}
else {
this._log('YandexImageRequest.downloadImageUrl() Error: Не удалось загрузить изображение с сайта!');
}
}
else {
this._log('YandexImageRequest.downloadImageUrl() Error: Не выбран навык!');
}
return null;
}
async downloadImageFile(imageDir) {
if (this.skillId) {
this._request.url = this._getImagesUrl();
this._request.header = Request_1.Request.HEADER_FORM_DATA;
this._request.attach = imageDir;
const query = await this.call();
if (query && typeof query.image.id !== 'undefined') {
return query.image;
}
else {
this._log('YandexImageRequest.downloadImageFile() Error: Не удалось загрузить изображение по пути: ' +
imageDir);
}
}
else {
this._log('YandexImageRequest.downloadImageFile() Error: Не выбран навык!');
}
return null;
}
async getLoadedImages() {
if (this.skillId) {
this._request.url = this._getImagesUrl();
const query = await this.call();
return query?.images || null;
}
else {
this._log('YandexImageRequest.getLoadedImages() Error: Не выбран навык!');
}
return null;
}
async deleteImage(imageId) {
if (this.skillId) {
if (imageId) {
this._request.url = `${this._getImagesUrl()}/${imageId}`;
this._request.customRequest = 'DELETE';
const query = await this.call();
if (query && typeof query.result !== 'undefined') {
return query.result;
}
else {
this._log('YandexImageRequest.deleteImage() Error: Не удалось удалить картинку!');
}
}
else {
this._log('YandexImageRequest.deleteImage() Error: Не выбрано изображение!');
}
}
else {
this._log('YandexImageRequest.deleteImage() Error: Не выбран навык!');
}
return null;
}
async deleteImages() {
if (this.skillId) {
const images = await this.getLoadedImages();
if (images) {
const results = await Promise.allSettled(images.map(async (image) => {
try {
await this.deleteImage(image.id);
return new Promise((resolve) => setTimeout(() => resolve(true), 100));
}
catch (e) {
this._log(`Ошибка при удалении изображения ${image.id}: ${e}`);
return false;
}
}));
return results.every((r) => r.status === 'fulfilled' && r.value === true);
}
else {
this._log('YandexImageRequest.deleteImages() Error: Не удалось получить загруженные звуки!');
}
}
else {
this._log('YandexImageRequest.deleteImages() Error: Не выбран навык!');
}
return false;
}
}
exports.YandexImageRequest = YandexImageRequest;