From c9fe467ad70f78eedd0295f7ac1a6fc9dd729236 Mon Sep 17 00:00:00 2001 From: Oiltea Date: Mon, 28 Aug 2023 12:17:10 +0800 Subject: [PATCH] =?UTF-8?q?fixbug-=E4=B8=AD=E6=96=87=E6=95=B0=E5=AD=97?= =?UTF-8?q?=E5=AD=97=E6=AF=8D=E6=B7=B7=E5=90=88=E6=8E=92=E5=BA=8F=E6=B7=B7?= =?UTF-8?q?=E4=B9=B1=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/github/catvod/api/AliYun.java | 2 +- .../java/com/github/catvod/bean/ali/Item.java | 4 +- .../java/com/github/catvod/spider/Ali.java | 6 +- .../catvod/utils/ChineseComparator.java | 124 ++++++++++++++++++ 4 files changed, 130 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/com/github/catvod/utils/ChineseComparator.java diff --git a/app/src/main/java/com/github/catvod/api/AliYun.java b/app/src/main/java/com/github/catvod/api/AliYun.java index d0d55ea..4274ef7 100644 --- a/app/src/main/java/com/github/catvod/api/AliYun.java +++ b/app/src/main/java/com/github/catvod/api/AliYun.java @@ -288,7 +288,7 @@ public class AliYun { List playFrom = Arrays.asList("原畫", "普畫"); List episode = new ArrayList<>(); List playUrl = new ArrayList<>(); - for (Item file : files) episode.add(file.getDisplayName() + "$" + API.get().shareId + "@" + file.getFileId() + findSubs(file.getName(), subs)); + for (Item file : files) episode.add(file.getDisplayName() + "$" + shareId + "@" + file.getFileId() + findSubs(file.getName(), subs)); for (int i = 0; i < playFrom.size(); i++) playUrl.add(TextUtils.join("#", episode)); Vod vod = new Vod(); vod.setVodId(url); diff --git a/app/src/main/java/com/github/catvod/bean/ali/Item.java b/app/src/main/java/com/github/catvod/bean/ali/Item.java index 0026a3d..5d3daf6 100644 --- a/app/src/main/java/com/github/catvod/bean/ali/Item.java +++ b/app/src/main/java/com/github/catvod/bean/ali/Item.java @@ -1,7 +1,7 @@ package com.github.catvod.bean.ali; import android.text.TextUtils; - +import com.github.catvod.utils.ChineseComparator; import com.github.catvod.utils.Utils; import com.google.gson.Gson; import com.google.gson.annotations.SerializedName; @@ -88,6 +88,6 @@ public class Item implements Comparable { @Override public int compareTo(Item item) { - return this.getName().compareTo(item.getName()); + return ChineseComparator.compare(this.getDisplayName(), item.getDisplayName()); } } diff --git a/app/src/main/java/com/github/catvod/spider/Ali.java b/app/src/main/java/com/github/catvod/spider/Ali.java index 255065b..bc6dd2b 100644 --- a/app/src/main/java/com/github/catvod/spider/Ali.java +++ b/app/src/main/java/com/github/catvod/spider/Ali.java @@ -36,7 +36,7 @@ public class Ali extends Spider { @Override public String playerContent(String flag, String id, List vipFlags) { - API.get().setShareId(id.split("@")[0]); + AliYun.get().setShareId(id.split("@")[0]); return AliYun.get().playerContent(id.split("@")[1].split("\\+"), flag.split("#")[0].equals("原畫")); } @@ -53,8 +53,8 @@ public class Ali extends Spider { if (matcher.find()) { String shareId = matcher.group(1); String fileId = matcher.groupCount() == 3 ? matcher.group(3) : ""; - API.get().setShareId(shareId); - playUrl.add(API.get().getVod(id, fileId).getVodPlayUrl()); + AliYun.get().setShareId(shareId); + playUrl.add(AliYun.get().getVod(id, fileId).getVodPlayUrl()); } } return TextUtils.join("$$$", playUrl); diff --git a/app/src/main/java/com/github/catvod/utils/ChineseComparator.java b/app/src/main/java/com/github/catvod/utils/ChineseComparator.java new file mode 100644 index 0000000..5f95b32 --- /dev/null +++ b/app/src/main/java/com/github/catvod/utils/ChineseComparator.java @@ -0,0 +1,124 @@ +package com.github.catvod.utils; + +import java.text.Collator; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; + +/** + * 按照 Windows 排序风格,对给定的数字、字母、汉字字符串进行排序 + * + * @author Oiltea + */ +public class ChineseComparator { + + /** + * 数字类型 + */ + private static final Integer TYPE_NUMBER = 0; + + /** + * 字符类型(非数字) + */ + private static final Integer TYPE_CHARACTER = 1; + + public static int compare(String o1, String o2) { + // 根据字符数组生成带分类的字符列表 + // List的第一位为该字符的类型(TYPE_NUMBER, TYPE_CHARACTER) + // List的第二位为该字符的内容(一位数字, 一位非数字, 多位数字) + List> o1CharList = getCharList(o1); + List> o2CharList = getCharList(o2); + + // 统一CharList的长度 + int max = Math.max(o1CharList.size(), o2CharList.size()); + while (o1CharList.size() < max) { + o1CharList.add(new ArrayList<>()); + } + while (o2CharList.size() < max) { + o2CharList.add(new ArrayList<>()); + } + + // 开始比较 + int compare = 0; + for (int i = 0; i < max; i++) { + List o1list = o1CharList.get(i); + List o2list = o2CharList.get(i); + + // CharList短的,排在前面 + if (o1list.size() == 0) { + compare = -1; + break; + } + if (o2list.size() == 0) { + compare = 1; + break; + } + + // 先比较类型 + Integer o1Type = (Integer) o1list.get(0); + Integer o2Type = (Integer) o2list.get(0); + int typeCompare = Integer.compare(o1Type, o2Type); + if (typeCompare != 0) { + // 类型不同,则数字在前,非数字在后 + compare = typeCompare; + break; + } else { + // 类型相同,则比较内容 + if (TYPE_NUMBER.equals(o1Type)) { + // 比较数字 + int o1Content = Integer.parseInt(o1list.get(1).toString()); + int o2Content = Integer.parseInt(o2list.get(1).toString()); + compare = Integer.compare(o1Content, o2Content); + } else if (TYPE_CHARACTER.equals(o1Type)) { + // 比较非数字 + String o1Content = (String) o1list.get(1); + String o2Content = (String) o2list.get(1); + compare = Collator.getInstance(Locale.CHINESE).compare(o1Content, o2Content); + } + // 如果不相等,则退出比较 + if (compare != 0) { + break; + } + } + } + return compare; + } + + /** + * 根据字符数组生成带分类的字符列表 + * + * @param text 字符串 + * @return 带分类的字符列表,List的第一位为该字符的类型(TYPE_NUMBER, TYPE_CHARACTER),第二位为该字符的内容 + */ + private static List> getCharList(String text) { + char[] chars = text.toCharArray(); + List> charList = new ArrayList<>(); + for (int i = 0; i < chars.length; i++) { + List list = new ArrayList<>(); + // 是否为数字 + char c = chars[i]; + if ((int) c >= '0' && (int) c <= '9') { + StringBuilder str = new StringBuilder(); + // 下一位是否为数字,如果为数字则组成多位数 + do { + str.append(c); + if (i + 1 < chars.length) { + c = chars[++i]; + } else { + break; + } + } while ((int) c >= '0' && (int) c <= '9'); + if (!(i + 1 == chars.length) || !((int) c >= '0' && (int) c <= '9')) { + i--; + } + list.add(TYPE_NUMBER); + list.add(str.toString()); + } else { + list.add(TYPE_CHARACTER); + list.add(String.valueOf(c)); + } + charList.add(list); + } + return charList; + } +} \ No newline at end of file