Skip to content

Commit

Permalink
support alternative Twitter embed services
Browse files Browse the repository at this point in the history
  • Loading branch information
jelni committed Dec 30, 2024
1 parent 981f717 commit d510fee
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/commands/cobalt_download.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::env;
use std::path::Path;

Expand All @@ -18,6 +19,9 @@ use crate::utilities::file_download::NetworkFile;
use crate::utilities::message_entities::{self, ToEntity};
use crate::utilities::telegram_utils;

const TWITTER_REPLACEMENTS: [&str; 5] =
["fxtwitter.com", "fixupx.com", "twittpr.com", "vxtwitter.com", "fixvx.com"];

#[derive(Deserialize)]
struct CobaltInstance<'a> {
name: &'a str,
Expand Down Expand Up @@ -86,18 +90,22 @@ async fn get_result(
url: &str,
audio_only: bool,
) -> Result<(String, Response), cobalt::Error> {
let url = TWITTER_REPLACEMENTS
.into_iter()
.find_map(|replacement| url.strip_prefix(&format!("https://{replacement}/")))
.map(|path| Cow::Owned(format!("https://twitter.com/{path}")))
.unwrap_or(Cow::Borrowed(url));

let instances = env::var("COBALT_INSTANCES").unwrap();
let instances = serde_json::from_str::<Vec<CobaltInstance>>(&instances).unwrap();

let mut error = None;

for instance in instances {
match cobalt::query(http_client, instance.url, instance.api_key, url, audio_only).await {
match cobalt::query(http_client, instance.url, instance.api_key, &url, audio_only).await {
Ok(result) => return Ok((instance.name.into(), result)),
Err(err) => {
if error.is_none() {
error = Some(err);
}
error = Some(err);
}
}
}
Expand Down

0 comments on commit d510fee

Please sign in to comment.