utils.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * @File : utils.js
  3. * @Author : jade
  4. * @Date : 2024/1/25 15:01
  5. * @Email : jadehh@1ive.com
  6. * @Software : Samples
  7. * @Desc :
  8. */
  9. import {Crypto} from "./cat.js";
  10. import {TextDecoder} from "./TextDecoder.js";
  11. // import {TextDecoder} from "text-decoding";
  12. let CHROME = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36";
  13. const MOBILEUA = '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';
  14. let RESOURCEURL = "https://gh.con.sh/https://raw.githubusercontent.com/jadehh/TV/js"
  15. function isSub(ext) {
  16. return ext === "srt" || ext === "ass" || ext === "ssa";
  17. }
  18. function isNumeric(str) {
  19. return !isNaN(parseInt(str));
  20. }
  21. function getSize(size) {
  22. if (size <= 0) return "";
  23. if (size > 1024 * 1024 * 1024 * 1024.0) {
  24. size /= (1024 * 1024 * 1024 * 1024.0);
  25. return size.toFixed(2) + "TB";
  26. } else if (size > 1024 * 1024 * 1024.0) {
  27. size /= (1024 * 1024 * 1024.0);
  28. return size.toFixed(2) + "GB";
  29. } else if (size > 1024 * 1024.0) {
  30. size /= (1024 * 1024.0);
  31. return size.toFixed(2) + "MB";
  32. } else {
  33. size /= 1024.0;
  34. return size.toFixed(2) + "KB";
  35. }
  36. }
  37. function removeExt(text) {
  38. return text.indexOf('.') > -1 ? text.substring(0, text.lastIndexOf(".")) : text;
  39. }
  40. async function log(str) {
  41. console.debug(str);
  42. }
  43. function isVideoFormat(url) {
  44. var RULE = /http((?!http).){12,}?\.(m3u8|mp4|flv|avi|mkv|rm|wmv|mpg|m4a|mp3)\?.*|http((?!http).){12,}\.(m3u8|mp4|flv|avi|mkv|rm|wmv|mpg|m4a|mp3)|http((?!http).)*?video\/tos*/;
  45. if (url.indexOf("url=http") > -1 || url.indexOf(".js") > -1 || url.indexOf(".css") > -1 || url.indexOf(".html") > -1) {
  46. return false;
  47. }
  48. return RULE.test(url);
  49. }
  50. function jsonParse(input, json) {
  51. var jsonPlayData = JSON.parse(json);
  52. var url = jsonPlayData.url;
  53. if (url.startsWith("//")) {
  54. url = "https:" + url;
  55. }
  56. if (!url.startsWith("http")) {
  57. return null;
  58. }
  59. if (url === input) {
  60. if (!isVideoFormat(url)) {
  61. return null;
  62. }
  63. }
  64. var headers = {};
  65. var ua = jsonPlayData["user-agent"] || "";
  66. if (ua.trim().length > 0) {
  67. headers["User-Agent"] = " " + ua;
  68. }
  69. var referer = jsonPlayData.referer || "";
  70. if (referer.trim().length > 0) {
  71. headers["Referer"] = " " + referer;
  72. }
  73. var taskResult = {
  74. header: headers, url: url
  75. };
  76. return taskResult;
  77. }
  78. function debug(obj) {
  79. for (var a in obj) {
  80. if (typeof (obj[a]) == "object") {
  81. debug(obj[a]); //递归遍历
  82. } else {
  83. console.debug(a + "=" + obj[a]);
  84. }
  85. }
  86. }
  87. function objectToStr(params = null, isBase64Encode = false) {
  88. let params_str_list = []
  89. if (params !== null) {
  90. for (const key of Object.keys(params)) {
  91. if (isBase64Encode) {
  92. params_str_list.push(`${key}=${encodeURIComponent(params[key])}`)
  93. } else {
  94. params_str_list.push(`${key}=${params[key]}`)
  95. }
  96. }
  97. }
  98. return params_str_list.join("&")
  99. }
  100. function sleep(delay) {
  101. const start = (new Date()).getTime();
  102. while ((new Date()).getTime() - start < delay * 1000) {
  103. continue;
  104. }
  105. }
  106. function getStrByRegex(pattern, str) {
  107. let matcher = pattern.exec(str);
  108. if (matcher !== null) {
  109. if (matcher.length >= 1) {
  110. if (matcher.length >= 1) return matcher[1]
  111. }
  112. }
  113. return "";
  114. }
  115. function getStrByRegexDefault(pattern, str) {
  116. let matcher = pattern.exec(str);
  117. if (matcher !== null) {
  118. if (matcher.length >= 1) {
  119. if (matcher.length >= 1) return matcher[1]
  120. }
  121. }
  122. return str;
  123. }
  124. function base64Encode(text) {
  125. return Crypto.enc.Base64.stringify(Crypto.enc.Utf8.parse(text));
  126. }
  127. function base64Decode(text) {
  128. return Crypto.enc.Utf8.stringify(Crypto.enc.Base64.parse(text));
  129. }
  130. function unescape(code) {
  131. return code.replace(/\\('|\\)/g, "$1").replace(/[\r\t\n]/g, " ");
  132. }
  133. function decode(buffer, encode_type) {
  134. let decoder = new TextDecoder(encode_type)
  135. return decoder.decode(buffer)
  136. }
  137. function getHost(url) {
  138. let url_list = url.split("/")
  139. return url_list[0] + "//" + url_list[2]
  140. }
  141. function unquote(str) {
  142. return str.replace(/^"(.*)"$/, '$1');
  143. }
  144. function md5Encode(text) {
  145. return Crypto.MD5(Crypto.enc.Utf8.parse(text)).toString();
  146. }
  147. function getUUID() {
  148. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  149. let r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
  150. return v.toString(16);
  151. }) + "-" + new Date().getTime().toString(16)
  152. }
  153. function objToList(list, key, split_value = "*") {
  154. let value_list = []
  155. for (const dic of list) {
  156. value_list.push(dic[key])
  157. }
  158. return value_list.join(split_value)
  159. }
  160. function getPropertiesAndMethods(obj) {
  161. let str = ""
  162. for (let key in obj) {
  163. if (typeof obj[key] === 'function') {
  164. str = str + "方法名:" + key + '()' + "\n";
  165. } else {
  166. str = str + "属性名:"+(key + ': ' + obj[key]) + "\n";
  167. }
  168. }
  169. return str
  170. }
  171. let patternAli = /(https:\/\/www\.aliyundrive\.com\/s\/[^"]+|https:\/\/www\.alipan\.com\/s\/[^"]+)/
  172. export {
  173. isSub,
  174. getSize,
  175. removeExt,
  176. log,
  177. isVideoFormat,
  178. jsonParse,
  179. debug,
  180. CHROME,
  181. objectToStr,
  182. sleep,
  183. getStrByRegex,
  184. RESOURCEURL,
  185. base64Encode,
  186. base64Decode,
  187. patternAli,
  188. unescape,
  189. decode,
  190. MOBILEUA,
  191. isNumeric,
  192. getHost,
  193. unquote,
  194. md5Encode,
  195. getStrByRegexDefault,
  196. getUUID,
  197. objToList,
  198. getPropertiesAndMethods
  199. };