use std::sync::Arc; use crate::scrapers::{Media, MediaType, apple_music::AppleMusicClient, backloggd, letterboxd}; use askama::Template; use rand::seq::IndexedRandom; use serde::Deserialize; #[derive(Deserialize)] pub struct IndexOptions { #[cfg(debug_assertions)] #[serde(default)] mock: bool, } type MediaList = [(MediaType, Option); 3]; #[derive(Debug, Clone)] pub struct Shas { pub website: Option, } #[derive(Template, Debug, Clone)] #[template(path = "index.html")] pub struct RootTemplate { media: MediaList, consumption_verb: &'static str, shas: Shas, } impl RootTemplate { pub fn new( apple_music_client: Arc, shas: Shas, #[allow(unused_variables)] options: &IndexOptions, ) -> RootTemplate { #[cfg(debug_assertions)] let media = if options.mock { mocked_media() } else { Self::fetch_media(apple_music_client) }; #[cfg(not(debug_assertions))] let media = Self::fetch_media(apple_music_client); let consumption_verb = Self::random_consumption_verb(); RootTemplate { media, consumption_verb, shas, } } fn fetch_media(apple_music_client: Arc) -> MediaList { [ (MediaType::Game, backloggd::try_cached_fetch()), (MediaType::Film, letterboxd::try_cached_fetch()), (MediaType::Song, apple_music_client.try_cached_fetch()), ] } fn random_consumption_verb() -> &'static str { static CONSUMPTION_VERBS: &[&str] = &["swallowed", "inhaled", "digested", "ingested"]; CONSUMPTION_VERBS.choose(&mut rand::rng()).unwrap() } } #[cfg(debug_assertions)] fn mocked_media() -> MediaList { [ (MediaType::Game, Some(Media { name: "Cyberpunk 2077: Ultimate Edition".to_owned(), image: "https://images.igdb.com/igdb/image/upload/t_cover_big/co7iy1.jpg".to_owned(), context: "Nintendo Switch 2".to_owned(), url: "https://backloggd.com/u/cherryfunk/logs/cyberpunk-2077-ultimate-edition/".to_owned() })), (MediaType::Film, Some(Media { name: "The Thursday Murder Club".to_owned(), image: "https://a.ltrbxd.com/resized/film-poster/6/6/6/2/8/6/666286-the-thursday-murder-club-0-230-0-345-crop.jpg?v=4bfeae38a7".to_owned(), context: "1 star".to_owned(), url: "https://letterboxd.com/ivom/film/the-thursday-murder-club/".to_owned() })), (MediaType::Song, Some(Media { name: "We Might Feel Unsound".to_owned(), image: "https://is1-ssl.mzstatic.com/image/thumb/Music124/v4/f4/b2/8e/f4b28ee4-01c6-232c-56a7-b97fd5b0e0ae/00602527857671.rgb.jpg/240x240bb.jpg".to_owned(), context: "James Blake — James Blake".to_owned(), url: "https://music.apple.com/gb/album/we-might-feel-unsound/1443124478?i=1443125024".to_owned() })), ] }