DefaultConfig.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package com.github.tvbox.osc.util;
  2. import android.content.Context;
  3. import android.content.pm.PackageInfo;
  4. import android.content.pm.PackageManager;
  5. import android.net.Uri;
  6. import android.text.TextUtils;
  7. import com.github.tvbox.osc.api.ApiConfig;
  8. import com.github.tvbox.osc.bean.MovieSort;
  9. import com.github.tvbox.osc.bean.SourceBean;
  10. import com.github.tvbox.osc.server.ControlManager;
  11. import com.google.gson.JsonElement;
  12. import com.google.gson.JsonObject;
  13. import java.util.ArrayList;
  14. import java.util.Collections;
  15. import java.util.List;
  16. import java.util.regex.Pattern;
  17. /**
  18. * @author pj567
  19. * @date :2020/12/21
  20. * @description:
  21. */
  22. public class DefaultConfig {
  23. public static List<MovieSort.SortData> adjustSort(String sourceKey, List<MovieSort.SortData> list, boolean withMy) {
  24. List<MovieSort.SortData> data = new ArrayList<>();
  25. if (sourceKey != null) {
  26. SourceBean sb = ApiConfig.get().getSource(sourceKey);
  27. ArrayList<String> categories = sb.getCategories();
  28. if (!categories.isEmpty()) {
  29. for (String cate : categories) {
  30. for (MovieSort.SortData sortData : list) {
  31. if (sortData.name.equals(cate)) {
  32. if (sortData.filters == null)
  33. sortData.filters = new ArrayList<>();
  34. data.add(sortData);
  35. }
  36. }
  37. }
  38. } else {
  39. for (MovieSort.SortData sortData : list) {
  40. if (sortData.filters == null)
  41. sortData.filters = new ArrayList<>();
  42. data.add(sortData);
  43. }
  44. }
  45. }
  46. if (withMy)
  47. data.add(0, new MovieSort.SortData("my0", "主页"));
  48. Collections.sort(data);
  49. return data;
  50. }
  51. public static int getAppVersionCode(Context mContext) {
  52. //包管理操作管理类
  53. PackageManager pm = mContext.getPackageManager();
  54. try {
  55. PackageInfo packageInfo = pm.getPackageInfo(mContext.getPackageName(), 0);
  56. return packageInfo.versionCode;
  57. } catch (PackageManager.NameNotFoundException e) {
  58. e.printStackTrace();
  59. }
  60. return -1;
  61. }
  62. public static String getAppVersionName(Context mContext) {
  63. //包管理操作管理类
  64. PackageManager pm = mContext.getPackageManager();
  65. try {
  66. PackageInfo packageInfo = pm.getPackageInfo(mContext.getPackageName(), 0);
  67. return packageInfo.versionName;
  68. } catch (PackageManager.NameNotFoundException e) {
  69. e.printStackTrace();
  70. }
  71. return "";
  72. }
  73. /**
  74. * 后缀
  75. *
  76. * @param name
  77. * @return
  78. */
  79. public static String getFileSuffix(String name) {
  80. if (TextUtils.isEmpty(name)) {
  81. return "";
  82. }
  83. int endP = name.lastIndexOf(".");
  84. return endP > -1 ? name.substring(endP) : "";
  85. }
  86. /**
  87. * 获取文件的前缀
  88. *
  89. * @param fileName
  90. * @return
  91. */
  92. public static String getFilePrefixName(String fileName) {
  93. if (TextUtils.isEmpty(fileName)) {
  94. return "";
  95. }
  96. int start = fileName.lastIndexOf(".");
  97. return start > -1 ? fileName.substring(0, start) : fileName;
  98. }
  99. private static final Pattern snifferMatch = Pattern.compile(
  100. "http((?!http).){20,}?\\.(m3u8|mp4|mp3|m4a|flv|avi|mkv|rm|wmv|wma|mpg)\\?.*|" +
  101. "http((?!http).){20,}\\.(m3u8|mp4|mp3|m4a|flv|avi|mkv|rm|wmv|wma|mpg)|" +
  102. "http((?!http).)*?video/tos*|" +
  103. "http((?!http).){20,}?/m3u8\\?pt=m3u8.*|" +
  104. "http((?!http).)*?default\\.ixigua\\.com/.*|" +
  105. "http((?!http).)*?dycdn-tos\\.pstatp[^\\?]*|" +
  106. "http.*?/player/m3u8play\\.php\\?url=.*|" +
  107. "http.*?/player/.*?[pP]lay\\.php\\?url=.*|" +
  108. "http.*?/playlist/m3u8/\\?vid=.*|" +
  109. "http.*?\\.php\\?type=m3u8&.*|" +
  110. "http.*?/download.aspx\\?.*|" +
  111. "http.*?/api/up_api.php\\?.*|" +
  112. "https.*?\\.66yk\\.cn.*|" +
  113. "http((?!http).)*?netease\\.com/file/.*"
  114. );
  115. public static boolean isVideoFormat(String url) {
  116. if (url.contains("=http") || url.contains(".html")) {
  117. return false;
  118. }
  119. Uri uri = Uri.parse(url);
  120. String path = uri.getPath();
  121. if (TextUtils.isEmpty(path)) {
  122. return false;
  123. }
  124. if (path.endsWith(".js") || path.endsWith(".css") || path.endsWith(".html")) {
  125. return false;
  126. }
  127. if (snifferMatch.matcher(url).find()) return true;
  128. return false;
  129. }
  130. public static String safeJsonString(JsonObject obj, String key, String defaultVal) {
  131. try {
  132. if (obj.has(key))
  133. return obj.getAsJsonPrimitive(key).getAsString().trim();
  134. else
  135. return defaultVal;
  136. } catch (Throwable th) {
  137. }
  138. return defaultVal;
  139. }
  140. public static int safeJsonInt(JsonObject obj, String key, int defaultVal) {
  141. try {
  142. if (obj.has(key))
  143. return obj.getAsJsonPrimitive(key).getAsInt();
  144. else
  145. return defaultVal;
  146. } catch (Throwable th) {
  147. }
  148. return defaultVal;
  149. }
  150. public static ArrayList<String> safeJsonStringList(JsonObject obj, String key) {
  151. ArrayList<String> result = new ArrayList<>();
  152. try {
  153. if (obj.has(key)) {
  154. if (obj.get(key).isJsonObject()) {
  155. result.add(obj.get(key).getAsString());
  156. } else {
  157. for (JsonElement opt : obj.getAsJsonArray(key)) {
  158. result.add(opt.getAsString());
  159. }
  160. }
  161. }
  162. } catch (Throwable th) {
  163. }
  164. return result;
  165. }
  166. public static String checkReplaceProxy(String urlOri) {
  167. if (urlOri.startsWith("proxy://"))
  168. return urlOri.replace("proxy://", ControlManager.get().getAddress(true) + "proxy?");
  169. return urlOri;
  170. }
  171. }