|
本帖最后由 36547583040 于 2025-3-24 20:32 编辑
湖北长江云js代码,需要websocket轮询,获取到的链接需要referer验证。
websocket的作用是为了验证参数client-id,经过测试,这个id可以重复使用,可以把websocket单独运行,然后用固定id去获取播放链接。获取的链接一个小时会断流,需要重新获取链接,官网也是。
使用方法:
npm install axios
npm install ws
node 文件名.js
const axios = require('axios')
const WebSocket = require('ws')
var ws;
async function gethubei_liveinfo(tv_name) {
let headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36',
}
let url = "https://news.hbtv.com.cn/app/tv/431"
const res = await axios.get(url, { headers: headers })
let clientId = "";
let passportid = ""
let acw_tc = ""
let aaLook = ""
let clientToken = ""
const cookies = res.headers['set-cookie'];
cookies.forEach((cookie, index) => {
if (cookie.includes("client-id")) {
// clientId = cookie.replace("client-id=","")
clientId = "c7ca84f3-9df9-4f03-bc08-47a2b9ce69da"
}
if (cookie.includes("PASSPORTID")) {
passportid = cookie.replace("PASSPORTID=", "").replace("; path=/; HttpOnly", "")
}
if (cookie.includes("acw_tc")) {
acw_tc = cookie.replace("acw_tc=", "").replace(";path=/;HttpOnly;Max-Age=1800", "")
}
if (cookie.includes("aa-look")) {
aaLook = cookie.replace("aa-look=", "")
}
if (cookie.includes("client-token")) {
clientToken = cookie.replace("client-token=", "")
}
});
const t = Math.round(+new Date / 1E3)
let cookiesStr = `CookieName=CookieValue;
PASSPORTID=${passportid};
Hm_lvt_9910fc6fdb612de131f642626cdedd8c=${t};
HMACCOUNT=3862602DA029B3F8;
Hm_lvt_2f29d6cea5a0b5c06250d0303f7076b7=${t};
acw_tc=${acw_tc};
Hm_lpvt_2f29d6cea5a0b5c06250d0303f7076b7=${t};
Hm_lpvt_9910fc6fdb612de131f642626cdedd8c=${t}`
cookiesStr = cookiesStr.replaceAll('\n', '')
const data = res.data;
const match = data.match(/liveList:\s*(\[.*?\]),\s*audioList/s);
let tvname = `new-${tv_name}`;
let q = "";
if (match) {
let jsonString = match[1]
const output = jsonString.replace(/^\s*\/\/.*$/gm, '');
const matches = [...output.matchAll(/stream:\s*"([^"]+)"/g)].map(match => match[1]);
matches.forEach(element => {
if (element.includes(tvname)) {
q = element;
}
});
} else {
console.error("未匹配到 liveList 数据");
}
q = `https://news.hbtv.com.cn/ajax/get_cdn_leech?url=${q}&client-id=${clientId}`
headers['cookie'] = cookiesStr
headers['referer'] = 'https://news.hbtv.com.cn/app/tv/431'
headers['x-requested-with'] = 'XMLHttpRequest'
let res_ = await axios.get(q, { headers: headers })
init_ws(aaLook,clientId,clientToken)
console.log(res_.data.data)
setInterval(()=>{
let msg = {
"aa_look": aaLook,
"client_id": clientId,
"client_token": clientToken
};
ws.send(JSON.stringify(msg));
console.log("websocket心跳发送成功")
console.log(`client-id:${clientId} \n`)
console.log(`aaLook:${aaLook} \n`)
console.log(`client_token:${clientToken} \n`)
},30000)
}
function init_ws(aaLook,clientId,clientToken) {
ws = new WebSocket('wss://remote-wa.cjyun.org.cn/liveweb', {
headers: {
'origin': 'https://news.hbtv.com.cn'
}
});
ws.on('open', () => {
console.log("websocket链接成功");
let msg = {
"aa_look": aaLook,
"client_id": clientId,
"client_token": clientToken
};
ws.send(JSON.stringify(msg));
console.log("websocket心跳发送成功")
console.log(`client-id:${clientId} \n`)
console.log(`aaLook:${aaLook} \n`)
console.log(`client_token:${clientToken} \n`)
// ws.close()
});
ws.on('message', (data) => {
let resMsg = JSON.parse(data);
if (resMsg.wsnode) {
const nodeData = resMsg.wsnode;
console.log("收到 wsnode,关闭连接");
// ws.close(); // 如果需要关闭连接
resolve(nodeData); // 返回结果
}
});
ws.on('close', (code, reason) => {
if (code !== 1000) {
reject(new Error(`连接异常关闭,代码: ${code},原因: ${reason.toString()}`));
}
});
ws.on('error', (error) => {
console.error("WebSocket 错误:", error);
reject(error);
});
}
//湖北卫视-hbws
//湖北经视-hbjs
//湖北综合-hbzh
//湖北影视-hbys
//湖北教育-hbjy
//垄上频道-hbls
gethubei_liveinfo("hbls")
声明:代码均来自网络,代码仅仅用于学习交流,如有侵权,请联系删除! |
|