feat: 添加dy直播解析 [beta]

This commit is contained in:
zhiyu1998 2024-09-30 14:32:03 +08:00
parent 5b1c59dfab
commit c5b6b4a21a
2 changed files with 39 additions and 13 deletions

View File

@ -34,7 +34,7 @@ import {
BILI_STREAM_INFO,
BILI_SUMMARY,
DY_COMMENT,
DY_INFO,
DY_INFO, DY_LIVE_INFO,
DY_TOUTIAO_INFO,
GENERAL_REQ_LINK,
HIBI_API_SERVICE,
@ -128,7 +128,7 @@ export class tools extends plugin {
fnc: "trans",
},
{
reg: "(v.douyin.com)",
reg: "(v.douyin.com|live.douyin.com)",
fnc: "douyin",
},
{
@ -305,16 +305,27 @@ export class tools extends plugin {
// 抖音解析
async douyin(e) {
const urlRex = /(http:|https:)\/\/v.douyin.com\/[A-Za-z\d._?%&+\-=\/#]*/g;
const douUrl = urlRex.exec(e.msg.trim())[0];
const res = await this.douyinRequest(douUrl);
// 当前版本需要填入cookie
if (_.isEmpty(this.douyinCookie)) {
e.reply(`检测到没有Cookie无法解析抖音${HELP_DOC}`);
const urlRex = /(http:\/\/|https:\/\/)(v|live).douyin.com\/[A-Za-z\d._?%&+\-=\/#]*/;
// 检测无效链接例如v.douyin.com
if (!urlRex.test(e.msg)) {
e.reply(`检测到这是一个无效链接,无法解析抖音${HELP_DOC}`);
return;
}
// 获取链接
let douUrl = urlRex.exec(e.msg.trim())[0];
if (douUrl.includes("v.douyin.com")) {
douUrl = await this.douyinRequest(douUrl)
}
// 获取 ID
const douId = /note\/(\d+)/g.exec(douUrl)?.[1] ||
/video\/(\d+)/g.exec(douUrl)?.[1] ||
/live.douyin.com\/(\d+)/.exec(douUrl)?.[1] ||
/live\/(\d+)/.exec(douUrl)?.[1];
// 当前版本需要填入cookie
if (_.isEmpty(this.douyinCookie) || _.isEmpty(douId)) {
e.reply(`检测到没有Cookie 或者 这是一个无效链接,无法解析抖音${HELP_DOC}`);
return;
}
const douId = /note\/(\d+)/g.exec(res)?.[1] || /video\/(\d+)/g.exec(res)?.[1];
// 以下是更新了很多次的抖音API历史且用且珍惜
// const url = `https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=${ douId }`;
// const url = `https://www.iesdouyin.com/aweme/v1/web/aweme/detail/?aweme_id=${ douId }&aid=1128&version_name=23.5.0&device_platform=android&os_version=2333`;
@ -325,7 +336,7 @@ export class tools extends plugin {
Referer: "https://www.douyin.com/",
cookie: this.douyinCookie,
};
const dyApi = DY_INFO.replace("{}", douId);
const dyApi = douUrl.includes("live") ? DY_LIVE_INFO.replaceAll("{}", douId) : DY_INFO.replace("{}", douId);
// a-bogus参数
const abParam = aBogus.generate_a_bogus(
new URLSearchParams(new URL(dyApi).search).toString(),
@ -333,7 +344,7 @@ export class tools extends plugin {
);
// const param = resp.data.result[0].paramsencode;
const resDyApi = `${dyApi}&a_bogus=${abParam}`;
headers['Referer'] = `https://www.douyin.com/video/${douId}`
headers['Referer'] = `https://www.douyin.com/`
// 定义一个dy请求
const dyResponse = () => axios.get(resDyApi, {
headers,
@ -341,7 +352,15 @@ export class tools extends plugin {
// 如果失败进行3次重试
try {
const data = await retryAxiosReq(dyResponse)
// logger.info(data)
// saveJsonToFile(data);
// 直播数据逻辑
if (douUrl.includes("live")) {
const item = await data.data.data?.[0];
const { title, cover, user_count_str } = item;
const dySendContent = `${this.identifyPrefix}识别:抖音直播,${title}`
e.reply([segment.image(cover?.url_list?.[0]), dySendContent, `\n🏄‍♂️在线人数:${user_count_str}人正在观看`]);
return;
}
const item = await data.aweme_detail;
// await saveJsonToFile(item);
// 如果为null则退出

View File

@ -113,6 +113,13 @@ export const DY_COMMENT = "https://www.douyin.com/aweme/v1/web/comment/list/?dev
*/
export const DY_TOUTIAO_INFO = "https://aweme.snssdk.com/aweme/v1/play/?video_id={}&ratio=1080p&line=0"
/**
* DY 直播信息
* @type {string}
*/
export const DY_LIVE_INFO = "https://live.douyin.com/webcast/room/web/enter/?device_platform=webapp&aid=6383&channel=channel_pc_web&pc_client_type=1&version_code=190500&version_name=19.5.0&cookie_enabled=true&screen_width=1920&screen_height=1080&browser_language=zh-CN&browser_platform=Win32&browser_name=Firefox&browser_version=124.0&browser_online=true&engine_name=Gecko&engine_version=122.0.0.0&os_name=Windows&os_version=10&cpu_core_num=12&device_memory=8&platform=PC&web_rid={}&room_id_str={}";
/**
* X API
* @type {string}