Skip to content

Commit

Permalink
refactor: 重构一下代码
Browse files Browse the repository at this point in the history
  • Loading branch information
amtoaer committed Jun 28, 2024
1 parent 76f61a2 commit 0f237e0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 25 deletions.
19 changes: 6 additions & 13 deletions crates/bili_sync/src/adapter/collection.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashSet;

use anyhow::{anyhow, Result};
use anyhow::Result;
use bili_sync_entity::*;
use sea_orm::entity::prelude::*;
use sea_orm::ActiveValue::Set;
Expand Down Expand Up @@ -84,14 +84,9 @@ impl VideoListModel for collection::Model {
) -> Result<()> {
for video_model in videos_model {
let video = Video::new(bili_clent, video_model.bvid.clone());
let tags = video.get_tags().await;
let view_info = if tags.is_ok() {
video.get_view_info().await
} else {
Err(anyhow!(""))
};
match (tags, view_info) {
(Ok(tags), Ok(view_info)) => {
let info: Result<_> = async { Ok((video.get_tags().await?, video.get_view_info().await?)) }.await;
match info {
Ok((tags, view_info)) => {
let VideoInfo::View { pages, .. } = &view_info else {
unreachable!("view_info must be VideoInfo::View")
};
Expand All @@ -105,9 +100,7 @@ impl VideoListModel for collection::Model {
video_active_model.save(&txn).await?;
txn.commit().await?;
}
(tags_err, pages_err) => {
// 两个值都是 ok 的情况不会执行到这里,因此 unwrap 是安全的
let e = tags_err.err().or(pages_err.err()).unwrap();
Err(e) => {
error!(
"获取视频 {} - {} 的详细信息失败,错误为:{}",
&video_model.bvid, &video_model.name, e
Expand All @@ -121,7 +114,7 @@ impl VideoListModel for collection::Model {
}
continue;
}
};
}
}
Ok(())
}
Expand Down
17 changes: 5 additions & 12 deletions crates/bili_sync/src/adapter/favorite.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashSet;

use anyhow::{anyhow, Result};
use anyhow::Result;
use bili_sync_entity::*;
use sea_orm::entity::prelude::*;
use sea_orm::ActiveValue::Set;
Expand Down Expand Up @@ -84,14 +84,9 @@ impl VideoListModel for favorite::Model {
) -> Result<()> {
for video_model in videos_model {
let video = Video::new(bili_clent, video_model.bvid.clone());
let tags = video.get_tags().await;
let pages_info = if tags.is_ok() {
video.get_pages().await
} else {
Err(anyhow!(""))
};
match (tags, pages_info) {
(Ok(tags), Ok(pages_info)) => {
let info: Result<_> = async { Ok((video.get_tags().await?, video.get_pages().await?)) }.await;
match info {
Ok((tags, pages_info)) => {
let txn = connection.begin().await?;
// 将分页信息写入数据库
create_video_pages(&pages_info, &video_model, &txn).await?;
Expand All @@ -102,9 +97,7 @@ impl VideoListModel for favorite::Model {
video_active_model.save(&txn).await?;
txn.commit().await?;
}
(tags_err, pages_err) => {
// 两个值都是 ok 的情况不会执行到这里,因此 unwrap 是安全的
let e = tags_err.err().or(pages_err.err()).unwrap();
Err(e) => {
error!(
"获取视频 {} - {} 的详细信息失败,错误为:{}",
&video_model.bvid, &video_model.name, e
Expand Down

0 comments on commit 0f237e0

Please sign in to comment.