From 73f97f937fafb34c0b51fed33685ebb32241be37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=B4=80=E1=B4=8D=E1=B4=9B=E1=B4=8F=E1=B4=80=E1=B4=87?= =?UTF-8?q?=CA=80?= Date: Fri, 31 May 2024 10:46:15 -0700 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=AF=8F=E6=AC=A1=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E5=89=8D=E6=A3=80=E6=9F=A5=E7=99=BB=E5=BD=95=E7=8A=B6=E6=80=81?= =?UTF-8?q?=EF=BC=8C=E9=81=BF=E5=85=8D=E5=87=AD=E6=8D=AE=E5=A4=B1=E6=95=88?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E7=9A=84=E9=9D=9E=E9=A2=84=E6=9C=9F=E8=A1=8C?= =?UTF-8?q?=E4=B8=BA=20(#112)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 每次执行前检查登录状态,避免凭据失效导致的非预期行为 * refactor: 减少代码长度 --- src/bilibili/client.rs | 11 ++++++++++- src/bilibili/credential.rs | 18 ++++++++++++++++++ src/main.rs | 12 ++++++++++-- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/bilibili/client.rs b/src/bilibili/client.rs index 206d3eb..2874d09 100644 --- a/src/bilibili/client.rs +++ b/src/bilibili/client.rs @@ -1,6 +1,6 @@ use std::sync::Arc; -use anyhow::Result; +use anyhow::{bail, Result}; use reqwest::{header, Method}; use crate::bilibili::Credential; @@ -85,4 +85,13 @@ impl BiliClient { CONFIG.credential.store(Some(Arc::new(new_credential))); CONFIG.save() } + + /// 检查凭据是否已设置且有效 + pub async fn is_login(&self) -> Result<()> { + let credential = CONFIG.credential.load(); + let Some(credential) = credential.as_deref() else { + bail!("no credential found"); + }; + credential.is_login(&self.client).await + } } diff --git a/src/bilibili/credential.rs b/src/bilibili/credential.rs index b6ad0de..1fae905 100644 --- a/src/bilibili/credential.rs +++ b/src/bilibili/credential.rs @@ -38,6 +38,24 @@ impl Credential { res["data"]["refresh"].as_bool().ok_or(anyhow!("check refresh failed")) } + /// 需要使用一个需要鉴权的接口来检查是否登录 + /// 此处使用查看用户状态数的接口,该接口返回内容少,请求成本低 + pub async fn is_login(&self, client: &Client) -> Result<()> { + client + .request( + Method::GET, + "https://api.bilibili.com/x/web-interface/nav/stat", + Some(self), + ) + .send() + .await? + .error_for_status()? + .json::() + .await? + .validate()?; + Ok(()) + } + pub async fn refresh(&self, client: &Client) -> Result { let correspond_path = Self::get_correspond_path(); let csrf = self.get_refresh_csrf(client, correspond_path).await?; diff --git a/src/main.rs b/src/main.rs index 1433821..bf7df44 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,10 @@ mod database; mod downloader; mod error; +use std::time::Duration; + use once_cell::sync::Lazy; +use tokio::time; use tracing_subscriber::util::SubscriberInitExt; use crate::bilibili::BiliClient; @@ -34,10 +37,15 @@ async fn main() -> ! { let connection = database_connection().await.unwrap(); migrate_database(&connection).await.unwrap(); loop { + if let Err(e) = bili_client.is_login().await { + error!("检查登录状态时遇到错误:{e},等待下一轮执行"); + time::sleep(Duration::from_secs(CONFIG.interval)).await; + continue; + } if anchor != chrono::Local::now().date_naive() { if let Err(e) = bili_client.check_refresh().await { error!("检查刷新 Credential 遇到错误:{e},等待下一轮执行"); - tokio::time::sleep(std::time::Duration::from_secs(CONFIG.interval)).await; + time::sleep(Duration::from_secs(CONFIG.interval)).await; continue; } anchor = chrono::Local::now().date_naive(); @@ -49,6 +57,6 @@ async fn main() -> ! { } } info!("所有收藏夹处理完毕,等待下一轮执行"); - tokio::time::sleep(std::time::Duration::from_secs(CONFIG.interval)).await; + time::sleep(Duration::from_secs(CONFIG.interval)).await; } }