Skip to content

Commit

Permalink
optimize: add user-agent config
Browse files Browse the repository at this point in the history
  • Loading branch information
snowtafir committed Oct 26, 2024
1 parent be2c870 commit 63f6ea2
Show file tree
Hide file tree
Showing 15 changed files with 165 additions and 121 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# 2.0.5
* 新增 User-Agent 配置项
* 优化获取动态数据
* 新增获取B站up数据的随机延迟配置项
* 新增puppeteer渲染图片测试脚本
Expand Down
8 changes: 4 additions & 4 deletions apps/bilibili.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import JSON from 'json5';
import lodash from 'lodash';
import { Bot, Plugin, Redis } from 'yunzaijs';
import { BiliQuery } from '@/models/bilibili/bilibili.query';
import { BiliTask } from '@/models/bilibili/bilibili.task';
import { BiliQuery } from '@/models/bilibili/bilibili.main.query';
import { BiliTask } from '@/models/bilibili/bilibili.main.task';
import Config from '@/utils/config';
import { _paths } from '@/utils/paths';
import { BiliGetWebData } from '@/models/bilibili/bilibili.get.web.data';
import { BiliGetWebData } from '@/models/bilibili/bilibili.main.get.web.data';
import {
applyLoginQRCode,
checkBiliLogin,
Expand All @@ -18,7 +18,7 @@ import {
saveLocalBiliCk,
saveLoginCookie,
readTempCk
} from '@/models/bilibili/bilibili.models';
} from '@/models/bilibili/bilibili.mian.models';

declare const logger: any;

Expand Down
8 changes: 7 additions & 1 deletion defaultConfig/bilibili/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ pushStatus: 1
# "0 5,35,51 * * * *" #每小时固定第5分0秒、第35分0秒、第51分0秒检测一次,共3次/h
pushTime: '*/23 * * * *'

# 请求头 User-Agent 列表。如出现 -352 风控,可尝试更换请求头,请根据需要自行添加或修改。可设置多个请求头,每次重启后会随机选择一个。获取方法请浏览器自行搜索。
userAgentList:
- Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
#- Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:131.0) Gecko/20100101 Firefox/131.0
#- Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0

# 推送监测间隔,单位为秒,默认7200秒即2小时,即以当前时间为基准,监测过去2小时内的动态,并推送。取值范围:3600-36000秒,即过去的1-10h。应大于pushTime的周期。
interval: 7200

Expand Down Expand Up @@ -41,7 +47,7 @@ pushMsgMode: 1
# 是否启用九宫格样式:默认 1 启用,0 不启用。此为最高优先级,九宫格为动态模式,特定大小/长宽比的图片资源将会动态启用九宫格/四宫格/无宫格样式。
boxGrid: 1

# B站动态卡片分片截图模式:默认 1 启用 0 不启用。启用,将会推送每条动态的全部内容;不启用,动态内容过长时候将只推送7500px长度的动态卡片,需关闭宫格模式。
# B站动态卡片分片截图模式:默认 1 启用 0 不启用。启用,将会推送每条动态的全部内容;不启用,动态内容过长时候将只推送noSplitHeight长度的动态卡片,需关闭宫格模式。
isSplit: 1

# 动态卡片非分片模式下的截图高度,默认7500px(仅填数字,无需填入单位),请勿设置过大或过小。关闭分片截图时生效。
Expand Down
99 changes: 0 additions & 99 deletions models/bilibili/bilibili.api.ts

This file was deleted.

136 changes: 136 additions & 0 deletions models/bilibili/bilibili.main.api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import Config from '@/utils/config';

class BiliApi {
biliConfigData: any;
USER_AGENT: string;
constructor() {
this.biliConfigData = Config.getUserConfig('bilibili', 'config');
this.USER_AGENT = BiliApi.BILIBILI_USER_AGENT;
this.initialize();
}

static BILIBILI_USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36';

//初始化User-Agent
async initialize() {
await this.initUserAgent();
}
async initUserAgent() {
const userAgentList = await this.biliConfigData.userAgentList;
if (userAgentList && userAgentList.length > 0) {
const randomIndex = Math.floor(Math.random() * userAgentList.length);
this.USER_AGENT = String(userAgentList[randomIndex]);
}
}

// 将静态常量赋值给实例属性
get BILIBIL_API() {
return {
//获取动态资源列表 wbi/无wbi parama = { host_mid: uid, timezone_offset: -480, platform: 'web', features: 'itemOpusStyle,listOnlyfans,opusBigCover,onlyfansVote', web_location: "333.999", ...getDmImg(), "x-bili-device-req-json": { "platform": "web", "device": "pc" }, "x-bili-web-req-json": { "spm_id": "333.999" }, w_rid, wts }
biliDynamicInfoList: `https://api.bilibili.com/x/polymer/web-dynamic/v1/feed/space`,

//获取关注数与粉丝数 parama = { vmid: uid }
biliUpFollowFans: `https://api.bilibili.com/x/relation/stat`,

//通过uid获取up详情 parama = { mid: uid, jsonp: jsonp }
biliSpaceUserInfo: `https://api.bilibili.com/x/space/acc/info`,

//parama = { mid: uid, token: '',platform: 'web', web_location: 1550101, w_webid, w_rid, wts }
biliSpaceUserInfoWbi: `https://api.bilibili.com/x/space/wbi/acc/info`,

//通过关键词${upKeyword}搜索up主 parama = { keyword: 'upKeyword', page: 1, search_type: 'bili_user', order: 'totalrank', pagesize: 5 }
biliSearchUp: `https://api.bilibili.com/x/web-interface/search/type`,
//通过关键词${upKeyword}搜索up主 parama = { keyword: 'upKeyword', page: 1, search_type: 'bili_user', order: 'totalrank' },需要wbi签名
biliSearchUpWbi: `https://api.bilibili.com/x/web-interface/wbi/search/type`,

biliLiveStatus: 'https://api.live.bilibili.com/room/v1/Room/get_status_info_by_uids',
biliCard: 'https://api.bilibili.com/x/web-interface/card',
biliStat: 'https://api.bilibili.com/x/relation/stat',
biliLiveUserInfo: 'https://api.live.bilibili.com/live_user/v1/Master/info',
biliOpusDetail: 'https://api.bilibili.com/x/polymer/web-dynamic/v1/opus/detail'
};
}

/**header */
get BILIBILI_HEADERS() {
return {
'Accept': '*/*',
'Accept-Language': 'zh-CN,en-US;q=0.5',
'Connection': 'keep-alive',
'Accept-Encoding': 'gzip, deflate, br, zstd',
'Cookie': '',
'pragma': 'no-cache',
'Cache-control': 'max-age=0',
'DNT': '1',
'Sec-GPC': '1',
'sec-ch-ua-platform': '',
'sec-ch-ua-mobile': '?0',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-site',
'Sec-Fetch-User': '?0',
'Priority': 'u=4',
'TE': 'trailers',
'User-Agent': this.USER_AGENT
};
}

/**Login header */
get BIlIBILI_LOGIN_HEADERS() {
return {
'Accept': '*/*',
'Accept-Language': 'zh-CN,en-US;q=0.5',
'Accept-Encoding': 'gzip, deflate, br, zstd',
'DNT': '1',
'Sec-GPC': '1',
'Upgrade-Insecure-Requests': '1',
'Sec-Fetch-Dest': 'document',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'none',
'Sec-Fetch-User': '?1',
'TE': 'trailers'
};
}

/**FullArticle header */
get BILIBILI_ARTICLE_HEADERS() {
return {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/png,image/svg+xml,*/*;q=0.8',
'Accept-Language': 'zh-CN,en-US;q=0.5',
'Accept-Encoding': 'gzip, deflate, br, zstd',
'Content-type': 'text/html; charset=utf-8',
'Cookie': '',
'pragma': 'no-cache',
'Cache-control': 'no-cache',
'DNT': '1',
'Sec-GPC': '1',
'sec-ch-ua-mobile': '?0',
'Sec-Fetch-Dest': 'document',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'same-site',
'Sec-Fetch-User': '?1',
'TE': 'trailers',
'Upgrade-Insecure-Requests': '1',
'User-Agent': this.USER_AGENT
};
}

get BILIBILI_DYNAMIC_SPACE_HEADERS() {
return {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate, br, zstd',
'Accept-Language': 'zh-CN,en-US;q=0.5',
'Connection': 'keep-alive',
'Priority': 'u=0, i',
'Sec-Fetch-Dest': 'document',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'none',
'Sec-Fetch-User': '?1',
'Sec-GPC': '1',
'Upgrade-Insecure-Requests': '1',
'User-Agent': this.USER_AGENT
};
}
}

export default new BiliApi();
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import axios from 'axios';
import lodash from 'lodash';
import { BiliApi } from '@/models/bilibili/bilibili.api';
import { cookieWithBiliTicket, readSavedCookieItems, readSavedCookieOtherItems, readSyncCookie } from '@/models/bilibili/bilibili.models';
import { getWbiSign } from '@/models/bilibili/bilibili.wbi';
import { getDmImg } from '@/models/bilibili/bilibili.dm.img';
import { getWebId } from '@/models/bilibili/bilibili.w_webid';
import BiliApi from '@/models/bilibili/bilibili.main.api';
import { cookieWithBiliTicket, readSavedCookieItems, readSavedCookieOtherItems, readSyncCookie } from '@/models/bilibili/bilibili.mian.models';
import { getWbiSign } from '@/models/bilibili/bilibili.risk.wbi';
import { getDmImg } from '@/models/bilibili/bilibili.risk.dm.img';
import { getWebId } from '@/models/bilibili/bilibili.risk.w_webid';

export class BiliGetWebData {
constructor(e?) {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import moment from 'moment';
import { Bot, Segment } from 'yunzaijs';
import { cookieWithBiliTicket, readSyncCookie } from '@/models/bilibili/bilibili.models';
import { BiliApi } from '@/models/bilibili/bilibili.api';
import { cookieWithBiliTicket, readSyncCookie } from '@/models/bilibili/bilibili.mian.models';
import BiliApi from '@/models/bilibili/bilibili.main.api';
import axios from 'axios';
import lodash from 'lodash';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { MainProps } from '@/components/dynamic/MainPage';
import Config from '@/utils/config';
import { renderPage } from '@/utils/image';
import { ScreenshotOptions } from '@/utils/puppeteer.render';
import { BiliGetWebData } from '@/models/bilibili/bilibili.get.web.data';
import { postGateway, readSyncCookie } from '@/models/bilibili/bilibili.models';
import { BiliQuery } from '@/models/bilibili/bilibili.query';
import { BiliGetWebData } from '@/models/bilibili/bilibili.main.get.web.data';
import { postGateway, readSyncCookie } from '@/models/bilibili/bilibili.mian.models';
import { BiliQuery } from '@/models/bilibili/bilibili.main.query';

declare const logger: any;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import { LoginProps } from '@/components/loginQrcode/Page';
import { renderPage } from '@/utils/image';
import { _paths } from '@/utils/paths';
import { ScreenshotOptions } from '@/utils/puppeteer.render';
import { BiliApi } from '@/models/bilibili/bilibili.api';
import { gen_buvid_fp } from '@/models/bilibili/bilibili.buid.fp';
import { getBiliTicket } from '@/models/bilibili/bilibili.ticket';
import BiliApi from '@/models/bilibili/bilibili.main.api';
import { gen_buvid_fp } from '@/models/bilibili/bilibili.risk.buid.fp';
import { getBiliTicket } from '@/models/bilibili/bilibili.risk.ticket';

declare const logger: any;

Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fetch from 'node-fetch';
import { createHmac } from 'crypto';
import { BiliApi } from '@/models/bilibili/bilibili.api';
import BiliApi from '@/models/bilibili/bilibili.main.api';

/**
* Generate HMAC-SHA256 signature
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import axios from 'axios';
import lodash from 'lodash';
import { Redis } from 'yunzaijs';
import { BiliApi } from '@/models/bilibili//bilibili.api';
import { cookieWithBiliTicket, readSyncCookie } from '@/models/bilibili/bilibili.models';
import BiliApi from '@/models/bilibili/bilibili.main.api';
import { cookieWithBiliTicket, readSyncCookie } from '@/models/bilibili/bilibili.mian.models';

export async function getWebId(uid?: number) {
const w_webid_key = 'Yz:yuki:bili:w_webid';
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "yuki-plugin",
"version": "2.0.5-9",
"version": "2.0.5-10",
"author": "snowtafir",
"description": "优纪插件,yunzai-V4 关于 微博推送、B站推送 等功能的拓展插件",
"main": "./index",
Expand Down

0 comments on commit 63f6ea2

Please sign in to comment.