Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
puripuri2100 committed Apr 3, 2024
1 parent fe40ee1 commit 9574360
Showing 1 changed file with 27 additions and 49 deletions.
76 changes: 27 additions & 49 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
//! オプションの各意味は以下のとおりです。
//!
//! - `--output`:解析で生成した情報を出力するフォルダ
//! - `--start`:日時範囲の始端(オプション)
//! - `--end`:日時範囲の終端(オプション)
//! - `--start`:年範囲の始端(オプション)
//! - `--end`:年範囲の終端(オプション)
//! - `--rows`:一度の処理の重さ(オプション)
//! - `--sleep-time`:一度の処理ごとに挟まるスリープ時間(オプション)
//!
Expand All @@ -31,9 +31,8 @@
//! (c) 2024 Naoki Kaneko (a.k.a. "puripuri2100")
//!
use anyhow::{anyhow, Context, Result};
use anyhow::Result;
use clap::Parser;
use regex::Regex;
use serde::{Deserialize, Serialize};
use tokio::fs::File;
use tokio::io::AsyncWriteExt;
Expand All @@ -44,10 +43,10 @@ use tracing::*;
struct AppArgs {
/// 検索する範囲の始端を<year>-<month>-<date>形式で与える
#[clap(short, long)]
start: Option<String>,
start: Option<usize>,
/// 検索する範囲の終端を<year>-<month>-<date>形式で与える
#[clap(short, long)]
end: Option<String>,
end: Option<usize>,
/// 出力先のフォルダ
#[clap(short, long)]
output: String,
Expand All @@ -59,25 +58,20 @@ struct AppArgs {
sleep_time: u64,
}

fn gen_list_url(
start: Option<(usize, usize, usize)>,
end: Option<(usize, usize, usize)>,
n: usize,
rows: usize,
) -> String {
let start_s = if let Some((y, m, d)) = start {
format!("{y}-{m}-{d}")
fn gen_list_url(start: Option<usize>, end: Option<usize>, n: usize, rows: usize) -> String {
let start_s = if let Some(y) = start {
format!("{y:0>4}")
} else {
String::from("1883-01-01")
String::from("*")
};
let end_s = if let Some((y, m, d)) = end {
format!("{y:0>4}-{m:0>2}-{d:0>2}")
let end_s = if let Some(y) = end {
format!("{y:0>4}")
} else {
String::from("NOW")
String::from("*")
};
let start_n = rows * n;
format!(
r"https://jorei.slis.doshisha.ac.jp/api/reiki/select?f.municipality_id.facet.limit=1788&facet.mincount=1&facet.range=announcement_date&facet.range.gap=%2B1YEAR&facet.range.start={start_s}T00%3A00%3A00Z&facet.range.end={end_s}&hl=true&hl.fl=content&hl.usePhraseHighlighter=true&q=collection%3Alatest&start={start_n}&rows={rows}&fq=&facet=true&facet.field=municipality_type&facet.field=city&facet.field=type&facet.field=h_type&facet.field=municipality_id"
r"https://jorei.slis.doshisha.ac.jp/api/reiki/select?f.municipality_id.facet.limit=1788&facet.mincount=1&facet.range=announcement_date&facet.range.gap=%2B1YEAR&facet.range.start=1883-01-01T00%3A00%3A00Z&facet.range.end=NOW&q=collection%3Alatest%20AND%20announcement_date%3A%5B{start_s}%20TO%20{end_s}%5D&start={start_n}&rows={rows}&fq=&facet=true&facet.field=municipality_type&facet.field=city&facet.field=type&facet.field=h_type&facet.field=municipality_id"
)
}

Expand All @@ -91,10 +85,10 @@ struct JoreiInfoListResponseDocs {
collected_date: Vec<String>,
updated_date: Vec<String>,
municipality_id: String,
prefecture: String,
city: String,
prefecture_kana: String,
city_kana: String,
prefecture: Option<String>,
city: Option<String>,
prefecture_kana: Option<String>,
city_kana: Option<String>,
municipality_type: String,
area: String,
id: String,
Expand Down Expand Up @@ -129,10 +123,14 @@ struct JoreiInfoDocs {
collected_date: Vec<String>,
updated_date: Vec<String>,
municipality_id: String,
prefecture: String,
city: String,
prefecture_kana: String,
city_kana: String,
#[serde(skip_serializing_if = "Option::is_none")]
prefecture: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
city: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
prefecture_kana: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
city_kana: Option<String>,
municipality_type: String,
area: String,
id: String,
Expand Down Expand Up @@ -201,32 +199,12 @@ async fn main() -> Result<()> {

init_logger().await?;

let re_day = Regex::new(r"(?<y>\d{4})-(?<m>\d{2})-(?<d>\d{2})").unwrap();
let start = if let Some(s) = &args.start {
let caps = re_day.captures(s).with_context(|| anyhow!("err"))?;
let y = &caps["y"].parse::<usize>()?;
let m = &caps["m"].parse::<usize>()?;
let d = &caps["d"].parse::<usize>()?;
Some((*y, *m, *d))
} else {
None
};
let end = if let Some(s) = &args.end {
let caps = re_day.captures(s).with_context(|| anyhow!("err"))?;
let y = &caps["y"].parse::<usize>()?;
let m = &caps["m"].parse::<usize>()?;
let d = &caps["d"].parse::<usize>()?;
Some((*y, *m, *d))
} else {
None
};

// jorei.slis.doshisa.ac.jpの証明書が壊れているので検証しない設定にする
let client = reqwest::Client::builder()
.danger_accept_invalid_certs(true)
.build()?;

let first_api_url = gen_list_url(start, end, 0, args.rows);
let first_api_url = gen_list_url(args.start, args.end, 0, args.rows);

let first_resp: JoreiListResponse = client.get(&first_api_url).send().await?.json().await?;
let first_resp = first_resp.response;
Expand All @@ -237,7 +215,7 @@ async fn main() -> Result<()> {

let mut stream = tokio_stream::iter(0..=(all_size / args.rows));
while let Some(n) = stream.next().await {
let list_api_url = gen_list_url(start, end, n, args.rows);
let list_api_url = gen_list_url(args.start, args.end, n, args.rows);

let list_resp: JoreiListResponse = client.get(&list_api_url).send().await?.json().await?;
let id_lst = list_resp.response.docs.iter().map(|d| &d.id);
Expand Down

0 comments on commit 9574360

Please sign in to comment.