ali_object.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /*
  2. * @Author: samples jadehh@live.com
  3. * @Date: 2023-12-14 11:03:04
  4. * @LastEditors: samples jadehh@live.com
  5. * @LastEditTime: 2023-12-14 11:03:04
  6. * @FilePath: /lib/ali_object.js
  7. * @Description: 阿里云盘基础类
  8. */
  9. import {_} from "./cat.js";
  10. const UA = "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1"
  11. const CLIENT_ID = "76917ccccd4441c39457a04f6084fb2f";
  12. import * as Utils from "./utils.js";
  13. // 引用会导致出错
  14. // import qs from "qs";
  15. // import axios from "axios";
  16. // import https from "https";
  17. function getHeader() {
  18. const params = {};
  19. params["User-Agent"] = UA;
  20. params.Referer = "https://www.aliyundrive.com/";
  21. return params;
  22. }
  23. class User {
  24. constructor() {
  25. this.driveId = "";
  26. this.userId = "";
  27. this.tokenType = "";
  28. this.accessToken = "";
  29. this.refreshToken = "";
  30. }
  31. static objectFrom(json_str) {
  32. if (_.isEmpty(json_str)) {
  33. return new User();
  34. }
  35. let resonse = JSON.parse(json_str), user = new User();
  36. user.driveId = resonse.default_drive_id;
  37. user.userId = resonse.user_id;
  38. user.tokenType = resonse.token_type;
  39. user.accessToken = resonse.access_token;
  40. user.refreshToken = resonse.refresh_token; // 刷新Token记录原有的Token
  41. return user;
  42. }
  43. getDriveId() {
  44. return _.isEmpty(this.driveId) ? "" : this.driveId;
  45. }
  46. getUserId() {
  47. return _.isEmpty(this.userId) ? "" : this.userId;
  48. }
  49. getTokenType() {
  50. return _.isEmpty(this.tokenType) ? "" : this.tokenType;
  51. }
  52. getAccessToken() {
  53. return _.isEmpty(this.accessToken) ? "" : this.accessToken;
  54. }
  55. getRefreshToken() {
  56. return _.isEmpty(this.refreshToken) ? "" : this.refreshToken;
  57. }
  58. setRefreshToken(refresh_token) {
  59. this.refreshToken = refresh_token
  60. }
  61. getAuthorization() {
  62. return this.getTokenType() + " " + this.getAccessToken();
  63. }
  64. isAuthed() {
  65. return this.getTokenType().length > 0 && this.getAccessToken().length > 0;
  66. }
  67. clean() {
  68. this.refreshToken = "";
  69. this.accessToken = "";
  70. return this;
  71. }
  72. async save() {
  73. await local.set("ali", "aliyundrive_user", this.toString());
  74. return this;
  75. }
  76. toString() {
  77. return JSON.stringify(this.toDict());
  78. }
  79. toDict() {
  80. return {
  81. default_drive_id: this.getDriveId(),
  82. user_id: this.getUserId(),
  83. token_type: this.getTokenType(),
  84. access_token: this.getAccessToken(),
  85. refresh_token: this.getRefreshToken()
  86. };
  87. }
  88. }
  89. class OAuth {
  90. constructor() {
  91. this.tokenType = "";
  92. this.accessToken = "";
  93. this.refreshToken = "";
  94. }
  95. static objectFrom(json_str) {
  96. if (_.isEmpty(json_str)) {
  97. return new OAuth();
  98. }
  99. let oauth_json = JSON.parse(json_str), oAuth = new OAuth();
  100. oAuth.tokenType = oauth_json.token_type;
  101. oAuth.accessToken = oauth_json.access_token;
  102. oAuth.refreshToken = oauth_json.refresh_token;
  103. return oAuth;
  104. }
  105. getTokenType() {
  106. return _.isEmpty(this.tokenType) ? "" : this.tokenType;
  107. }
  108. getAccessToken() {
  109. return _.isEmpty(this.accessToken) ? "" : this.accessToken;
  110. }
  111. getRefreshToken() {
  112. return _.isEmpty(this.refreshToken) ? "" : this.refreshToken;
  113. }
  114. getAuthorization() {
  115. return this.getTokenType() + " " + this.getAccessToken();
  116. }
  117. clean() {
  118. this.refreshToken = "";
  119. this.accessToken = "";
  120. return this;
  121. }
  122. async save() {
  123. await local.set("ali", "aliyundrive_oauth", this.toString());
  124. return this;
  125. }
  126. toString() {
  127. return JSON.stringify(this.toDict());
  128. }
  129. toDict() {
  130. return {
  131. token_type: this.getTokenType(), access_token: this.getAccessToken(), refresh_token: this.getRefreshToken()
  132. };
  133. }
  134. }
  135. class Drive {
  136. constructor() {
  137. this.defaultDriveId = "";
  138. this.resourceDriveId = "";
  139. this.backupDriveId = "";
  140. }
  141. static objectFrom(json_str) {
  142. if (_.isEmpty(json_str)) {
  143. return new Drive();
  144. }
  145. let obj = JSON.parse(json_str), drive = new Drive();
  146. drive.defaultDriveId = obj.default_drive_id;
  147. drive.resourceDriveId = obj.resource_drive_id;
  148. drive.backupDriveId = obj.backup_drive_id;
  149. return drive;
  150. }
  151. getDefaultDriveId() {
  152. return _.isEmpty(this.defaultDriveId) ? "" : this.defaultDriveId;
  153. }
  154. getResourceDriveId() {
  155. return _.isEmpty(this.resourceDriveId) ? "" : this.resourceDriveId;
  156. }
  157. getBackupDriveId() {
  158. return _.isEmpty(this.backupDriveId) ? "" : this.backupDriveId;
  159. }
  160. clean() {
  161. this.defaultDriveId = "";
  162. this.backupDriveId = "";
  163. this.resourceDriveId = "";
  164. return this;
  165. }
  166. async save() {
  167. await local.set("ali", "aliyundrive_drive", this.toString());
  168. return this;
  169. }
  170. toString() {
  171. const params = {
  172. default_drive_id: this.getDefaultDriveId(),
  173. resource_drive_id: this.getResourceDriveId(),
  174. backup_drive_id: this.getBackupDriveId()
  175. };
  176. return JSON.stringify(params);
  177. }
  178. }
  179. class Code {
  180. constructor() {
  181. this.redirectUri = "";
  182. }
  183. static objectFrom(json_str) {
  184. if (_.isEmpty(json_str)) {
  185. return new Code();
  186. }
  187. let code_json = JSON.parse(json_str), code = new Code();
  188. code.redirectUri = code_json.redirectUri;
  189. return code;
  190. }
  191. getRedirectUri() {
  192. return _.isEmpty(this.redirectUri) ? "" : this.redirectUri;
  193. }
  194. getCode() {
  195. return this.getRedirectUri().split("code=")[1];
  196. }
  197. }
  198. class Item {
  199. constructor(file_id) {
  200. this.items = [];
  201. this.nextMarker = "";
  202. this.fileId = file_id;
  203. this.shareId = "";
  204. this.name = "";
  205. this.type = "";
  206. this.fileExtension = "";
  207. this.category = "";
  208. this.size = "";
  209. this.parent = "";
  210. this.shareToken = "";
  211. this.shareIndex = 0;
  212. }
  213. static objectFrom(json_str, shareToken,shareIndex) {
  214. if (_.isEmpty(json_str)) {
  215. return new Item();
  216. }
  217. let item_json = JSON.parse(json_str), item = new Item();
  218. item.nextMarker = typeof item_json.next_marker == "undefined" ? "" : item_json.next_marker;
  219. item.fileId = typeof item_json.file_id == "undefined" ? "" : item_json.file_id;
  220. item.shareId = typeof item_json.share_id == "undefined" ? "" : item_json.share_id;
  221. item.shareToken = shareToken
  222. item.name = typeof item_json.name == "undefined" ? "" : item_json.name;
  223. item.type = typeof item_json.type == "undefined" ? "" : item_json.type;
  224. item.fileExtension = typeof item_json.file_extension == "undefined" ? "" : item_json.file_extension;
  225. item.category = typeof item_json.category == "undefined" ? "" : item_json.category;
  226. item.size = typeof item_json.size == "undefined" ? "" : item_json.size;
  227. item.parent = typeof item_json.parent_file_id == "undefined" ? "" : item_json.parent_file_id;
  228. item.shareIndex = shareIndex
  229. typeof item.items != "undefined" && Array.isArray(item_json.items) && !_.isEmpty(item_json.items) && item_json.items.forEach(function (x) {
  230. let new_item = Item.objectFrom(JSON.stringify((x)), shareToken,shareIndex)
  231. item.items.push(new_item);
  232. });
  233. return item;
  234. }
  235. getItems() {
  236. return _.isEmpty(this.items) ? [] : this.items;
  237. }
  238. getNextMarker() {
  239. return _.isEmpty(this.nextMarker) ? "" : this.nextMarker;
  240. }
  241. getFileId() {
  242. return _.isEmpty(this.fileId) ? "" : this.fileId;
  243. }
  244. getShareId() {
  245. return _.isEmpty(this.shareId) ? "" : this.shareId;
  246. }
  247. getFileExtension() {
  248. return _.isEmpty(this.fileExtension) ? "" : this.fileExtension;
  249. }
  250. getName() {
  251. return _.isEmpty(this.name) ? "" : this.name;
  252. }
  253. getType() {
  254. return _.isEmpty(this.type) ? "" : this.type;
  255. }
  256. getExt() {
  257. return _.isEmpty(this.fileExtension) ? "" : this.fileExtension;
  258. }
  259. getCategory() {
  260. return _.isEmpty(this.category) ? "" : this.category;
  261. }
  262. getSize() {
  263. return this.size === 0 ? "" : "[" + Utils.getSize(this.size) + "]";
  264. }
  265. getParent() {
  266. return _.isEmpty(this.parent) ? "" : "[" + this.parent + "]";
  267. }
  268. getShareIndex(){
  269. return this.shareIndex
  270. }
  271. parentFunc(item) {
  272. this.parent = item;
  273. return this;
  274. }
  275. getDisplayName(type_name) {
  276. let name = this.getName();
  277. name = name.replaceAll("玩偶哥 q 频道:【神秘的哥哥们】", "")
  278. if (type_name === "电视剧") {
  279. let replaceNameList = ["4k", "4K"]
  280. name = name.replaceAll("." + this.getFileExtension(), "")
  281. name = name.replaceAll(" ", "").replaceAll(" ", "")
  282. for (const replaceName of replaceNameList) {
  283. name = name.replaceAll(replaceName, "")
  284. }
  285. name = Utils.getStrByRegexDefault(/\.S01E(.*?)\./, name)
  286. const numbers = name.match(/\d+/g);
  287. if (!_.isEmpty(numbers) && numbers.length > 0) {
  288. name = numbers[0]
  289. }
  290. }
  291. return name + " " + this.getParent() + " " + this.getSize();
  292. }
  293. getEpisodeUrl(type_name){
  294. return this.getDisplayName(type_name) + "$" + this.getFileId() + "+" + this.shareId + "+" + this.shareToken
  295. }
  296. }
  297. class Sub {
  298. constructor() {
  299. this.url = "";
  300. this.name = "";
  301. this.lang = "";
  302. this.format = "";
  303. }
  304. static create() {
  305. return new Sub();
  306. }
  307. setName(name) {
  308. this.name = name;
  309. return this;
  310. }
  311. setUrl(url) {
  312. this.url = url;
  313. return this;
  314. }
  315. setLang(lang) {
  316. this.lang = lang;
  317. return this;
  318. }
  319. setFormat(format) {
  320. this.format = format;
  321. return this;
  322. }
  323. setExt(ext) {
  324. switch (ext) {
  325. case "vtt":
  326. return this.setFormat("text/vtt");
  327. case "ass":
  328. case "ssa":
  329. return this.setFormat("text/x-ssa");
  330. default:
  331. return this.setFormat("application/x-subrip");
  332. }
  333. }
  334. }
  335. async function getUserCache() {
  336. return await local.get("ali", "aliyundrive_user");
  337. }
  338. async function getOAuthCache() {
  339. return await local.get("ali", "aliyundrive_oauth");
  340. }
  341. function getShareId(share_url) {
  342. let patternAli = /https:\/\/www\.alipan\.com\/s\/([^\\/]+)(\/folder\/([^\\/]+))?|https:\/\/www\.aliyundrive\.com\/s\/([^\\/]+)(\/folder\/([^\\/]+))?/
  343. let matches = patternAli.exec(share_url)
  344. const filteredArr = matches.filter(item => item !== undefined);
  345. if (filteredArr.length > 1) {
  346. return matches[1]
  347. } else {
  348. return ""
  349. }
  350. }
  351. async function ali_request(url, opt) {
  352. let resp;
  353. let data;
  354. try {
  355. data = opt ? opt.data || null : null;
  356. const postType = opt ? opt.postType || null : null;
  357. const returnBuffer = opt ? opt.buffer || 0 : 0;
  358. const timeout = opt ? opt.timeout || 5000 : 5000;
  359. const headers = opt ? opt.headers || {} : {};
  360. if (postType === 'form') {
  361. headers['Content-Type'] = 'application/x-www-form-urlencoded';
  362. if (data != null) {
  363. data = qs.stringify(data, {encode: false});
  364. }
  365. }
  366. let respType = returnBuffer === 1 || returnBuffer === 2 ? 'arraybuffer' : undefined;
  367. resp = await axios(url, {
  368. responseType: respType,
  369. method: opt ? opt.method || 'get' : 'get',
  370. headers: headers,
  371. data: data,
  372. timeout: timeout,
  373. httpsAgent: https.Agent({
  374. rejectUnauthorized: false,
  375. }),
  376. });
  377. data = resp.data;
  378. const resHeader = {};
  379. for (const hks of resp.headers) {
  380. const v = hks[1];
  381. resHeader[hks[0]] = Array.isArray(v) ? (v.length === 1 ? v[0] : v) : v;
  382. }
  383. if (!returnBuffer) {
  384. if (typeof data === 'object') {
  385. data = JSON.stringify(data);
  386. }
  387. } else if (returnBuffer === 1) {
  388. return {code: resp.status, headers: resHeader, content: data};
  389. } else if (returnBuffer === 2) {
  390. return {code: resp.status, headers: resHeader, content: data.toString('base64')};
  391. }
  392. return {code: resp.status, headers: resHeader, content: data};
  393. } catch (error) {
  394. // await Utils.log(`请求失败,URL为:${url},失败原因为:${error}`)
  395. resp = error.response
  396. try {
  397. return {code: resp.status, headers: resp.headers, content: JSON.stringify(resp.data)};
  398. } catch (err) {
  399. return {headers: {}, content: ''};
  400. }
  401. }
  402. }
  403. async function post(url, params) {
  404. url = url.startsWith("https") ? url : "https://api.aliyundrive.com/" + url;
  405. let response = await postJson(url, params, getHeader());
  406. return response.content;
  407. }
  408. async function postJson(url, params, headers) {
  409. params["Content-Type"] = "application/json";
  410. return await req(url, {
  411. headers: headers, method: "post", data: params
  412. });
  413. }
  414. export {
  415. UA,
  416. CLIENT_ID,
  417. OAuth,
  418. Code,
  419. Sub,
  420. User,
  421. Item,
  422. Drive,
  423. getHeader,
  424. getShareId,
  425. getOAuthCache,
  426. getUserCache,
  427. post,
  428. postJson
  429. };