Skip to content

Commit

Permalink
bbn: Add sitemap_xml mod
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Feb 6, 2023
1 parent 3f02024 commit f988e98
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
50 changes: 48 additions & 2 deletions bbn/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions bbn/crates/bbn/Cargo.toml
Expand Up @@ -25,6 +25,7 @@ structopt = "0.3.21"
thiserror = { workspace = true }
tokio = { workspace = true }
xdg = "2.2.0"
sitemap-xml = { path = "../../../sitemap-xml" }

[dev-dependencies]
assert_cmd = "1.0.4"
Expand Down
1 change: 1 addition & 0 deletions bbn/crates/bbn/src/command.rs
Expand Up @@ -3,6 +3,7 @@ mod date_range;
pub mod hatena_blog;
mod json;
mod list;
mod sitemap_xml;
mod view;

pub use self::config::config;
Expand Down
36 changes: 36 additions & 0 deletions bbn/crates/bbn/src/command/sitemap_xml.rs
@@ -0,0 +1,36 @@
use std::{fs::File, io::BufWriter, path::PathBuf};

use anyhow::Context;
use bbn_repository::{BbnRepository, Query};
use sitemap_xml::writer::SitemapWriter;

use crate::config_repository::ConfigRepository;

pub fn run(out_dir: PathBuf) -> anyhow::Result<()> {
let config_repository = ConfigRepository::new()?;
let config = config_repository
.load()
.context("The configuration file does not found. Use `bbn config` command.")?;
let data_dir = config.data_dir().to_path_buf();

let bbn_repository = BbnRepository::new(data_dir);
let query = Query::try_from("date:1970-01-01/9999-12-31")?;
let entry_ids = bbn_repository.find_ids_by_query(query)?;

let path = out_dir.join("sitemap.xml");
let file = File::create(path)?;
let writer = BufWriter::new(file);
let mut writer = SitemapWriter::start(writer)?;

for entry_id in entry_ids {
let date = entry_id.date();
let yyyy = date.year().to_string();
let mm = date.month().to_string();
let dd = date.day_of_month().to_string();
writer.write(format!("https://blog.bouzuya.net/{yyyy}/{mm}/{dd}/").as_str())?;
}

writer.end()?;

Ok(())
}

0 comments on commit f988e98

Please sign in to comment.