mirror of
https://github.com/YGGverse/mb.git
synced 2026-04-01 01:25:29 +00:00
initial commit
This commit is contained in:
parent
f2b0a1979d
commit
8e7df9ff72
14 changed files with 1011 additions and 0 deletions
88
src/feed.rs
Normal file
88
src/feed.rs
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
mod link;
|
||||
|
||||
use chrono::{DateTime, Utc};
|
||||
use link::Link;
|
||||
use url::Url;
|
||||
|
||||
/// Export crawl index to the RSS file
|
||||
pub struct Feed {
|
||||
buffer: String,
|
||||
canonical: Link,
|
||||
}
|
||||
|
||||
impl Feed {
|
||||
pub fn new(
|
||||
title: &str,
|
||||
description: Option<&str>,
|
||||
canonical: Option<Url>,
|
||||
capacity: usize,
|
||||
) -> Self {
|
||||
let t = chrono::Utc::now().to_rfc2822();
|
||||
let mut buffer = String::with_capacity(capacity);
|
||||
|
||||
buffer.push_str("<?xml version=\"1.0\" encoding=\"UTF-8\"?><rss version=\"2.0\"><channel>");
|
||||
|
||||
buffer.push_str("<pubDate>");
|
||||
buffer.push_str(&t);
|
||||
buffer.push_str("</pubDate>");
|
||||
|
||||
buffer.push_str("<lastBuildDate>");
|
||||
buffer.push_str(&t);
|
||||
buffer.push_str("</lastBuildDate>");
|
||||
|
||||
buffer.push_str("<title>");
|
||||
buffer.push_str(title);
|
||||
buffer.push_str("</title>");
|
||||
|
||||
if let Some(d) = description {
|
||||
buffer.push_str("<description>");
|
||||
buffer.push_str(d);
|
||||
buffer.push_str("</description>")
|
||||
}
|
||||
|
||||
if let Some(ref c) = canonical {
|
||||
// @TODO required the RSS specification!
|
||||
buffer.push_str("<link>");
|
||||
buffer.push_str(c.as_str());
|
||||
buffer.push_str("</link>")
|
||||
}
|
||||
|
||||
Self {
|
||||
buffer,
|
||||
canonical: Link::from_url(canonical),
|
||||
}
|
||||
}
|
||||
|
||||
/// Append `item` to the feed `channel`
|
||||
pub fn push(&mut self, guid: i64, time: DateTime<Utc>, title: String, message: &str) {
|
||||
self.buffer.push_str(&format!(
|
||||
"<item><guid>{guid}</guid><title>{title}</title><link>{}</link>",
|
||||
self.canonical.link(guid)
|
||||
));
|
||||
|
||||
self.buffer.push_str("<description>");
|
||||
self.buffer.push_str(&escape(message));
|
||||
self.buffer.push_str("</description>");
|
||||
|
||||
self.buffer.push_str("<pubDate>");
|
||||
self.buffer.push_str(&time.to_rfc2822());
|
||||
self.buffer.push_str("</pubDate>");
|
||||
|
||||
self.buffer.push_str("</item>")
|
||||
}
|
||||
|
||||
/// Write final bytes
|
||||
pub fn commit(mut self) -> String {
|
||||
self.buffer.push_str("</channel></rss>");
|
||||
self.buffer
|
||||
}
|
||||
}
|
||||
|
||||
fn escape(value: &str) -> String {
|
||||
value
|
||||
.replace('&', "&")
|
||||
.replace('<', "<")
|
||||
.replace('>', ">")
|
||||
.replace('"', """)
|
||||
.replace("'", "'")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue