add video length for Bilibili preview share

- also fixed bilibili.XWebView[cid] may decode failed
This commit is contained in:
A.C.Sukazyo Eyre 2024-11-13 14:42:22 +08:00
parent 5f08408ccf
commit 487f1ffe51
Signed by: Eyre_S
GPG Key ID: EFB47D98FE082FAD
7 changed files with 76 additions and 13 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.4.0-beta.1 VERSION = 1.4.0-beta.2
USE_DELTA = false USE_DELTA = false
VERSION_DELTA = VERSION_DELTA =

View File

@ -11,10 +11,13 @@ import cc.sukazyo.cono.morny.data.social.{SocialTwitterParser, SocialWeiboParser
import cc.sukazyo.cono.morny.extra.BilibiliForms.{BiliB23, BiliVideoId} import cc.sukazyo.cono.morny.extra.BilibiliForms.{BiliB23, BiliVideoId}
import cc.sukazyo.cono.morny.extra.bilibili.XWebAPI import cc.sukazyo.cono.morny.extra.bilibili.XWebAPI
import cc.sukazyo.cono.morny.util.tgapi.TelegramExtensions.Message.entitiesSafe import cc.sukazyo.cono.morny.util.tgapi.TelegramExtensions.Message.entitiesSafe
import cc.sukazyo.cono.morny.util.CommonFormat.formatDurationTimers
import com.pengrad.telegrambot.model.Chat import com.pengrad.telegrambot.model.Chat
import com.pengrad.telegrambot.model.request.ParseMode import com.pengrad.telegrambot.model.request.ParseMode
import com.pengrad.telegrambot.request.{SendMessage, SendPhoto, SendSticker} import com.pengrad.telegrambot.request.{SendMessage, SendPhoto, SendSticker}
import java.time.Duration
class OnGetSocial (using coeur: MornyCoeur) extends EventListener { class OnGetSocial (using coeur: MornyCoeur) extends EventListener {
override def onMessage (using event: EventEnv): Unit = { override def onMessage (using event: EventEnv): Unit = {
@ -102,7 +105,8 @@ object OnGetSocial {
.caption( .caption(
// language=html // language=html
s"""<a href="https://www.bilibili.com/video/av${video.av}"><b>${h(video_info.data.title)}</b></a> s"""<a href="https://www.bilibili.com/video/av${video.av}"><b>${h(video_info.data.title)}</b></a>
| <a href="https://space.bilibili.com/${video_info.data.owner.mid}">@${h(video_info.data.owner.name)}</a> | <i>${formatDurationTimers(Duration.ofSeconds(video_info.data.duration))}</i> <a href="https://space.bilibili.com/${video_info.data.owner.mid}">@${h(video_info.data.owner.name)}</a>
|
|${h(video_info.data.desc)}""".stripMargin |${h(video_info.data.desc)}""".stripMargin
).parseMode(ParseMode.HTML) ).parseMode(ParseMode.HTML)

View File

@ -7,9 +7,12 @@ import cc.sukazyo.cono.morny.extra.weibo.MApi
import cc.sukazyo.cono.morny.extra.BilibiliForms.{BiliB23, BiliVideoId} import cc.sukazyo.cono.morny.extra.BilibiliForms.{BiliB23, BiliVideoId}
import cc.sukazyo.cono.morny.extra.bilibili.XWebAPI import cc.sukazyo.cono.morny.extra.bilibili.XWebAPI
import cc.sukazyo.cono.morny.util.tgapi.formatting.TelegramParseEscape.escapeHtml as h import cc.sukazyo.cono.morny.util.tgapi.formatting.TelegramParseEscape.escapeHtml as h
import cc.sukazyo.cono.morny.util.CommonFormat.formatDurationTimers
import com.pengrad.telegrambot.model.Update import com.pengrad.telegrambot.model.Update
import com.pengrad.telegrambot.model.request.{InlineQueryResultPhoto, ParseMode} import com.pengrad.telegrambot.model.request.{InlineQueryResultPhoto, ParseMode}
import java.time.Duration
class ShareToolSocialContent extends ITelegramQuery { class ShareToolSocialContent extends ITelegramQuery {
override def query (event: Update): List[InlineQueryUnit[_]] | Null = { override def query (event: Update): List[InlineQueryUnit[_]] | Null = {
@ -40,7 +43,8 @@ class ShareToolSocialContent extends ITelegramQuery {
).caption( ).caption(
// language=html // language=html
s"""<a href="https://www.bilibili.com/video/av${video.av}"><b>${h(video_info.data.title)}</b></a> s"""<a href="https://www.bilibili.com/video/av${video.av}"><b>${h(video_info.data.title)}</b></a>
| <a href="https://space.bilibili.com/${video_info.data.owner.mid}">@${h(video_info.data.owner.name)}</a> | <i>${formatDurationTimers(Duration.ofSeconds(video_info.data.duration))}</i> <a href="https://space.bilibili.com/${video_info.data.owner.mid}">@${h(video_info.data.owner.name)}</a>
|
|${h(video_info.data.desc)}""".stripMargin |${h(video_info.data.desc)}""".stripMargin
).parseMode(ParseMode.HTML)) ).parseMode(ParseMode.HTML))
} }

View File

@ -5,7 +5,6 @@ import cc.sukazyo.cono.morny.data.social.SocialContent.SocialMediaType.{Photo, V
import cc.sukazyo.cono.morny.MornyCoeur import cc.sukazyo.cono.morny.MornyCoeur
import cc.sukazyo.cono.morny.bot.query.InlineQueryUnit import cc.sukazyo.cono.morny.bot.query.InlineQueryUnit
import cc.sukazyo.cono.morny.util.tgapi.TelegramExtensions.Bot.exec import cc.sukazyo.cono.morny.util.tgapi.TelegramExtensions.Bot.exec
import cc.sukazyo.cono.morny.util.tgapi.formatting.NamingUtils.inlineQueryId
import com.pengrad.telegrambot.model.request.* import com.pengrad.telegrambot.model.request.*
import com.pengrad.telegrambot.request.{SendMediaGroup, SendMessage} import com.pengrad.telegrambot.request.{SendMediaGroup, SendMessage}

View File

@ -26,7 +26,7 @@ case class XWebView (
owner: XWebView.User, owner: XWebView.User,
stat: Ignore, stat: Ignore,
dynamic: String, dynamic: String,
cid: Int, cid: Long,
dimension: Ignore, dimension: Ignore,
premiere: Ignore, premiere: Ignore,
teenage_mode: Int, teenage_mode: Int,

View File

@ -2,8 +2,9 @@ package cc.sukazyo.cono.morny.util
import cc.sukazyo.cono.morny.util.EpochDateTime.{DurationMillis, EpochMillis} import cc.sukazyo.cono.morny.util.EpochDateTime.{DurationMillis, EpochMillis}
import java.time.{Instant, LocalDateTime, ZoneId, ZoneOffset} import java.time.{Duration, Instant, LocalDateTime, ZoneId, ZoneOffset}
import java.time.format.DateTimeFormatter import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoUnit
/** Some formatting (convert some data to some standard output type) /** Some formatting (convert some data to some standard output type)
* methods normalized based on Morny's usage * methods normalized based on Morny's usage
@ -35,9 +36,9 @@ object CommonFormat {
* *
* for example, timestamp [[0]] describes 1970-1-1 00:00:00 in * for example, timestamp [[0]] describes 1970-1-1 00:00:00 in
* UTC+0, so, use the `timestamp` `0` and `utfOffset` `0` will * UTC+0, so, use the `timestamp` `0` and `utfOffset` `0` will
* returns `"1970-1-1 00:00:00:000"`; however, at the same time, * return `"1970-1-1 00:00:00:000"`; however, at the same time,
* in UTC+8, the local time is 1970-1-1 08:00:00:000, so use * in UTC+8, the local time is 1970-1-1 08:00:00:000, so use
* the `timestamp` `0` and the `utcOffset` `8` will returns * the `timestamp` `0` and the `utcOffset` `8` will return
* `"1970-1-1 08:00:00:000"` * `"1970-1-1 08:00:00:000"`
* *
* @return the time-zone local date-time-millis [[String]] describes the timestamp. * @return the time-zone local date-time-millis [[String]] describes the timestamp.
@ -63,7 +64,7 @@ object CommonFormat {
) )
) )
/** human readable [[String]] that describes the millis duration. /** human-readable [[String]] that describes the millis duration.
* *
* {{{ * {{{
* scala> formatDuration(10) * scala> formatDuration(10)
@ -88,4 +89,49 @@ object CommonFormat {
sb ++= (duration % 1000).toString ++= "ms" sb ++= (duration % 1000).toString ++= "ms"
sb toString sb toString
def formatDuration (duration: Duration, minUnit: ChronoUnit = ChronoUnit.MILLIS): String =
List[(Long, String, ChronoUnit)](
(duration.toNanosPart % 1_000_000, "ns", ChronoUnit.NANOS),
(duration.toMillisPart, "ms", ChronoUnit.MILLIS),
(duration.toSecondsPart, "s", ChronoUnit.SECONDS),
(duration.toMinutesPart, "min", ChronoUnit.MINUTES),
(duration.toHoursPart, "h", ChronoUnit.HOURS),
(duration.toDaysPart, "d", ChronoUnit.DAYS)
).filter((_, _, level) => level.ordinal() >= minUnit.ordinal())
.map((value, unit, _) => (value, unit))
.map((value, unit) => if value != 0 then Right(s"$value$unit") else Left(s"$value$unit"))
.reverse.dropWhile(_.isLeft)
.map {
case Left(value) => value
case Right(value) => value
}.mkString(" ")
def formatDurationTimers (duration: Duration, minUnit: ChronoUnit = ChronoUnit.SECONDS): String =
val result = List[(Long, Int, String, ChronoUnit)]( // define each levels parameter
(duration.toNanosPart % 1_000, 3, "", ChronoUnit.NANOS),
(duration.toNanosPart / 1000 % 1_000, 3, ".", ChronoUnit.MICROS),
(duration.toMillisPart, 3, ".", ChronoUnit.MILLIS),
(duration.toSecondsPart, 2, ":", ChronoUnit.SECONDS),
(duration.toMinutesPart, 2, ":", ChronoUnit.MINUTES),
(duration.toHoursPart, 2, ":", ChronoUnit.HOURS),
(duration.toDaysPart, 0, "!", ChronoUnit.DAYS)
).filter((_, _, _, level) => level.ordinal() >= minUnit.ordinal()) // filter out smaller than minUnit levels
.map { (value, minWidth, prefixed, level) => // stringify the duration number, and fill to left if it is 0
val stringed = value.toString.reverse.padTo(minWidth, '0').reverse
val data = (prefixed, stringed)
if value != 0 then (Right(data), level) else (Left(data), level)
}.reverse.dropWhile{ (value, level) => // filter out units with 0 value, on larger units side
value.isLeft && (level.ordinal() > ChronoUnit.SECONDS.ordinal()) // but do not filter out units smaller than seconds
}.map((value, _) => value).map { // just fold data
case Left(data) => data
case Right(data) => data
}.map((prefixed, value) => prefixed + value)
.mkString("") // make it as a string
.drop(1) // drop unnecessary prefixed unit-prefix
if result.startsWith("00.") then // when the max unit is already 0 second, return with 0 seconds
result.drop(1)
else if result.contains(":") || result.contains(".") then // when there contains more than one unit
result.dropWhile(_=='0') // drop the prefixed 0 on the largest unit
else "0:" + result // when the max unit is smaller than seconds, add the 0-minute parts
} }

View File

@ -1,12 +1,22 @@
package cc.sukazyo.cono.morny package cc.sukazyo.cono.morny
import cc.sukazyo.cono.morny.util.UniversalCommand import cc.sukazyo.cono.morny.util.CommonFormat
import scala.io.StdIn import java.time.Duration
import java.time.temporal.ChronoUnit
@main def MornyCLI (): Unit = { @main def MornyCLI (): Unit = {
print("$ java -jar morny-coeur-\"+MornySystem.VERSION_FULL+\".jar ") val duration = Duration.ZERO
ServerMain main UniversalCommand(StdIn readLine) // .plusDays(2)
// .plusHours(21)
// .plusMinutes(2)
// .plusSeconds(5)
.plusMillis(123)
.plusNanos(876548)
val echo = CommonFormat.formatDurationTimers(duration, ChronoUnit.SECONDS)
println(echo)
} }