add tests for BilibiliForms, support b23 video link and better v-part parse

This commit is contained in:
A.C.Sukazyo Eyre 2023-10-17 18:49:57 +08:00
parent 60dbcef140
commit 40bdbec1ec
Signed by: Eyre_S
GPG Key ID: C17CE40291207874
3 changed files with 28 additions and 10 deletions

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.2.0-alpha1 VERSION = 1.2.0-alpha2
USE_DELTA = false USE_DELTA = false
VERSION_DELTA = VERSION_DELTA =

View File

@ -13,13 +13,16 @@ object BilibiliForms {
case class BiliVideoId (av: Long, bv: String, part: Int|Null = null) 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_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, * @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 * can take some tracking params (will be ignored), can be a search
* result link (have `s/` path). * 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 * @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, * @return the [[BiliVideoId]] contains raw or converted av id, and raw or converted bv id,
* and video part id. * and video part id.
@ -28,9 +31,16 @@ object BilibiliForms {
def parse_videoUrl (url: String): BiliVideoId = def parse_videoUrl (url: String): BiliVideoId =
url match url match
case REGEX_BILI_VIDEO(_url_v, _url_av, _url_bv, _url_param, _url_v_part, _raw_av, _raw_bv) => 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 av = select(_url_av, _raw_av)
val bv = select(_url_bv, _raw_bv) 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) { if (av == null) {
assert(bv != null) assert(bv != null)
BiliVideoId(BiliTool.toAv(bv), bv, part) BiliVideoId(BiliTool.toAv(bv), bv, part)
@ -38,6 +48,7 @@ object BilibiliForms {
val _av = av.toLong val _av = av.toLong
BiliVideoId(_av, BiliTool.toBv(_av), part) BiliVideoId(_av, BiliTool.toBv(_av), part)
} }
case _ => throw IllegalArgumentException(s"not a valid Bilibili video link: $url") case _ => throw IllegalArgumentException(s"not a valid Bilibili video link: $url")
private val httpClient = OkHttpClient private val httpClient = OkHttpClient
@ -50,7 +61,7 @@ object BilibiliForms {
* *
* result url can be used in [[parse_videoUrl]] * 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 IllegalArgumentException the input `url` is not a b23.tv url
* @throws IllegalStateException some exception occurred when getting information from remote * @throws IllegalStateException some exception occurred when getting information from remote
* host, or failed to parse the information got * host, or failed to parse the information got

View File

@ -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.data.BilibiliForms.*
import cc.sukazyo.cono.morny.test.MornyTests import cc.sukazyo.cono.morny.test.MornyTests
import org.scalatest.prop.TableDrivenPropertyChecks import org.scalatest.prop.TableDrivenPropertyChecks
import org.scalatest.tagobjects.{Network, Slow}
class BilibiliFormsTest extends MornyTests with TableDrivenPropertyChecks { class BilibiliFormsTest extends MornyTests with TableDrivenPropertyChecks {
@ -54,9 +55,8 @@ class BilibiliFormsTest extends MornyTests with TableDrivenPropertyChecks {
BiliVideoId(455017605L, "1Q541167Qg", 1) BiliVideoId(455017605L, "1Q541167Qg", 1)
parse_videoUrl("https://www.bilibili.com/video/av455017605?p=1&vd_source=123456") shouldEqual parse_videoUrl("https://www.bilibili.com/video/av455017605?p=1&vd_source=123456") shouldEqual
BiliVideoId(455017605L, "1Q541167Qg", 1) 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
// 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)
// BiliVideoId(455017605L, "1Q541167Qg", 5)
"av id with more than 12 digits should not be parsed" in : "av id with more than 12 digits should not be parsed" in :
an[IllegalArgumentException] should be thrownBy parse_videoUrl("av4550176087554") 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://bilibili.cc/video/av123456")
an[IllegalArgumentException] should be thrownBy parse_videoUrl("https://vxbilibili.com/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("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好文明") 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 :" - { "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") an[IllegalArgumentException] should be thrownBy destructB23Url("https://b23.tv/BV1Q541167Qg")
forAll (examples) { (origin, result) => 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 destructB23Url(origin) shouldEqual result
} }