Major refactor
This commit is contained in:
39
crates/siren-bot/src/ytdlp/mod.rs
Normal file
39
crates/siren-bot/src/ytdlp/mod.rs
Normal file
@@ -0,0 +1,39 @@
|
||||
mod model;
|
||||
|
||||
pub use model::*;
|
||||
use std::process::{Child, Command, Output, Stdio};
|
||||
|
||||
const YOUTUBE_DL_COMMAND: &str = "yt-dlp";
|
||||
|
||||
pub struct YtDlp {
|
||||
command: Command,
|
||||
args: Vec<String>,
|
||||
}
|
||||
|
||||
impl YtDlp {
|
||||
pub fn new() -> Self {
|
||||
let mut cmd = Command::new(YOUTUBE_DL_COMMAND);
|
||||
cmd
|
||||
.env("LC_ALL", "en_US.UTF-8")
|
||||
.stdout(Stdio::piped())
|
||||
.stdin(Stdio::piped())
|
||||
.stderr(Stdio::piped());
|
||||
Self {
|
||||
command: cmd,
|
||||
args: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn arg(&mut self, arg: &str) -> &mut Self {
|
||||
self.args.push(arg.to_owned());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn execute(&mut self) -> std::io::Result<Output> {
|
||||
self
|
||||
.command
|
||||
.args(self.args.clone())
|
||||
.spawn()
|
||||
.and_then(Child::wait_with_output)
|
||||
}
|
||||
}
|
||||
35
crates/siren-bot/src/ytdlp/model.rs
Normal file
35
crates/siren-bot/src/ytdlp/model.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum YtDlpItem {
|
||||
PlaylistItem {
|
||||
id: String,
|
||||
url: String,
|
||||
title: String,
|
||||
duration: Option<f64>,
|
||||
playlist_index: Option<i32>,
|
||||
},
|
||||
VideoItem {
|
||||
id: String,
|
||||
webpage_url: String,
|
||||
title: String,
|
||||
duration: Option<f64>,
|
||||
},
|
||||
}
|
||||
|
||||
impl YtDlpItem {
|
||||
pub fn get_title(&self) -> &str {
|
||||
match self {
|
||||
YtDlpItem::PlaylistItem { title, .. } => title,
|
||||
YtDlpItem::VideoItem { title, .. } => title,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_url(&self) -> &str {
|
||||
match self {
|
||||
YtDlpItem::PlaylistItem { url, .. } => url,
|
||||
YtDlpItem::VideoItem { webpage_url, .. } => webpage_url,
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user