mirror of
https://github.com/jhbruhn/ics-adapter.git
synced 2025-03-15 03:25:49 +00:00
Allow multiple urls which are split with a semicolon
This commit is contained in:
parent
868d27a113
commit
35349b9d04
1 changed files with 53 additions and 50 deletions
11
src/main.rs
11
src/main.rs
|
@ -10,13 +10,14 @@ use serde::Serialize;
|
||||||
|
|
||||||
async fn handler(params: HashMap<String, String>) -> Result<impl Reply, Rejection> {
|
async fn handler(params: HashMap<String, String>) -> Result<impl Reply, Rejection> {
|
||||||
match params.get("url") {
|
match params.get("url") {
|
||||||
Some(url) => Ok(warp::reply::json(&convert(&url, params.get("days")).await.map_err(|_| warp::reject::reject())?.entries)),
|
Some(url) => Ok(warp::reply::json(&convert(&[url], params.get("days")).await.map_err(|_| warp::reject::reject())?.entries)),
|
||||||
None => Err(warp::reject::reject())
|
None => Err(warp::reject::reject())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn new_handler(url: String, params: HashMap<String, String>) -> Result<impl Reply, Rejection> {
|
async fn new_handler(url: String, params: HashMap<String, String>) -> Result<impl Reply, Rejection> {
|
||||||
Ok(warp::reply::json(&convert(&url, params.get("days")).await.map_err(|_| warp::reject::reject())?.entries))
|
let urls: Vec<&str> = url.split(";").collect();
|
||||||
|
Ok(warp::reply::json(&convert(&urls, params.get("days")).await.map_err(|_| warp::reject::reject())?.entries))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
|
@ -34,7 +35,9 @@ struct CustomCalendarEntry {
|
||||||
isallday: bool,
|
isallday: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn convert(url: &str, days: Option<&String>) -> Result<CustomCalendar> {
|
async fn convert(urls: &[&str], days: Option<&String>) -> Result<CustomCalendar> {
|
||||||
|
let mut entries = Vec::new();
|
||||||
|
for url in urls {
|
||||||
let url = urlencoding::decode(url)?.into_owned();
|
let url = urlencoding::decode(url)?.into_owned();
|
||||||
let ics_text = reqwest::get(url)
|
let ics_text = reqwest::get(url)
|
||||||
.await?
|
.await?
|
||||||
|
@ -43,7 +46,6 @@ async fn convert(url: &str, days: Option<&String>) -> Result<CustomCalendar> {
|
||||||
|
|
||||||
let calendar = ics_text.parse::<Calendar>().map_err(|e| anyhow::Error::msg(e))?;
|
let calendar = ics_text.parse::<Calendar>().map_err(|e| anyhow::Error::msg(e))?;
|
||||||
|
|
||||||
let mut entries = Vec::new();
|
|
||||||
|
|
||||||
let filter_start = chrono::Utc::now().date_naive().and_hms_opt(0, 0, 0).unwrap().and_utc();
|
let filter_start = chrono::Utc::now().date_naive().and_hms_opt(0, 0, 0).unwrap().and_utc();
|
||||||
let filter_end = filter_start + chrono::Duration::days(days.unwrap_or(&String::from("1")).parse().unwrap_or(1));
|
let filter_end = filter_start + chrono::Duration::days(days.unwrap_or(&String::from("1")).parse().unwrap_or(1));
|
||||||
|
@ -91,6 +93,7 @@ async fn convert(url: &str, days: Option<&String>) -> Result<CustomCalendar> {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Ok(CustomCalendar{entries})
|
Ok(CustomCalendar{entries})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue