proper datetime
This commit is contained in:
26
src/lib.rs
26
src/lib.rs
@@ -1,4 +1,4 @@
|
||||
use chrono::NaiveDate;
|
||||
use chrono::DateTime;
|
||||
use serde_json::Value;
|
||||
|
||||
pub struct Feed {
|
||||
@@ -50,13 +50,17 @@ impl Feed {
|
||||
ret.push_str(&format!(
|
||||
"<title>{} ({})</title>",
|
||||
episode.title,
|
||||
episode.date.format("%d. %m.")
|
||||
episode.timestamp.format("%d.%m.")
|
||||
));
|
||||
ret.push_str(&format!(
|
||||
"<enclosure url=\"{}\" length=\"0\" type=\"audio/mpeg\"/>\n",
|
||||
quick_xml::escape::escape(&episode.media_url)
|
||||
));
|
||||
ret.push_str("<description>Journal</description>");
|
||||
ret.push_str(&format!(
|
||||
"<pubDate>{}</pubDate>",
|
||||
episode.timestamp.to_rfc2822()
|
||||
));
|
||||
ret.push_str("</item>");
|
||||
}
|
||||
|
||||
@@ -98,7 +102,7 @@ pub struct Broadcast {
|
||||
pub url: String,
|
||||
pub media_url: String,
|
||||
pub title: String,
|
||||
pub date: NaiveDate,
|
||||
pub timestamp: DateTime<chrono::Utc>,
|
||||
}
|
||||
|
||||
impl Broadcast {
|
||||
@@ -121,20 +125,22 @@ impl Broadcast {
|
||||
return Err(format!("{url} has no title").into());
|
||||
};
|
||||
|
||||
let Some(date) = data["broadcastDay"].as_number() else {
|
||||
return Err(format!("{url} has no broadcastDay").into());
|
||||
let Some(timestamp) = data["start"].as_number() else {
|
||||
return Err(format!("{url} has no start").into());
|
||||
};
|
||||
let Ok(date) = NaiveDate::parse_from_str(&date.to_string(), "%Y%m%d") else {
|
||||
return Err(
|
||||
format!("broadcastDay in {url} not in a valid format (%Y%m%d): {date}").into(),
|
||||
);
|
||||
let Some(timestamp) = DateTime::from_timestamp(timestamp.as_i64().unwrap() / 1000, 0)
|
||||
else {
|
||||
return Err(format!(
|
||||
"broadcastDay in {url} not in a valid format (unix timestamp): {timestamp}"
|
||||
)
|
||||
.into());
|
||||
};
|
||||
|
||||
Ok(Some(Self {
|
||||
url,
|
||||
media_url,
|
||||
title: title.into(),
|
||||
date,
|
||||
timestamp,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user