Add RSS feed
This commit is contained in:
parent
db1dcc0704
commit
b4412c00ad
66
src/io.rs
66
src/io.rs
|
@ -1,12 +1,53 @@
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::fs::OpenOptions;
|
use std::fs::OpenOptions;
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
use std::path::{PathBuf};
|
use std::path::PathBuf;
|
||||||
use std::env;
|
use std::env;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
use chrono::Utc;
|
||||||
|
use rss::{ChannelBuilder, ItemBuilder, Item, Guid};
|
||||||
|
|
||||||
use crate::structures::*;
|
use crate::structures::*;
|
||||||
|
|
||||||
|
pub fn write_feed(lang: &str) {
|
||||||
|
// Path to atom feed
|
||||||
|
let mut path = PathBuf::new();
|
||||||
|
path.push("static");
|
||||||
|
path.push(lang);
|
||||||
|
path.push("rss.xml");
|
||||||
|
|
||||||
|
let file = match File::create(path) {
|
||||||
|
Err(why) => panic!("couldn't create feed: {}", why),
|
||||||
|
Ok(file) => file,
|
||||||
|
};
|
||||||
|
|
||||||
|
let recipes = db_get_all_recipes();
|
||||||
|
let mut feed_items: Vec<Item> = Vec::new();
|
||||||
|
for i in recipes {
|
||||||
|
feed_items.push(ItemBuilder::default()
|
||||||
|
.title(i.name)
|
||||||
|
.link(format!("/{}", &i.shortcode))
|
||||||
|
.guid(Guid {
|
||||||
|
value: i.id.as_simple().to_string(),
|
||||||
|
permalink: false})
|
||||||
|
.pub_date(i.posted_date.to_string())
|
||||||
|
.build());
|
||||||
|
}
|
||||||
|
|
||||||
|
let channel = ChannelBuilder::default()
|
||||||
|
.title("Catgirl Cooking")
|
||||||
|
.link("https://catgirl.cooking")
|
||||||
|
.description("New recipes for catgirl.cooking")
|
||||||
|
.last_build_date(Utc::now().to_string())
|
||||||
|
.items(feed_items)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
match channel.write_to(file) {
|
||||||
|
Ok(_) => println!("wrote feed to file"),
|
||||||
|
Err(e) => panic!("couldn't write feed to file: {}", e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn write_html(html: String, lang: &str, shortcode: Option<&str>) {
|
pub fn write_html(html: String, lang: &str, shortcode: Option<&str>) {
|
||||||
// Create a path to write the file to
|
// Create a path to write the file to
|
||||||
let mut path = PathBuf::new();
|
let mut path = PathBuf::new();
|
||||||
|
@ -146,3 +187,26 @@ pub fn db_get_all_recipes() -> Vec<Recipe> {
|
||||||
|
|
||||||
return recipes_list;
|
return recipes_list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn db_get_last_recipe() -> Recipe {
|
||||||
|
let db = match open_database("recipes") {
|
||||||
|
Some(database) => database,
|
||||||
|
None => panic!(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let last_key = match db.last() {
|
||||||
|
Ok(pair) => match pair {
|
||||||
|
Some(key) => key.0,
|
||||||
|
None => panic!("error getting first key"),
|
||||||
|
}
|
||||||
|
Err(error) => panic!("error getting first key: {}", error),
|
||||||
|
};
|
||||||
|
|
||||||
|
let read_entry;
|
||||||
|
match bincode::deserialize(&last_key) {
|
||||||
|
Ok(result) => read_entry = result,
|
||||||
|
Err(error) => panic!("faled to deserialize entry: {}", error),
|
||||||
|
}
|
||||||
|
|
||||||
|
return read_entry;
|
||||||
|
}
|
||||||
|
|
|
@ -5,9 +5,6 @@ mod structures;
|
||||||
mod io;
|
mod io;
|
||||||
mod tests;
|
mod tests;
|
||||||
|
|
||||||
use crate::structures::*;
|
|
||||||
use crate::structures::{MeasurementUnits::*, TimeUnits::*};
|
|
||||||
|
|
||||||
fn construct_main_page() {
|
fn construct_main_page() {
|
||||||
// Specify info to go onto the base page
|
// Specify info to go onto the base page
|
||||||
let base_page = build_html::HtmlPage::new()
|
let base_page = build_html::HtmlPage::new()
|
||||||
|
@ -22,7 +19,7 @@ fn construct_main_page() {
|
||||||
let footer = Container::new(ContainerType::Footer)
|
let footer = Container::new(ContainerType::Footer)
|
||||||
.with_raw(r#"<hr>"#) // Line seperator
|
.with_raw(r#"<hr>"#) // Line seperator
|
||||||
.with_link("/", "home") // Link to the root page
|
.with_link("/", "home") // Link to the root page
|
||||||
.with_link("/atom.xml", "atom") // Link the the Atom feed
|
.with_link("/rss.xml", "rss") // Link the the Atom feed
|
||||||
// License info
|
// License info
|
||||||
.with_paragraph("Software licensed under the CNPLv7+")
|
.with_paragraph("Software licensed under the CNPLv7+")
|
||||||
.with_paragraph("Recipes under Public Domain");
|
.with_paragraph("Recipes under Public Domain");
|
||||||
|
@ -70,6 +67,7 @@ fn rebuild() {
|
||||||
for i in io::db_get_all_recipes() {
|
for i in io::db_get_all_recipes() {
|
||||||
i.construct_page();
|
i.construct_page();
|
||||||
}
|
}
|
||||||
|
io::write_feed("en");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
Loading…
Reference in New Issue