pan123.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import axios from "axios";
  2. import {ENV} from "./env.js";
  3. import {base64Decode} from "../libs_drpy/crypto-util.js";
  4. class Pan123 {
  5. constructor() {
  6. this.regex = /https:\/\/(www.123684.com|www.123865.com|www.123912.com|www.123pan.com|www.123pan.cn|www.123592.com)\/s\/([^\\/]+)/
  7. this.api = 'https://www.123684.com/b/api/share/';
  8. this.loginUrl = 'https://login.123pan.com/api/user/sign_in';
  9. this.cate = ''
  10. }
  11. async init() {
  12. if(this.passport){
  13. console.log("获取盘123账号成功")
  14. }
  15. if(this.password){
  16. console.log("获取盘123密码成功")
  17. }
  18. if(this.auth){
  19. let info = JSON.parse(CryptoJS.enc.Base64.parse(this.auth.split('.')[1]).toString(CryptoJS.enc.Utf8))
  20. if(info.exp > Math.floor(Date.now() / 1000)){
  21. console.log("登录成功")
  22. }else {
  23. console.log("登录过期,重新登录")
  24. await this.loin()
  25. }
  26. }else {
  27. console.log("尚未登录,开始登录")
  28. await this.loin()
  29. }
  30. }
  31. get passport(){
  32. return ENV.get('pan_passport')
  33. }
  34. get password(){
  35. return ENV.get('pan_password')
  36. }
  37. get auth(){
  38. return ENV.get('pan_auth')
  39. }
  40. async loin(){
  41. let data = JSON.stringify({
  42. "passport": this.passport,
  43. "password": this.password,
  44. "remember": true
  45. });
  46. let config = {
  47. method: 'POST',
  48. url: this.loginUrl,
  49. headers: {
  50. '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',
  51. 'Content-Type': 'application/json',
  52. 'App-Version': '43',
  53. 'Referer': 'https://login.123pan.com/centerlogin?redirect_url=https%3A%2F%2Fwww.123684.com&source_page=website',
  54. },
  55. data: data
  56. };
  57. let auth = (await axios.request(config)).data
  58. ENV.set('pan_auth',auth.data.token)
  59. }
  60. getShareData(url){
  61. url = decodeURIComponent(url);
  62. const matches = this.regex.exec(url);
  63. if(url.indexOf('?') > 0){
  64. this.SharePwd = url.split('?')[1].match(/[A-Za-z0-9]+/)[0];
  65. console.log(this.SharePwd)
  66. }
  67. if (matches) {
  68. if(matches[2].indexOf('?') > 0){
  69. return matches[2].split('?')[0]
  70. }else {
  71. return matches[2].match(/www/g)?matches[1]:matches[2];
  72. }
  73. }
  74. return null;
  75. }
  76. async getFilesByShareUrl(shareKey){
  77. let file = {}
  78. let cate = await this.getShareInfo(shareKey, this.SharePwd, 0, 0)
  79. if(cate && Array.isArray(cate)){
  80. await Promise.all(cate.map(async (item) => {
  81. if (!(item.filename in file)) {
  82. file[item.filename] = [];
  83. }
  84. const fileData = await this.getShareList(item.shareKey,item.SharePwd,item.next, item.fileId);
  85. if (fileData && fileData.length > 0) {
  86. file[item.filename].push(...fileData);
  87. }
  88. }));
  89. }
  90. // 过滤掉空数组
  91. for (let key in file) {
  92. if (file[key].length === 0) {
  93. delete file[key];
  94. }
  95. }
  96. return file;
  97. }
  98. async getShareInfo(shareKey,SharePwd,next,ParentFileId) {
  99. let cate = []
  100. let list = await axios.get(this.api+"get",{
  101. headers: {},
  102. params: {
  103. "limit": "100",
  104. "next": next,
  105. "orderBy": "file_name",
  106. "orderDirection": "asc",
  107. "shareKey": shareKey,
  108. "SharePwd": SharePwd,
  109. "ParentFileId": ParentFileId,
  110. "Page": "1"
  111. }
  112. });
  113. if(list.status === 200){
  114. if(list.data.code === 5103){
  115. console.log(list.data.message);
  116. }else {
  117. let info = list.data.data;
  118. let next = info.Next;
  119. let infoList = info.InfoList
  120. infoList.forEach(item => {
  121. if(item.Category === 0){
  122. cate.push({
  123. filename:item.FileName,
  124. shareKey:shareKey,
  125. SharePwd:SharePwd,
  126. next:next,
  127. fileId:item.FileId
  128. });
  129. }
  130. })
  131. let result = await Promise.all(cate.map(async (it)=> this.getShareInfo(shareKey,SharePwd,next, it.fileId)));
  132. result = result.filter(item => item !== undefined && item !== null);
  133. return [...cate,...result.flat()];
  134. }
  135. }
  136. }
  137. async getShareList(shareKey,SharePwd,next,ParentFileId) {
  138. let video = []
  139. let infoList = (await axios.get(this.api+"get",{
  140. headers: {},
  141. params: {
  142. "limit": "100",
  143. "next": next,
  144. "orderBy": "file_name",
  145. "orderDirection": "asc",
  146. "shareKey": shareKey,
  147. "SharePwd": SharePwd,
  148. "ParentFileId": ParentFileId,
  149. "Page": "1"
  150. }
  151. })).data.data.InfoList;
  152. infoList.forEach(it=>{
  153. if(it.Category === 2){
  154. video.push({
  155. ShareKey: shareKey,
  156. FileId: it.FileId,
  157. S3KeyFlag: it.S3KeyFlag,
  158. Size: it.Size,
  159. Etag: it.Etag,
  160. FileName: it.FileName,
  161. })
  162. }
  163. })
  164. return video;
  165. }
  166. async getDownload(shareKey,FileId,S3KeyFlag,Size,Etag) {
  167. await this.init();
  168. let data = JSON.stringify({
  169. "ShareKey": shareKey,
  170. "FileID": FileId,
  171. "S3KeyFlag": S3KeyFlag,
  172. "Size": Size,
  173. "Etag": Etag
  174. });
  175. let config = {
  176. method: 'POST',
  177. url: `${this.api}download/info`,
  178. headers: {
  179. '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',
  180. 'Authorization': `Bearer ${this.auth}`,
  181. 'Content-Type': 'application/json;charset=UTF-8',
  182. 'platform': 'android',
  183. },
  184. data: data
  185. };
  186. let down = (await axios.request(config)).data.data
  187. return base64Decode((new URL(down.DownloadURL)).searchParams.get('params'));
  188. }
  189. async getLiveTranscoding(shareKey,FileId,S3KeyFlag,Size,Etag){
  190. await this.init();
  191. let config = {
  192. method: 'GET',
  193. url: `https://www.123684.com/b/api/video/play/info`,
  194. headers: {
  195. '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',
  196. 'Authorization': `Bearer ${this.auth}`,
  197. 'Content-Type': 'application/json;charset=UTF-8',
  198. 'platform': 'android',
  199. },
  200. params:{
  201. "etag": Etag,
  202. "size": Size,
  203. "from": "1",
  204. "shareKey": shareKey
  205. }
  206. };
  207. let down = (await axios.request(config)).data.data.video_play_info
  208. let videoinfo = []
  209. down.forEach(item => {
  210. if(item.url!==''){
  211. videoinfo.push({
  212. name:item.resolution,
  213. url:item.url
  214. })
  215. }
  216. })
  217. return videoinfo;
  218. }
  219. }
  220. export const Pan = new Pan123();