chzzk
Version:
네이버 라이브 스트리밍 서비스 CHZZK의 비공식 API 라이브러리
78 lines (77 loc) • 4.14 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChzzkClient = void 0;
const chat_1 = require("./chat");
const api_1 = require("./api");
const const_1 = require("./const");
const chat_2 = require("./api/chat");
class ChzzkClient {
constructor(options = {}) {
this.live = new api_1.ChzzkLive(this);
this.search = new api_1.ChzzkSearch(this);
this.manage = new api_1.ChzzkManage(this);
options.baseUrls = options.baseUrls || const_1.DEFAULT_BASE_URLS;
options.userAgent = options.userAgent || "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36";
this.options = options;
}
get hasAuth() {
return !!(this.options.nidAuth && this.options.nidSession);
}
get chat() {
const func = (options) => {
if (typeof options == "string") {
if (options.length != 6) {
throw new Error("Invalid chat channel ID");
}
return chat_1.ChzzkChat.fromClient(options, this);
}
return new chat_1.ChzzkChat(Object.assign({ client: this, baseUrls: this.options.baseUrls, pollInterval: 30 * 1000 }, options));
};
func.accessToken = (chatChannelId) => __awaiter(this, void 0, void 0, function* () { return (0, chat_2.accessToken)(this, chatChannelId); });
func.profileCard = (chatChannelId, userIdHash) => __awaiter(this, void 0, void 0, function* () { return (0, chat_2.profileCard)(this, chatChannelId, userIdHash); });
func.notice = (chatChannelId, options) => __awaiter(this, void 0, void 0, function* () { return (0, chat_2.notice)(this, chatChannelId, options); });
return func;
}
user() {
return __awaiter(this, void 0, void 0, function* () {
return this.fetch(`${this.options.baseUrls.gameBaseUrl}/v1/user/getUserStatus`)
.then(r => r.json())
.then(data => { var _a; return (_a = data['content']) !== null && _a !== void 0 ? _a : null; });
});
}
channel(channelId) {
return __awaiter(this, void 0, void 0, function* () {
return this.fetch(`/service/v1/channels/${channelId}`)
.then(r => r.json())
.then(data => data['content'])
.then(content => (content === null || content === void 0 ? void 0 : content.channelId) ? content : null);
});
}
video(videoNo) {
return __awaiter(this, void 0, void 0, function* () {
return this.fetch(`/service/v1/videos/${videoNo}`)
.then(r => r.json())
.then(r => { var _a; return (_a = r['content']) !== null && _a !== void 0 ? _a : null; });
});
}
fetch(pathOrUrl, options) {
const headers = Object.assign({ "User-Agent": this.options.userAgent }, ((options === null || options === void 0 ? void 0 : options.headers) || {}));
if (this.hasAuth) {
headers["Cookie"] = `NID_AUT=${this.options.nidAuth}; NID_SES=${this.options.nidSession}`;
}
if (pathOrUrl.startsWith("/") && !pathOrUrl.startsWith(this.options.baseUrls.gameBaseUrl)) {
pathOrUrl = `${this.options.baseUrls.chzzkBaseUrl}${pathOrUrl}`;
}
return fetch(pathOrUrl, Object.assign(Object.assign({}, options), { headers }));
}
}
exports.ChzzkClient = ChzzkClient;
;