Skip to content

Commit

Permalink
refactor: 移除不必要的标记和代码块,统一 use 格式 (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
amtoaer authored Jul 21, 2024
1 parent 86e8580 commit 33e758b
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 41 deletions.
2 changes: 1 addition & 1 deletion crates/bili_sync/src/adapter/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion crates/bili_sync/src/adapter/favorite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion crates/bili_sync/src/adapter/watch_later.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion crates/bili_sync/src/bilibili/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions crates/bili_sync/src/bilibili/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 2 additions & 4 deletions crates/bili_sync/src/bilibili/video.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -63,7 +62,6 @@ impl<'a> Video<'a> {
Self { client, aid, bvid }
}

#[allow(dead_code)]
/// 直接调用视频信息接口获取详细的视频信息
pub async fn get_view_info(&self) -> Result<VideoInfo> {
let mut res = self
Expand Down
30 changes: 1 addition & 29 deletions crates/bili_sync/src/utils/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -32,19 +32,16 @@ impl Status {

/// 从低到高检查状态,如果该位置的任务应该继续尝试执行,则返回 true,否则返回 false
fn should_run(&self, size: usize) -> Vec<bool> {
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);
}
Expand All @@ -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);
}
Expand All @@ -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)
Expand All @@ -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<bool> {
self.0.should_run(5)
}
Expand All @@ -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<bool> {
self.0.should_run(4)
}
Expand Down
5 changes: 1 addition & 4 deletions crates/bili_sync/src/workflow.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![allow(dead_code, unused_variables)]

use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::pin::Pin;
Expand Down Expand Up @@ -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!("已开启仅扫描模式,跳过视频下载...");
Expand All @@ -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<dyn VideoListModel>,
video_streams: Pin<Box<dyn Stream<Item = VideoInfo> + 'a>>,
connection: &DatabaseConnection,
Expand Down

0 comments on commit 33e758b

Please sign in to comment.