Something went wrong. Try again.
A terminal UI for Libby libbui.vinnymac.dev
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133//! URLs the UI hands to a browser or an image fetcher.//!//! None of these make a request. Permalinks let the user open a title in//! Libby for reading or listening.
use url::Url;
use crate::ids::{LibraryKey, TitleId};
const LIBBY_APP: &str = "https://libbyapp.com/";const SHARE: &str = "https://share.libbyapp.com/";const RESIZER: &str = "https://ic.od-cdn.com/resize";
/// The title's page in the Libby web app, scoped to a library:/// `https://libbyapp.com/library/{key}/everything/page-1/{titleId}`.#[must_use]pub fn libby_title_url(library: &LibraryKey, title: &TitleId) -> Url { with_segments( LIBBY_APP, &[ "library", library.as_str(), "everything", "page-1", title.as_str(), ], )}
/// The library-independent share link: `https://share.libbyapp.com/title/{titleId}`.#[must_use]pub fn libby_share_url(title: &TitleId) -> Url { with_segments(SHARE, &["title", title.as_str()])}
/// The title on the library's classic OverDrive site:/// `https://{key}.overdrive.com/media/{titleId}`.////// # Errors////// [`crate::Error::InvalidInput`] when the key cannot be a host name label.pub fn overdrive_title_url(library: &LibraryKey, title: &TitleId) -> crate::Result<Url> { let key = library.as_str(); if key.is_empty() || !key.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'-') { return Err(crate::Error::InvalidInput(format!( "library key {key:?} is not a host name label" ))); } let base = Url::parse(&format!("https://{key}.overdrive.com/")) .map_err(|e| crate::Error::InvalidInput(format!("library key {key:?}: {e}")))?; Ok(with_segments(base.as_str(), &["media", title.as_str()]))}
/// A cover resized (and cropped to fit) by OverDrive's image CDN:/// `https://ic.od-cdn.com/resize?type=auto&width=W&height=H&force=true&quality=80&url={href}`.////// `href` is a cover href from [`crate::models::Cover`]; it is percent-encoded/// into the query.#[must_use]pub fn cover_resize_url(href: &str, width: u32, height: u32) -> Url { let width = width.to_string(); let height = height.to_string(); Url::parse_with_params( RESIZER, [ ("type", "auto"), ("width", width.as_str()), ("height", height.as_str()), ("force", "true"), ("quality", "80"), ("url", href), ], ) .expect("the resizer base URL is a constant valid URL")}
fn with_segments(base: &str, segments: &[&str]) -> Url { let mut url = Url::parse(base).expect("permalink bases are constant valid URLs"); url.path_segments_mut() .expect("permalink bases are http(s) URLs") .pop_if_empty() .extend(segments); url}
#[cfg(test)]mod tests { #![allow(clippy::unwrap_used, reason = "a failing test should panic")]
use super::*;
#[test] fn permalinks() { let key = LibraryKey::new("lapl"); let title = TitleId::new("2308988"); assert_eq!( libby_title_url(&key, &title).as_str(), "https://libbyapp.com/library/lapl/everything/page-1/2308988" ); assert_eq!( libby_share_url(&title).as_str(), "https://share.libbyapp.com/title/2308988" ); assert_eq!( overdrive_title_url(&key, &title).unwrap().as_str(), "https://lapl.overdrive.com/media/2308988" ); assert!(overdrive_title_url(&LibraryKey::new("evil.com/x"), &title).is_err()); }
#[test] fn segments_are_percent_encoded() { let url = libby_title_url(&LibraryKey::new("a/b"), &TitleId::new("1 2")); assert_eq!( url.as_str(), "https://libbyapp.com/library/a%2Fb/everything/page-1/1%202" ); }
#[test] fn resizer_encodes_the_cover_href() { let url = cover_resize_url( "https://img1.od-cdn.com/ImageType-100/0111-1/{ABC}Img100.jpg", 200, 300, ); assert_eq!( url.as_str(), "https://ic.od-cdn.com/resize?type=auto&width=200&height=300&force=true&quality=80&url=https%3A%2F%2Fimg1.od-cdn.com%2FImageType-100%2F0111-1%2F%7BABC%7DImg100.jpg" ); }}