feat: rss

This commit is contained in:
Kilerd Chan 2019-04-19 11:52:36 +08:00
parent 57a5577cf4
commit 680599d4eb
4 changed files with 75 additions and 54 deletions

View File

@ -17,7 +17,6 @@ tera = "0.11.0"
pulldown-cmark = { version = "0.1.2", default-features = false }
chrono = { version = "*", features = ["serde"] }
rust-crypto = "^0.2"
rss = "1.6.1"
actix-web= "1.0.0-alpha.6"
actix-files = "0.1.0-alpha.6"
@ -28,3 +27,4 @@ http = "0.1.17"
rand = "0.6.5"
pretty_env_logger = "0.3.0"
time = "0.1.42"
rss = "1.7.0"

View File

@ -17,6 +17,7 @@ use actix_web::{
use dotenv::dotenv;
use crate::pg_pool::database_pool_establish;
use actix_web::web::route;
use rand::prelude::*;
use std::rc::Rc;
use std::sync::Arc;
@ -77,7 +78,8 @@ fn main() -> std::io::Result<()> {
.service(routers::admin::admin_login)
.service(routers::admin::admin_authentication),
)
// .service(routers::article::get_article_by_url)
.service(routers::rss::rss_page)
.service(routers::article::get_article_by_url)
})
.bind(("127.0.0.1", 8000))?
.system_exit()

View File

@ -5,7 +5,7 @@ pub mod article;
use actix_web::error::Error;
use futures::future::{err, ok, FutureResult};
//pub mod rss;
pub mod rss;
//pub mod catacher;
//pub mod graphql;

View File

@ -1,51 +1,70 @@
//use rss::ChannelBuilder;
//use rss::Channel;
//use rss::{Item, ItemBuilder};
//use rocket::response::content;
//use crate::guard::SettingMap;
//use crate::pg_pool::DbConn;
//use crate::models::Article;
//use crate::response::ArticleResponse;
//use std::collections::HashMap;
//
//#[get("/rss")]
//pub fn rss(setting: SettingMap, conn: DbConn) -> content::Xml<String> {
// let result = Article::load_all(false, &conn);
// let article_responses: Vec<ArticleResponse> = result.iter().map(ArticleResponse::from).collect();
//
// let items: Vec<Item> = article_responses.into_iter().map(|item| {
// let url = match item.article.url.clone() {
// Some(content) => if !content.eq("") {
// format!("{}/{}", setting.url, content)
// } else {
// format!("{}/archives/{}", setting.url, item.article.id)
// },
// None => format!("{}/archives/{}", setting.url, item.article.id)
// };
// ItemBuilder::default()
// .title(item.article.title.clone())
// .link(url)
// .description(item.description)
// .content(item.markdown_content)
// .pub_date(item.article.publish_at.to_string())
// .build()
// .unwrap()
// }).collect();
//
// let mut namespaces: HashMap<String, String> = HashMap::new();
// namespaces.insert("dc".to_string(), "http://purl.org/dc/elements/1.1/".to_string());
// namespaces.insert("content".to_string(), "http://purl.org/rss/1.0/modules/content/".to_string());
// namespaces.insert("atom".to_string(), "http://www.w3.org/2005/Atom".to_string());
// namespaces.insert("media".to_string(), "http://search.yahoo.com/mrss/".to_string());
//
// let channel: Channel = ChannelBuilder::default()
// .title(setting.title)
// .description(setting.description)
// .generator("Rubble".to_string())
// .link(setting.url.clone())
// .items(items)
// .namespaces(namespaces)
// .build()
// .unwrap();
// content::Xml(channel.to_string())
//}
use crate::models::{article::Article, setting::Setting, CRUD};
use crate::pg_pool::Pool;
use crate::view::article::ArticleView;
use actix_web::{get, web, HttpResponse, Responder};
use rss::{Channel, ChannelBuilder, Item, ItemBuilder};
use std::collections::HashMap;
#[get("/rss")]
pub fn rss_page(conn: web::Data<Pool>) -> impl Responder {
let connection = conn.get().unwrap();
let articles = Article::read(&connection);
let setting = Setting::load(&connection);
// let article_responses: Vec<ArticleResponse> = articles.iter().map(ArticleView::from).collect();
let items: Vec<Item> = articles
.iter()
.map(ArticleView::from)
.map(|item| {
let url = match item.article.url.clone() {
Some(content) => {
if content.eq("") {
format!("{}/archives/{}", setting.url, item.article.id)
} else {
format!("{}/{}", setting.url, content)
}
}
_ => format!("{}/archives/{}", setting.url, item.article.id),
};
ItemBuilder::default()
.title(item.article.title.clone())
.link(url)
.description(item.description)
.content(item.markdown_content)
.pub_date(item.article.publish_at.to_string())
.build()
.unwrap()
})
.collect();
let mut namespaces: HashMap<String, String> = HashMap::new();
namespaces.insert(
"dc".to_string(),
"http://purl.org/dc/elements/1.1/".to_string(),
);
namespaces.insert(
"content".to_string(),
"http://purl.org/rss/1.0/modules/content/".to_string(),
);
namespaces.insert(
"atom".to_string(),
"http://www.w3.org/2005/Atom".to_string(),
);
namespaces.insert(
"media".to_string(),
"http://search.yahoo.com/mrss/".to_string(),
);
let channel: Channel = ChannelBuilder::default()
.title(setting.title)
.description(setting.description)
.generator("Rubble".to_string())
.link(setting.url.clone())
.items(items)
.namespaces(namespaces)
.build()
.unwrap();
HttpResponse::Ok()
.content_type("text/xml; charset=utf-8")
.body(channel.to_string())
}