Skip to content

Commit

Permalink
Add 支持自定义的提交参数 (#171)
Browse files Browse the repository at this point in the history
* 1

* 支持自定义的提交参数

* 变量名格式统一
  • Loading branch information
XiaoMiku01 authored Sep 25, 2024
1 parent a8f1a48 commit b3a2445
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 30 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@
*.png
*.part
data.db*
*.whl
*.whl
.vscode
*.json
*.yaml
59 changes: 35 additions & 24 deletions crates/biliup/src/uploader/bilibili.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use crate::uploader::credential::LoginInfo;
use serde::ser::Error;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::collections::HashMap;

use std::fmt::{Display, Formatter};
use std::num::ParseIntError;
use std::str::FromStr;
Expand Down Expand Up @@ -123,10 +125,17 @@ pub struct Studio {
#[clap(long)]
#[serde(default)]
pub up_close_danmu: bool,

// #[clap(long)]
// #[serde(default)]
// pub submit_by_app: bool,
/// 自定义提交参数
#[clap(long, value_parser = parse_extra_fields)]
#[serde(flatten)]
pub extra_fields: Option<HashMap<String, Value>>,
}

fn parse_extra_fields(s: &str) -> std::result::Result<HashMap<String, Value>, String> {
serde_json::from_str(s).map_err(|e| e.to_string())
}

#[derive(Default, Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -201,8 +210,7 @@ impl FromStr for Vid {
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
let s = s.trim();
if s.len() < 3 {
return s.parse::<u64>()
.map(Vid::Aid);
return s.parse::<u64>().map(Vid::Aid);
}
match &s[..2] {
"BV" => Ok(Vid::Bvid(s.to_string())),
Expand All @@ -228,27 +236,27 @@ pub struct BiliBili {

impl BiliBili {
pub async fn submit(&self, studio: &Studio) -> Result<ResponseData> {
let ret: ResponseData = reqwest::Client::builder()
.user_agent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 Chrome/63.0.3239.108")
.timeout(Duration::new(60, 0))
.build()?
.post(format!(
"http://member.bilibili.com/x/vu/client/add?access_key={}",
self.login_info.token_info.access_token
))
.json(studio)
.send()
.await?
.json()
.await?;
info!("{:?}", ret);
if ret.code == 0 {
info!("投稿成功");
Ok(ret)
} else {
Err(Kind::Custom(format!("{:?}", ret)))
}
let ret: ResponseData = reqwest::Client::builder()
.user_agent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 Chrome/63.0.3239.108")
.timeout(Duration::new(60, 0))
.build()?
.post(format!(
"http://member.bilibili.com/x/vu/client/add?access_key={}",
self.login_info.token_info.access_token
))
.json(studio)
.send()
.await?
.json()
.await?;
info!("{:?}", ret);
if ret.code == 0 {
info!("投稿成功");
Ok(ret)
} else {
Err(Kind::Custom(format!("{:?}", ret)))
}
}

pub async fn submit_by_app(&self, studio: &Studio) -> Result<ResponseData> {
let payload = {
Expand All @@ -267,7 +275,10 @@ impl BiliBili {
});

let urlencoded = serde_urlencoded::to_string(&payload)?;
let sign = crate::credential::Credential::sign(&urlencoded, crate::credential::AppKeyStore::BiliTV.appsec());
let sign = crate::credential::Credential::sign(
&urlencoded,
crate::credential::AppKeyStore::BiliTV.appsec(),
);
payload["sign"] = Value::from(sign);
payload
};
Expand Down
17 changes: 12 additions & 5 deletions crates/stream-gears/src/uploader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use futures::StreamExt;
use pyo3::prelude::*;
use pyo3::pyclass;

use std::collections::HashMap;
use std::path::PathBuf;
use std::time::Instant;
use tracing::info;
Expand All @@ -28,7 +29,7 @@ pub enum UploadLine {
Tx,
Txa,
Bda,
Alia
Alia,
}

#[derive(FromPyObject)]
Expand Down Expand Up @@ -60,13 +61,15 @@ pub struct StudioPre {
lossless_music: u8,
no_reprint: u8,
open_elec: u8,
#[builder(default=false)]
#[builder(default = false)]
up_close_reply: bool,
#[builder(default=false)]
#[builder(default = false)]
up_selection_reply: bool,
#[builder(default=false)]
#[builder(default = false)]
up_close_danmu: bool,
desc_v2_credit: Vec<PyCredit>,
#[builder(default)]
extra_fields: Option<HashMap<String, serde_json::Value>>,
}

pub async fn upload(studio_pre: StudioPre) -> Result<ResponseData> {
Expand All @@ -93,6 +96,7 @@ pub async fn upload(studio_pre: StudioPre) -> Result<ResponseData> {
no_reprint,
open_elec,
desc_v2_credit,
extra_fields,
..
} = studio_pre;

Expand Down Expand Up @@ -173,6 +177,7 @@ pub async fn upload(studio_pre: StudioPre) -> Result<ResponseData> {
.no_reprint(no_reprint)
.open_elec(open_elec)
.desc_v2(Some(desc_v2))
.extra_fields(extra_fields)
.build();

if !studio.cover.is_empty() {
Expand Down Expand Up @@ -216,6 +221,7 @@ pub async fn upload_by_app(studio_pre: StudioPre) -> Result<ResponseData> {
up_selection_reply,
up_close_danmu,
desc_v2_credit,
extra_fields,
} = studio_pre;

let bilibili = login_by_cookies(&cookie_file).await;
Expand Down Expand Up @@ -298,6 +304,7 @@ pub async fn upload_by_app(studio_pre: StudioPre) -> Result<ResponseData> {
.up_selection_reply(up_selection_reply)
.up_close_danmu(up_close_danmu)
.desc_v2(Some(desc_v2))
.extra_fields(extra_fields)
.build();

if !studio.cover.is_empty() {
Expand All @@ -312,4 +319,4 @@ pub async fn upload_by_app(studio_pre: StudioPre) -> Result<ResponseData> {
}

Ok(bilibili.submit_by_app(&studio).await?)
}
}

0 comments on commit b3a2445

Please sign in to comment.