原文链接: B站 自动投币 需要登录后的cookie
b站经验获取
投币前
投币后
csrf对应的cookie字段
投币和点赞需要重写headers中的referer, 不然会出错, 即请求是由视频页面发出的
投币
// 投币
export async function coin(aid, multiply = 1, select_like = 0) {
const url = "https://api.bilibili.com/x/web-interface/coin/add"
headers['referer'] = `https://www.bilibili.com/video/${enc(aid)}`
const data = {
aid, // av的id号
multiply, // 硬币数目
select_like, // 是否同时喜欢
cross_domain: true, // 跨域
csrf
}
let resp = await axios.post(
url, data, {headers},
)
return resp.data
}
点赞
// 点赞
export async function like(aid, like = 1) {
headers['referer'] = `https://www.bilibili.com/video/${enc(aid)}`
const data = {
aid, //498566183 497918057
like, // 1 点赞,2 取消点赞
csrf
}
const url = 'https://api.bilibili.com/x/web-interface/archive/like'
let resp = await axios.post(
url, data, {headers},
)
return resp.data
}
获得b站首页的html
export async function getHtml(url = bilibili_url) {
const headers = {
"accept": "*/*",
"accept-language": "zh-CN,zh;q=0.9,en;q=0.8",
"content-type": "application/x-www-form-urlencoded; charset=UTF-8",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-site",
"referrer": "https://www.bilibili.com",
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36',
}
const resp = await axios.get(url, {headers})
return resp.data
}
设置action, 将secrets作为环境变量注入到env中
name: everyPush
on: [push]
# on:
# schedule:
# * is a special character in YAML so you have to quote this string
# - cron: '*/15 * * * *'
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: bilibili-auto-update
run: |
yarn install
yarn test
env:
BILIBILI_COOKIE: ${{ secrets.BILIBILI_COOKIE }}
获取env中的变量
import {env} from "process";
const BILIBILI_COOKIE = env.BILIBILI_COOKIE
const headers = {
"origin": "https://www.bilibili.com",
"referer": "https://www.bilibili.com/video/BV1HK411575w?spm_id_from=333.851.b_62696c695f7265706f72745f646f756761.2",
"accept-encoding": "gzip, deflate, br",
"accept-language": "zh-CN,zh;q=0.9,en;q=0.8",
"content-length": "94",
"accept": "*/*",
"content-type": "application/x-www-form-urlencoded; charset=UTF-8",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-site",
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36',
'cookie': BILIBILI_COOKIE
}
const s = headers['cookie']
let re = /bili_jct=(.*?);/;
let list = re.exec(s);
const csrf = list[1];
// const csrf = '';
export {
headers,
csrf
}