-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathl_huya.js
232 lines (213 loc) · 6.25 KB
/
l_huya.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/**
新版虎牙解析
cron 0 6 0/6 * * * l_huya.js
*/
const {
fireFetch,
COMM_CONF,
getRandomInt,
encrypt,
parseUrlSearch,
genUrlSearch,
} = require("./utils/utils.js");
//批量
const fs = require("fs");
const path = require("path");
const { Env } = require("./utils/ql");
const { sendNotify } = require("./utils/sendNotify");
const $ = new Env("虎牙【一起看】");
const CONFIG = {
headers: {
"User-Agent": COMM_CONF.MOBILE_USER_AGENT,
},
M_HOST: "https://m.huya.com",
UID: "1463962478092",
};
const fireHyFetch = async (url, opts = {}, isJson = false) => {
return fireFetch(opts.fullUrl ? url : CONFIG.M_HOST + url, opts, isJson);
};
//提取初始直播url
const extractLiveLineUrl = async (roomId, all = false) => {
const url = `https://mp.huya.com/cache.php?m=Live&do=profileRoom&roomid=${roomId}`;
const res = await fireHyFetch(url, { fullUrl: true }, true);
if (res.status !== 200) {
return all ? {} : "";
}
const { data } = res,
multiLine = data?.stream?.hls.multiLine || [],
liveUrl = multiLine
.map(item => item.url)
.filter(Boolean)
.pop();
return all ? data : liveUrl;
};
//匿名登录,获取uid
const annoyLogin = async () => {
const ruid = getRandomInt(1460000000000, 1650000000000);
try {
const url = "https://udblgn.huya.com/web/anonymousLogin";
const params = {
appId: 5002,
byPass: 3,
context: "",
version: "2.4",
data: {},
};
const res = await fireFetch(
url,
{
method: "post",
body: JSON.stringify(params),
headers: { "Content-Type": "application/json" },
},
true
);
const { data } = res;
return data.uid || ruid;
} catch (e) {
return ruid;
}
};
//解析
const getHuyaRealUrl = async (roomId, rawUrl = "", type = "flv") => {
const liveUrl = rawUrl || (await extractLiveLineUrl(roomId));
if (!liveUrl) {
return "";
}
const query = parseUrlSearch(liveUrl);
const uuid = Number(
((Date.now() % 1e10) * 1e3 + ((1e3 * Math.random()) | 0)) % 4294967295
),
uid = await annoyLogin(),
seqid = parseInt(uid) + Date.now();
const s = encrypt(`${seqid}|${query.ctype}|${query.t}`),
pathname = liveUrl.split("?").shift().split("/").pop(),
_sStreamName = pathname.replace("." + type, ""),
_fm = Buffer.from(query.fm, "base64").toString(),
n = _fm
.replace("$0", uid + "")
.replace("$1", _sStreamName)
.replace("$2", s)
.replace("$3", query.wsTime),
wsSecret = encrypt(n),
host = rawUrl.startsWith("http")
? liveUrl.split("/").filter(Boolean)[1]
: liveUrl.split("/").filter(Boolean).shift();
const { fm, ...rest } = query,
searchObj = {
...rest,
wsSecret,
seqid,
uuid,
uid,
ver: 1,
sv: 2110211124,
radio: 4000,
};
// console.log(query);
const hostname = `https://${host}`;
return `${hostname}/src/${_sStreamName}.${type}${genUrlSearch(searchObj)}`;
};
//获取url和名称
const getHuyaLiveInfo = async roomId => {
const info = await extractLiveLineUrl(roomId, true),
multiLine = info?.stream?.flv.multiLine || [],
rawUrl = multiLine.map(item => item.url).shift(),
roomLiveInfo = info?.roomInfo?.tLiveInfo || info?.liveData || {},
name = `【${roomLiveInfo.sNick || roomLiveInfo.nick}】${
roomLiveInfo.sRoomName || roomLiveInfo.roomName
}`;
//console.log(info);
return { url: await getHuyaRealUrl(roomId, rawUrl), name, room_id: roomId };
};
//当前直播的房间
const getRooms = async all => {
//否则只获取 一起看 的房间
//const tmpIds = [4201];
const tmpIds = [4201, 4183, 2067, 4061, 2079];
const genUrl = t =>
`https://live.cdn.huya.com/livelist/game/tagLivelist?gameId=2135&tmpId=${t}&callback=getLiveListJsonpCallback&page=1`;
const genAllUrl = t =>
`https://www.huya.com/cache.php?m=LiveList&do=getLiveListByPage${
all ? "" : "&gameId=2135"
}&tagAll=0&callback=getLiveListJsonpCallback&page=${t}`;
const rooms = [];
let page = 1,
totalPage = 1;
while (page <= totalPage) {
console.log(`获取${!all ? "【一起看】" : ""}第${page}页 的房间列表`);
let resTxt = await fireFetch(
genAllUrl(page),
{ data: { datas: [] } },
false
);
resTxt = resTxt
.replace("getLiveListJsonpCallback(", "")
.replace("}})", "}}");
const res = JSON.parse(resTxt);
const { data, status } = res;
if (status === 200) {
const list = data.datas || [];
const rs = list.map(
({ profileRoom, introduction, nick, uid, gameFullName }) => ({
roomid: profileRoom,
introduction,
nick,
uid,
gameFullName,
})
);
rooms.push(...rs);
}
totalPage = data.totalPage;
page++;
}
return rooms;
};
(async () => {
/* getHuyaLiveInfo("11352944").then((url) => {
console.log(url);
}); */
const all = process?.env?.HUYA_ALL;
const jsonList = [],
rooms = await getRooms(all);
for (let i = 0; i < rooms.length; i++) {
const room = rooms[i],
key = room.roomid;
console.log(`正在解析${i + 1}第个房间,ID: ${key}, 共${rooms.length}个`);
const json = await getHuyaLiveInfo(key); //JSON.parse(out.replace(/\'/g, '"'));
json.name = room.nick
? `【${room.nick}】${room.introduction}`
: json.name || "未知名称";
json.group = `虎牙${
room.gameFullName ? "【" + room.gameFullName + "】" : ""
}`;
console.log("房间解析结果:", json);
jsonList.push(json);
}
fs.mkdirSync(path.resolve(__dirname, `./data`), { recursive: true });
fs.writeFileSync(
path.resolve(__dirname, `./data/huya.json`),
JSON.stringify(jsonList)
);
console.log("当前总数量", jsonList.length);
sendNotify(
`虎牙${!all ? "【一起看】" : ""}`,
`直播url解析执行完毕,共${jsonList.length}个`
);
const m3u_list = ["#EXTM3U"];
for (const i in jsonList) {
const obj = jsonList[i],
url = obj["url1"] || obj["url2"] || obj["url"];
if (url) {
m3u_list.push(
`#EXTINF:-1 group-title="${obj.group}" tvg-id="${obj.room_id}", ${obj.name}`,
url
);
}
}
fs.writeFileSync(
path.resolve(__dirname, `./data/huya.m3u`),
m3u_list.join("\n")
);
})();