-
Notifications
You must be signed in to change notification settings - Fork 289
Expand file tree
/
Copy pathpan123.js
More file actions
335 lines (318 loc) · 11.8 KB
/
pan123.js
File metadata and controls
335 lines (318 loc) · 11.8 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/**
* 123网盘API操作类
* 提供123网盘的文件分享、下载、播放等功能
*/
import axios from "axios";
import {ENV} from "../env.js";
import {base64Decode} from "../../libs_drpy/crypto-util.js";
/**
* 123网盘操作类
* 支持文件分享链接解析、文件列表获取、下载链接生成等功能
*/
class Pan123 {
constructor() {
// 支持的123网盘域名正则表达式
this.regex = /https:\/\/(www.123684.com|www.123865.com|www.123912.com|www.123pan.com|www.123pan.cn|www.123592.com)\/s\/([^\\/]+)/
this.api = 'https://www.123684.com/b/api/share/';
this.loginUrl = 'https://login.123pan.com/api/user/sign_in';
this.cate = ''
}
/**
* 初始化方法,检查登录状态
*/
async init() {
if (!this.auth) {
try {
await this.login();
} catch (error) {
console.error('登录失败:', error.message);
}
}
}
// 获取账号
get passport() {
return ENV.get('pan_passport')
}
// 获取密码
get password() {
return ENV.get('pan_password')
}
// 获取认证token
get auth() {
return ENV.get('pan_auth')
}
/**
* 登录方法
*/
async login() {
let data = JSON.stringify({
"passport": this.passport,
"password": this.password,
"remember": true
});
let config = {
method: 'POST',
url: this.loginUrl,
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36',
'Content-Type': 'application/json',
'App-Version': '43',
'Referer': 'https://login.123pan.com/centerlogin?redirect_url=https%3A%2F%2Fwww.123684.com&source_page=website',
},
data: data
};
// 发送登录请求并保存token
let auth = (await axios.request(config)).data
ENV.set('pan_auth', auth.data.token)
}
/**
* 解析分享链接,提取分享密钥和提取码
* @param {string} url 分享链接
* @returns {string|null} 分享密钥
*/
getShareData(url) {
this.SharePwd = ''
// 移除URL前后的反引号
url = url.replace(/^`|`$/g, '');
url = decodeURIComponent(url);
// 处理提取码格式
if (url.indexOf('提取码') > 0 && url.indexOf('?') < 0) {
url = url.replace(/提取码:|提取码|提取码:/g, '?')
}
if (url.indexOf('提取码') > 0 && url.indexOf('?') > 0) {
url = url.replace(/提取码:|提取码|提取码:/g, '')
}
if (url.indexOf(':') > 0) {
url = url.replace(':', '')
}
const matches = this.regex.exec(url);
// 提取分享密码
if (url.indexOf('?') > 0) {
const queryString = url.split('?')[1];
// 使用正则表达式提取pwd参数
const pwdMatch = queryString.match(/pwd=([^&]+)/i);
if (pwdMatch && pwdMatch[1]) {
this.SharePwd = pwdMatch[1];
}
}
if (matches) {
let shareKey = '';
if (matches[2].indexOf('?') > 0) {
shareKey = matches[2].split('?')[0];
} else if (matches[2].indexOf('html') > 0) {
shareKey = matches[2].replace('.html', '');
} else {
shareKey = matches[2].match(/www/g) ? matches[1] : matches[2];
}
return shareKey;
}
return null;
}
/**
* 根据分享链接获取文件列表
* @param {string} shareKey 分享密钥
* @returns {Object} 文件列表对象
*/
async getFilesByShareUrl(shareKey) {
let file = {}
try {
// 获取分享信息,返回完整的文件列表
let videos = await this.getShareInfo(shareKey, this.SharePwd, 0, 0)
if (videos && Array.isArray(videos)) {
// 直接使用文件名作为键,保存完整的文件信息
videos.forEach(video => {
if (video.Category === 2) {
if (!(video.FileName in file)) {
file[video.FileName] = [];
}
file[video.FileName].push(video);
}
});
}
} catch (error) {
console.error('获取文件列表失败:', error.message);
}
return file;
}
/**
* 获取分享信息
* @param {string} shareKey 分享密钥
* @param {string} SharePwd 分享密码
* @param {number} next 下一页标识
* @param {number} ParentFileId 父文件夹ID
* @returns {Array} 分类信息数组
*/
async getShareInfo(shareKey, SharePwd, next, ParentFileId) {
let cate = []
try {
const apiUrl = this.api + `get?limit=100&next=${next}&orderBy=file_name&orderDirection=asc&shareKey=${shareKey.trim()}&SharePwd=${SharePwd || ''}&ParentFileId=${ParentFileId}&Page=1`;
let list = await axios.get(apiUrl, {
headers: {},
});
if (list.status === 200 && list.data.code === 0) {
let info = list.data.data;
let next = info.Next;
let infoList = info.InfoList
// 处理文件夹
let folders = infoList.filter(item => item.Category === 0);
// 处理视频文件
let videos = infoList.filter(item => item.Category === 2);
// 先处理视频文件,返回完整的文件信息
if (videos.length > 0) {
cate.push(...videos);
}
// 如果有文件夹,递归处理
if (folders.length > 0) {
for (let item of folders) {
// 递归处理子文件夹时,next参数应该为0,获取第一页
let subCate = await this.getShareInfo(shareKey, SharePwd, 0, item.FileId);
if (subCate && subCate.length > 0) {
cate.push(...subCate);
}
}
}
}
} catch (error) {
console.error('获取分享信息失败:', error.message);
}
return cate;
}
/**
* 获取分享文件列表
* @param {string} shareKey 分享密钥
* @param {string} SharePwd 分享密码
* @param {number} next 下一页标识
* @param {number} ParentFileId 父文件夹ID
* @returns {Array} 视频文件列表
*/
async getShareList(shareKey, SharePwd, next, ParentFileId) {
let video = []
try {
let link = this.api + `get?limit=100&next=${next}&orderBy=file_name&orderDirection=asc&shareKey=${shareKey.trim()}&SharePwd=${SharePwd || ''}&ParentFileId=${ParentFileId}&Page=1`
let infoList = (await axios.request({
method: 'get',
url: link,
headers: {},
})).data;
if (infoList.code === 0 && infoList.data) {
// 筛选视频文件
infoList.data.InfoList.forEach(it => {
if (it.Category === 2) {
video.push({
ShareKey: shareKey,
FileId: it.FileId,
S3KeyFlag: it.S3KeyFlag,
Size: it.Size,
Etag: it.Etag,
FileName: it.FileName,
})
}
})
}
} catch (error) {
console.error('获取分享文件列表失败:', error.message);
}
return video;
}
/**
* 获取文件下载链接
* @param {string} shareKey 分享密钥
* @param {string} FileId 文件ID
* @param {string} S3KeyFlag S3密钥标识
* @param {number} Size 文件大小
* @param {string} Etag 文件标签
* @returns {string} 解码后的下载链接
*/
async getDownload(shareKey, FileId, S3KeyFlag, Size, Etag) {
try {
await this.init();
let data = JSON.stringify({
"ShareKey": shareKey,
"FileID": FileId,
"S3KeyFlag": S3KeyFlag,
"Size": Size,
"Etag": Etag
});
let config = {
method: 'POST',
url: `${this.api}download/info`,
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36',
'Authorization': `Bearer ${this.auth}`,
'Content-Type': 'application/json;charset=UTF-8',
'platform': 'android',
},
data: data
};
let response = await axios.request(config);
if (response.data && response.data.code === 0 && response.data.data && response.data.data.DownloadURL) {
let down = response.data.data;
let downloadUrl = down.DownloadURL;
let url = new URL(downloadUrl);
let params = url.searchParams.get('params');
if (params) {
let decodedUrl = base64Decode(params);
return decodedUrl;
} else {
console.error('下载链接缺少params参数');
return downloadUrl;
}
} else {
console.error('获取下载链接失败:', response.data.message || '未知错误');
return '';
}
} catch (error) {
console.error('获取下载链接失败:', error.message);
return '';
}
}
/**
* 获取视频在线播放链接
* @param {string} shareKey 分享密钥
* @param {string} FileId 文件ID
* @param {string} S3KeyFlag S3密钥标识
* @param {number} Size 文件大小
* @param {string} Etag 文件标签
* @returns {Array} 不同清晰度的播放链接数组
*/
async getLiveTranscoding(shareKey, FileId, S3KeyFlag, Size, Etag) {
try {
await this.init();
let config = {
method: 'GET',
url: `https://www.123684.com/b/api/video/play/info`,
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36',
'Authorization': `Bearer ${this.auth}`,
'Content-Type': 'application/json;charset=UTF-8',
'platform': 'android',
},
params: {
"etag": Etag,
"size": Size,
"from": "1",
"shareKey": shareKey
}
};
let down = (await axios.request(config)).data.data
if (down?.video_play_info) {
let videoinfo = []
// 处理不同清晰度的播放链接
down.video_play_info.forEach(item => {
if (item.url !== '') {
videoinfo.push({
name: item.resolution,
url: item.url
})
}
})
return videoinfo;
}
} catch (error) {
console.error('获取视频播放链接失败:', error.message);
}
return []
}
}
// 导出Pan123实例
export const Pan = new Pan123();