|
1 | 1 | use std::time::Duration; |
2 | 2 |
|
3 | 3 | use anyhow::{Context as _, Result, anyhow}; |
4 | | -use api_req::{ApiCaller as _, COOKIE_JAR, CookieStore as _}; |
| 4 | +use api_req::{ApiCaller as _, COOKIE_JAR, CookieStore as _, error::ApiErr}; |
5 | 5 | use futures::StreamExt as _; |
6 | 6 | use qrcode::{QrCode, render::unicode}; |
7 | 7 | use tokio::time::sleep; |
@@ -138,3 +138,40 @@ async fn logout_account(account_id: i64, cookies: String) -> Result<()> { |
138 | 138 | _ => Err(anyhow!("Failed to logout: {}", message.unwrap_or_default())), |
139 | 139 | } |
140 | 140 | } |
| 141 | + |
| 142 | +pub async fn check(account_id: i64) -> Result<()> { |
| 143 | + let db = db().await; |
| 144 | + let account = db.get_account(account_id).await?; |
| 145 | + check_account(account).await?; |
| 146 | + Ok(()) |
| 147 | +} |
| 148 | + |
| 149 | +pub async fn check_all() -> Result<()> { |
| 150 | + let db = db().await; |
| 151 | + let accounts = db.all_accounts().await?; |
| 152 | + for account in accounts { |
| 153 | + check_account(account).await?; |
| 154 | + } |
| 155 | + Ok(()) |
| 156 | +} |
| 157 | + |
| 158 | +async fn check_account(account: account::Model) -> Result<()> { |
| 159 | + set_cookie_jar(parse_cookies(&account.cookies)); |
| 160 | + match BiliApi::request(WbiPayload).await { |
| 161 | + Ok(WbiResp { |
| 162 | + data: WbiData { mid, .. }, |
| 163 | + }) => { |
| 164 | + if mid == account.account_id { |
| 165 | + info!("Check passed. Hello😊, {}.", account.name); |
| 166 | + } else { |
| 167 | + error!("Bilibili returned unmatched user id") |
| 168 | + } |
| 169 | + } |
| 170 | + Err(ApiErr::UnDeserializeable(_)) => error!( |
| 171 | + "Bilibili returned unexpected json, cookies expired: account<{} {}>", |
| 172 | + account.name, account.account_id |
| 173 | + ), |
| 174 | + Err(e) => return Err(e.into()), |
| 175 | + } |
| 176 | + Ok(()) |
| 177 | +} |
0 commit comments