Skip to content

Commit

Permalink
add primary cobalt instances
Browse files Browse the repository at this point in the history
  • Loading branch information
james.croucher committed Nov 7, 2024
1 parent 16df4b9 commit 364d60a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
1 change: 1 addition & 0 deletions assyst-common/src/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub struct Entitlements {
pub struct CobaltApiInstance {
pub url: String,
pub key: String,
pub primary: Option<bool>,
}

#[derive(Deserialize, Clone)]
Expand Down
16 changes: 12 additions & 4 deletions assyst-core/src/command/services/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ impl ParseArgument for DownloadFlags {
let audio = int_arg_bool!(ctxt, "audio", false);
let verbose = int_arg_bool!(ctxt, "verbose", false);

Ok(Self { audio, quality, verbose })
Ok(Self {
audio,
quality,
verbose,
})
}
}

Expand Down Expand Up @@ -161,7 +165,9 @@ pub async fn download(ctxt: CommandCtxt<'_>, url: Word, options: DownloadFlags)
video_tasks.spawn(async move {
let _lock = loop {
let r#try = l.iter().find(|x| x.try_lock().is_ok());
if let Some(l) = r#try { break l.lock().await } else {
if let Some(l) = r#try {
break l.lock().await;
} else {
let time = thread_rng().gen_range(10..1500);
sleep(Duration::from_millis(time)).await;
}
Expand All @@ -175,7 +181,9 @@ pub async fn download(ctxt: CommandCtxt<'_>, url: Word, options: DownloadFlags)
match media {
Ok(Ok(m)) => {
let mut z_lock = z.lock().await;
let r#type = if let Some(t) = filetype::get_sig(&m) { t } else {
let r#type = if let Some(t) = filetype::get_sig(&m) {
t
} else {
failed.lock().unwrap().push(format!("{url}: Unknown signature"));
return;
};
Expand Down Expand Up @@ -268,7 +276,7 @@ pub async fn download(ctxt: CommandCtxt<'_>, url: Word, options: DownloadFlags)
ctxt.reply((
result,
&format!(
"Took {}\n{}",
"Took {}\n{}\n",
format_duration(&ctxt.data.execution_timings.processing_time_start.elapsed()),
format!(
"Downloaded with {}",
Expand Down
2 changes: 2 additions & 0 deletions assyst-core/src/rest/web_media_download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ pub async fn download_web_media(client: &Client, url: &str, opts: WebDownloadOpt
bail!("No available instances are defined.");
}
urls.shuffle(&mut thread_rng());
// put primary ones first
urls.sort_by(|a, b| b.primary.unwrap_or(false).cmp(&a.primary.unwrap_or(false)));
urls
};

Expand Down
6 changes: 4 additions & 2 deletions config.template.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ proxy = []
filer = ""
# Bad translation URL.
bad_translation = ""
# Cobalt API instances
cobalt_api = [{ url = "", key = "" }]
# Cobalt API instances - "primary" is optional and default false
# "primary" is always the first instance that is tried, if multiple are primary it selects the lowest index primary and rest are ignored
# "primary" can also be excluded for no primary instance
cobalt_api = [{ url = "", key = "", primary = true }]

[authentication]
# Token to authenticate with Discord.
Expand Down

0 comments on commit 364d60a

Please sign in to comment.