mirror of
https://github.com/Eyre-S/Coeur-Morny-Cono.git
synced 2024-11-24 20:17:38 +08:00
refactor of Bilibili APIs
This commit is contained in:
parent
3c42738816
commit
9c4e2e0ec8
@ -10,7 +10,7 @@ VERSION = 1.4.0-SNAPSHOT
|
||||
USE_DELTA = false
|
||||
VERSION_DELTA =
|
||||
|
||||
CODENAME = guanggu
|
||||
CODENAME = pocheng
|
||||
|
||||
# dependencies
|
||||
|
||||
|
@ -14,8 +14,8 @@ class ShareToolBilibili (using coeur: MornyCoeur) extends ITelegramQuery {
|
||||
private val TITLE_BILI_BV = "[bilibili] Share video / BV"
|
||||
private val ID_PREFIX_BILI_AV = "[morny/share/bili/av]"
|
||||
private val ID_PREFIX_BILI_BV = "[morny/share/bili/bv]"
|
||||
private val LINK_PREFIX = "https://bilibili.com/video/"
|
||||
private val SHARE_FORMAT_HTML = "<a href='%s'>%s</a>"
|
||||
private def formatShareHTML (url: String, name: String): String = SHARE_FORMAT_HTML.format(url, name)
|
||||
|
||||
override def query (event: Update): List[InlineQueryUnit[_]] | Null = {
|
||||
|
||||
@ -31,27 +31,20 @@ class ShareToolBilibili (using coeur: MornyCoeur) extends ITelegramQuery {
|
||||
parse_videoUrl(destructB23Url(event.inlineQuery.query))
|
||||
catch
|
||||
case _: IllegalArgumentException =>
|
||||
return null;
|
||||
return null
|
||||
case e: IllegalStateException =>
|
||||
logger error exceptionLog(e)
|
||||
coeur.daemons.reporter.exception(e)
|
||||
return null;
|
||||
return null
|
||||
|
||||
val av = result.av
|
||||
val bv = result.bv
|
||||
val id_av = s"av$av"
|
||||
val id_bv = s"BV$bv"
|
||||
val linkParams = if (result.part != null) s"?p=${result.part}" else ""
|
||||
val link_av = LINK_PREFIX + id_av + linkParams
|
||||
val link_bv = LINK_PREFIX + id_bv + linkParams
|
||||
List(
|
||||
InlineQueryUnit(InlineQueryResultArticle(
|
||||
inlineQueryId(ID_PREFIX_BILI_AV + av), TITLE_BILI_AV + av,
|
||||
InputTextMessageContent(SHARE_FORMAT_HTML.format(link_av, id_av)).parseMode(ParseMode HTML)
|
||||
inlineQueryId(ID_PREFIX_BILI_AV + result.av), TITLE_BILI_AV + result.av,
|
||||
InputTextMessageContent(formatShareHTML(result.avLink, result.toAvString)).parseMode(ParseMode HTML)
|
||||
)),
|
||||
InlineQueryUnit(InlineQueryResultArticle(
|
||||
inlineQueryId(ID_PREFIX_BILI_BV + bv), TITLE_BILI_BV + bv,
|
||||
InputTextMessageContent(SHARE_FORMAT_HTML.format(link_bv, id_bv)).parseMode(ParseMode HTML)
|
||||
inlineQueryId(ID_PREFIX_BILI_BV + result.bv), TITLE_BILI_BV + result.bv,
|
||||
InputTextMessageContent(formatShareHTML(result.bvLink, result.toBvString)).parseMode(ParseMode HTML)
|
||||
))
|
||||
)
|
||||
|
||||
|
@ -11,7 +11,23 @@ import scala.util.matching.Regex
|
||||
|
||||
object BilibiliForms {
|
||||
|
||||
case class BiliVideoId (av: Long, bv: String, part: Int|Null = null)
|
||||
case class BiliVideoId (av: Long, bv: String, part: Int|Null = null):
|
||||
def withPart (part: Int|Null): BiliVideoId = BiliVideoId(av, bv, part)
|
||||
def link (useFormat: BiliVideoId.Formats = BiliVideoId.Formats.AV): String =
|
||||
val useId: String = useFormat match
|
||||
case BiliVideoId.Formats.AV => avLink
|
||||
case BiliVideoId.Formats.BV => bvLink
|
||||
s"https://www.bilibili.com/video/$useId" +
|
||||
(if part == null then "" else s"?p=$part")
|
||||
def avLink: String = link(BiliVideoId.Formats.AV)
|
||||
def bvLink: String = link(BiliVideoId.Formats.BV)
|
||||
def toAvString: String = s"av$av"
|
||||
def toBvString: String = s"BV$bv"
|
||||
object BiliVideoId:
|
||||
enum Formats:
|
||||
case AV, BV
|
||||
def fromAv (av: Long): BiliVideoId = BiliVideoId(av, BiliTool.toBv(av))
|
||||
def fromBv (bv: String): BiliVideoId = BiliVideoId(BiliTool.toAv(bv), bv)
|
||||
|
||||
private val REGEX_BILI_ID = "^((?:av|AV)(\\d{1,16})|(?:bv1|BV1)([A-HJ-NP-Za-km-z1-9]{9}))$"r
|
||||
private val REGEX_BILI_V_PART_IN_URL_PARAM = "(?:&|^)p=(\\d+)"r
|
||||
@ -36,23 +52,23 @@ object BilibiliForms {
|
||||
val av = select(_url_av, _raw_av)
|
||||
val bv = "1" + select(_url_bv, _raw_bv)
|
||||
|
||||
val part_part = if (_url_param == null) null else
|
||||
val part_part = if _url_param == null then 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
|
||||
val part: Int | Null = if part_part != null then part_part toInt else null
|
||||
|
||||
if (av == null) {
|
||||
assert(bv != null)
|
||||
BiliVideoId(BiliTool.toAv(bv), bv, part)
|
||||
BiliVideoId fromBv bv withPart part
|
||||
} else {
|
||||
val _av = av.toLong
|
||||
BiliVideoId(_av, BiliTool.toBv(_av), part)
|
||||
BiliVideoId fromAv _av withPart part
|
||||
}
|
||||
|
||||
case _ => throw IllegalArgumentException(s"not a valid Bilibili video link: $url")
|
||||
|
||||
private val httpClient = OkHttpSyncBackend()
|
||||
private lazy val httpClient = OkHttpSyncBackend()
|
||||
|
||||
/** get the bilibili video url from b23.tv share url.
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user