diff --git a/Cargo.lock b/Cargo.lock index e33d460..97421b5 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -263,6 +263,12 @@ dependencies = [ "libc", ] +[[package]] +name = "anyhow" +version = "1.0.75" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" + [[package]] name = "atom_syndication" version = "0.12.2" @@ -575,6 +581,18 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "dateparser" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18e537db44ee4822af930a9a2c9eedfcde9d47e01c6731fbf34ea01b5a7ccf7d" +dependencies = [ + "anyhow", + "chrono", + "lazy_static", + "regex", +] + [[package]] name = "deranged" version = "0.3.9" @@ -1818,6 +1836,7 @@ dependencies = [ "actix-web", "bcrypt", "chrono", + "dateparser", "diesel", "dotenv", "env_logger", diff --git a/Cargo.toml b/Cargo.toml index 0454a4c..1f05dd6 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ env_logger = "0.9.3" scraper = "0.14.0" actix-cors = "0.6.4" chrono = { version = "0.4.31", features = ["serde"] } +dateparser = "0.2.0" [dependencies.serde_json] version = "1.0.86" diff --git a/src/main.rs b/src/main.rs index 1ee7fe9..c174a26 100755 --- a/src/main.rs +++ b/src/main.rs @@ -53,7 +53,7 @@ async fn main() -> std::io::Result<()> { } }) .configure(views::views_factory); - return app; + app }) .bind("127.0.0.1:8001")? .run() diff --git a/src/reader/sync.rs b/src/reader/sync.rs index 033ba08..b287a5e 100644 --- a/src/reader/sync.rs +++ b/src/reader/sync.rs @@ -12,11 +12,34 @@ use crate::{ }, }; use actix_web::{web, HttpRequest, HttpResponse, Responder}; -use chrono::{Local, NaiveDateTime}; +use chrono::{DateTime, Local, NaiveDateTime}; +use dateparser::parse; use diesel::prelude::*; use rss::Item; use scraper::{Html, Selector}; +fn get_date(date_str: &str) -> Result { + // let format_string = "%a, %d %b %Y %H:%M:%S %z"; + let format_string = "%Y-%m-%dT%H:%M:%S%Z"; + + let result = parse(date_str).unwrap(); + log::info!("Date: {:?}", result); + + match NaiveDateTime::parse_from_str(&result.to_string(), format_string) { + Ok(r) => Ok(r), + Err(_) => { + let datetime = DateTime::parse_from_rfc2822(date_str); + match datetime { + Ok(r) => NaiveDateTime::parse_from_str(&r.to_rfc3339(), format_string), + Err(_) => match DateTime::parse_from_rfc2822(date_str) { + Ok(r) => NaiveDateTime::parse_from_str(&r.to_rfc3339(), format_string), + Err(e) => Err(e), + }, + } + } + } +} + fn create_feed_item(item: Item, feed: &Feed, connection: &mut PgConnection) { let item_title = item.title.clone().unwrap(); log::info!("Create feed item: {}", item_title); @@ -56,8 +79,7 @@ fn create_feed_item(item: Item, feed: &Feed, connection: &mut PgConnection) { log::info!("{:?}", item.pub_date()); let mut time: NaiveDateTime = Local::now().naive_local(); if item.pub_date().is_some() { - let format_string = "%a, %d %b %Y %H:%M:%S %z"; - time = match NaiveDateTime::parse_from_str(item.pub_date().unwrap(), format_string) { + time = match get_date(item.pub_date().unwrap()) { Ok(date) => date, Err(err) => { log::error!("could not unwrap pub date: {}", err); @@ -78,7 +100,7 @@ fn create_feed_item(item: Item, feed: &Feed, connection: &mut PgConnection) { log::info!("Insert Result: {:?}", insert_result); } else { - log::info!("Item {} already exists.", feed.title); + log::info!("Item {} already exists.", item_title); } } diff --git a/src/views/auth/logout.rs b/src/views/auth/logout.rs index 0c8cceb..1986b17 100755 --- a/src/views/auth/logout.rs +++ b/src/views/auth/logout.rs @@ -1,3 +1,3 @@ pub async fn logout() -> String { - format!("logout view") + "logout view".to_string() } diff --git a/src/views/path.rs b/src/views/path.rs index ff10055..8d262fb 100755 --- a/src/views/path.rs +++ b/src/views/path.rs @@ -8,10 +8,9 @@ impl Path { match self.backend { true => { let path: String = self.prefix.to_owned() + &following_path; - return String::from("/api/v1") + &path; + String::from("/api/v1") + &path } false => self.prefix.to_owned() + &following_path, } } } -