Skip to content

Commit

Permalink
Merge pull request #20 from nanashi-1/add-parallelism
Browse files Browse the repository at this point in the history
Add parallelism
  • Loading branch information
nanashi-1 committed May 7, 2024
2 parents 27e0c0a + d111e08 commit 9e386ac
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 16 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ path = "src/cli/main.rs"

[dependencies]
clap = { version = "4.4.10", features = ["derive"] }
rayon = "1.10.0"
rsubs-lib = "0.1.8"
serde = "1.0.188"
serde_json = "1.0.105"
Expand Down
42 changes: 28 additions & 14 deletions src/cli/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use bilibili_extractor_lib::combiner::Combinable;
use bilibili_extractor_lib::error::Result;
use bilibili_extractor_lib::metadata::{EpisodeId, EpisodeMetadata, SeasonMetadata};
use bilibili_extractor_lib::subtitle::{JsonSubtitle, SubtitleFormat};
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use rsubs_lib::srt::SRTFile;
use rsubs_lib::vtt::VTTFile;
use std::fs::{read_to_string, rename};
Expand Down Expand Up @@ -54,17 +55,27 @@ impl Compiler {
}

pub fn compile_normal_episodes(&self, episodes: Vec<&EpisodeMetadata>) -> Result<()> {
episodes
.iter()
.try_for_each(|e| self.compile_normal_episode(e))?;
match self.context.is_parallel {
true => episodes
.par_iter()
.try_for_each(|e| self.compile_normal_episode(e))?,
false => episodes
.iter()
.try_for_each(|e| self.compile_normal_episode(e))?,
};

Ok(())
}

pub fn compile_special_episodes(&self, episodes: Vec<&EpisodeMetadata>) -> Result<()> {
episodes
.iter()
.try_for_each(|e| self.compile_special_episode(e))?;
match self.context.is_parallel {
true => episodes
.par_iter()
.try_for_each(|e| self.compile_special_episode(e))?,
false => episodes
.iter()
.try_for_each(|e| self.compile_special_episode(e))?,
};

Ok(())
}
Expand All @@ -79,10 +90,13 @@ impl Compiler {
SubtitleFormat::get_episode_subtitle_type(episode, &self.context.language)?
);

let mut spinner = create_spinner(&format!(
"Compiling {} EP{:0>2}...",
episode.title, episode.episode
));
let mut spinner = match self.context.is_parallel {
true => create_spinner("Compiling episodes in parallel..."),
false => create_spinner(&format!(
"Compiling {} EP{:0>2}...",
episode.title, episode.episode
)),
};

let subtitle_path = episode.get_subtitle_path(&self.context.language)?;
let binding = episode.path.join("subtitle.ass");
Expand Down Expand Up @@ -128,10 +142,10 @@ impl Compiler {
SubtitleFormat::get_episode_subtitle_type(episode, &self.context.language)?
);

let mut spinner = create_spinner(&format!(
"Compiling {} {}...",
episode.title, episode.episode
));
let mut spinner = match self.context.is_parallel {
true => create_spinner("Compiling episodes in parallel..."),
false => create_spinner(&format!("Compiling {} {}", episode.title, episode.episode)),
};

let subtitle_path = episode.get_subtitle_path(&self.context.language)?;
let binding = episode.path.join("subtitle.ass");
Expand Down
4 changes: 2 additions & 2 deletions src/cli/lister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl Lister {
#[cfg(not(debug_assertions))]
episodes
.iter()
.for_each(|e| println!(" {} EP{:0>2}", e.title, e.episode));
.for_each(|e| println!(" {} {}", e.title, e.episode.get_full_display()));

#[cfg(debug_assertions)]
episodes.iter().for_each(|e| {
Expand All @@ -77,7 +77,7 @@ impl Lister {
#[cfg(not(debug_assertions))]
episodes
.iter()
.for_each(|e| println!(" {} {}", e.title, e.episode_name));
.for_each(|e| println!(" {} {}", e.title, e.episode.get_full_display()));

#[cfg(debug_assertions)]
episodes.iter().for_each(|e| {
Expand Down
6 changes: 6 additions & 0 deletions src/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct Context {
pub subtitle_type: SubtitleType,
pub packager: Packager,
pub input_path: String,
pub is_parallel: bool,
}

#[derive(Parser)]
Expand Down Expand Up @@ -52,6 +53,9 @@ enum SubCommands {

#[clap(long, help = "Set language for the subtitle.")]
use_hard_subtitle: bool,

#[clap(long, short, help = "Compile episodes in parallel.")]
parallel: bool,
},
}

Expand Down Expand Up @@ -110,6 +114,7 @@ fn main() {
copy,
language,
use_hard_subtitle,
parallel,
} => {
let context = Context {
language,
Expand All @@ -122,6 +127,7 @@ fn main() {
config: PackagerConfig { copy },
},
input_path: input,
is_parallel: parallel,
};

#[cfg(debug_assertions)]
Expand Down

0 comments on commit 9e386ac

Please sign in to comment.