chzzk
Version:
네이버 라이브 스트리밍 서비스 CHZZK의 비공식 API 라이브러리
118 lines (117 loc) • 5.17 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.ChzzkSearch = exports.DEFAULT_LOUNGE_SEARCH_OPTIONS = exports.DEFAULT_SEARCH_OPTIONS = void 0;
exports.DEFAULT_SEARCH_OPTIONS = {
size: 13,
offset: 0
};
exports.DEFAULT_LOUNGE_SEARCH_OPTIONS = {
limit: 50,
offset: 0
};
class ChzzkSearch {
constructor(client) {
this.client = client;
}
videos(keyword, options = exports.DEFAULT_SEARCH_OPTIONS) {
return __awaiter(this, void 0, void 0, function* () {
return this.search("videos", keyword, options).then(r => {
return {
size: r.size,
nextOffset: r.nextOffset,
videos: r.data.map((data) => {
const video = data['video'];
const channel = data['channel'];
return Object.assign(Object.assign({}, video), { channel });
})
};
});
});
}
lives(keyword, options = exports.DEFAULT_SEARCH_OPTIONS) {
return __awaiter(this, void 0, void 0, function* () {
return this.search("lives", keyword, options).then(r => {
return {
size: r.size,
nextOffset: r.nextOffset,
lives: r.data.map((data) => {
const live = data['live'];
const channel = data['channel'];
const livePlaybackJson = live['livePlaybackJson'];
const livePlayback = livePlaybackJson ? JSON.parse(livePlaybackJson) : null;
delete live['livePlaybackJson'];
return Object.assign(Object.assign({}, live), { livePlayback,
channel });
})
};
});
});
}
channels(keyword, options = exports.DEFAULT_SEARCH_OPTIONS) {
return __awaiter(this, void 0, void 0, function* () {
return this.search("channels", keyword, options).then(r => {
return {
size: r.size,
nextOffset: r.nextOffset,
channels: r.data.map((data) => data['channel'])
};
});
});
}
autoComplete(keyword, options = exports.DEFAULT_SEARCH_OPTIONS) {
return __awaiter(this, void 0, void 0, function* () {
const params = new URLSearchParams({
keyword,
size: options.size.toString(),
offset: options.offset.toString()
}).toString();
return this.client.fetch(`${this.client.options.baseUrls.gameBaseUrl}/v2/search/lounges/auto-complete?${params}`)
.then(r => r.json())
.then(data => data['content']['data']);
});
}
lounges(keyword, options = exports.DEFAULT_LOUNGE_SEARCH_OPTIONS) {
return __awaiter(this, void 0, void 0, function* () {
const params = new URLSearchParams({
keyword,
limit: options.limit.toString(),
offset: options.offset.toString()
}).toString();
return this.client.fetch(`${this.client.options.baseUrls.gameBaseUrl}/v2/search/lounges?${params}`)
.then(r => r.json())
.then(data => data['content']);
});
}
search(type, keyword, options = exports.DEFAULT_SEARCH_OPTIONS) {
return __awaiter(this, void 0, void 0, function* () {
const params = new URLSearchParams({
keyword,
size: options.size.toString(),
offset: options.offset.toString()
}).toString();
return this.client.fetch(`/service/v1/search/${type}?${params}`)
.then(r => r.json())
.then(data => {
var _a, _b, _c;
const content = data['content'];
if (!content)
return null;
return {
size: content['size'],
nextOffset: (_c = (_b = (_a = content['page']) === null || _a === void 0 ? void 0 : _a['next']) === null || _b === void 0 ? void 0 : _b['offset']) !== null && _c !== void 0 ? _c : 0,
data: content['data']
};
});
});
}
}
exports.ChzzkSearch = ChzzkSearch;
;