UNPKG

node-nicovideo-api

Version:

nicovideo api (video, live, etc..) wrapper package for node.js

184 lines (157 loc) 5.29 kB
// Generated by CoffeeScript 1.10.0 (function() { var Deferred, FETCH_INTERVAL, MyList, MyListMeta, NicoMyListApi, NicoUrl, Request, _, tokenRegexp; _ = require("lodash"); Request = require("request-promise"); Deferred = require("promise-native-deferred"); NicoUrl = require("../NicoURL"); MyListMeta = require("./MyListMeta"); MyList = require("./MyList"); FETCH_INTERVAL = 30 * 1000; tokenRegexp = /NicoAPI.token = "([0-9a-z\-]*)";/; /** * ニコニコ動画のマイリスト操作APIのラッピングを行います。 * (参考: http://efcl.info/wiki/niconicoapi/) * * @TODO Manage MyList instances for support dispose. * @class NicoMyListApi */ module.exports = NicoMyListApi = (function() { /** * @private * @property {NicoSession} _session */ /** * 認証トークン * @private * @property {Object} _token * @property {Number} _token.timestamp トークンを取得した時間(ミリ秒) * @property {String} _token.token マイリスト操作用トークン */ /** * @constructor * @class NicoMyListApi * @param {NicoSession} session */ function NicoMyListApi(_session) { this._session = _session; this._token = { timestamp: null, token: null }; } /** * マイリストを操作するための認証トークンを取得します。 * @method fetchToken * @return {Promise} */ NicoMyListApi.prototype.fetchToken = function() { if ((this._token.token != null) && (Date.now() - this._token.timestamp) < FETCH_INTERVAL) { return Promise.resolve(this._token.token); } return Request.get({ resolveWithFullResponse: true, url: NicoUrl.MyList.FETCH_TOKEN, jar: this._session.cookie }).then((function(_this) { return function(res) { var token; token = tokenRegexp.exec(res.body); if (token[1] != null) { _this._token.timestamp = Date.now(); _this._token.token = token[1]; return Promise.resolve(token[1]); } else { return Promise.reject("NicoMyListApi: Failed to pick token."); } }; })(this)); }; /** * マイリストの一覧を取得します。 * @method fetchMyListsIndex * @param {boolean} withoutHome * trueを指定すると"とりあえずマイリスト"を一覧から除外します。 * @return {Promise} * - resolve : (mylists: Array.<MyListItemIndex>) * - reject : (message: String) */ NicoMyListApi.prototype.fetchOwnedListIndex = function(withoutHome) { if (withoutHome == null) { withoutHome = false; } return Request.get({ resolveWithFullResponse: true, url: NicoUrl.MyList.GET_GROUPS, jar: this._session.cookie }).then(function(res) { var e, error, lists, result; try { result = JSON.parse(res.body); lists = []; if (result.status !== "ok") { return Promise.reject("Failed to fetch mylist. (reason unknown)"); } if (withoutHome === false) { lists.push(MyListMeta.instance("home")); } _.each(result.mylistgroup, (function(_this) { return function(group) { lists.push(MyListMeta.instance(group)); }; })(this)); return Promise.resolve(lists); } catch (error) { e = error; return Promise.reject("Failed to fetch mylist. (" + e.message + ")"); } }); }; /** * MyListインスタンスを取得します。 * @method fetchMyList * @param {MyListItemIndex|number} id * MyListItemIndexかマイリストIDを渡します。 * @return {Promise(MyList, string)} * 取得できればMyListオブジェクトと共にresolveされ、 * そうでなければエラーメッセージと共にrejectされます */ NicoMyListApi.prototype.getHandlerFor = function(id) { if (id == null) { id = "home"; } return new Promise((function(_this) { return function(resolve, reject) { if (id instanceof MyListMeta) { resolve(MyList.instanceById(id, _this._session)); return; } if (id !== "home") { id = id | 0; } _this.fetchOwnedListIndex(false).then(function(metaList) { var meta; meta = _.where(metaList, { id: id }); if (meta.length === 0) { return reject("Can't find specified mylist.(" + id + ")"); } else { return resolve(MyList.instanceById(meta[0], _this._session)); } }); }; })(this)).then((function(_this) { return function(mylist) { var defer; defer = new Deferred; mylist.fetch().then((function() { return defer.resolve(mylist); }), defer.reject); return defer.promise; }; })(this)); }; return NicoMyListApi; })(); }).call(this);