From 40bdbec1ecdb814fcac488b547f1d3cbb70795fc Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Tue, 17 Oct 2023 18:49:57 +0800 Subject: [PATCH] add tests for BilibiliForms, support b23 video link and better v-part parse --- gradle.properties | 2 +- .../cono/morny/data/BilibiliForms.scala | 19 +++++++++++++++---- .../cono/morny/data/BilibiliFormsTest.scala | 17 ++++++++++++----- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/gradle.properties b/gradle.properties index 0945157..de9eac3 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,7 +5,7 @@ MORNY_ARCHIVE_NAME = morny-coeur MORNY_CODE_STORE = https://github.com/Eyre-S/Coeur-Morny-Cono MORNY_COMMIT_PATH = https://github.com/Eyre-S/Coeur-Morny-Cono/commit/%s -VERSION = 1.2.0-alpha1 +VERSION = 1.2.0-alpha2 USE_DELTA = false VERSION_DELTA = diff --git a/src/main/scala/cc/sukazyo/cono/morny/data/BilibiliForms.scala b/src/main/scala/cc/sukazyo/cono/morny/data/BilibiliForms.scala index 7bf78a4..962c98b 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/data/BilibiliForms.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/data/BilibiliForms.scala @@ -13,13 +13,16 @@ object BilibiliForms { case class BiliVideoId (av: Long, bv: String, part: Int|Null = null) private val REGEX_BILI_ID = "^((?:av|AV)(\\d{1,12})|(?:bv|BV)([A-HJ-NP-Za-km-z1-9]{10}))$"r - private val REGEX_BILI_VIDEO: Regex = "^(?:(?: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}))$" r + private val REGEX_BILI_V_PART_IN_URL_PARAM = "(?:&|^)p=(\\d+)"r + private val REGEX_BILI_VIDEO: Regex = "^(?:(?:https?://)?(?:(?:www\\.)?bilibili\\.com(?:/s)?/video/|b23\\.tv/)((?: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}))$" r - /** parse a Bilibili video link to a [[BiliVideoId]] format Bilibili Video Id + /** parse a Bilibili video link to a [[BiliVideoId]] format Bilibili Video Id. * * @param url the Bilibili video link -- should be a valid link with av/BV, * can take some tracking params (will be ignored), can be a search * result link (have `s/` path). + * Or, it can also be a b23 video link: starts with b23.tv hostname with + * no www. prefix, and no /video/ path. * @throws IllegalArgumentException when the link is not the valid bilibili video link * @return the [[BiliVideoId]] contains raw or converted av id, and raw or converted bv id, * and video part id. @@ -28,9 +31,16 @@ object BilibiliForms { def parse_videoUrl (url: String): BiliVideoId = url match case REGEX_BILI_VIDEO(_url_v, _url_av, _url_bv, _url_param, _url_v_part, _raw_av, _raw_bv) => + val av = select(_url_av, _raw_av) val bv = select(_url_bv, _raw_bv) - val part: Int | Null = if (_url_v_part != null) _url_v_part toInt else null + + val part_part = if (_url_param == null) null else + REGEX_BILI_V_PART_IN_URL_PARAM.findFirstMatchIn(_url_param) match + case Some(part) => part.group(1) + case None => null + val part: Int | Null = if (part_part != null) part_part toInt else null + if (av == null) { assert(bv != null) BiliVideoId(BiliTool.toAv(bv), bv, part) @@ -38,6 +48,7 @@ object BilibiliForms { val _av = av.toLong BiliVideoId(_av, BiliTool.toBv(_av), part) } + case _ => throw IllegalArgumentException(s"not a valid Bilibili video link: $url") private val httpClient = OkHttpClient @@ -50,7 +61,7 @@ object BilibiliForms { * * result url can be used in [[parse_videoUrl]] * - * @param url b23.tv share url + * @param url b23.tv share url. * @throws IllegalArgumentException the input `url` is not a b23.tv url * @throws IllegalStateException some exception occurred when getting information from remote * host, or failed to parse the information got diff --git a/src/test/scala/cc/sukazyo/cono/morny/test/cc/sukazyo/cono/morny/data/BilibiliFormsTest.scala b/src/test/scala/cc/sukazyo/cono/morny/test/cc/sukazyo/cono/morny/data/BilibiliFormsTest.scala index 9108053..93388f5 100644 --- a/src/test/scala/cc/sukazyo/cono/morny/test/cc/sukazyo/cono/morny/data/BilibiliFormsTest.scala +++ b/src/test/scala/cc/sukazyo/cono/morny/test/cc/sukazyo/cono/morny/data/BilibiliFormsTest.scala @@ -3,6 +3,7 @@ package cc.sukazyo.cono.morny.test.cc.sukazyo.cono.morny.data import cc.sukazyo.cono.morny.data.BilibiliForms.* import cc.sukazyo.cono.morny.test.MornyTests import org.scalatest.prop.TableDrivenPropertyChecks +import org.scalatest.tagobjects.{Network, Slow} class BilibiliFormsTest extends MornyTests with TableDrivenPropertyChecks { @@ -54,9 +55,8 @@ class BilibiliFormsTest extends MornyTests with TableDrivenPropertyChecks { BiliVideoId(455017605L, "1Q541167Qg", 1) parse_videoUrl("https://www.bilibili.com/video/av455017605?p=1&vd_source=123456") shouldEqual BiliVideoId(455017605L, "1Q541167Qg", 1) - // todo: implement it -// parse_videoUrl("bilibili.com/video/AV455017605?mid=12hdowhAI&p=5&x=D82EQ&289EHD8AHDOIWU8=r2aur9%3Bi0%3AJ%7BRQJH%28QJ.%5BropWG%3AKR%24%28O%7BGR") shouldEqual -// BiliVideoId(455017605L, "1Q541167Qg", 5) + parse_videoUrl("bilibili.com/video/AV455017605?mid=12hdowhAI&p=5&x=D82EQ&289EHD8AHDOIWU8=r2aur9%3Bi0%3AJ%7BRQJH%28QJ.%5BropWG%3AKR%24%28O%7BGR") shouldEqual + BiliVideoId(455017605L, "1Q541167Qg", 5) "av id with more than 12 digits should not be parsed" in : an[IllegalArgumentException] should be thrownBy parse_videoUrl("av4550176087554") @@ -77,9 +77,16 @@ class BilibiliFormsTest extends MornyTests with TableDrivenPropertyChecks { an[IllegalArgumentException] should be thrownBy parse_videoUrl("https://bilibili.cc/video/av123456") an[IllegalArgumentException] should be thrownBy parse_videoUrl("https://vxbilibili.com/video/av123456") an[IllegalArgumentException] should be thrownBy parse_videoUrl("https://bilibiliexc.com/video/av123456") - an[IllegalArgumentException] should be thrownBy parse_videoUrl("b23.tv/av123456") // todo: support it an[IllegalArgumentException] should be thrownBy parse_videoUrl("C# does not have type erasure. C# has actual generic types deeply baked into the runtime.\n\n好文明") + "url which is a b23 video link should be parsed" in: + parse_videoUrl("https://b23.tv/av688730800") shouldEqual BiliVideoId(688730800L, "1T24y197V2") + parse_videoUrl("http://b23.tv/BV1T24y197V2") shouldEqual BiliVideoId(688730800L, "1T24y197V2") + parse_videoUrl("b23.tv/BV1T24y197V2") shouldEqual BiliVideoId(688730800L, "1T24y197V2") + "b23 video link should not take www. or /video prefix" in: + an[IllegalArgumentException] should be thrownBy parse_videoUrl("https://www.b23.tv/av123456") + an[IllegalArgumentException] should be thrownBy parse_videoUrl("https://b23.tv/video/av123456") + } "while destruct b23.tv share link :" - { @@ -101,7 +108,7 @@ class BilibiliFormsTest extends MornyTests with TableDrivenPropertyChecks { an[IllegalArgumentException] should be thrownBy destructB23Url("https://b23.tv/BV1Q541167Qg") forAll (examples) { (origin, result) => - s"b23 link $origin should be destructed to $result" in: + s"b23 link $origin should be destructed to $result" taggedAs (Slow, Network) in: destructB23Url(origin) shouldEqual result }