diff options
Diffstat (limited to '')
| -rw-r--r-- | src/blog.rs | 12 | ||||
| -rw-r--r-- | src/main.rs | 59 | ||||
| -rw-r--r-- | src/templates.rs | 13 | ||||
| -rw-r--r-- | src/visits.rs | 14 |
4 files changed, 89 insertions, 9 deletions
diff --git a/src/blog.rs b/src/blog.rs index 9acb5e2..36204e7 100644 --- a/src/blog.rs +++ b/src/blog.rs @@ -17,12 +17,12 @@ static DIRECTORY: &str = "./blog"; #[derive(Clone)] pub struct Blogpost { - file_name: String, - title: String, - published_at: DateTime<Utc>, - updated_at: Option<DateTime<Utc>>, - tags: Vec<String>, - content: String, + pub file_name: String, + pub title: String, + pub published_at: DateTime<Utc>, + pub updated_at: Option<DateTime<Utc>>, + pub tags: Vec<String>, + pub content: String, } #[derive(Deserialize)] diff --git a/src/main.rs b/src/main.rs index 7253a16..bbfcbff 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,9 +12,11 @@ mod scrobbles; mod skweets; mod templates; mod utils; +mod visits; use std::{collections::HashSet, time::Duration}; +use chrono::{DateTime, Utc}; use i18n::set_language; use poem::http::StatusCode; use poem::i18n::unic_langid::langid; @@ -53,25 +55,69 @@ struct Static; #[handler] async fn home(Data(reqwest): Data<&reqwest::Client>, locale: Locale) -> templates::Home { let listenbrainz_client = listenbrainz::raw::Client::new(); - let (live, listenbrainz, blogposts, poems) = tokio::join!( + let (live, listenbrainz, blogposts, poems, visits) = tokio::join!( live::get_live_status(reqwest), scrobbles::get_now_playing(&listenbrainz_client), // skweets::get_recents(&clients.skinnyverse), Blogpost::get_articles(), - Poem::get_articles() + Poem::get_articles(), + visits::get_visits(reqwest) ); let is_live = live.unwrap_or_default().online; let listenbrainz = listenbrainz.unwrap_or_default(); let blogposts = blogposts.unwrap_or_default(); let poems = poems.unwrap_or_default(); + // let visits = visits.ok().map(|v| v.count); + let visits = visits + .ok() + .map(|v| v.count.replace("\u{202f}", "")) + .and_then(|c| c.parse().ok()); + + let mut posts = blogposts + .iter() + .take(10) + .map(Clone::clone) + .map(HomeFeedItem::Blogpost) + .chain( + poems + .iter() + .take(10) + .map(Clone::clone) + .map(HomeFeedItem::Poem), + ) + .collect::<Vec<_>>(); + + posts.sort_by_key(|i| i.published_at()); + posts.reverse(); + posts.truncate(10); + let poem = poems.choose(&mut rand::thread_rng()).cloned(); + templates::Home { title: locale.text("title").unwrap(), is_live, listenbrainz, + feed: posts, blogposts, poem, locale, + filter_tags: HashSet::new(), + jiggle: 8, + visits, + } +} + +pub enum HomeFeedItem { + Blogpost(Blogpost), + Poem(Poem), +} + +impl HomeFeedItem { + pub fn published_at(&self) -> DateTime<Utc> { + match self { + HomeFeedItem::Blogpost(post) => post.published_at, + HomeFeedItem::Poem(poem) => poem.published_at, + } } } @@ -160,6 +206,14 @@ async fn contact(locale: Locale) -> templates::Contact { } #[handler] +async fn about(locale: Locale) -> templates::About { + templates::About { + title: locale.text("title-about").unwrap(), + locale, + } +} + +#[handler] async fn plants() -> Result<()> { Err(BlossomError::Unimplemented) } @@ -229,6 +283,7 @@ async fn main() -> std::result::Result<(), std::io::Error> { .at("/poetry/:poem", get(get_poem)) .at("/feed", get(feed)) .at("/contact", get(contact)) + .at("/about", get(about)) .at("/plants", get(plants)) .nest("/static/", EmbeddedFilesEndpoint::<Static>::new()) .catch_all_error(custom_error) diff --git a/src/templates.rs b/src/templates.rs index 66462eb..c0b3c14 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -5,9 +5,9 @@ use poem::http::StatusCode; use rand::{thread_rng, Rng}; use crate::i18n::Locale; -use crate::poetry; use crate::posts::Post; use crate::{blog, scrobbles::NowPlayingData}; +use crate::{poetry, HomeFeedItem}; mod filters { pub fn mytruncate(s: impl std::fmt::Display, length: usize) -> ::askama::Result<String> { @@ -23,9 +23,20 @@ pub struct Home { pub title: String, pub is_live: bool, pub listenbrainz: NowPlayingData, + pub feed: Vec<HomeFeedItem>, pub blogposts: Vec<blog::Blogpost>, pub poem: Option<poetry::Poem>, pub locale: Locale, + pub filter_tags: HashSet<String>, + pub jiggle: isize, + pub visits: Option<usize>, +} + +#[derive(Template)] +#[template(path = "about.html")] +pub struct About { + pub title: String, + pub locale: Locale, } #[derive(Template)] diff --git a/src/visits.rs b/src/visits.rs new file mode 100644 index 0000000..1e0685c --- /dev/null +++ b/src/visits.rs @@ -0,0 +1,14 @@ +use reqwest::Client; +use serde::Deserialize; + +use crate::Result; + +#[derive(Deserialize, Default)] +pub struct Visits { + pub count: String, +} + +pub async fn get_visits(client: &Client) -> Result<Visits> { + let endpoint = "https://stats.blos.sm/counter/TOTAL.json"; + Ok(client.get(endpoint).send().await?.json::<Visits>().await?) +} |
