Skip to content

Commit

Permalink
refactor: 尝试重构
Browse files Browse the repository at this point in the history
  • Loading branch information
amtoaer committed Jul 21, 2024
1 parent 64b7e27 commit f6edcc3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
2 changes: 1 addition & 1 deletion crates/bili_sync/src/bilibili/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl BiliClient {
credential.is_login(&self.client).await
}

/// 获取 wbi key,用于生成请求签名
/// 获取 wbi img,用于生成请求签名
pub async fn wbi_img(&self) -> Result<WbiImg> {
let credential = CONFIG.credential.load();
let Some(credential) = credential.as_deref() else {
Expand Down
19 changes: 10 additions & 9 deletions crates/bili_sync/src/bilibili/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ impl<'a> Collection<'a> {
let mixin_key = MIXIN_KEY.load();
let mixin_key = mixin_key.as_deref().unwrap();
let page = page.to_string();
let url = match self.collection.collection_type {
CollectionType::Series => format!(
"https://api.bilibili.com/x/series/archives?{}",
let (url, query) = match self.collection.collection_type {
CollectionType::Series => (
"https://api.bilibili.com/x/series/archives",
encoded_query(
vec![
("mid", self.collection.mid.as_str()),
Expand All @@ -143,11 +143,11 @@ impl<'a> Collection<'a> {
("pn", page.as_str()),
("ps", "30"),
],
mixin_key
)
mixin_key,
),
),
CollectionType::Season => format!(
"https://api.bilibili.com/x/polymer/web-space/seasons_archives_list?{}",
CollectionType::Season => (
"https://api.bilibili.com/x/polymer/web-space/seasons_archives_list",
encoded_query(
vec![
("mid", self.collection.mid.as_str()),
Expand All @@ -156,12 +156,13 @@ impl<'a> Collection<'a> {
("page_num", page.as_str()),
("page_size", "30"),
],
mixin_key
)
mixin_key,
),
),
};
self.client
.request(Method::GET, &url)
.query(&query)
.send()
.await?
.error_for_status()?
Expand Down
33 changes: 24 additions & 9 deletions crates/bili_sync/src/bilibili/credential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,16 +222,21 @@ pub fn get_mixin_key(wbi_img: WbiImg) -> Option<String> {
Some(MIXIN_KEY_ENC_TAB.iter().take(32).map(|&x| key[x] as char).collect())
}

pub fn encoded_query(params: Vec<(&str, &str)>, mixin_key: &str) -> String {
_encoded_query(params, mixin_key, chrono::Local::now().timestamp().to_string().as_str())
pub fn encoded_query<'a>(params: Vec<(&'a str, impl Into<String>)>, mixin_key: &str) -> Vec<(&'a str, String)> {
let params = params.into_iter().map(|(k, v)| (k, v.into())).collect();
_encoded_query(params, mixin_key, chrono::Local::now().timestamp().to_string())
}

fn _encoded_query<'a>(mut params: Vec<(&str, &'a str)>, mixin_key: &str, timestamp: &'a str) -> String {
fn _encoded_query<'a>(params: Vec<(&'a str, String)>, mixin_key: &str, timestamp: String) -> Vec<(&'a str, String)> {
let mut params: Vec<(&'a str, String)> = params
.into_iter()
.map(|(k, v)| (k, v.chars().filter(|&x| !"!'()*".contains(x)).collect::<String>()))
.collect();
params.push(("wts", timestamp));
params.sort_by(|a, b| a.0.cmp(b.0));
let query = serde_urlencoded::to_string(params).unwrap().replace('+', "%20");
let w_rid = md5::compute(query.clone() + mixin_key);
query + &format!("&w_rid={:?}", w_rid)
let query = serde_urlencoded::to_string(&params).unwrap().replace('+', "%20");
params.push(("w_rid", format!("{:x}", md5::compute(query.clone() + mixin_key))));
params
}

#[cfg(test)]
Expand Down Expand Up @@ -276,11 +281,21 @@ mod tests {
assert_eq!(mixin_key, Some("ea1db124af3c7062474693fa704f4ff8".to_string()));
assert_eq!(
_encoded_query(
vec![("foo", "114"), ("bar", "514"), ("zab", "1919810")],
vec![
("foo", "114".to_string()),
("bar", "514".to_string()),
("zab", "1919810".to_string())
],
&mixin_key.unwrap(),
"1702204169",
"1702204169".to_string(),
),
"bar=514&foo=114&wts=1702204169&zab=1919810&w_rid=8f6f2b5b3d485fe1886cec6a0be8c5d4"
vec![
("bar", "514".to_string()),
("foo", "114".to_string()),
("wts", "1702204169".to_string()),
("zab", "1919810".to_string()),
("w_rid", "8f6f2b5b3d485fe1886cec6a0be8c5d4".to_string()),
]
)
}
}

0 comments on commit f6edcc3

Please sign in to comment.