mirror of
https://github.com/amtoaer/bili-sync.git
synced 2026-09-06 07:57:12 +08:00
fix: 修复部分写法错误
This commit is contained in:
+30
-17
@@ -3,7 +3,6 @@ use std::path::{Path, PathBuf};
|
|||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
|
|
||||||
use anyhow::{bail, Result};
|
use anyhow::{bail, Result};
|
||||||
use dirs::config_dir;
|
|
||||||
use entity::{favorite, page, video};
|
use entity::{favorite, page, video};
|
||||||
use filenamify::filenamify;
|
use filenamify::filenamify;
|
||||||
use futures::stream::{FuturesOrdered, FuturesUnordered};
|
use futures::stream::{FuturesOrdered, FuturesUnordered};
|
||||||
@@ -17,8 +16,11 @@ use tokio::fs;
|
|||||||
use tokio::sync::{Mutex, Semaphore};
|
use tokio::sync::{Mutex, Semaphore};
|
||||||
|
|
||||||
use super::status::{PageStatus, VideoStatus};
|
use super::status::{PageStatus, VideoStatus};
|
||||||
use super::utils::{unhandled_videos_pages, ModelWrapper, NFOMode, NFOSerializer, TEMPLATE};
|
use super::utils::{
|
||||||
|
unhandled_videos_pages, update_pages_model, update_videos_model, ModelWrapper, NFOMode, NFOSerializer, TEMPLATE,
|
||||||
|
};
|
||||||
use crate::bilibili::{BestStream, BiliClient, BiliError, FavoriteList, FilterOption, PageInfo, Video};
|
use crate::bilibili::{BestStream, BiliClient, BiliError, FavoriteList, FilterOption, PageInfo, Video};
|
||||||
|
use crate::config::CONFIG;
|
||||||
use crate::core::utils::{
|
use crate::core::utils::{
|
||||||
create_video_pages, create_videos, exist_labels, filter_unfilled_videos, handle_favorite_info, total_video_count,
|
create_video_pages, create_videos, exist_labels, filter_unfilled_videos, handle_favorite_info, total_video_count,
|
||||||
};
|
};
|
||||||
@@ -135,10 +137,15 @@ pub async fn download_unprocessed_videos(
|
|||||||
// 对于视频,允许五个同时下载(视频内还有分页、不同分页还有多种下载任务)
|
// 对于视频,允许五个同时下载(视频内还有分页、不同分页还有多种下载任务)
|
||||||
let semaphore = Semaphore::new(5);
|
let semaphore = Semaphore::new(5);
|
||||||
let downloader = Downloader::default();
|
let downloader = Downloader::default();
|
||||||
let mut uppers_mutex: HashMap<i32, (Mutex<()>, Mutex<()>)> = HashMap::new();
|
let mut uppers_mutex: HashMap<i64, (Mutex<()>, Mutex<()>)> = HashMap::new();
|
||||||
for (video_model, _) in &unhandled_videos_pages {
|
for (video_model, _) in &unhandled_videos_pages {
|
||||||
uppers_mutex.insert(video_model.upper_id, (Mutex::new(()), Mutex::new(())));
|
uppers_mutex.insert(video_model.upper_id, (Mutex::new(()), Mutex::new(())));
|
||||||
}
|
}
|
||||||
|
let upper_path = {
|
||||||
|
let config = CONFIG.lock().unwrap();
|
||||||
|
config.upper_path.clone()
|
||||||
|
};
|
||||||
|
let upper_path = Path::new(&upper_path);
|
||||||
let mut tasks = unhandled_videos_pages
|
let mut tasks = unhandled_videos_pages
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|(video_model, pages_model)| {
|
.map(|(video_model, pages_model)| {
|
||||||
@@ -150,6 +157,7 @@ pub async fn download_unprocessed_videos(
|
|||||||
connection,
|
connection,
|
||||||
&semaphore,
|
&semaphore,
|
||||||
&downloader,
|
&downloader,
|
||||||
|
upper_path,
|
||||||
upper_mutex,
|
upper_mutex,
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
@@ -169,18 +177,18 @@ pub async fn download_unprocessed_videos(
|
|||||||
}
|
}
|
||||||
// 满十个就写入数据库
|
// 满十个就写入数据库
|
||||||
if models.len() == 10 {
|
if models.len() == 10 {
|
||||||
video::Entity::insert_many(std::mem::replace(&mut models, Vec::with_capacity(10)))
|
update_videos_model(std::mem::replace(&mut models, Vec::with_capacity(10)), connection).await?;
|
||||||
.exec(connection)
|
|
||||||
.await?;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !models.is_empty() {
|
if !models.is_empty() {
|
||||||
video::Entity::insert_many(models).exec(connection).await?;
|
update_videos_model(models, connection).await?;
|
||||||
}
|
}
|
||||||
info!("download videos in favorite: {} done.", favorite_model.f_id);
|
info!("download videos in favorite: {} done.", favorite_model.f_id);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 暂时这样做,后面提取成上下文
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
pub async fn download_video_pages(
|
pub async fn download_video_pages(
|
||||||
bili_client: &BiliClient,
|
bili_client: &BiliClient,
|
||||||
video_model: video::Model,
|
video_model: video::Model,
|
||||||
@@ -188,6 +196,7 @@ pub async fn download_video_pages(
|
|||||||
connection: &DatabaseConnection,
|
connection: &DatabaseConnection,
|
||||||
semaphore: &Semaphore,
|
semaphore: &Semaphore,
|
||||||
downloader: &Downloader,
|
downloader: &Downloader,
|
||||||
|
upper_path: &Path,
|
||||||
upper_mutex: &(Mutex<()>, Mutex<()>),
|
upper_mutex: &(Mutex<()>, Mutex<()>),
|
||||||
) -> Result<video::ActiveModel> {
|
) -> Result<video::ActiveModel> {
|
||||||
let permit = semaphore.acquire().await;
|
let permit = semaphore.acquire().await;
|
||||||
@@ -199,10 +208,7 @@ pub async fn download_video_pages(
|
|||||||
|
|
||||||
let base_path = Path::new(&video_model.path);
|
let base_path = Path::new(&video_model.path);
|
||||||
let upper_id = video_model.upper_id.to_string();
|
let upper_id = video_model.upper_id.to_string();
|
||||||
let base_upper_path = config_dir()
|
let base_upper_path = upper_path
|
||||||
.unwrap()
|
|
||||||
.join("bili-sync")
|
|
||||||
.join("upper")
|
|
||||||
.join(upper_id.chars().next().unwrap().to_string())
|
.join(upper_id.chars().next().unwrap().to_string())
|
||||||
.join(upper_id);
|
.join(upper_id);
|
||||||
|
|
||||||
@@ -292,12 +298,18 @@ pub async fn dispatch_download_page(
|
|||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|page_model| download_page(bili_client, video_model, page_model, &child_semaphore, downloader))
|
.map(|page_model| download_page(bili_client, video_model, page_model, &child_semaphore, downloader))
|
||||||
.collect::<FuturesUnordered<_>>();
|
.collect::<FuturesUnordered<_>>();
|
||||||
// 任务结束会返回 Result<PageModel>
|
|
||||||
|
|
||||||
let mut models = Vec::with_capacity(10);
|
let mut models = Vec::with_capacity(10);
|
||||||
|
let mut should_error = false;
|
||||||
while let Some(res) = tasks.next().await {
|
while let Some(res) = tasks.next().await {
|
||||||
match res {
|
match res {
|
||||||
Ok(model) => {
|
Ok(model) => {
|
||||||
|
if let Set(status) = model.download_status {
|
||||||
|
let status = PageStatus::new(status);
|
||||||
|
if status.should_run().iter().any(|v| *v) {
|
||||||
|
// 有一个分页没下载完成,就应该将视频本身标记为未完成
|
||||||
|
should_error = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
models.push(model);
|
models.push(model);
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
@@ -308,13 +320,14 @@ pub async fn dispatch_download_page(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if models.len() == 10 {
|
if models.len() == 10 {
|
||||||
page::Entity::insert_many(std::mem::replace(&mut models, Vec::with_capacity(10)))
|
update_pages_model(std::mem::replace(&mut models, Vec::with_capacity(10)), connection).await?;
|
||||||
.exec(connection)
|
|
||||||
.await?;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !models.is_empty() {
|
if !models.is_empty() {
|
||||||
page::Entity::insert_many(models).exec(connection).await?;
|
update_pages_model(models, connection).await?;
|
||||||
|
}
|
||||||
|
if should_error {
|
||||||
|
bail!("Some pages failed to download");
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -225,6 +225,29 @@ pub async fn unhandled_videos_pages(
|
|||||||
.all(connection)
|
.all(connection)
|
||||||
.await?)
|
.await?)
|
||||||
}
|
}
|
||||||
|
/// 更新视频 model 的下载状态
|
||||||
|
pub async fn update_videos_model(videos: Vec<video::ActiveModel>, connection: &DatabaseConnection) -> Result<()> {
|
||||||
|
video::Entity::insert_many(videos)
|
||||||
|
.on_conflict(
|
||||||
|
OnConflict::column(video::Column::Id)
|
||||||
|
.update_column(video::Column::DownloadStatus)
|
||||||
|
.to_owned(),
|
||||||
|
)
|
||||||
|
.exec(connection)
|
||||||
|
.await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 更新视频页 model 的下载状态
|
||||||
|
pub async fn update_pages_model(pages: Vec<page::ActiveModel>, connection: &DatabaseConnection) -> Result<()> {
|
||||||
|
let query = page::Entity::insert_many(pages).on_conflict(
|
||||||
|
OnConflict::column(page::Column::Id)
|
||||||
|
.update_columns([page::Column::DownloadStatus, page::Column::Path])
|
||||||
|
.to_owned(),
|
||||||
|
);
|
||||||
|
query.exec(connection).await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
/// serde xml 似乎不太好用,先这么裸着写
|
/// serde xml 似乎不太好用,先这么裸着写
|
||||||
/// (真是又臭又长啊
|
/// (真是又臭又长啊
|
||||||
|
|||||||
Reference in New Issue
Block a user