From 33e758bd91e8fa7c6644953400cd6b4d408f775d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=B4=80=E1=B4=8D=E1=B4=9B=E1=B4=8F=E1=B4=80=E1=B4=87?= =?UTF-8?q?=CA=80?= Date: Sun, 21 Jul 2024 19:16:52 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=A7=BB=E9=99=A4=E4=B8=8D?= =?UTF-8?q?=E5=BF=85=E8=A6=81=E7=9A=84=E6=A0=87=E8=AE=B0=E5=92=8C=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E5=9D=97=EF=BC=8C=E7=BB=9F=E4=B8=80=20use=20=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=20(#144)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/bili_sync/src/adapter/collection.rs | 2 +- crates/bili_sync/src/adapter/favorite.rs | 2 +- crates/bili_sync/src/adapter/watch_later.rs | 2 +- crates/bili_sync/src/bilibili/collection.rs | 1 - crates/bili_sync/src/bilibili/mod.rs | 8 ++++++ crates/bili_sync/src/bilibili/video.rs | 6 ++--- crates/bili_sync/src/utils/status.rs | 30 +-------------------- crates/bili_sync/src/workflow.rs | 5 +--- 8 files changed, 15 insertions(+), 41 deletions(-) diff --git a/crates/bili_sync/src/adapter/collection.rs b/crates/bili_sync/src/adapter/collection.rs index 93e294e..c047d2c 100644 --- a/crates/bili_sync/src/adapter/collection.rs +++ b/crates/bili_sync/src/adapter/collection.rs @@ -11,7 +11,7 @@ use sea_orm::entity::prelude::*; use sea_orm::ActiveValue::Set; use sea_orm::{DatabaseConnection, QuerySelect, TransactionTrait}; -use super::VideoListModel; +use crate::adapter::VideoListModel; use crate::bilibili::{BiliClient, BiliError, Collection, CollectionItem, CollectionType, Video, VideoInfo}; use crate::config::TEMPLATE; use crate::utils::id_time_key; diff --git a/crates/bili_sync/src/adapter/favorite.rs b/crates/bili_sync/src/adapter/favorite.rs index 6ebc55b..7d9d484 100644 --- a/crates/bili_sync/src/adapter/favorite.rs +++ b/crates/bili_sync/src/adapter/favorite.rs @@ -11,7 +11,7 @@ use sea_orm::entity::prelude::*; use sea_orm::ActiveValue::Set; use sea_orm::{DatabaseConnection, QuerySelect, TransactionTrait}; -use super::VideoListModel; +use crate::adapter::VideoListModel; use crate::bilibili::{BiliClient, BiliError, FavoriteList, Video, VideoInfo}; use crate::config::TEMPLATE; use crate::utils::id_time_key; diff --git a/crates/bili_sync/src/adapter/watch_later.rs b/crates/bili_sync/src/adapter/watch_later.rs index 3f8e214..43e0d1c 100644 --- a/crates/bili_sync/src/adapter/watch_later.rs +++ b/crates/bili_sync/src/adapter/watch_later.rs @@ -11,7 +11,7 @@ use sea_orm::entity::prelude::*; use sea_orm::ActiveValue::Set; use sea_orm::{DatabaseConnection, QuerySelect, TransactionTrait}; -use super::VideoListModel; +use crate::adapter::VideoListModel; use crate::bilibili::{BiliClient, BiliError, Video, VideoInfo, WatchLater}; use crate::config::TEMPLATE; use crate::utils::id_time_key; diff --git a/crates/bili_sync/src/bilibili/collection.rs b/crates/bili_sync/src/bilibili/collection.rs index dd788bf..8991616 100644 --- a/crates/bili_sync/src/bilibili/collection.rs +++ b/crates/bili_sync/src/bilibili/collection.rs @@ -3,7 +3,6 @@ use std::fmt::{Display, Formatter}; use anyhow::Result; -use arc_swap::access::Access; use async_stream::stream; use futures::Stream; use reqwest::Method; diff --git a/crates/bili_sync/src/bilibili/mod.rs b/crates/bili_sync/src/bilibili/mod.rs index 65e5a53..16c0138 100644 --- a/crates/bili_sync/src/bilibili/mod.rs +++ b/crates/bili_sync/src/bilibili/mod.rs @@ -133,6 +133,14 @@ mod tests { #[tokio::test] async fn test_video_info_type() { let bili_client = BiliClient::new(); + set_global_mixin_key( + bili_client + .wbi_img() + .await + .map(|x| x.into_mixin_key()) + .unwrap() + .unwrap(), + ); let video = Video::new(&bili_client, "BV1Z54y1C7ZB".to_string()); assert!(matches!(video.get_view_info().await, Ok(VideoInfo::View { .. }))); let collection_item = CollectionItem { diff --git a/crates/bili_sync/src/bilibili/video.rs b/crates/bili_sync/src/bilibili/video.rs index d1e29a7..115b9c5 100644 --- a/crates/bili_sync/src/bilibili/video.rs +++ b/crates/bili_sync/src/bilibili/video.rs @@ -4,12 +4,11 @@ use futures::TryStreamExt; use prost::Message; use reqwest::Method; -use super::credential::encoded_query; -use super::MIXIN_KEY; use crate::bilibili::analyzer::PageAnalyzer; use crate::bilibili::client::BiliClient; +use crate::bilibili::credential::encoded_query; use crate::bilibili::danmaku::{DanmakuElem, DanmakuWriter, DmSegMobileReply}; -use crate::bilibili::{Validate, VideoInfo}; +use crate::bilibili::{Validate, VideoInfo, MIXIN_KEY}; static MASK_CODE: u64 = 2251799813685247; static XOR_CODE: u64 = 23442827791579; @@ -63,7 +62,6 @@ impl<'a> Video<'a> { Self { client, aid, bvid } } - #[allow(dead_code)] /// 直接调用视频信息接口获取详细的视频信息 pub async fn get_view_info(&self) -> Result { let mut res = self diff --git a/crates/bili_sync/src/utils/status.rs b/crates/bili_sync/src/utils/status.rs index eb771da..2708550 100644 --- a/crates/bili_sync/src/utils/status.rs +++ b/crates/bili_sync/src/utils/status.rs @@ -13,7 +13,7 @@ pub struct Status(u32); impl Status { /// 如果 status 整体大于等于 1 << 31,则表示任务已经被处理过,不再需要重试。 /// 数据库可以使用 status < Status::handled() 来筛选需要处理的内容。 - pub fn handled() -> u32 { + pub const fn handled() -> u32 { 1 << 31 } @@ -32,19 +32,16 @@ impl Status { /// 从低到高检查状态,如果该位置的任务应该继续尝试执行,则返回 true,否则返回 false fn should_run(&self, size: usize) -> Vec { - assert!(size < 10, "u32 can only store 10 status"); (0..size).map(|x| self.check_continue(x)).collect() } /// 如果任务的执行次数小于 STATUS_MAX_RETRY,说明可以继续运行 fn check_continue(&self, offset: usize) -> bool { - assert!(offset < 10, "u32 can only store 10 status"); self.get_status(offset) < STATUS_MAX_RETRY } /// 根据任务结果更新状态,如果任务成功,设置为 STATUS_OK,否则加一 fn update_status(&mut self, result: &[Result<()>]) { - assert!(result.len() < 10, "u32 can only store 10 status"); for (i, res) in result.iter().enumerate() { self.set_result(res, i); } @@ -65,17 +62,6 @@ impl Status { } } - /// 根据 mask 设置状态,如果 mask 为 false,则清除对应的状态 - fn set_mask(&mut self, mask: &[bool]) { - assert!(mask.len() < 10, "u32 can only store 10 status"); - for (i, &m) in mask.iter().enumerate() { - if !m { - self.clear(i); - self.set_flag(false); - } - } - } - fn plus_one(&mut self, offset: usize) { self.0 += 1 << (3 * offset); } @@ -84,10 +70,6 @@ impl Status { self.0 |= STATUS_OK << (3 * offset); } - fn clear(&mut self, offset: usize) { - self.0 &= !(STATUS_OK << (3 * offset)); - } - fn get_status(&self, offset: usize) -> u32 { let helper = !0u32; (self.0 & (helper << (offset * 3)) & (helper >> (32 - 3 * offset - 3))) >> (offset * 3) @@ -109,11 +91,6 @@ impl VideoStatus { Self(Status::new(status)) } - pub fn set_mask(&mut self, clear: &[bool]) { - assert!(clear.len() == 5, "VideoStatus should have 5 status"); - self.0.set_mask(clear) - } - pub fn should_run(&self) -> Vec { self.0.should_run(5) } @@ -139,11 +116,6 @@ impl PageStatus { Self(Status::new(status)) } - pub fn set_mask(&mut self, clear: &[bool]) { - assert!(clear.len() == 4, "PageStatus should have 4 status"); - self.0.set_mask(clear) - } - pub fn should_run(&self) -> Vec { self.0.should_run(4) } diff --git a/crates/bili_sync/src/workflow.rs b/crates/bili_sync/src/workflow.rs index ea1bb25..0195cf0 100644 --- a/crates/bili_sync/src/workflow.rs +++ b/crates/bili_sync/src/workflow.rs @@ -1,5 +1,3 @@ -#![allow(dead_code, unused_variables)] - use std::collections::HashMap; use std::path::{Path, PathBuf}; use std::pin::Pin; @@ -31,7 +29,7 @@ pub async fn process_video_list( connection: &DatabaseConnection, ) -> Result<()> { let (video_list_model, video_streams) = video_list_from(args, path, bili_client, connection).await?; - let video_list_model = refresh_video_list(bili_client, video_list_model, video_streams, connection).await?; + let video_list_model = refresh_video_list(video_list_model, video_streams, connection).await?; let video_list_model = fetch_video_details(bili_client, video_list_model, connection).await?; if ARGS.scan_only { warn!("已开启仅扫描模式,跳过视频下载..."); @@ -42,7 +40,6 @@ pub async fn process_video_list( /// 请求接口,获取视频列表中所有新添加的视频信息,将其写入数据库 pub async fn refresh_video_list<'a>( - bili_client: &'a BiliClient, video_list_model: Box, video_streams: Pin + 'a>>, connection: &DatabaseConnection,