fix input av/bv may be too long or too small to process

This commit is contained in:
A.C.Sukazyo Eyre 2023-04-07 16:55:09 +08:00
parent f990df70ea
commit 9c03a59512
Signed by: Eyre_S
GPG Key ID: C17CE40291207874
6 changed files with 37 additions and 8 deletions

1
_book

@ -1 +0,0 @@
Subproject commit 33e051fee5acb5118d6c023039ff9c1db6aa0c60

View File

@ -102,11 +102,11 @@ java {
} }
tasks.withType(JavaCompile) { tasks.withType(JavaCompile).configureEach {
options.encoding = proj_file_encoding.name() options.encoding = proj_file_encoding.name()
} }
tasks.withType(Javadoc) { tasks.withType(Javadoc).configureEach {
options.encoding = proj_file_encoding.name() options.encoding = proj_file_encoding.name()
options.docEncoding = proj_file_encoding.name() options.docEncoding = proj_file_encoding.name()
options.charSet = proj_file_encoding.name() options.charSet = proj_file_encoding.name()

View File

@ -5,7 +5,7 @@ MORNY_ARCHIVE_NAME = morny-coeur
MORNY_CODE_STORE = https://github.com/Eyre-S/Coeur-Morny-Cono MORNY_CODE_STORE = https://github.com/Eyre-S/Coeur-Morny-Cono
MORNY_COMMIT_PATH = https://github.com/Eyre-S/Coeur-Morny-Cono/commit/%s MORNY_COMMIT_PATH = https://github.com/Eyre-S/Coeur-Morny-Cono/commit/%s
VERSION = 1.0.0-RC3.4 VERSION = 1.0.0-RC3.5
USE_DELTA = false USE_DELTA = false
VERSION_DELTA = VERSION_DELTA =

View File

@ -25,16 +25,20 @@ public class GetUsernameAndId implements ITelegramCommand {
final String[] args = command.getArgs(); final String[] args = command.getArgs();
// 不支持大于一个参数
if (args.length > 1) { MornyCoeur.extra().exec(new SendMessage( if (args.length > 1) { MornyCoeur.extra().exec(new SendMessage(
event.message().chat().id(), event.message().chat().id(),
"[Unavailable] Too much arguments." "[Unavailable] Too much arguments."
).replyToMessageId(event.message().messageId())); return; } ).replyToMessageId(event.message().messageId())); return; }
// 发送者自己的 id
long userId = event.message().from().id(); long userId = event.message().from().id();
// 如果有回复某个人则使用被回复人的 id
if (event.message().replyToMessage()!= null) { if (event.message().replyToMessage()!= null) {
userId = event.message().replyToMessage().from().id(); userId = event.message().replyToMessage().from().id();
} }
// 如果有指定 id则使用指定的 id
if (args.length > 0) { if (args.length > 0) {
try { try {
userId = Long.parseLong(args[0]); userId = Long.parseLong(args[0]);
@ -47,6 +51,7 @@ public class GetUsernameAndId implements ITelegramCommand {
} }
} }
// 重新获取用户对象
final GetChatMemberResponse response = MornyCoeur.getAccount().execute( final GetChatMemberResponse response = MornyCoeur.getAccount().execute(
new GetChatMember(event.message().chat().id(), userId) new GetChatMember(event.message().chat().id(), userId)
); );
@ -59,6 +64,7 @@ public class GetUsernameAndId implements ITelegramCommand {
return; return;
} }
// 获取并发送用户信息
final User user = response.chatMember().user(); final User user = response.chatMember().user();
if (user.id() == 136817688) { if (user.id() == 136817688) {

View File

@ -13,7 +13,6 @@ import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
//import static cc.sukazyo.cono.morny.Log.logger;
import static cc.sukazyo.cono.morny.util.tgapi.formatting.NamedUtils.inlineIds; import static cc.sukazyo.cono.morny.util.tgapi.formatting.NamedUtils.inlineIds;
public class ShareToolBilibili implements ITelegramQuery { public class ShareToolBilibili implements ITelegramQuery {
@ -22,7 +21,7 @@ public class ShareToolBilibili implements ITelegramQuery {
public static final String TITLE_BILI_BV = "[bilibili] Share video / BV"; public static final String TITLE_BILI_BV = "[bilibili] Share video / BV";
public static final String ID_PREFIX_BILI_AV = "[morny/share/bili/av]"; public static final String ID_PREFIX_BILI_AV = "[morny/share/bili/av]";
public static final String ID_PREFIX_BILI_BV = "[morny/share/bili/bv]"; public static final String ID_PREFIX_BILI_BV = "[morny/share/bili/bv]";
public static final Pattern REGEX_BILI_VIDEO = Pattern.compile("^(?:(?:https?://)?(?:www\\.)?bilibili\\.com(?:/s)?/video/((?:av|AV)(\\d+)|(?:bv|BV)([A-HJ-NP-Za-km-z1-9]+))/?(\\?(?:p=(\\d+))?.*)?|(?:av|AV)(\\d+)|(?:bv|BV)([A-HJ-NP-Za-km-z1-9]+))$"); public static final Pattern REGEX_BILI_VIDEO = Pattern.compile("^(?:(?:https?://)?(?:www\\.)?bilibili\\.com(?:/s)?/video/((?:av|AV)(\\d{1,12})|(?:bv|BV)([A-HJ-NP-Za-km-z1-9]{10}))/?(\\?(?:p=(\\d+))?.*)?|(?:av|AV)(\\d{1,12})|(?:bv|BV)([A-HJ-NP-Za-km-z1-9]{10}))$");
private static final String SHARE_FORMAT_HTML = "<a href='%s'>%s</a>"; private static final String SHARE_FORMAT_HTML = "<a href='%s'>%s</a>";

View File

@ -16,6 +16,22 @@ public class BiliTool {
private static final char[] BV_TEMPLATE = "1 4 1 7 ".toCharArray(); private static final char[] BV_TEMPLATE = "1 4 1 7 ".toCharArray();
private static final int[] BV_TEMPLATE_FILTER = new int[]{9, 8, 1, 6, 2, 4}; private static final int[] BV_TEMPLATE_FILTER = new int[]{9, 8, 1, 6, 2, 4};
public static class IllegalFormatException extends RuntimeException {
private IllegalFormatException (String bv, String reason) {
super("`%s` is not a valid 10 digits base58 BV id: %s".formatted(bv, reason));
}
private IllegalFormatException (String bv, int length) {
this(bv, "length is %d.".formatted(length));
}
private IllegalFormatException (String bv, char c, int location) {
this(bv, "char `%s` is not in base58 char table (in position %d)".formatted(c, location));
}
}
/** /**
* Convert a <a href="https://www.bilibili.com/">Bilibili</a> AV video id format to BV id format. * Convert a <a href="https://www.bilibili.com/">Bilibili</a> AV video id format to BV id format.
* <p> * <p>
@ -27,17 +43,26 @@ public class BiliTool {
* <p> * <p>
* for now , the BV id has 10 digits. * for now , the BV id has 10 digits.
* the method <b>available while the <u>av-id < 2^27</u></b>, while it theoretically available when the av-id < 2^30. * the method <b>available while the <u>av-id < 2^27</u></b>, while it theoretically available when the av-id < 2^30.
* <p>
* this method allows input only 10 digits base58 BV id, if the input is not formatted by this method, it will throw
* an Exception.
* *
* @see <a href="https://www.zhihu.com/question/381784377/answer/1099438784">mcfx的回复: 如何看待 2020 3 23 日哔哩哔哩将稿件的av 变更为BV </a> * @see <a href="https://www.zhihu.com/question/381784377/answer/1099438784">mcfx的回复: 如何看待 2020 3 23 日哔哩哔哩将稿件的av 变更为BV </a>
* *
* @param bv the BV id, a string in (a special) base58 number format, <b>without "BV" prefix</b>. * @param bv the BV id, a string in (a special) base58 number format, <b>without "BV" prefix</b>.
* @return the AV id corresponding to this bv id in <a href="https://www.bilibili.com/">Bilibili</a>, formatted as a number. * @return the AV id corresponding to this bv id in <a href="https://www.bilibili.com/">Bilibili</a>, formatted as a number.
* @throws IllegalFormatException if the input BV id is not the 10 digits base58 String.
*/ */
@Nonnegative @Nonnegative
public static long toAv (@Nonnull String bv) { public static long toAv (@Nonnull String bv) throws IllegalFormatException {
long av = 0; long av = 0;
if (bv.length() != 10)
throw new IllegalFormatException(bv, bv.length());
for (int i = 0; i < BV_TEMPLATE_FILTER.length; i++) { for (int i = 0; i < BV_TEMPLATE_FILTER.length; i++) {
av += BV_TABLE_REVERSED.get(bv.charAt(BV_TEMPLATE_FILTER[i])) * Math.pow(TABLE_INT,i); final Integer tableToken = BV_TABLE_REVERSED.get(bv.charAt(BV_TEMPLATE_FILTER[i]));
if (tableToken == null)
throw new IllegalFormatException(bv, bv.charAt(BV_TEMPLATE_FILTER[i]), BV_TEMPLATE_FILTER[i]);
av += tableToken * Math.pow(TABLE_INT,i);
} }
return (av-V_CONV_ADD)^V_CONV_XOR; return (av-V_CONV_ADD)^V_CONV_XOR;
} }