utils.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. function isSub(ext) {
  2. return ext == "srt" || ext == "ass" || ext == "ssa";
  3. }
  4. function getSize(size) {
  5. if (size <= 0) return "";
  6. if (size > 1024 * 1024 * 1024 * 1024.0) {
  7. size /= (1024 * 1024 * 1024 * 1024.0);
  8. return size.toFixed(2) + "TB";
  9. } else if (size > 1024 * 1024 * 1024.0) {
  10. size /= (1024 * 1024 * 1024.0);
  11. return size.toFixed(2) + "GB";
  12. } else if (size > 1024 * 1024.0) {
  13. size /= (1024 * 1024.0);
  14. return size.toFixed(2) + "MB";
  15. } else {
  16. size /= 1024.0;
  17. return size.toFixed(2) + "KB";
  18. }
  19. }
  20. function removeExt(text) {
  21. return text.indexOf('.') > -1 ? text.substring(0, text.lastIndexOf(".")) : text;
  22. }
  23. async function log(str) {
  24. console.debug(str);
  25. }
  26. function isVideoFormat(url) {
  27. 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*/;
  28. if (url.indexOf("url=http") > -1 || url.indexOf(".js") > -1 || url.indexOf(".css") > -1 || url.indexOf(".html") > -1) {
  29. return false;
  30. }
  31. return RULE.test(url);
  32. }
  33. function jsonParse(input, json) {
  34. var jsonPlayData = JSON.parse(json);
  35. var url = jsonPlayData.url;
  36. if (url.startsWith("//")) {
  37. url = "https:" + url;
  38. }
  39. if (!url.startsWith("http")) {
  40. return null;
  41. }
  42. if (url === input) {
  43. if (!isVideoFormat(url)) {
  44. return null;
  45. }
  46. }
  47. var headers = {};
  48. var ua = jsonPlayData["user-agent"] || "";
  49. if (ua.trim().length > 0) {
  50. headers["User-Agent"] = " " + ua;
  51. }
  52. var referer = jsonPlayData.referer || "";
  53. if (referer.trim().length > 0) {
  54. headers["Referer"] = " " + referer;
  55. }
  56. var taskResult = {
  57. header: headers,
  58. url: url
  59. };
  60. return taskResult;
  61. }
  62. function debug(obj) {
  63. for (var a in obj) {
  64. if (typeof(obj[a]) == "object") {
  65. debug(obj[a]); //递归遍历
  66. } else {
  67. console.debug(a + "=" + obj[a]);
  68. }
  69. }
  70. }
  71. export { isSub, getSize, removeExt, log, isVideoFormat, jsonParse, debug};