ytd/src/provider.rs
2026-04-08 12:52:44 +03:00

67 lines
1.9 KiB
Rust

use anyhow::{Result, anyhow};
use invidious::{ClientSync, ClientSyncTrait};
use rustypipe::client::RustyPipe;
pub struct Video {
pub id: String,
pub name: String,
pub is_live: bool,
pub is_short: bool,
pub is_upcoming: bool,
}
pub enum Provider {
Youtube(RustyPipe),
Invidious(ClientSync),
}
impl Provider {
pub fn youtube() -> Self {
Self::Youtube(RustyPipe::new())
}
pub fn invidious(instance: Option<String>) -> Self {
let mut client = ClientSync::default();
if let Some(i) = instance {
client.set_instance(i)
}
Self::Invidious(client)
}
pub async fn videos(&self, channel_id: &str) -> Result<Vec<Video>> {
let mut videos = Vec::new();
match self {
Provider::Youtube(client) => {
for video in client
.query()
.channel_videos(channel_id)
.await?
.content
.items
{
videos.push(Video {
id: video.id,
name: video.name,
is_live: video.is_live,
is_short: video.is_short,
is_upcoming: video.is_upcoming,
})
}
}
Provider::Invidious(client) => {
for video in client
.channel_videos(channel_id, None)
.map_err(|e| anyhow!("{e:?}"))?
.videos
{
videos.push(Video {
id: video.id,
name: video.title,
is_live: video.live,
is_short: video.length <= 60,
is_upcoming: video.upcoming,
})
}
}
}
Ok(videos)
}
}