node-nicovideo-api
Version:
nicovideo api (video, live, etc..) wrapper package for node.js
125 lines (100 loc) • 4.18 kB
JavaScript
// Generated by CoffeeScript 1.10.0
/**
* Properties
* - threadId : number -- コメントサーバー内のスレッドID
* - date : Date -- コメント投稿日時
* - locale : string -- 投稿元国情報("ja-jp", "jp"など、詳細不明)
* - command : string -- コメント投稿時に設定されたコマンド(184など)
* - isMyPost : boolean -- 自分で投稿したコメントか
* - user -- 投稿したユーザー情報
* - id : number|string -- ユーザー番号(匿名コメントの場合は文字列)
* - score : number -- このユーザーのNGスコア
* - accountType : number -- アカウント種別(0:一般, 1:プレミアム, 3:配信者)
* - isPremium : boolean -- プレミアム会員かどうか
* - isAnonymous : boolean -- 匿名コメントかどうか
*/
(function() {
var Cheerio, NicoLiveComment, REGEXP_GT, REGEXP_LT, _, __, deepFreeze;
_ = require("lodash");
__ = require("lodash-deep");
Cheerio = require("cheerio");
deepFreeze = require("deep-freeze");
REGEXP_LT = /</g;
REGEXP_GT = />/g;
NicoLiveComment = (function() {
NicoLiveComment.AccountTypes = deepFreeze({
GENERAL: 0,
PREMIUM: 1,
DISTRIBUTOR: 3,
ADMIN: 6
});
/**
* 規定の形式のXMLからNicoLiveCommentモデルを生成します。
*
* ニコ生サーバーから配信されてくる以下のような形式のコメント(1行)を第1引数に渡してください。
* <chat thread="##" vpos="##" date="##" date_usec="##" user_id="##" premium="#" locale="**">コメント内容</chat>
*
* @param {String} xml ニコ生コメントサーバーから受信したXMLコメントデータ
* @param {Number} loggedUserId 現在ログイン中のユーザーのID
* @return {NicoLiveComment}
*/
NicoLiveComment.fromRawXml = function(xml, loggedUserId) {
var $xml, props, ref;
$xml = Cheerio(xml);
props = {
threadId: $xml.attr("thread"),
date: new Date($xml.attr("date") * 1000),
locale: $xml.attr("locale"),
command: $xml.attr("mail"),
comment: $xml.text().replace(REGEXP_GT, ">").replace(REGEXP_LT, "<"),
vpos: $xml.attr("vpos") | 0,
isMyPost: $xml.attr("yourpost") === "1" || (($xml.attr("user_id") | 0) === loggedUserId),
user: {
id: /^\d+$/.test(ref = $xml.attr("user_id")) ? ref | 0 : ref,
score: $xml.attr("score") | 0,
accountType: $xml.attr("premium") | 0,
isPremium: ($xml.attr("premium") | 0) > 0,
isAnonymous: $xml.attr("anonymity") | 0 !== 0
}
};
return new NicoLiveComment(props);
};
function NicoLiveComment(_attr) {
this._attr = _attr;
Object.defineProperties(this, {
command: {
value: this.get("command")
},
comment: {
value: this.get("comment")
}
});
}
NicoLiveComment.prototype.get = function(path) {
return __.deepGet(this._attr, path);
};
NicoLiveComment.prototype.isNormalComment = function() {
return !(this.isControlComment() && this.isPostByDistributor());
};
NicoLiveComment.prototype.isControlComment = function() {
var accountType, userid;
userid = this.get("user.id");
accountType = this.get("user.accountType");
return (userid === 900000000) || (accountType === NicoLiveComment.AccountTypes.ADMIN);
};
NicoLiveComment.prototype.isPostByDistributor = function() {
return this.get("user.accountType") === NicoLiveComment.AccountTypes.DISTRIBUTOR;
};
NicoLiveComment.prototype.isPostBySelf = function() {
return this.get("isMyPost");
};
NicoLiveComment.prototype.isPostByAnonymous = function() {
return this.get("user.isAnonymous");
};
NicoLiveComment.prototype.isPostByPremiumUser = function() {
return this.get("user.isPremium");
};
return NicoLiveComment;
})();
module.exports = NicoLiveComment;
}).call(this);