chore: add fail fallback exception catch

This commit is contained in:
2024-10-14 19:24:45 +08:00
parent c4d8c0eaf0
commit bfbeee2880
1 file changed
+28 -22
@@ -10,6 +10,7 @@ package cn.rtast.fancybot.commands.parse
import cn.rtast.fancybot.commands.misc.ShortLinkCommand.Companion.makeShortLink import cn.rtast.fancybot.commands.misc.ShortLinkCommand.Companion.makeShortLink
import cn.rtast.fancybot.entity.douyin.DouyinVideo import cn.rtast.fancybot.entity.douyin.DouyinVideo
import cn.rtast.fancybot.util.Http import cn.rtast.fancybot.util.Http
import cn.rtast.fancybot.util.Logger
import cn.rtast.fancybot.util.misc.drawCustomImage import cn.rtast.fancybot.util.misc.drawCustomImage
import cn.rtast.fancybot.util.misc.toBufferedImage import cn.rtast.fancybot.util.misc.toBufferedImage
import cn.rtast.fancybot.util.misc.toByteArray import cn.rtast.fancybot.util.misc.toByteArray
@@ -33,6 +34,7 @@ object DouyinVideoParseCommand {
private val shortLinkRegex = Regex("https://v\\.douyin\\.com/[a-zA-Z0-9]+/") private val shortLinkRegex = Regex("https://v\\.douyin\\.com/[a-zA-Z0-9]+/")
private val singleVideoRegex = Regex("https://www\\.douyin\\.com/video/([0-9]+)") private val singleVideoRegex = Regex("https://www\\.douyin\\.com/video/([0-9]+)")
private val douyinApiUrl = "https://douyin.wtf/api/douyin/web/fetch_one_video".proxy private val douyinApiUrl = "https://douyin.wtf/api/douyin/web/fetch_one_video".proxy
private val logger = Logger.getLogger<DouyinVideoParseCommand>()
private const val IMAGE_WIDTH = 1200 private const val IMAGE_WIDTH = 1200
private const val IMAGE_HEIGHT = 800 private const val IMAGE_HEIGHT = 800
@@ -74,28 +76,32 @@ object DouyinVideoParseCommand {
} }
suspend fun parse(message: GroupMessage) { suspend fun parse(message: GroupMessage) {
val id = if (shortLinkRegex.find(message.rawMessage) != null) { try {
val shortUrl = shortLinkRegex.find(message.rawMessage)?.value ?: "" val id = if (shortLinkRegex.find(message.rawMessage) != null) {
val request = Request.Builder().url(shortUrl).build() val shortUrl = shortLinkRegex.find(message.rawMessage)?.value ?: ""
val finalUrl = tempHttpClient.newCall(request).execute().also { it.close() } val request = Request.Builder().url(shortUrl).build()
extractVideoId(finalUrl.request.url.toString()) val finalUrl = tempHttpClient.newCall(request).execute().also { it.close() }
} else if (singleVideoRegex.find(message.rawMessage) != null) extractVideoId(message.rawMessage) else "" extractVideoId(finalUrl.request.url.toString())
if (id.isNotBlank()) { } else if (singleVideoRegex.find(message.rawMessage) != null) extractVideoId(message.rawMessage) else ""
val response = Http.get<DouyinVideo>( if (id.isNotBlank()) {
douyinApiUrl, val response = Http.get<DouyinVideo>(
mapOf("aweme_id" to id) douyinApiUrl,
) mapOf("aweme_id" to id)
val videoPlayUrlShortUrl = response.data.awemeDetail.video.bitRate )
.first().playAddr.urlList.first().makeShortLink() val videoPlayUrlShortUrl = response.data.awemeDetail.video.bitRate
val shareUrlShortUrl = response.data.awemeDetail.shareUrl.makeShortLink() .first().playAddr.urlList.first().makeShortLink()
val image = this.createDouyinVideoCard(response) val shareUrlShortUrl = response.data.awemeDetail.shareUrl.makeShortLink()
val msg = MessageChain.Builder() val image = this.createDouyinVideoCard(response)
.addImage(image, true) val msg = MessageChain.Builder()
.addText("视频地址: $shareUrlShortUrl") .addImage(image, true)
.addNewLine() .addText("视频地址: $shareUrlShortUrl")
.addText("下载地址: $videoPlayUrlShortUrl") .addNewLine()
.build() .addText("下载地址: $videoPlayUrlShortUrl")
message.reply(msg) .build()
message.reply(msg)
}
} catch (e: Exception) {
logger.warn("抖音视频解析失败: ${e.message}")
} }
} }
} }