From 35024feaa7fc81d42f938dc923bcfbec742aa421 Mon Sep 17 00:00:00 2001 From: ForgQi <34411314+ForgQi@users.noreply.github.com> Date: Fri, 8 Aug 2025 02:00:12 +0800 Subject: [PATCH] Refactor segment handling and SubmitOption parsing Replaces the custom Segment class in Python with the PySegment class from Rust, and exposes PySegment to Python via PyO3. Refactors SubmitOption to implement FromStr for string parsing, updating usage accordingly. These changes improve interoperability between Python and Rust components and standardize enum parsing. Bump stream-gears version to 1.0.5 Updated the stream-gears crate version from 1.0.4 to 1.0.5 in Cargo.toml and Cargo.lock to prepare for a new release. --- Cargo.lock | 2 +- biliup/engine/download.py | 4 +--- crates/biliup/src/uploader/util.rs | 7 +++++-- crates/stream-gears/Cargo.toml | 2 +- crates/stream-gears/src/lib.rs | 19 ++++++++++++++++--- crates/stream-gears/src/uploader.rs | 3 ++- 6 files changed, 26 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ebebc0e..82acba3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5289,7 +5289,7 @@ checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" [[package]] name = "stream-gears" -version = "1.0.4" +version = "1.0.5" dependencies = [ "anyhow", "biliup", diff --git a/biliup/engine/download.py b/biliup/engine/download.py index 8f19d53..d23e0cf 100644 --- a/biliup/engine/download.py +++ b/biliup/engine/download.py @@ -542,10 +542,8 @@ class DownloadBase(ABC): def stream_gears_download(url, headers, file_name, segment_time=None, file_size=None, file_name_callback: Callable[[str], None] = None): - class Segment: - pass - segment = Segment() + segment = stream_gears.PySegment() if segment_time: seg_time = segment_time.split(':') # print(int(seg_time[0]) * 60 * 60 + int(seg_time[1]) * 60 + int(seg_time[2])) diff --git a/crates/biliup/src/uploader/util.rs b/crates/biliup/src/uploader/util.rs index b6199aa..0ad0b54 100644 --- a/crates/biliup/src/uploader/util.rs +++ b/crates/biliup/src/uploader/util.rs @@ -1,3 +1,4 @@ +use std::str::FromStr; #[cfg(feature = "cli")] use clap::ValueEnum; @@ -10,9 +11,11 @@ pub enum SubmitOption { BCutAndroid, } -impl SubmitOption { +impl FromStr for SubmitOption { + type Err = String; + /// Parse a string into SubmitOption, compatible with both clap and manual parsing - pub fn parse_str(s: &str) -> Result { + fn from_str(s: &str) -> Result { match s.to_lowercase().as_str() { "app" => Ok(SubmitOption::App), "bcutandroid" | "b-cut-android" | "bcut_android" => Ok(SubmitOption::BCutAndroid), diff --git a/crates/stream-gears/Cargo.toml b/crates/stream-gears/Cargo.toml index 010df3a..61603c6 100644 --- a/crates/stream-gears/Cargo.toml +++ b/crates/stream-gears/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "stream-gears" -version = "1.0.4" +version = "1.0.5" edition = "2024" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/crates/stream-gears/src/lib.rs b/crates/stream-gears/src/lib.rs index 4942d58..4220f9b 100644 --- a/crates/stream-gears/src/lib.rs +++ b/crates/stream-gears/src/lib.rs @@ -17,14 +17,26 @@ use biliup::downloader::util::Segmentable; use tracing_subscriber::layer::SubscriberExt; -#[derive(FromPyObject, Debug)] +#[derive(Debug, Clone)] +#[pyclass(set_all)] pub struct PySegment { - #[pyo3(attribute("time"))] + // #[pyo3(attribute("time"))] time: Option, - #[pyo3(attribute("size"))] + // #[pyo3(attribute("size"))] size: Option, } +#[pymethods] +impl PySegment { + #[new] + fn new() -> Self { + PySegment{ + time: None, + size: None, + } + } +} + #[pyfunction] #[pyo3(signature = (url,header_map,file_name,segment,proxy = None))] fn download( @@ -347,6 +359,7 @@ fn stream_gears(m: &Bound<'_, PyModule>) -> PyResult<()> { m.add_function(wrap_pyfunction!(login_by_web_cookies, m)?)?; m.add_function(wrap_pyfunction!(login_by_web_qrcode, m)?)?; m.add_class::()?; + m.add_class::()?; Ok(()) } diff --git a/crates/stream-gears/src/uploader.rs b/crates/stream-gears/src/uploader.rs index 19c6a5b..8fae6bf 100644 --- a/crates/stream-gears/src/uploader.rs +++ b/crates/stream-gears/src/uploader.rs @@ -12,6 +12,7 @@ use pyo3::pyclass; use std::collections::HashMap; use std::path::PathBuf; +use std::str::FromStr; use std::time::Instant; use tracing::info; @@ -190,7 +191,7 @@ pub async fn upload(studio_pre: StudioPre, submit: Option<&str>, proxy: Option<& } let submit = match submit { - Some(submit) => SubmitOption::parse_str(submit).unwrap_or(SubmitOption::App), + Some(submit) => SubmitOption::from_str(submit).unwrap_or(SubmitOption::App), _ => SubmitOption::App, };