use std::fmt;
use crate::client::deps::github::GitHubFetcher;
use crate::client::deps::{Asset, WantedRelease};
use crate::error::Result;
use crate::utils::platform::{Architecture, Platform};
#[derive(Debug)]
pub struct YoutubeFetcher {
fetcher: GitHubFetcher,
}
impl YoutubeFetcher {
pub fn new(owner: impl Into<String>, repo: impl Into<String>) -> Self {
let owner = owner.into();
let repo = repo.into();
tracing::debug!(
owner = %owner,
repo = %repo,
"⚙️ Creating new YoutubeFetcher"
);
Self {
fetcher: GitHubFetcher::new(owner, repo),
}
}
pub async fn fetch_release(&self, auth_token: Option<String>) -> Result<WantedRelease> {
tracing::debug!(
has_token = auth_token.is_some(),
"📦 Fetching yt-dlp release for current platform"
);
self.fetcher.fetch_release(auth_token, Self::select_asset).await
}
fn select_asset<'a>(
release: &'a crate::client::deps::Release,
platform: &Platform,
architecture: &Architecture,
) -> Option<&'a Asset> {
tracing::debug!(
platform = ?platform,
architecture = ?architecture,
asset_count = release.assets.len(),
"⚙️ Selecting yt-dlp asset for platform"
);
let base_name = "yt-dlp";
release.assets.iter().find(|asset| {
let name = &asset.name;
match (platform, architecture) {
(Platform::Windows, Architecture::X64) => name == &format!("{}.exe", base_name),
(Platform::Windows, Architecture::X86) => name == &format!("{}_x86.exe", base_name),
(Platform::Linux, Architecture::X64) => name == &format!("{}_linux", base_name),
(Platform::Linux, Architecture::Armv7l) => name == &format!("{}_linux_armv7l", base_name),
(Platform::Linux, Architecture::Aarch64) => name == &format!("{}_linux_aarch64", base_name),
(Platform::Mac, _) => name == &format!("{}_macos", base_name),
_ => false,
}
})
}
}
impl fmt::Display for YoutubeFetcher {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "YoutubeFetcher(fetcher={})", self.fetcher)
}
}