Skip to content

Commit ce492eb

Browse files
committed
Prepend date MM-DD-YYYY to filename
1 parent f222c76 commit ce492eb

File tree

4 files changed

+17
-6
lines changed

4 files changed

+17
-6
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ edition = "2021"
99
reqwest = { version = "0.11", features = ["json"] }
1010
tokio = { version = "1", features = ["full"] }
1111
rss = "2.0"
12-
html2md = "0.2.13"
12+
html2md = "0.2.13"
13+
chrono = "0.4"

src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
1010
let output_dir = std::env::args().nth(2).expect("no dir provided");
1111

1212
let mut op = parser::Parser::new(url, output_dir);
13-
op.fetch_and_parse().await?;
13+
op.fetch_and_parse()
14+
.await
15+
.expect("Could not fetch & parse posts");
1416
op.save_files().expect("Could not save files");
1517

1618
Ok(())

src/parser.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
use chrono::{DateTime, FixedOffset, Utc};
12
use html2md::parse_html;
23
use rss::Channel;
34
use std::error::Error;
45
use std::fs;
56
use std::fs::File;
67
use std::io::Write;
78
use std::path::Path;
8-
// use std::path::Path;
99

1010
pub struct Parser {
1111
pub output_dir: String,
@@ -111,15 +111,19 @@ pub struct Post {
111111
title: String,
112112
md: String,
113113
html: String,
114-
pubdate: String,
114+
pubdate: DateTime<FixedOffset>,
115115
}
116116

117117
impl Post {
118118
fn new(html: &str, md: &str, url: &str, title: &str, pubdate: &str) -> Self {
119+
let pubdate = match DateTime::parse_from_rfc2822(pubdate) {
120+
Ok(d) => d,
121+
Err(err) => panic!("{}", err),
122+
};
119123
let post = Post {
124+
pubdate,
120125
url: String::from(url),
121126
title: String::from(title),
122-
pubdate: String::from(pubdate),
123127
md: String::from(md),
124128
html: String::from(html),
125129
};
@@ -137,6 +141,7 @@ impl Post {
137141

138142
fn filename(&self) -> String {
139143
let slug = self.slug().expect("unable to get slug for filename");
140-
format!("{}.md", slug)
144+
let date = self.pubdate.format("%m-%d-%Y");
145+
format!("{}-{}.md", date, slug)
141146
}
142147
}

0 commit comments

Comments
 (0)