From b3a2445e38bc63e3c453bb7d0508255af71e1e34 Mon Sep 17 00:00:00 2001 From: XiaoMiku01 Date: Wed, 25 Sep 2024 09:53:38 +0800 Subject: [PATCH] =?UTF-8?q?Add=20=E6=94=AF=E6=8C=81=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E7=9A=84=E6=8F=90=E4=BA=A4=E5=8F=82=E6=95=B0=20(#171)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 1 * 支持自定义的提交参数 * 变量名格式统一 --- .gitignore | 5 ++- crates/biliup/src/uploader/bilibili.rs | 59 +++++++++++++++----------- crates/stream-gears/src/uploader.rs | 17 +++++--- 3 files changed, 51 insertions(+), 30 deletions(-) diff --git a/.gitignore b/.gitignore index d3c7556..6737b0c 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,7 @@ *.png *.part data.db* -*.whl \ No newline at end of file +*.whl +.vscode +*.json +*.yaml \ No newline at end of file diff --git a/crates/biliup/src/uploader/bilibili.rs b/crates/biliup/src/uploader/bilibili.rs index d5fc9da..884d243 100644 --- a/crates/biliup/src/uploader/bilibili.rs +++ b/crates/biliup/src/uploader/bilibili.rs @@ -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; @@ -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>, +} + +fn parse_extra_fields(s: &str) -> std::result::Result, String> { + serde_json::from_str(s).map_err(|e| e.to_string()) } #[derive(Default, Debug, Serialize, Deserialize)] @@ -201,8 +210,7 @@ impl FromStr for Vid { fn from_str(s: &str) -> std::result::Result { let s = s.trim(); if s.len() < 3 { - return s.parse::() - .map(Vid::Aid); + return s.parse::().map(Vid::Aid); } match &s[..2] { "BV" => Ok(Vid::Bvid(s.to_string())), @@ -228,27 +236,27 @@ pub struct BiliBili { impl BiliBili { pub async fn submit(&self, studio: &Studio) -> Result { - 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 { let payload = { @@ -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 }; diff --git a/crates/stream-gears/src/uploader.rs b/crates/stream-gears/src/uploader.rs index b81762f..57f025d 100644 --- a/crates/stream-gears/src/uploader.rs +++ b/crates/stream-gears/src/uploader.rs @@ -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; @@ -28,7 +29,7 @@ pub enum UploadLine { Tx, Txa, Bda, - Alia + Alia, } #[derive(FromPyObject)] @@ -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, + #[builder(default)] + extra_fields: Option>, } pub async fn upload(studio_pre: StudioPre) -> Result { @@ -93,6 +96,7 @@ pub async fn upload(studio_pre: StudioPre) -> Result { no_reprint, open_elec, desc_v2_credit, + extra_fields, .. } = studio_pre; @@ -173,6 +177,7 @@ pub async fn upload(studio_pre: StudioPre) -> Result { .no_reprint(no_reprint) .open_elec(open_elec) .desc_v2(Some(desc_v2)) + .extra_fields(extra_fields) .build(); if !studio.cover.is_empty() { @@ -216,6 +221,7 @@ pub async fn upload_by_app(studio_pre: StudioPre) -> Result { up_selection_reply, up_close_danmu, desc_v2_credit, + extra_fields, } = studio_pre; let bilibili = login_by_cookies(&cookie_file).await; @@ -298,6 +304,7 @@ pub async fn upload_by_app(studio_pre: StudioPre) -> Result { .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() { @@ -312,4 +319,4 @@ pub async fn upload_by_app(studio_pre: StudioPre) -> Result { } Ok(bilibili.submit_by_app(&studio).await?) -} \ No newline at end of file +}