Skip to content

Commit

Permalink
fixd douyin parser
Browse files Browse the repository at this point in the history
  • Loading branch information
qingshu-ui committed Sep 26, 2024
1 parent 163d893 commit f9b2da2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class BotStatusReport(
val bot = botFactory.createBot(cfg.selfId, botSession)

@EventListener(ApplicationReadyEvent::class)
fun onStarted() {
fun onStarted() = kotlin.runCatching {
val completedTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss"))
val msg = MsgUtils.builder()
.text("Bot is started on: $completedTime")
Expand All @@ -40,7 +40,7 @@ class BotStatusReport(
}

@PreDestroy
fun onDestroy() {
fun onDestroy() = kotlin.runCatching {
val completedTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss"))
val msg = MsgUtils.builder()
.text("Bot is closed at: $completedTime")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import io.github.qingshu.ayaka.annotation.MessageHandlerFilter
import io.github.qingshu.ayaka.dto.constant.AtEnum
import io.github.qingshu.ayaka.dto.event.message.AnyMessageEvent
import io.github.qingshu.ayaka.example.annotation.Slf4j
import io.github.qingshu.ayaka.example.annotation.Slf4j.Companion.log
import io.github.qingshu.ayaka.example.dto.DouYinParseDTO
import io.github.qingshu.ayaka.example.utils.NetUtils
import io.github.qingshu.ayaka.example.utils.Regex
Expand All @@ -30,7 +29,7 @@ class DouYinParse(
private val coroutine: CoroutineScope,
) : BotPlugin {

private fun request(msg: String): DouYinParseDTO.Detail {
private fun request(msg: String) = runCatching<DouYinParseDTO.Detail> {
val shortURL = RegexUtils.group("url", msg, Regex.DOU_YIN_SHORT_URL).trim()
if (shortURL.isBlank()) throw Exception("DouYin URL cannot be blank")
val videoId = NetUtils.get(shortURL).use { resp ->
Expand All @@ -40,32 +39,36 @@ class DouYinParse(
}

return NetUtils.get("http://117.72.12.207/api/douyin/web/fetch_one_video?aweme_id=$videoId").use { resp ->
val data = mapper.readTree(resp.body?.string())
mapper.readValue(data["data"].asText(), DouYinParseDTO::class.java) ?: throw Exception("DouYin parse failed")
val jsonStr = resp.body?.string().orEmpty()
val data = mapper.readTree(jsonStr)
mapper.treeToValue(data["data"], DouYinParseDTO::class.java)
}.detail
}
}.getOrThrow()

@EventHandler
@MessageHandlerFilter(at = AtEnum.NEED)
fun handler(event: AnyMessageEvent) {
try {
if (!RegexUtils.check(event.message, Regex.DOU_YIN_SHORT_URL)) return
val bot = event.bot
val messageId = event.messageId
bot.sendMsg(
event = event,
msg = MsgUtils.builder().reply(messageId).text("好的,宝子").build(),
)
coroutine.launch {
if (!RegexUtils.check(event.message, Regex.DOU_YIN_SHORT_URL)) return
val bot = event.bot
val messageId = event.messageId
bot.sendMsg(
event = event,
msg = MsgUtils.builder().reply(messageId).text("好的,宝贝,请稍等").build(),
)
coroutine.launch {
kotlin.runCatching {
val data = request(event.message)
val msg = MsgUtils.builder()
.video(data.video.play.urls[0], data.video.cover.urls[0])
.build()
val echo = bot.sendMsg(event, msg, false)
log.info("$echo")
bot.sendMsg(event, msg, false)
}.onFailure {
val msg = MsgUtils.builder()
.reply(messageId)
.text("哦,失败了嘛,不要灰心: ${it.message}")
.build()
bot.sendMsg(event, msg)
}
} catch (e: Exception) {
throw e
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,5 @@ interface DouYinVideoRepository : JpaRepository<DouYinVideoEntity, Int> {
fun findAllTags(): List<String>

@Transactional
override fun <S : DouYinVideoEntity> save(entity: S): S {
TODO("Not yet implemented")
}
override fun <S : DouYinVideoEntity> save(entity: S): S
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,5 @@ interface DriftBottleRepository: JpaRepository<DriftBottleEntity, Int> {
fun countAllByOpenIsFalse(): Int

@Transactional
override fun <S : DriftBottleEntity> save(entity: S): S{
TODO("Not yet implemented")
}
override fun <S : DriftBottleEntity> save(entity: S): S
}

0 comments on commit f9b2da2

Please sign in to comment.