From 2dd01c8d3047d0a6cf27322914884ceb35b37544 Mon Sep 17 00:00:00 2001 From: Louis <836250617@qq.com> Date: Tue, 20 Feb 2024 03:09:25 +0800 Subject: [PATCH] handle expired; pull bvid is force now --- CHANGELOG.md | 6 +++++- Cargo.lock | 4 ++-- Cargo.toml | 2 +- fav_cli/Cargo.toml | 2 +- fav_cli/src/bili/action.rs | 26 +++++++++++++++++--------- fav_core/src/error.rs | 12 ++++++++---- fav_utils/Cargo.toml | 2 +- fav_utils/src/bili/ops.rs | 5 ++++- 8 files changed, 39 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dacf636..0f7fa79 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,11 +9,15 @@ Note: In this file, do not use the hard wrap in the middle of a sentence for com --> ## [Unreleased] +## [0.2.1] - 2024-02-20 +- Handle Expired. +- Pull: If `pull bvid`, `fav` will force to pull it, as long as it's tracked and not expired. + ## [0.2.0] - 2024-02-20 - Broken upgrade: the new `fav` is not compatible with the old `fav`. You need to delete `.fav` dir and re-`init` your `fav` after upgrading to `0.2.0`. - Refactor: `fav` is completely rewritten in rusty style, and is now more generic and more maintainable. - Simplify: Only `fetch` `pull` `status` `track` `init` `auth` `daemon` `completion` commands are supported now. The `modify` command is removed, since it's too tedious to modify status through a CLI tool. -- Status: Now `status` only show id, title and few status. +- Status: Now `status` only show id, title and few status.What's more, use --sets instead of --list, --res instead of --video - Track: Now `track` does not support resource not in remote favorite sets. (In other words, there's no data only in local, but not in remote.) - Pull: Now `pull` will call `fetch` first, and resources not tracked and fetched will never able to be pulled. - Init: Only support bilibili now, so no args needed after `init`. diff --git a/Cargo.lock b/Cargo.lock index 6ff5da7..f857b1a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -357,7 +357,7 @@ dependencies = [ [[package]] name = "fav_cli" -version = "0.0.1-alpha1" +version = "0.0.1-alpha2" dependencies = [ "chrono", "clap", @@ -405,7 +405,7 @@ dependencies = [ [[package]] name = "fav_utils" -version = "0.0.1-beta1" +version = "0.0.1-beta2" dependencies = [ "fav_core", "indicatif", diff --git a/Cargo.toml b/Cargo.toml index 0a96c47..a82f006 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,7 @@ documentation = "" [workspace.dependencies] fav_core = { path = "fav_core", version = "0.0.1-beta2" } fav_derive = { path = "fav_derive", version = "0.0.1-beta1" } -fav_utils = { path = "fav_utils", version = "0.0.1-beta1" } +fav_utils = { path = "fav_utils", version = "0.0.1-beta2" } fav_cli = { path = "fav_cli" } fav_utils_old = { path = "fav_utils_old" } diff --git a/fav_cli/Cargo.toml b/fav_cli/Cargo.toml index 1072012..7a446d2 100644 --- a/fav_cli/Cargo.toml +++ b/fav_cli/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "fav_cli" -version = "0.0.1-alpha1" +version = "0.0.1-alpha2" authors.workspace = true description = "Fav's CLI, helping persist the remote source. Repo: https://github.com/kingwingfly/fav" license.workspace = true diff --git a/fav_cli/src/bili/action.rs b/fav_cli/src/bili/action.rs index 53eabcc..d94bdf3 100644 --- a/fav_cli/src/bili/action.rs +++ b/fav_cli/src/bili/action.rs @@ -32,7 +32,7 @@ pub fn status(id: String) -> FavCoreResult<()> { } else if let Some(r) = try_find_res(&mut sets, &id_) { r.table(); } else { - return Err(FavCoreError::IdNotFound(id)); + return Err(FavCoreError::IdNotUsable(id)); } Ok(()) } @@ -59,7 +59,11 @@ pub(super) async fn fetch() -> FavCoreResult<()> { let mut sub = sets.subset(|s| s.check_status(StatusFlags::TRACK)); bili.batch_fetch_set(&mut sub).await?; for set in sub.iter_mut() { - let mut sub = set.subset(|r| r.check_status(StatusFlags::TRACK)); + let mut sub = set.subset(|r| { + r.check_status(StatusFlags::TRACK) + & !r.check_status(StatusFlags::FETCHED) + & !r.check_status(StatusFlags::EXPIRED) + }); bili.batch_fetch_res(&mut sub).await?; } sets.write() @@ -74,7 +78,7 @@ pub(super) fn track(id: String) -> FavCoreResult<()> { } else if let Some(r) = try_find_res(&mut sets, &id_) { r.on_status(StatusFlags::TRACK); } else { - return Err(FavCoreError::IdNotFound(id)); + return Err(FavCoreError::IdNotUsable(id)); } sets.write() } @@ -88,7 +92,7 @@ pub(super) fn untrack(id: String) -> FavCoreResult<()> { } else if let Some(r) = try_find_res(&mut sets, &id_) { r.off_status(StatusFlags::TRACK); } else { - return Err(FavCoreError::IdNotFound(id)); + return Err(FavCoreError::IdNotUsable(id)); } sets.write() } @@ -101,6 +105,7 @@ pub(super) async fn pull_all() -> FavCoreResult<()> { for set in sub.iter_mut() { let mut sub = set.subset(|r| { !r.check_status(StatusFlags::SAVED) + & !r.check_status(StatusFlags::EXPIRED) & r.check_status(StatusFlags::TRACK | StatusFlags::FETCHED) }); bili.batch_pull_res(&mut sub).await?; @@ -114,17 +119,20 @@ pub(super) async fn pull(id: String) -> FavCoreResult<()> { let mut sets = BiliSets::read()?; let id_ = Id::from(&id); if let Some(s) = try_find_set(&mut sets, &id_) { - let mut sub = - s.subset(|r| !r.check_status(StatusFlags::SAVED) & r.check_status(StatusFlags::TRACK)); + let mut sub = s.subset(|r| { + !r.check_status(StatusFlags::SAVED) + & !r.check_status(StatusFlags::EXPIRED) + & r.check_status(StatusFlags::TRACK | StatusFlags::FETCHED) + }); bili.batch_pull_res(&mut sub).await?; } else if let Some(r) = try_find_res(&mut sets, &id_) { - if !r.check_status(StatusFlags::SAVED) + if !r.check_status(StatusFlags::EXPIRED) & r.check_status(StatusFlags::TRACK | StatusFlags::FETCHED) { bili.pull_res(r).await?; } } else { - return Err(FavCoreError::IdNotFound(id)); + return Err(FavCoreError::IdNotUsable(id)); } sets.write() } @@ -140,7 +148,7 @@ pub(crate) async fn daemon(interval: u64) { .format("%Y-%m-%d %H:%M:%S") .to_string(); info!( - "Next job will be {} minutes later at {}.", + "Next job will be {} minutes later at {}.\n", interval, next_ts_local ); tokio::select! { diff --git a/fav_core/src/error.rs b/fav_core/src/error.rs index 604223c..a2c3c6b 100644 --- a/fav_core/src/error.rs +++ b/fav_core/src/error.rs @@ -25,8 +25,8 @@ pub enum FavCoreError { ProtobufError(protobuf::Error), /// IO error IoError(std::io::Error), - /// Id not found - IdNotFound(String), + /// Id not usable + IdNotUsable(String), } impl std::fmt::Display for FavCoreError { @@ -44,8 +44,12 @@ impl std::fmt::Display for FavCoreError { FavCoreError::IoError(source) => { write!(f, "IOErr: {}; Maybe you didn't run `fav init`", source) } - FavCoreError::IdNotFound(source) => { - write!(f, "Id<{}> not found", source) + FavCoreError::IdNotUsable(source) => { + write!( + f, + "Id<{}> not usable; Or maybe the resource is expired", + source + ) } } } diff --git a/fav_utils/Cargo.toml b/fav_utils/Cargo.toml index e015776..77f21a3 100644 --- a/fav_utils/Cargo.toml +++ b/fav_utils/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "fav_utils" -version = "0.0.1-beta1" +version = "0.0.1-beta2" authors.workspace = true description = "Fav's utils crate; A collection of utilities and data structures for the fav project" license.workspace = true diff --git a/fav_utils/src/bili/ops.rs b/fav_utils/src/bili/ops.rs index b9d4ef9..8a1d14c 100644 --- a/fav_utils/src/bili/ops.rs +++ b/fav_utils/src/bili/ops.rs @@ -89,7 +89,10 @@ impl ResOps for Bili { let params = vec![resource.bvid.clone()]; tokio::select! { res = self.request_proto::(ApiKind::FetchRes, params, "/data") => { - *resource |= res?; + match res { + Ok(res) => *resource |= res, + Err(_) => resource.on_status(StatusFlags::EXPIRED), + } resource.on_status(StatusFlags::FETCHED); }, _ = tokio::signal::ctrl_c() => {