mirror of
https://gitlab.com/Anson-Projects/zine.git
synced 2025-07-27 08:41:25 +00:00
init commit
This commit is contained in:
88
src/main.rs
Normal file
88
src/main.rs
Normal file
@@ -0,0 +1,88 @@
|
||||
extern crate feed_rs;
|
||||
extern crate maud;
|
||||
extern crate reqwest;
|
||||
|
||||
use feed_rs::model::Entry;
|
||||
use feed_rs::parser;
|
||||
use maud::{html, Markup};
|
||||
use reqwest::blocking::get;
|
||||
use std::cmp::Reverse;
|
||||
use std::error::Error;
|
||||
use std::fs;
|
||||
use std::fs::write;
|
||||
use std::fs::DirBuilder;
|
||||
use std::path::Path;
|
||||
|
||||
fn fetch_feed(url: &str) -> Result<Vec<Entry>, Box<dyn Error>> {
|
||||
let content = get(url)?.text()?;
|
||||
let feed = parser::parse(content.as_bytes())?;
|
||||
Ok(feed.entries)
|
||||
}
|
||||
|
||||
fn generate_html(entries: Vec<Entry>) -> Markup {
|
||||
html! {
|
||||
(maud::DOCTYPE)
|
||||
html {
|
||||
head {
|
||||
title { "Anson Zine" }
|
||||
link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@1/css/pico.min.css";
|
||||
|
||||
}
|
||||
body {
|
||||
h1 { "Aggregated Feed" }
|
||||
ul {
|
||||
@for entry in entries {
|
||||
li {
|
||||
@if let Some(link) = entry.links.first() {
|
||||
a href=(link.href) {
|
||||
@if let Some(title) = entry.title {
|
||||
(title.content)
|
||||
}
|
||||
}
|
||||
}
|
||||
p {
|
||||
({
|
||||
entry.published.unwrap_or(entry.updated.unwrap_or_default())
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
let binding = fs::read_to_string("feeds.txt").unwrap();
|
||||
let feed_urls: Vec<&str> = binding.lines().collect();
|
||||
|
||||
let mut entries: Vec<Entry> = Vec::new();
|
||||
|
||||
for url in feed_urls {
|
||||
match fetch_feed(url) {
|
||||
Ok(mut feed_entries) => entries.append(&mut feed_entries),
|
||||
Err(e) => println!("Failed to fetch or parse feed {}: {}", url, e),
|
||||
}
|
||||
}
|
||||
|
||||
// Remove any entries that don't have a timestamp, and then sort by timestamps
|
||||
entries.retain(|entry| entry.published.is_some() || entry.updated.is_some());
|
||||
entries
|
||||
.sort_by_key(|entry| Reverse(entry.published.unwrap_or(entry.updated.unwrap_or_default())));
|
||||
|
||||
let html_string = generate_html(entries).into_string();
|
||||
|
||||
let output_path = Path::new("output/index.html");
|
||||
DirBuilder::new()
|
||||
.recursive(true)
|
||||
.create(output_path.parent().unwrap())
|
||||
.unwrap();
|
||||
|
||||
match write(output_path, html_string) {
|
||||
Ok(_) => println!("Successfully wrote to {}", output_path.display()),
|
||||
Err(e) => eprintln!("Failed to write to {}: {}", output_path.display(), e),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
Reference in New Issue
Block a user