added rss generator
This commit is contained in:
1
backend/public/src/utils/mod.rs
Normal file
1
backend/public/src/utils/mod.rs
Normal file
@ -0,0 +1 @@
|
||||
pub mod rss;
|
90
backend/public/src/utils/rss.rs
Normal file
90
backend/public/src/utils/rss.rs
Normal file
@ -0,0 +1,90 @@
|
||||
use crate::routes::posts;
|
||||
use std::collections::HashMap;
|
||||
use xml::escape::escape_str_pcdata;
|
||||
|
||||
pub struct RssEntry {
|
||||
pub title: String,
|
||||
pub link: String,
|
||||
pub description: Option<String>,
|
||||
pub created_at: String,
|
||||
pub author: String,
|
||||
pub guid: String,
|
||||
}
|
||||
|
||||
impl From<posts::Post> for RssEntry {
|
||||
fn from(post: posts::Post) -> Self {
|
||||
let web_url = std::env::var("BASE_URI_WEB").expect("Environment variable not found");
|
||||
let post_url = format!("{}{}{}", web_url, "/posts/", post.post_id.to_string());
|
||||
let author_full_name = format!("{} {}", post.first_name.unwrap(), post.last_name.unwrap());
|
||||
|
||||
Self {
|
||||
title: post.title.clone(),
|
||||
link: post_url.clone(),
|
||||
description: Some(post.body.clone()),
|
||||
created_at: post
|
||||
.created_at
|
||||
.expect("No timestamp was available")
|
||||
.to_rfc2822(),
|
||||
author: author_full_name,
|
||||
guid: post_url.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl RssEntry {
|
||||
pub fn to_item(&self) -> String {
|
||||
format!(
|
||||
r#"
|
||||
<item>
|
||||
<title><![CDATA[{}]]></title>
|
||||
<description><![CDATA[{}]]></description>
|
||||
<pubDate>{}</pubDate>
|
||||
<link>{}</link>
|
||||
<guid isPermaLink="true">{}</guid>
|
||||
</item>
|
||||
"#,
|
||||
self.title,
|
||||
self.description.clone().unwrap_or_default(),
|
||||
self.created_at,
|
||||
self.guid,
|
||||
self.guid
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn generate_rss(
|
||||
title: &str,
|
||||
description: &str,
|
||||
link: &str,
|
||||
posts: &HashMap<String, posts::Post>,
|
||||
) -> String {
|
||||
println!("{:?}", posts);
|
||||
let values = posts.clone().into_values();
|
||||
println!("{:?}", values);
|
||||
|
||||
let rss_entries = values
|
||||
.map(|p| p.into())
|
||||
.map(|r: RssEntry| r.to_item())
|
||||
.collect::<String>();
|
||||
|
||||
let safe_title = escape_str_pcdata(title);
|
||||
let safe_description = escape_str_pcdata(description);
|
||||
println!("{:?}", rss_entries);
|
||||
|
||||
format!(
|
||||
r#"<?xml version="1.0" encoding="UTF-8"?>
|
||||
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
|
||||
<channel>
|
||||
<title>{safe_title}</title>
|
||||
<description>{safe_description}</description>
|
||||
<link>{link}</link>
|
||||
<language>en-us</language>
|
||||
<ttl>60</ttl>
|
||||
<generator>Kyouma 1.0.0-SE</generator>
|
||||
<atom:link href="https://wyattjmiller.com/posts.xml" rel="self" type="application/rss+xml" />
|
||||
{}
|
||||
</channel>
|
||||
</rss>"#,
|
||||
rss_entries
|
||||
)
|
||||
}
|
Reference in New Issue
Block a user