mirror of
https://github.com/amtoaer/bili-sync.git
synced 2026-09-05 07:28:04 +08:00
refactor: 移除不必要的标记和代码块,统一 use 格式 (#144)
This commit is contained in:
@@ -11,7 +11,7 @@ use sea_orm::entity::prelude::*;
|
|||||||
use sea_orm::ActiveValue::Set;
|
use sea_orm::ActiveValue::Set;
|
||||||
use sea_orm::{DatabaseConnection, QuerySelect, TransactionTrait};
|
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::bilibili::{BiliClient, BiliError, Collection, CollectionItem, CollectionType, Video, VideoInfo};
|
||||||
use crate::config::TEMPLATE;
|
use crate::config::TEMPLATE;
|
||||||
use crate::utils::id_time_key;
|
use crate::utils::id_time_key;
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ use sea_orm::entity::prelude::*;
|
|||||||
use sea_orm::ActiveValue::Set;
|
use sea_orm::ActiveValue::Set;
|
||||||
use sea_orm::{DatabaseConnection, QuerySelect, TransactionTrait};
|
use sea_orm::{DatabaseConnection, QuerySelect, TransactionTrait};
|
||||||
|
|
||||||
use super::VideoListModel;
|
use crate::adapter::VideoListModel;
|
||||||
use crate::bilibili::{BiliClient, BiliError, FavoriteList, Video, VideoInfo};
|
use crate::bilibili::{BiliClient, BiliError, FavoriteList, Video, VideoInfo};
|
||||||
use crate::config::TEMPLATE;
|
use crate::config::TEMPLATE;
|
||||||
use crate::utils::id_time_key;
|
use crate::utils::id_time_key;
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ use sea_orm::entity::prelude::*;
|
|||||||
use sea_orm::ActiveValue::Set;
|
use sea_orm::ActiveValue::Set;
|
||||||
use sea_orm::{DatabaseConnection, QuerySelect, TransactionTrait};
|
use sea_orm::{DatabaseConnection, QuerySelect, TransactionTrait};
|
||||||
|
|
||||||
use super::VideoListModel;
|
use crate::adapter::VideoListModel;
|
||||||
use crate::bilibili::{BiliClient, BiliError, Video, VideoInfo, WatchLater};
|
use crate::bilibili::{BiliClient, BiliError, Video, VideoInfo, WatchLater};
|
||||||
use crate::config::TEMPLATE;
|
use crate::config::TEMPLATE;
|
||||||
use crate::utils::id_time_key;
|
use crate::utils::id_time_key;
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
use std::fmt::{Display, Formatter};
|
use std::fmt::{Display, Formatter};
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use arc_swap::access::Access;
|
|
||||||
use async_stream::stream;
|
use async_stream::stream;
|
||||||
use futures::Stream;
|
use futures::Stream;
|
||||||
use reqwest::Method;
|
use reqwest::Method;
|
||||||
|
|||||||
@@ -133,6 +133,14 @@ mod tests {
|
|||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_video_info_type() {
|
async fn test_video_info_type() {
|
||||||
let bili_client = BiliClient::new();
|
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());
|
let video = Video::new(&bili_client, "BV1Z54y1C7ZB".to_string());
|
||||||
assert!(matches!(video.get_view_info().await, Ok(VideoInfo::View { .. })));
|
assert!(matches!(video.get_view_info().await, Ok(VideoInfo::View { .. })));
|
||||||
let collection_item = CollectionItem {
|
let collection_item = CollectionItem {
|
||||||
|
|||||||
@@ -4,12 +4,11 @@ use futures::TryStreamExt;
|
|||||||
use prost::Message;
|
use prost::Message;
|
||||||
use reqwest::Method;
|
use reqwest::Method;
|
||||||
|
|
||||||
use super::credential::encoded_query;
|
|
||||||
use super::MIXIN_KEY;
|
|
||||||
use crate::bilibili::analyzer::PageAnalyzer;
|
use crate::bilibili::analyzer::PageAnalyzer;
|
||||||
use crate::bilibili::client::BiliClient;
|
use crate::bilibili::client::BiliClient;
|
||||||
|
use crate::bilibili::credential::encoded_query;
|
||||||
use crate::bilibili::danmaku::{DanmakuElem, DanmakuWriter, DmSegMobileReply};
|
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 MASK_CODE: u64 = 2251799813685247;
|
||||||
static XOR_CODE: u64 = 23442827791579;
|
static XOR_CODE: u64 = 23442827791579;
|
||||||
@@ -63,7 +62,6 @@ impl<'a> Video<'a> {
|
|||||||
Self { client, aid, bvid }
|
Self { client, aid, bvid }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
/// 直接调用视频信息接口获取详细的视频信息
|
/// 直接调用视频信息接口获取详细的视频信息
|
||||||
pub async fn get_view_info(&self) -> Result<VideoInfo> {
|
pub async fn get_view_info(&self) -> Result<VideoInfo> {
|
||||||
let mut res = self
|
let mut res = self
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ pub struct Status(u32);
|
|||||||
impl Status {
|
impl Status {
|
||||||
/// 如果 status 整体大于等于 1 << 31,则表示任务已经被处理过,不再需要重试。
|
/// 如果 status 整体大于等于 1 << 31,则表示任务已经被处理过,不再需要重试。
|
||||||
/// 数据库可以使用 status < Status::handled() 来筛选需要处理的内容。
|
/// 数据库可以使用 status < Status::handled() 来筛选需要处理的内容。
|
||||||
pub fn handled() -> u32 {
|
pub const fn handled() -> u32 {
|
||||||
1 << 31
|
1 << 31
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,19 +32,16 @@ impl Status {
|
|||||||
|
|
||||||
/// 从低到高检查状态,如果该位置的任务应该继续尝试执行,则返回 true,否则返回 false
|
/// 从低到高检查状态,如果该位置的任务应该继续尝试执行,则返回 true,否则返回 false
|
||||||
fn should_run(&self, size: usize) -> Vec<bool> {
|
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()
|
(0..size).map(|x| self.check_continue(x)).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 如果任务的执行次数小于 STATUS_MAX_RETRY,说明可以继续运行
|
/// 如果任务的执行次数小于 STATUS_MAX_RETRY,说明可以继续运行
|
||||||
fn check_continue(&self, offset: usize) -> bool {
|
fn check_continue(&self, offset: usize) -> bool {
|
||||||
assert!(offset < 10, "u32 can only store 10 status");
|
|
||||||
self.get_status(offset) < STATUS_MAX_RETRY
|
self.get_status(offset) < STATUS_MAX_RETRY
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 根据任务结果更新状态,如果任务成功,设置为 STATUS_OK,否则加一
|
/// 根据任务结果更新状态,如果任务成功,设置为 STATUS_OK,否则加一
|
||||||
fn update_status(&mut self, result: &[Result<()>]) {
|
fn update_status(&mut self, result: &[Result<()>]) {
|
||||||
assert!(result.len() < 10, "u32 can only store 10 status");
|
|
||||||
for (i, res) in result.iter().enumerate() {
|
for (i, res) in result.iter().enumerate() {
|
||||||
self.set_result(res, i);
|
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) {
|
fn plus_one(&mut self, offset: usize) {
|
||||||
self.0 += 1 << (3 * offset);
|
self.0 += 1 << (3 * offset);
|
||||||
}
|
}
|
||||||
@@ -84,10 +70,6 @@ impl Status {
|
|||||||
self.0 |= STATUS_OK << (3 * offset);
|
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 {
|
fn get_status(&self, offset: usize) -> u32 {
|
||||||
let helper = !0u32;
|
let helper = !0u32;
|
||||||
(self.0 & (helper << (offset * 3)) & (helper >> (32 - 3 * offset - 3))) >> (offset * 3)
|
(self.0 & (helper << (offset * 3)) & (helper >> (32 - 3 * offset - 3))) >> (offset * 3)
|
||||||
@@ -109,11 +91,6 @@ impl VideoStatus {
|
|||||||
Self(Status::new(status))
|
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> {
|
pub fn should_run(&self) -> Vec<bool> {
|
||||||
self.0.should_run(5)
|
self.0.should_run(5)
|
||||||
}
|
}
|
||||||
@@ -139,11 +116,6 @@ impl PageStatus {
|
|||||||
Self(Status::new(status))
|
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> {
|
pub fn should_run(&self) -> Vec<bool> {
|
||||||
self.0.should_run(4)
|
self.0.should_run(4)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
#![allow(dead_code, unused_variables)]
|
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
@@ -31,7 +29,7 @@ pub async fn process_video_list(
|
|||||||
connection: &DatabaseConnection,
|
connection: &DatabaseConnection,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let (video_list_model, video_streams) = video_list_from(args, path, bili_client, connection).await?;
|
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?;
|
let video_list_model = fetch_video_details(bili_client, video_list_model, connection).await?;
|
||||||
if ARGS.scan_only {
|
if ARGS.scan_only {
|
||||||
warn!("已开启仅扫描模式,跳过视频下载...");
|
warn!("已开启仅扫描模式,跳过视频下载...");
|
||||||
@@ -42,7 +40,6 @@ pub async fn process_video_list(
|
|||||||
|
|
||||||
/// 请求接口,获取视频列表中所有新添加的视频信息,将其写入数据库
|
/// 请求接口,获取视频列表中所有新添加的视频信息,将其写入数据库
|
||||||
pub async fn refresh_video_list<'a>(
|
pub async fn refresh_video_list<'a>(
|
||||||
bili_client: &'a BiliClient,
|
|
||||||
video_list_model: Box<dyn VideoListModel>,
|
video_list_model: Box<dyn VideoListModel>,
|
||||||
video_streams: Pin<Box<dyn Stream<Item = VideoInfo> + 'a>>,
|
video_streams: Pin<Box<dyn Stream<Item = VideoInfo> + 'a>>,
|
||||||
connection: &DatabaseConnection,
|
connection: &DatabaseConnection,
|
||||||
|
|||||||
Reference in New Issue
Block a user