Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
cobalt 10 released
Browse files Browse the repository at this point in the history
  • Loading branch information
khyerdev committed Sep 10, 2024
1 parent ce350de commit e022da9
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 127 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tcobalt"
version = "1.2.1"
version = "1.3.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
18 changes: 13 additions & 5 deletions src/args/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,17 @@ pub fn load_config_into(args: &mut Vec<String>, instance_list: &mut Vec<String>)
args.push("-f".into());
args.push(option[1].into())
},
"audio-only" => {
"proxy" => {
if option[1].to_lowercase().as_str() == "true" {
args.push("-a".into())
args.push("-x".into())
}
},
"mute-audio" => {
if option[1].to_lowercase().as_str() == "true" {
}
"download-mode" => {
if option[1].to_lowercase().as_str() == "auto" {
args.push("-=".into())
} else if option[1].to_lowercase().as_str() == "audio" {
args.push("-a".into())
} else if option[1].to_lowercase().as_str() == "mute" {
args.push("-m".into())
}
},
Expand Down Expand Up @@ -79,6 +83,10 @@ pub fn load_config_into(args: &mut Vec<String>, instance_list: &mut Vec<String>)
args.push("-i".into());
args.push(option[1].into())
},
"bitrate" => {
args.push("-b".into());
args.push(option[1].into())
}
_ => ()
}
}
Expand Down
53 changes: 34 additions & 19 deletions src/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ pub struct Args {
pub c_video_codec: types::VideoCodec,
pub c_video_quality: u16,
pub c_audio_format: types::AudioFormat,
pub c_audio_only: bool,
pub c_audio_muted: bool,
pub c_audio_bitrate: u16,
pub c_download_mode: types::DownloadMode,
pub c_twitter_gif: bool,
pub c_tt_full_audio: bool,
pub c_tt_h265: bool,
pub c_dublang: bool,
pub c_disable_metadata: bool,
pub accept_language: String,
pub out_filename: Option<String>,
pub c_fname_style: types::FilenamePattern,
pub same_filenames: bool,
pub picker_choice: u8,
pub cobalt_instance: String,
pub help_flag: Option<types::Help>
pub help_flag: Option<types::Help>,
pub c_proxy: bool
}
impl Args {
pub fn get() -> Self {
Expand All @@ -37,21 +37,21 @@ impl Args {
c_video_codec: types::VideoCodec::H264,
c_video_quality: 1080,
c_audio_format: types::AudioFormat::MP3,
c_audio_only: false,
c_audio_muted: false,
c_download_mode: types::DownloadMode::Auto,
c_twitter_gif: false,
out_filename: None,
same_filenames: false,
c_audio_bitrate: 128,
help_flag: None,
method: None,
bulk_array: None,
picker_choice: 0,
c_fname_style: types::FilenamePattern::Classic,
c_tt_full_audio: false,
c_tt_h265: false,
c_dublang: false,
c_disable_metadata: false,
cobalt_instance: String::from("co.wuk.sh"),
c_proxy: false,
cobalt_instance: String::from("api.cobalt.tools"),
accept_language: String::from("en")
}
}
Expand Down Expand Up @@ -104,20 +104,20 @@ impl Args {
"--vcodec" => expected.push(ExpectedFlags::VideoCodec),
"--vquality" => expected.push(ExpectedFlags::VideoQuality),
"--aformat" => expected.push(ExpectedFlags::AudioFormat),
"--audio-only" => self.c_audio_only = !self.c_audio_only,
"--mute-audio" => self.c_audio_muted = !self.c_audio_muted,
"--audio-only" => self.c_download_mode = types::DownloadMode::Audio,
"--mute-audio" => self.c_download_mode = types::DownloadMode::Mute,
"--auto" => self.c_download_mode = types::DownloadMode::Auto,
"--twitter-gif" => self.c_twitter_gif = !self.c_twitter_gif,
"--tt-full-audio" => self.c_tt_full_audio = !self.c_tt_full_audio,
"--tt-h265" => self.c_tt_h265 = !self.c_tt_h265,
"--dublang" => {
self.c_dublang = true;
expected.push(ExpectedFlags::Language);
},
"--dublang" => expected.push(ExpectedFlags::Language),
"--no-metadata" => self.c_disable_metadata = !self.c_disable_metadata,
"--output" => expected.push(ExpectedFlags::Output),
"--fname-style" => expected.push(ExpectedFlags::FilenamePattern),
"--pick" => expected.push(ExpectedFlags::Picker),
"--instance" => expected.push(ExpectedFlags::Instance),
"--bitrate" => expected.push(ExpectedFlags::Bitrate),
"--proxy" => self.c_proxy = !self.c_proxy,
_ => {
if self.c_url == None && arg.contains("https://") {
self.c_url = Some(arg.clone());
Expand All @@ -141,20 +141,22 @@ impl Args {
'c' => expected.push(ExpectedFlags::VideoCodec),
'q' => expected.push(ExpectedFlags::VideoQuality),
'f' => expected.push(ExpectedFlags::AudioFormat),
'a' => self.c_audio_only = !self.c_audio_only,
'm' => self.c_audio_muted = !self.c_audio_muted,
'a' => self.c_download_mode = types::DownloadMode::Audio,
'm' => self.c_download_mode = types::DownloadMode::Mute,
'g' => self.c_twitter_gif = !self.c_twitter_gif,
'u' => self.c_tt_full_audio = !self.c_tt_full_audio,
'h' => self.c_tt_h265 = !self.c_tt_h265,
'l' => {
self.c_dublang = true;
expected.push(ExpectedFlags::Language);
},
'n' => self.c_disable_metadata = !self.c_disable_metadata,
'o' => expected.push(ExpectedFlags::Output),
's' => expected.push(ExpectedFlags::FilenamePattern),
'p' => expected.push(ExpectedFlags::Picker),
'i' => expected.push(ExpectedFlags::Instance),
'x' => self.c_proxy = !self.c_proxy,
'=' => self.c_download_mode = types::DownloadMode::Auto,
'b' => expected.push(ExpectedFlags::Bitrate),
_ => return Err(types::ParseError::throw_invalid(&format!("Invalid character {c} in multi-flag argument: {arg}")))
}
}
Expand Down Expand Up @@ -227,6 +229,13 @@ impl Args {
url.truncate(idx);
}
self.cobalt_instance = url;
},
ExpectedFlags::Bitrate => {
if arg == "320" || arg == "256" || arg == "128" || arg == "96" || arg == "64" || arg == "8" {
self.c_audio_bitrate = arg.parse::<u16>().unwrap();
} else {
return Err(types::ParseError::throw_invalid("Make sure you select a valid bitrate! (320/256/128/96/64/8)"));
}
}
}
}
Expand Down Expand Up @@ -326,7 +335,13 @@ impl Args {
},
"list" | "l" => self.method = Some(types::Method::List),
"version" | "v" | "-v" | "--version" => self.method = Some(types::Method::Version),
"cobalt-version" | "cv" | "c" => self.method = Some(types::Method::CobaltVersion),
"cobalt-version" | "cv" | "c" => {
if self.raw.get(2).is_some() {
self.method = Some(types::Method::CobaltVersion(self.raw[2].clone()))
} else {
self.method = Some(types::Method::CobaltVersion(String::from("api.cobalt.tools")))
}
},
"gen-config" | "gc" => self.method = Some(types::Method::GenConfig),

unknown => return Err(types::ParseError::throw_invalid(&format!("Unrecognized tcobalt method: {}", unknown)))
Expand All @@ -347,5 +362,5 @@ impl Args {

#[derive(Debug)]
enum ExpectedFlags {
VideoCodec, VideoQuality, AudioFormat, Output, FilenamePattern, Picker, Language, Instance
VideoCodec, VideoQuality, AudioFormat, Output, FilenamePattern, Picker, Language, Instance, Bitrate
}
11 changes: 10 additions & 1 deletion src/args/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,23 @@ impl FilenamePattern {
format!("{self:?}").to_lowercase()
}
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum DownloadMode {
Auto, Audio, Mute
}
impl Default for DownloadMode {
fn default() -> Self {
Self::Auto
}
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Help {
Get, List, Bulk, Help, Examples, Config, GenConfig
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Method {
Get, List, Bulk, Help, Version, CobaltVersion, GenConfig
Get, List, Bulk, Help, Version, CobaltVersion(String), GenConfig
}

#[derive(Debug, PartialEq, Eq)]
Expand Down
41 changes: 23 additions & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,14 @@ async fn main() -> std::process::ExitCode {
args::types::Method::List => println!("{}", strings::get_str("info", "supported")),
args::types::Method::Help => unreachable!(),
args::types::Method::Version => println!("{}", strings::get_str("info", "version").replace("{}", VERSION.trim())),
args::types::Method::CobaltVersion => {
let request = reqwest::Client::new().get("https://co.wuk.sh/api/serverInfo")
args::types::Method::CobaltVersion(api_url) => {
let request = reqwest::Client::new().get(format!("https://{}/", api_url))
.header("User-Agent", &format!("tcobalt {}", VERSION.trim()));
if debug { eprintln!("[DEBUG] Sending GET request to cobalt ...") };
let ver = match request.send().await {
Ok(res) => res.text().await.unwrap_or("{\"version\":\"unknown\",\"commit\":\"unknown\",\"branch\":\"unknown\"}".to_string()),
Ok(res) => {
res.text().await.expect("cobalt returned nothing")
},
Err(e) => {
eprintln!("Cobalt server did not respond: {}", e.to_string());
return std::process::ExitCode::FAILURE;
Expand All @@ -102,10 +104,12 @@ async fn main() -> std::process::ExitCode {
return std::process::ExitCode::FAILURE;
}
};
let version = stats.get("version").unwrap().get_str().unwrap();
let commit = stats.get("commit").unwrap().get_str().unwrap();
let branch = stats.get("branch").unwrap().get_str().unwrap();
println!("Cobalt (by wukko) version {version}");
let cobalt = stats.get("cobalt").unwrap().get_object().unwrap();
let git = stats.get("git").unwrap().get_object().unwrap();
let version = cobalt.get("version").unwrap().get_str().unwrap();
let commit = git.get("commit").unwrap().get_str().unwrap();
let branch = git.get("branch").unwrap().get_str().unwrap();
println!("Cobalt (by wukko and jj) version {version}");
println!("Latest commit on branch \"{branch}\": {commit}");
},
args::types::Method::GenConfig => {
Expand Down Expand Up @@ -133,11 +137,10 @@ async fn execute_get_media(args: Args, bulk: u16, debug: bool) -> bool {
let json = proc::cobalt_args(&args);
let download_url: &str = args.c_url.as_ref().unwrap();

let request = reqwest::Client::new().post(format!("https://{}/api/json", &args.cobalt_instance))
let request = reqwest::Client::new().post(format!("https://{}/", &args.cobalt_instance))
.header("User-Agent", &format!("tcobalt {}", VERSION.trim()))
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.header("Accept-Language", &args.accept_language)
.body(json);

if debug { eprintln!("[DEBUG {download_url}] Sending POST request to cobalt server ...") };
Expand All @@ -150,16 +153,16 @@ async fn execute_get_media(args: Args, bulk: u16, debug: bool) -> bool {
let status = json.get("status".into()).unwrap().get_str().unwrap();
match status.as_str() {
"error" => {
let text = json.get("text").unwrap().get_str().unwrap();
eprintln!("Cobalt returned error:\n\"{text}\"\n(when downloading from {download_url})");
let text = json.get("error".into()).unwrap().get_object().unwrap().get("code".into()).unwrap().get_str().unwrap();
eprintln!("Cobalt returned error: \"{text}\" (when downloading from {download_url})");
return false;
},
"stream" | "redirect" | "success" | "picker" => {
"tunnel" | "redirect" | "picker" => {
if debug { eprintln!("[DEBUG {download_url}] Cobalt returned a response") };

let url = proc::get_url(&args, &status, &json);

let media = if args.c_audio_only {
let media = if args.c_download_mode == tcargs::types::DownloadMode::Audio {
"audio"
} else {
"video"
Expand All @@ -171,7 +174,13 @@ async fn execute_get_media(args: Args, bulk: u16, debug: bool) -> bool {
let res = attempt!(stream_request.send().await, "Live renderer did not respond:\n\"{}\"\n(when downloading from {download_url})");

if debug { eprintln!("[DEBUG {download_url}] Response received from stream") };
let filename = proc::extract_filename(&args, res.headers(), bulk, debug);
let mut filename = json.get("filename".into()).unwrap().get_str().unwrap();
if let Some(custom_name) = args.out_filename {
filename = custom_name;
}
if bulk > 0 {
filename = format!("{bulk}_{filename}");
}
println!(
"Downloading {} from {} ...",
media,
Expand All @@ -192,10 +201,6 @@ async fn execute_get_media(args: Args, bulk: u16, debug: bool) -> bool {

println!("Your {media} is ready! >> {display}")
},
"rate-limit" => {
eprintln!("You are being rate limited by cobalt! Please try again later.\n(when downloading from {download_url})");
return false;
}
_ => unreachable!()
}
true
Expand Down
Loading

0 comments on commit e022da9

Please sign in to comment.