Skip to content

Commit

Permalink
更换新版web登录&登录二维码改为get请求
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonwangmvp committed May 29, 2024
1 parent 641e79a commit 5496d79
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/libs/exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ impl Item<'_> {
return format!("{}", eps[0].title);
}
format!(
"{}-{}. {}-{}",
"{}-{}. {}~{}",
min,
max,
eps[0].title,
Expand Down
8 changes: 4 additions & 4 deletions src/libs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,21 +133,21 @@ pub async fn login(method: LoginMethod) {
}
}
LoginMethod::QRCODE => {
let (qr_data, oauth) = network::get_qr_data(&config).await;
let code = QrCode::new(&qr_data).unwrap();
let (qr_url, qrcode_key) = network::get_qr_data(&config).await;
let code = QrCode::new(&qr_url).unwrap();
let image = code
.render::<qrcode::render::unicode::Dense1x2>()
.dark_color(qrcode::render::unicode::Dense1x2::Dark)
.light_color(qrcode::render::unicode::Dense1x2::Light)
.build();
println!("{}", image);
log.success("二维码已生成,请扫描二维码登录");
log.info(format!("如果显示错误,请手动访问:{}", qr_data));
log.info(format!("如果显示错误,请手动访问:{}", qr_url));
log.loading("等待扫描...");
let mut last_status = "NotScan";
loop {
tokio::time::sleep(Duration::from_secs(1)).await;
match network::check_qr_status(&config, oauth.clone()).await {
match network::check_qr_status(&config, qrcode_key.clone()).await {
network::QRStatus::NotScan => {
if last_status != "NotScan" {
log.done();
Expand Down
50 changes: 26 additions & 24 deletions src/libs/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub async fn get_user_info(config: &Config) -> Option<UserInfo> {
}

pub async fn get_qr_data(config: &Config) -> (String, String) {
let url = "https://passport.bilibili.com/qrcode/getLoginUrl";
let url = "https://passport.bilibili.com/x/passport-login/web/qrcode/generate";
let mut log = paris::Logger::new();
log.loading("加载二维码");
let client = config.get_client();
Expand Down Expand Up @@ -101,14 +101,14 @@ pub async fn get_qr_data(config: &Config) -> (String, String) {
.unwrap()
.as_str()
.unwrap();
let oauth_key = value
let qrcode_key = value
.get("data")
.unwrap()
.get("oauthKey")
.get("qrcode_key")
.unwrap()
.as_str()
.unwrap();
(url.to_string(), oauth_key.to_string())
(url.to_string(), qrcode_key.to_string())
} else {
log.done();
log.error("无法获取二维码数据 请检查网络");
Expand All @@ -126,31 +126,30 @@ pub enum QRStatus {
Invalid, // 无效
}

pub async fn check_qr_status(config: &Config, oauth: String) -> QRStatus {
let url = "https://passport.bilibili.com/qrcode/getLoginInfo";
pub async fn check_qr_status(config: &Config, qrcode_key: String) -> QRStatus {
let url = "https://passport.bilibili.com/x/passport-login/web/qrcode/poll";
let mut log = paris::Logger::new();
let client = config.get_client();
let mut params = HashMap::new();
params.insert("oauthKey", oauth.clone());
if let Ok(resp) = client.post(url).form(&params).send().await {
let url_qrcode = format!("{}?{}{}", url, "qrcode_key=", &qrcode_key);
if let Ok(resp) = client.get(url_qrcode).send().await {
let value: serde_json::Value = resp.json().await.unwrap();
let data = value.get("data").unwrap();
if data.is_number() {
let code = data.as_i64().unwrap();
if code == -4 {
return QRStatus::NotScan;
}
if code == -5 {
return QRStatus::Scanning;
}
if code == -2 {
return QRStatus::Invalid;
}
} else {
let code = data.get("code").unwrap().as_i64().unwrap();

if code == 86101 {
return QRStatus::NotScan;
}
else if code == 86090 {
return QRStatus::Scanning;
}
else if code == 86038 {
return QRStatus::Invalid;
}
else if code == 0 {
let data = value.get("data").unwrap();
// dbg!(resp.cookies());
let url = data.get("url").unwrap().as_str().unwrap();
let sessdata = url.split("&SESSDATA=").collect::<Vec<&str>>()[1]
let sessdata = url.split("SESSDATA=").collect::<Vec<&str>>()[1]
.split('&')
.collect::<Vec<&str>>()[0]
.to_string();
Expand Down Expand Up @@ -384,9 +383,12 @@ pub async fn down_to<T: AsRef<Path>>(config: &Config, url: String, path: T) -> O

fn extract_episode_number(input: &str) -> Option<String> {
if let Some(start) = input.find('第') {
if let Some(end) = input[start + 3..].chars().position(|c| c == '话') {
if let Some(end) = input[start + 3..].char_indices().find(|(_, c)| *c == '话').map(|(i, _)| i) {
let extracted: String = input[start + 3..start + 3 + end].chars().collect();
return Some(extracted);
// 检查提取的字符串是否都是中文字符
if extracted.chars().all(|c| c.is_ascii() && !c.is_alphabetic() && !c.is_ascii_whitespace()) {
return Some(extracted);
}
}
}
None
Expand Down

0 comments on commit 5496d79

Please sign in to comment.