Skip to content

Commit

Permalink
fix: 源rid_str获取
Browse files Browse the repository at this point in the history
  • Loading branch information
shanmiteko committed May 3, 2024
1 parent 1a08c0a commit fc41750
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 109 deletions.
220 changes: 122 additions & 98 deletions lib/core/searcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ function parseDynamicCard(ditem) {
["DYNAMIC_TYPE_AV", 8],
["DYNAMIC_TYPE_ARTICLE", 64]
]);
const dy_type2chat_type = new Map([
["DYNAMIC_TYPE_FORWARD", 17],
["DYNAMIC_TYPE_DRAW", 11],
["DYNAMIC_TYPE_WORD", 17],
["DYNAMIC_TYPE_AV", 1],
["DYNAMIC_TYPE_ARTICLE", 12]
]);
/* 转发者的UID */
obj.uid = ditem?.modules?.module_author?.mid || 0
/* 转发者的name */
Expand Down Expand Up @@ -126,9 +133,22 @@ function parseDynamicCard(ditem) {
/* 源动态类型 */
obj.origin_type = dy_typeenum2num.get(ditem?.orig?.type) || 0
/* 被转发者的rid(用于发评论) */
obj.origin_rid_str = ditem?.orig?.basic?.comment_id_str || ""
switch (ditem?.orig?.type) {
case "DYNAMIC_TYPE_DRAW":
obj.origin_rid_str = ditem?.orig?.modules?.module_dynamic?.major?.draw?.id?.toString() || ""
break;
case "DYNAMIC_TYPE_AV":
obj.origin_rid_str = ditem?.orig?.modules?.module_dynamic?.major?.archive?.aid || ""
break;
case "DYNAMIC_TYPE_ARTICLE":
obj.origin_rid_str = ditem?.orig?.modules?.module_dynamic?.major?.article?.id?.toString() || ""
break;
default:
obj.origin_rid_str = ditem?.orig?.id_str || ""
break;
}
/* 用于发送评论 */
obj.origin_chat_type = ditem?.orig?.basic?.comment_type || 0
obj.origin_chat_type = dy_type2chat_type.get(ditem?.orig?.type) || 0
/* 被转发者的动态的ID !!!!此为大数需使用字符串值,不然JSON.parse()会有丢失精度 */
obj.origin_dynamic_id = ditem?.orig?.id_str || ""
/* 预约抽奖信息 */
Expand Down Expand Up @@ -165,88 +185,33 @@ function oldParseDynamicCard(dynamic_detail_card) {
const { strToJson } = utils;
/**临时储存单个动态中的信息 */
let obj = {};
const { desc, card, extension, extend_json = "{}", display = {} } = dynamic_detail_card
, { is_liked = 1, user_profile = {} } = desc
, { info = {} } = user_profile
, cardToJson = strToJson(card)
, extendjsonToJson = strToJson(extend_json)
, { add_on_card_info = [] } = display
, { item } = cardToJson;
const dy_type2chat_type = new Map([[1, 17], [2, 11], [4, 17], [8, 1], [64, 12]]);
/* 转发者的UID */
obj.uid = desc.uid
/* 转发者的name */
obj.uname = info.uname || ''
/* 动态是否点过赞 */
obj.is_liked = is_liked > 0
/* 动态的ts10 */
obj.create_time = desc.timestamp
/* 动态类型 */
obj.type = desc.type
/* 用于发送评论 */
obj.rid_str = desc.rid_str.length > 12 ? desc.dynamic_id_str : desc.rid_str;
/* 用于发送评论 */
obj.chat_type = dy_type2chat_type.get(obj.type) || 0;
/* 转发者的动态ID !!!!此为大数需使用字符串值,不然JSON.parse()会有丢失精度 */
obj.dynamic_id = desc.dynamic_id_str;
/* 定位@信息 */
obj.ctrl = (extendjsonToJson.ctrl) || [];
/* 预约抽奖信息 */
if (add_on_card_info.length > 0) {
const [status, oid_str, text] = add_on_card_info
.filter(it => typeof it.reserve_attach_card !== 'undefined'
&& typeof it.reserve_attach_card.reserve_lottery !== 'undefined'
&& typeof it.reserve_attach_card.reserve_button !== 'undefined')
.map(({ reserve_attach_card }) => [
reserve_attach_card.reserve_button.status,
reserve_attach_card.oid_str,
reserve_attach_card.reserve_lottery.text])[0] || [];
if (status === 1) {
obj.reserve_id = oid_str;
obj.reserve_lottery_text = text;
}
}
if (extendjsonToJson[""]) {
let r = extendjsonToJson[""].reserve || {};
let { reserve_id, reserve_lottery } = r;
if (reserve_lottery === 1) {
obj.reserve_id = reserve_id + "";
obj.reserve_lottery_text = "信息丢失";
}
}
if (extend_json.match(/"":\{"lottery/)) {
obj.is_charge_lottery = true
}
/* 是否有官方抽奖 */
obj.hasOfficialLottery = extension && extension.lott && true;
/* 转发者的描述 纯文字内容 图片动态描述 后两个分别是视频动态的描述和视频本身的描述*/
obj.description =
(item && ((item.content || '') + (item.description || '')))
|| (
(cardToJson.dynamic || '')
+ (cardToJson.desc || '')
+ (cardToJson.vest && cardToJson.vest.content || '')
)
|| '';
/* 转发 */
if (obj.type === 1) {
const { origin_extension, origin, origin_extend_json = "{}" } = cardToJson
, originToJson = strToJson(origin)
, { add_on_card_info = [] } = display.origin || {}
, originExtendjsonToJson = strToJson(origin_extend_json)
, { user, item } = originToJson;
/* 源动态的ts10 */
obj.origin_create_time = desc.origin.timestamp;
/* 被转发者的UID */
obj.origin_uid = desc.origin.uid;
/* 源动态类型 */
obj.origin_type = desc.orig_type
/* 被转发者的rid(用于发评论) */
obj.origin_rid_str = desc.origin.rid_str.length > 12 ? desc.origin.dynamic_id_str : desc.origin.rid_str;
try {
const { desc, card, extension, extend_json = "{}", display = {} } = dynamic_detail_card
, { is_liked = 1, user_profile = {} } = desc
, { info = {} } = user_profile || {}
, cardToJson = strToJson(card)
, extendjsonToJson = strToJson(extend_json)
, { add_on_card_info = [] } = display || {}
, { item } = cardToJson;
const dy_type2chat_type = new Map([[1, 17], [2, 11], [4, 17], [8, 1], [64, 12]]);
/* 转发者的UID */
obj.uid = desc.uid
/* 转发者的name */
obj.uname = info.uname || ''
/* 动态是否点过赞 */
obj.is_liked = is_liked > 0
/* 动态的ts10 */
obj.create_time = desc.timestamp
/* 动态类型 */
obj.type = desc.type
/* 用于发送评论 */
obj.origin_chat_type = dy_type2chat_type.get(obj.origin_type) || 0
/* 被转发者的动态的ID !!!!此为大数需使用字符串值,不然JSON.parse()会有丢失精度 */
obj.origin_dynamic_id = desc.orig_dy_id_str;
obj.rid_str = desc.rid_str.length > 12 ? desc.dynamic_id_str : desc.rid_str;
/* 用于发送评论 */
obj.chat_type = dy_type2chat_type.get(obj.type) || 0;
/* 转发者的动态ID !!!!此为大数需使用字符串值,不然JSON.parse()会有丢失精度 */
obj.dynamic_id = desc.dynamic_id_str;
/* 定位@信息 */
obj.ctrl = (extendjsonToJson.ctrl) || [];
/* 预约抽奖信息 */
if (add_on_card_info.length > 0) {
const [status, oid_str, text] = add_on_card_info
Expand All @@ -258,30 +223,89 @@ function oldParseDynamicCard(dynamic_detail_card) {
reserve_attach_card.oid_str,
reserve_attach_card.reserve_lottery.text])[0] || [];
if (status === 1) {
obj.origin_reserve_id = oid_str;
obj.origin_reserve_lottery_text = text;
obj.reserve_id = oid_str;
obj.reserve_lottery_text = text;
}
}
if (originExtendjsonToJson[""]) {
let r = originExtendjsonToJson[""].reserve || {};
if (extendjsonToJson[""]) {
let r = extendjsonToJson[""].reserve || {};
let { reserve_id, reserve_lottery } = r;
if (reserve_lottery === 1) {
obj.origin_reserve_id = reserve_id + "";
obj.origin_reserve_lottery_text = "信息丢失";
obj.reserve_id = reserve_id + "";
obj.reserve_lottery_text = "信息丢失";
}
}
if (origin_extend_json.match(/"":\{"lottery/)) {
obj.origin_is_charge_lottery = true
if (extend_json.match(/"":\{"lottery/)) {
obj.is_charge_lottery = true
}
/* 是否有官方抽奖 */
obj.origin_hasOfficialLottery = origin_extension && origin_extension.lott;
/* 被转发者的name */
obj.origin_uname = (user && (user.name || user.uname)) || '';
/* 被转发者的描述 */
obj.origin_description =
(item && (item.content || '' + item.description || ''))
|| (originToJson.dynamic || '' + originToJson.desc || '')
obj.hasOfficialLottery = extension && extension.lott && true;
/* 转发者的描述 纯文字内容 图片动态描述 后两个分别是视频动态的描述和视频本身的描述*/
obj.description =
(item && ((item.content || '') + (item.description || '')))
|| (
(cardToJson.dynamic || '')
+ (cardToJson.desc || '')
+ (cardToJson.vest && cardToJson.vest.content || '')
)
|| '';
/* 转发 */
if (obj.type === 1) {
const { origin_extension, origin, origin_extend_json = "{}" } = cardToJson
, originToJson = strToJson(origin)
, { add_on_card_info = [] } = display.origin || {}
, originExtendjsonToJson = strToJson(origin_extend_json)
, { user, item } = originToJson;
/* 源动态的ts10 */
obj.origin_create_time = desc.origin.timestamp;
/* 被转发者的UID */
obj.origin_uid = desc.origin.uid;
/* 源动态类型 */
obj.origin_type = desc.orig_type
/* 被转发者的rid(用于发评论) */
obj.origin_rid_str = desc.origin.rid_str.length > 12 ? desc.origin.dynamic_id_str : desc.origin.rid_str;
/* 用于发送评论 */
obj.origin_chat_type = dy_type2chat_type.get(obj.origin_type) || 0
/* 被转发者的动态的ID !!!!此为大数需使用字符串值,不然JSON.parse()会有丢失精度 */
obj.origin_dynamic_id = desc.orig_dy_id_str;
/* 预约抽奖信息 */
if (add_on_card_info.length > 0) {
const [status, oid_str, text] = add_on_card_info
.filter(it => typeof it.reserve_attach_card !== 'undefined'
&& typeof it.reserve_attach_card.reserve_lottery !== 'undefined'
&& typeof it.reserve_attach_card.reserve_button !== 'undefined')
.map(({ reserve_attach_card }) => [
reserve_attach_card.reserve_button.status,
reserve_attach_card.oid_str,
reserve_attach_card.reserve_lottery.text])[0] || [];
if (status === 1) {
obj.origin_reserve_id = oid_str;
obj.origin_reserve_lottery_text = text;
}
}
if (originExtendjsonToJson[""]) {
let r = originExtendjsonToJson[""].reserve || {};
let { reserve_id, reserve_lottery } = r;
if (reserve_lottery === 1) {
obj.origin_reserve_id = reserve_id + "";
obj.origin_reserve_lottery_text = "信息丢失";
}
}
if (origin_extend_json.match(/"":\{"lottery/)) {
obj.origin_is_charge_lottery = true
}
/* 是否有官方抽奖 */
obj.origin_hasOfficialLottery = origin_extension && origin_extension.lott;
/* 被转发者的name */
obj.origin_uname = (user && (user.name || user.uname)) || '';
/* 被转发者的描述 */
obj.origin_description =
(item && (item.content || '' + item.description || ''))
|| (originToJson.dynamic || '' + originToJson.desc || '')
|| '';
}
} catch (e) {
log.error("动态卡片解析", e)
}

return obj
Expand Down
2 changes: 1 addition & 1 deletion test/article.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const bili_client = require("../lib/net/bili");
const util = require('./util');

(async () => {
await util.par_run([0], [
await util.par_run([], [
// 0
async () => {
let info = await bili_client.getOneArticleByCv(22112353);
Expand Down
20 changes: 10 additions & 10 deletions test/dynamic_card.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const searcher = require("../lib/core/searcher");
const util = require('./util');

(async () => {
await util.par_run([0, 1, 2, 3, 4, 5, 6, 7, 8], [
await util.par_run([3], [
// 0
async () => {
let info = await bili_client.getOneDynamicByDyid("728424890210713624");
Expand All @@ -23,11 +23,11 @@ const util = require('./util');
},
// 3
async () => {
let card = searcher.parseDynamicCard(await bili_client.getOneDynamicByDyid("747169355882561625"));
assert(card.chat_type == 11)
card = searcher.parseDynamicCard(await bili_client.getOneDynamicByDyid("747441158580338693"));
assert(card.chat_type == 17)
assert(card.origin_chat_type == 11)
let card = searcher.parseDynamicCard(await bili_client.getOneDynamicByDyid("900172162530279445"));
assert.equal(card.chat_type, 11)
card = searcher.parseDynamicCard(await bili_client.getOneDynamicByDyid("926978638295859236"));
assert.equal(card.chat_type, 17)
assert.equal(card.origin_chat_type, 11)
},
// 4
async () => {
Expand All @@ -52,15 +52,15 @@ const util = require('./util');
},
// 7
async () => {
const dy = await bili_client.getOneDynamicByDyid("832208853440397352");
const dy = await bili_client.getOneDynamicByDyid("924676093465591832");
const card = searcher.parseDynamicCard(dy)
assert.equal(card.reserve_id, "3106984");
assert.equal(card.reserve_id, "3715576");
},
// 8
async () => {
const dy = await bili_client.getOneDynamicByDyid("832966468497834066");
const dy = await bili_client.getOneDynamicByDyid("925061227481137187");
const card = searcher.parseDynamicCard(dy)
assert.equal(card.origin_reserve_id, "3106984");
assert.equal(card.origin_reserve_id, "3715576");
},
])

Expand Down

0 comments on commit fc41750

Please sign in to comment.