@sushibtw/youtubei
Version:
Simple package to get information from youtube such as videos, playlists, channels, video information & comments, related videos, up next video, and more!
74 lines (73 loc) • 3.35 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 });
const constants_1 = require("../constants");
const common_1 = require("../common");
const _1 = require(".");
/** Youtube Client */
class Client {
constructor(options = {}) {
this.options = Object.assign({ hl: "en", gl: "US", cookie: "" }, options);
this.http = new common_1.HTTP(this);
}
/**
* Searches for videos / playlists / channels
*
* @param query The search query
* @param searchOptions Search options
*
*/
search(query, searchOptions) {
return __awaiter(this, void 0, void 0, function* () {
const options = Object.assign({ type: "all" }, searchOptions);
const result = new _1.SearchResult();
yield result.init(this, query, options);
return result;
});
}
/**
* Search for videos / playlists / channels and returns the first result
*
* @return Can be {@link VideoCompact} | {@link PlaylistCompact} | {@link Channel} | `undefined`
*/
findOne(query, searchOptions) {
return __awaiter(this, void 0, void 0, function* () {
return (yield this.search(query, searchOptions)).shift();
});
}
/** Get playlist information and its videos by playlist id or URL */
getPlaylist(playlistIdOrUrl) {
return __awaiter(this, void 0, void 0, function* () {
const playlistId = common_1.getQueryParameter(playlistIdOrUrl, "list");
const response = yield this.http.post(`${constants_1.I_END_POINT}/browse`, {
data: { browseId: `VL${playlistId}` },
});
if (response.data.error || response.data.alerts)
return undefined;
return new _1.Playlist({ client: this }).load(response.data);
});
}
/** Get video information by video id or URL */
getVideo(videoIdOrUrl) {
return __awaiter(this, void 0, void 0, function* () {
const videoId = common_1.getQueryParameter(videoIdOrUrl, "v");
const response = yield this.http.get(`${constants_1.WATCH_END_POINT}`, {
params: { v: videoId, pbj: "1" },
});
if (!response.data[3].response.contents)
return undefined;
return (!response.data[2].playerResponse.playabilityStatus.liveStreamability
? new _1.Video({ client: this }).load(response.data)
: new _1.LiveVideo({ client: this }).load(response.data));
});
}
}
exports.default = Client;