From 0644a094bf0a9ef3a11661a32f339010bcf17c55 Mon Sep 17 00:00:00 2001 From: cel 🌸 Date: Fri, 2 Jan 2026 01:23:45 +0100 Subject: add visits counter to home --- TODO.md | 4 ++-- resources/en/base.ftl | 1 + src/main.rs | 12 ++++++++++-- src/templates.rs | 1 + src/visits.rs | 14 ++++++++++++++ templates/home.html | 7 +++++++ 6 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 src/visits.rs diff --git a/TODO.md b/TODO.md index f09ae64..49323f7 100644 --- a/TODO.md +++ b/TODO.md @@ -12,7 +12,7 @@ [x] update serving https badges better thumb LOL [ ] migrate to ructe [ ] opengraph -[ ] publish new poetry +[x] publish new poetry [x] make sure posts are organised in date order [x] atom feed [x] tags @@ -40,7 +40,7 @@ [ ] comments [ ] brush font PLZ [ ] deploy database yum -[ ] visitor counter +[x] visitor counter [ ] guestbook [ ] webmention [x] h-card diff --git a/resources/en/base.ftl b/resources/en/base.ftl index 7c58882..aef187d 100644 --- a/resources/en/base.ftl +++ b/resources/en/base.ftl @@ -8,3 +8,4 @@ filter-by-tags = Filter by Tags permalink = Permalink untitled = Untitled now-playing = Now Playing +visits = {" "}visits diff --git a/src/main.rs b/src/main.rs index 7247c33..bbfcbff 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,7 @@ mod scrobbles; mod skweets; mod templates; mod utils; +mod visits; use std::{collections::HashSet, time::Duration}; @@ -54,17 +55,23 @@ 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() @@ -96,6 +103,7 @@ async fn home(Data(reqwest): Data<&reqwest::Client>, locale: Locale) -> template locale, filter_tags: HashSet::new(), jiggle: 8, + visits, } } diff --git a/src/templates.rs b/src/templates.rs index c086487..c0b3c14 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -29,6 +29,7 @@ pub struct Home { pub locale: Locale, pub filter_tags: HashSet, pub jiggle: isize, + pub visits: Option, } #[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 { + let endpoint = "https://stats.blos.sm/counter/TOTAL.json"; + Ok(client.get(endpoint).send().await?.json::().await?) +} diff --git a/templates/home.html b/templates/home.html index 95e614c..ba6615f 100644 --- a/templates/home.html +++ b/templates/home.html @@ -50,6 +50,13 @@ {% include "latestposts.html" %} {% include "random-poem.html" %} + {% match visits %} + {% when Some(visit_count) %} +
+ {{ visit_count }}{{ locale.text("visits").unwrap() }}
+ {% when None %} + {% endmatch %} -- cgit