Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
gwy15 committed Aug 12, 2023
1 parent 194fa23 commit ec1c238
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 35 deletions.
42 changes: 20 additions & 22 deletions src/input_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,30 @@ impl std::str::FromStr for InputType {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match url::Url::parse(s) {
Ok(url) => {
if s.starts_with("http") {
if let Ok(url) = url::Url::parse(s) {
info!("输入类型为 URL,解析中...");
Self::from_url(url)
return Self::from_url(url);
}
Err(_) => {
if s.chars().all(|c| c.is_ascii_alphanumeric()) {
if s.starts_with("BV") {
return Ok(InputType::BV {
bv: s.to_string(),
p: None,
});
}
if let Ok(t) = Self::from_episode_or_season_str(s) {
return Ok(t);
}
}

let path = PathBuf::from(s);
if path.is_dir() {
Ok(InputType::Folder(path))
} else {
Ok(InputType::File(path))
}
}
if s.chars().all(|c| c.is_ascii_alphanumeric()) {
if s.starts_with("BV") {
return Ok(InputType::BV {
bv: s.to_string(),
p: None,
});
}
if let Ok(t) = Self::from_episode_or_season_str(s) {
return Ok(t);
}
}

let path = PathBuf::from(s);
if path.is_dir() {
Ok(InputType::Folder(path))
} else {
Ok(InputType::File(path))
}
}
}

Expand Down
34 changes: 22 additions & 12 deletions src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,9 @@ async fn convert(request: web::Json<ConvertRequest>) -> HttpResponse {
}));
}
};
log::info!("download {} danmu, title={}", danmu.len(), title);
let r = danmu2ass::convert(
danmu.into_iter().map(|i| Ok(i.into())),
title.clone(),
&mut output,
req.config,
&req.denylist,
);
log::info!("danmu downloaded, title={}", title);
let r =
danmu2ass::convert(danmu, title.clone(), &mut output, req.config, &req.denylist);
if let Err(e) = r {
return HttpResponse::BadRequest().json(json!({
"errmsg": format!("{e:#?}")
Expand All @@ -84,9 +79,21 @@ async fn convert(request: web::Json<ConvertRequest>) -> HttpResponse {
.body(output)
}

async fn run_input_type(input_type: InputType) -> anyhow::Result<(String, Vec<DanmakuElem>)> {
type Iter = Box<dyn Iterator<Item = anyhow::Result<danmu2ass::Danmu>>>;

async fn run_input_type(input_type: InputType) -> anyhow::Result<(String, Iter)> {
let client = biliapi::connection::new_client()?;
match input_type {
InputType::File(path) => {
let file = std::fs::File::open(&path)?;
let parser = danmu2ass::Parser::new(std::io::BufReader::with_capacity(1 << 20, file));
let filename = path
.file_name()
.and_then(|s| s.to_str())
.unwrap_or("danmu")
.to_string();
Ok((filename, Box::new(parser)))
}
InputType::BV { bv, p } => {
let p = p.unwrap_or(1);
// get info for video
Expand All @@ -98,7 +105,8 @@ async fn run_input_type(input_type: InputType) -> anyhow::Result<(String, Vec<Da

let danmu =
danmu2ass::bilibili::get_danmu_for_video(page.cid, page.duration.as_secs()).await?;
Ok((info.title, danmu))
let danmu = danmu.into_iter().map(|i| Ok(i.into()));
Ok((info.title, Box::new(danmu)))
}
InputType::Season { season_id } => {
let mut season_info =
Expand All @@ -110,7 +118,8 @@ async fn run_input_type(input_type: InputType) -> anyhow::Result<(String, Vec<Da
let danmu =
danmu2ass::bilibili::get_danmu_for_video(episode.cid, episode.duration_ms / 1000)
.await?;
Ok((title, danmu))
let danmu = danmu.into_iter().map(|i| Ok(i.into()));
Ok((title, Box::new(danmu)))
}
InputType::Episode { episode_id } => {
let season_info = danmu2ass::bilibili::Season::request(&client, ("ep_id", episode_id))
Expand All @@ -124,7 +133,8 @@ async fn run_input_type(input_type: InputType) -> anyhow::Result<(String, Vec<Da
let title = format!("{} - {}", season_info.title, ep.title);
let danmu =
danmu2ass::bilibili::get_danmu_for_video(ep.cid, ep.duration_ms / 1000).await?;
Ok((title, danmu))
let danmu = danmu.into_iter().map(|i| Ok(i.into()));
Ok((title, Box::new(danmu)))
}
_ => {
bail!("Unsupported input type");
Expand Down
2 changes: 1 addition & 1 deletion webui/src/components/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
hide-details
outlined
v-model="display_source"
label="选择 xml 或输入 URL / BV 号 / ep 号"
label="输入 URL / BV 号 / ep 号 / 本地绝对路径 / 本地相对路径 / 选择 XML 文件"
append-icon="mdi-file-upload-outline"
@click:append="select_xml"
/>
Expand Down

0 comments on commit ec1c238

Please sign in to comment.