diff options
Diffstat (limited to 'crates/yt/src/storage/db/subscription.rs')
-rw-r--r-- | crates/yt/src/storage/db/subscription.rs | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/crates/yt/src/storage/db/subscription.rs b/crates/yt/src/storage/db/subscription.rs new file mode 100644 index 0000000..07e5eec --- /dev/null +++ b/crates/yt/src/storage/db/subscription.rs @@ -0,0 +1,41 @@ +use std::collections::HashMap; + +use anyhow::Result; +use log::debug; +use url::Url; +use yt_dlp::{json_cast, options::YoutubeDLOptions}; + +#[derive(Clone, Debug)] +pub(crate) struct Subscription { + /// The human readable name of this subscription + pub(crate) name: String, + + /// The URL this subscription subscribes to + pub(crate) url: Url, +} + +impl Subscription { + #[must_use] + pub(crate) fn new(name: String, url: Url) -> Self { + Self { name, url } + } +} + +#[derive(Default, Debug)] +pub(crate) struct Subscriptions(pub(crate) HashMap<String, Subscription>); + +/// Check whether an URL could be used as a subscription URL +pub(crate) async fn check_url(url: Url) -> Result<bool> { + let yt_dlp = YoutubeDLOptions::new() + .set("playliststart", 1) + .set("playlistend", 10) + .set("noplaylist", false) + .set("extract_flat", "in_playlist") + .build()?; + + let info = yt_dlp.extract_info(&url, false, false)?; + + debug!("{info:#?}"); + + Ok(info.get("_type").map(|v| json_cast!(v, as_str)) == Some("playlist")) +} |