feat: 写入 upper 头像,整理代码

This commit is contained in:
amtoaer
2024-03-27 23:26:22 +08:00
parent 986bb11a13
commit 29d893e3fc
4 changed files with 92 additions and 45 deletions

View File

@@ -1,43 +1,60 @@
use std::sync::Arc;
use entity::video;
use futures_util::{pin_mut, StreamExt};
use log::info;
use sea_orm::entity::prelude::*;
use sea_orm::ActiveValue::Set;
use crate::bilibili::{BiliClient, FavoriteList, Video};
use crate::core::utils::{
create_video_pages, create_videos, exists_bvids_favtime, filter_videos, handle_favorite_info,
create_video_pages, create_videos, exist_labels, filter_videos, handle_favorite_info,
};
use crate::Result;
pub async fn process_favorite(
bili_client: Arc<BiliClient>,
fid: i32,
fid: &str,
connection: Arc<DatabaseConnection>,
) -> Result<()> {
let favorite_list = FavoriteList::new(bili_client.clone(), fid.to_string());
let info = favorite_list.get_info().await?;
let favorite_obj = handle_favorite_info(&info, connection.as_ref()).await?;
println!(
"Hi there! I'm going to scan this favorite: {:?}",
favorite_obj
);
let video_stream = favorite_list.into_video_stream().chunks(10);
refresh_favorite(bili_client.clone(), fid, connection.clone()).await?;
download_favorite(bili_client.clone(), fid, connection.clone()).await?;
Ok(())
}
pub async fn refresh_favorite(
bili_client: Arc<BiliClient>,
fid: &str,
connection: Arc<DatabaseConnection>,
) -> Result<()> {
let bili_favorite_list = FavoriteList::new(bili_client.clone(), fid.to_owned());
let favorite_list_info = bili_favorite_list.get_info().await?;
let favorite_model = handle_favorite_info(&favorite_list_info, connection.as_ref()).await?;
info!("Scan the favorite: {fid}");
let video_stream = bili_favorite_list.into_video_stream().chunks(10);
pin_mut!(video_stream);
while let Some(videos_info) = video_stream.next().await {
let exist_bvids_pubtimes =
exists_bvids_favtime(&videos_info, favorite_obj.id, connection.as_ref()).await?;
let exist_labels = exist_labels(&videos_info, &favorite_model, connection.as_ref()).await?;
let should_break = videos_info
.iter()
// 出现 bvid 和 fav_time 都相同的记录,说明已经到达了上次处理到的位置
.any(|v| exist_bvids_pubtimes.contains(&(v.bvid.clone(), v.fav_time.naive_utc())));
create_videos(&videos_info, &favorite_obj, connection.as_ref()).await?;
let all_unprocessed_videos =
filter_videos(&videos_info, &favorite_obj, true, true, connection.as_ref()).await?;
if !all_unprocessed_videos.is_empty() {
for video in all_unprocessed_videos {
let bili_video = Video::new(bili_client.clone(), video.bvid.clone());
let pages = bili_video.get_pages().await?;
create_video_pages(&pages, &video, connection.as_ref()).await?;
.any(|v| exist_labels.contains(&(v.bvid.clone(), v.fav_time.naive_utc())));
create_videos(&videos_info, &favorite_model, connection.as_ref()).await?;
let unrefreshed_video_models = filter_videos(
&videos_info,
&favorite_model,
true,
true,
connection.as_ref(),
)
.await?;
if !unrefreshed_video_models.is_empty() {
for video_model in unrefreshed_video_models {
let bili_video = Video::new(bili_client.clone(), video_model.bvid.clone());
let pages_info = bili_video.get_pages().await?;
create_video_pages(&pages_info, &video_model, connection.as_ref()).await?;
let mut video_active_model: video::ActiveModel = video_model.into();
video_active_model.single_page = Set(Some(pages_info.len() == 1));
video_active_model.save(connection.as_ref()).await?;
}
}
if should_break {
@@ -46,3 +63,12 @@ pub async fn process_favorite(
}
Ok(())
}
#[allow(unused_variables)]
pub async fn download_favorite(
bili_client: Arc<BiliClient>,
fid: &str,
connection: Arc<DatabaseConnection>,
) -> Result<()> {
todo!();
}