Skip to content

Commit 60a6840

Browse files
committed
fix: time parsing
1 parent c74c2ca commit 60a6840

File tree

8 files changed

+66
-26
lines changed

8 files changed

+66
-26
lines changed

src/api/ing/publish.rs renamed to src/api/ing/create.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use anyhow::Result;
55
use serde_json::json;
66

77
impl Ing {
8-
pub async fn publish(&self, content: &str) -> Result<()> {
8+
pub async fn create(&self, content: &str) -> Result<()> {
99
let client = reqwest::Client::new();
1010

1111
let req = {

src/api/ing/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
pub mod comment;
2-
pub mod publish;
2+
pub mod create;
33

44
use crate::infra::result::IntoResult;
55
use anyhow::bail;
@@ -40,7 +40,8 @@ pub enum IngSendFrom {
4040
Sms = 5,
4141
CellPhone = 6,
4242
Web = 8,
43-
Code = 9,
43+
VsCode = 9,
44+
Cli = 13,
4445
}
4546

4647
impl TryFrom<usize> for IngSendFrom {
@@ -55,7 +56,8 @@ impl TryFrom<usize> for IngSendFrom {
5556
5 => Self::Sms,
5657
6 => Self::CellPhone,
5758
8 => Self::Web,
58-
9 => Self::Code,
59+
9 => Self::VsCode,
60+
13 => Self::Cli,
5961
u => bail!("Unknown value of ing source: {}", u),
6062
}
6163
.into_ok()

src/api/news/get_list.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub struct NewsEntry {
1717
#[serde(rename = "TopicId")]
1818
pub topic_id: usize,
1919
#[serde(rename = "TopicIcon")]
20-
pub topic_icon_url: String,
20+
pub topic_icon_url: Option<String>,
2121
#[serde(rename = "ViewCount")]
2222
pub view_count: usize,
2323
#[serde(rename = "CommentCount")]

src/display/colorful.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::api::news::get_list::NewsEntry;
66
use crate::api::post::get_one::PostEntry;
77
use crate::api::user::info::UserInfo;
88
use crate::infra::iter::IteratorExt;
9+
use crate::infra::time::patch_rfc3339;
910
use anyhow::Result;
1011
use chrono::DateTime;
1112
use colored::Colorize;
@@ -59,9 +60,11 @@ pub fn list_ing(ing_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>, rev: b
5960
.iter()
6061
.dyn_rev(rev)
6162
.for_each(|(ing, comment_list)| {
62-
let create_time = DateTime::parse_from_rfc3339(&format!("{}Z", ing.create_time))
63-
.map(|dt| dt.format("%m-%d %H:%M").to_string())
64-
.unwrap();
63+
let create_time = {
64+
let rfc3339 = patch_rfc3339(&ing.create_time);
65+
let dt = DateTime::parse_from_rfc3339(&rfc3339).unwrap();
66+
dt.format("%m-%d %H:%M").to_string()
67+
};
6568

6669
print!("{}", create_time.dimmed());
6770
if ing.is_lucky {
@@ -141,10 +144,18 @@ pub fn show_post_meta(entry: &Result<PostEntry>) {
141144
println!("Tags {}", tags_text);
142145
}
143146
}
144-
let create_time = DateTime::parse_from_rfc3339(&format!("{}Z", entry.create_time)).unwrap();
145-
println!("Create {}", create_time.format("%Y/%m/%d %H:%M"));
146-
let modify_time = DateTime::parse_from_rfc3339(&format!("{}Z", entry.create_time)).unwrap();
147-
println!("Modify {}", modify_time.format("%Y/%m/%d %H:%M"));
147+
let create_time = {
148+
let rfc3339 = patch_rfc3339(&entry.create_time);
149+
let dt = DateTime::parse_from_rfc3339(&rfc3339).unwrap();
150+
dt.format("%Y-%m-%d %H:%M")
151+
};
152+
println!("Create {}", create_time);
153+
let modify_time = {
154+
let rfc3339 = patch_rfc3339(&entry.modify_time);
155+
let dt = DateTime::parse_from_rfc3339(&rfc3339).unwrap();
156+
dt.format("%Y-%m-%d %H:%M")
157+
};
158+
println!("Modify {}", modify_time);
148159
println!("Link https:{}", entry.url);
149160
}
150161

@@ -208,9 +219,11 @@ pub fn list_news(news_list: &Result<Vec<NewsEntry>>, rev: bool) {
208219
.iter()
209220
.dyn_rev(rev)
210221
.for_each(|news| {
211-
let create_time = DateTime::parse_from_rfc3339(&format!("{}Z", news.create_time))
212-
.map(|dt| dt.format("%m-%d %H:%M").to_string())
213-
.unwrap();
222+
let create_time = {
223+
let rfc3339 = patch_rfc3339(&news.create_time);
224+
let dt = DateTime::parse_from_rfc3339(&rfc3339).unwrap();
225+
dt.format("%Y-%m-%d %H:%M").to_string()
226+
};
214227

215228
let url = format!("https://news.cnblogs.com/n/{}", news.id);
216229
println!("{} {}", create_time.dimmed(), url.dimmed(),);

src/display/normal.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::api::news::get_list::NewsEntry;
66
use crate::api::post::get_one::PostEntry;
77
use crate::api::user::info::UserInfo;
88
use crate::infra::iter::IteratorExt;
9+
use crate::infra::time::patch_rfc3339;
910
use anyhow::Result;
1011
use chrono::DateTime;
1112
use std::fmt::Display;
@@ -58,9 +59,11 @@ pub fn list_ing(ing_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>, rev: b
5859
.iter()
5960
.dyn_rev(rev)
6061
.for_each(|(ing, comment_list)| {
61-
let create_time = DateTime::parse_from_rfc3339(&format!("{}Z", ing.create_time))
62-
.map(|dt| dt.format("%m-%d %H:%M").to_string())
63-
.unwrap();
62+
let create_time = {
63+
let rfc3339 = patch_rfc3339(&ing.create_time);
64+
let dt = DateTime::parse_from_rfc3339(&rfc3339).unwrap();
65+
dt.format("%m-%d %H:%M")
66+
};
6467

6568
print!("{}", create_time);
6669
if ing.is_lucky {
@@ -140,10 +143,18 @@ pub fn show_post_meta(entry: &Result<PostEntry>) {
140143
println!("Tags {}", tags_text);
141144
}
142145
}
143-
let create_time = DateTime::parse_from_rfc3339(&format!("{}Z", entry.create_time)).unwrap();
144-
println!("Create {}", create_time.format("%Y/%m/%d %H:%M"));
145-
let modify_time = DateTime::parse_from_rfc3339(&format!("{}Z", entry.create_time)).unwrap();
146-
println!("Modify {}", modify_time.format("%Y/%m/%d %H:%M"));
146+
let create_time = {
147+
let rfc3339 = patch_rfc3339(&entry.create_time);
148+
let dt = DateTime::parse_from_rfc3339(&rfc3339).unwrap();
149+
dt.format("%Y-%m-%d %H:%M")
150+
};
151+
println!("Create {}", create_time);
152+
let modify_time = {
153+
let rfc3339 = patch_rfc3339(&entry.modify_time);
154+
let dt = DateTime::parse_from_rfc3339(&rfc3339).unwrap();
155+
dt.format("%Y-%m-%d %H:%M")
156+
};
157+
println!("Modify {}", modify_time);
147158
println!("Link https:{}", entry.url);
148159
}
149160

@@ -206,9 +217,11 @@ pub fn list_news(news_list: &Result<Vec<NewsEntry>>, rev: bool) {
206217
.iter()
207218
.dyn_rev(rev)
208219
.for_each(|news| {
209-
let create_time = DateTime::parse_from_rfc3339(&format!("{}Z", news.create_time))
210-
.map(|dt| dt.format("%m-%d %H:%M").to_string())
211-
.unwrap();
220+
let create_time = {
221+
let rfc3339 = patch_rfc3339(&news.create_time);
222+
let dt = DateTime::parse_from_rfc3339(&rfc3339).unwrap();
223+
dt.format("%Y-%m-%d %H:%M")
224+
};
212225

213226
let url = format!("https://news.cnblogs.com/n/{}", news.id);
214227
println!("{} {}", create_time, url);

src/infra/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ pub mod iter;
44
pub mod json;
55
pub mod option;
66
pub mod result;
7+
pub mod time;

src/infra/time.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// HACK:
2+
// Sometimes cnblogs' web API returns time string like: "2023-09-12T14:07:00" or "2019-02-06T08:45:53.94"
3+
// This will patch it to standard RFC3339 format
4+
pub fn patch_rfc3339(time_str: &str) -> String {
5+
if time_str.len() != 25 {
6+
let u8vec: Vec<_> = time_str.bytes().into_iter().take(19).collect();
7+
format!("{}+08:00", String::from_utf8(u8vec).unwrap())
8+
} else {
9+
time_str.to_owned()
10+
}
11+
}

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ async fn main() -> Result<()> {
7878
}
7979
_ if let Some(content) = parser::publish_ing(&args) => {
8080
let content = try {
81-
Ing::new(pat?).publish(content).await?;
81+
Ing::new(pat?).create(content).await?;
8282
content
8383
};
8484
foe.then(||panic_if_err(&content));

0 commit comments

Comments
 (0)