We do a little bit of refactoring
This commit is contained in:
parent
fe16cde78f
commit
8e4392cb51
3 changed files with 41 additions and 24 deletions
7
Cargo.lock
generated
7
Cargo.lock
generated
|
@ -70,6 +70,12 @@ dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anyhow"
|
||||||
|
version = "1.0.68"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2cb2f989d18dd141ab8ae82f64d1a8cdd37e0840f73a406896cf5e99502fab61"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "async-stream"
|
name = "async-stream"
|
||||||
version = "0.3.3"
|
version = "0.3.3"
|
||||||
|
@ -1497,6 +1503,7 @@ dependencies = [
|
||||||
name = "rooster"
|
name = "rooster"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"anyhow",
|
||||||
"figment",
|
"figment",
|
||||||
"icalendar",
|
"icalendar",
|
||||||
"log",
|
"log",
|
||||||
|
|
|
@ -12,7 +12,7 @@ sentry = "0.29.1"
|
||||||
log = "0.4.17"
|
log = "0.4.17"
|
||||||
serde = "1.0.152"
|
serde = "1.0.152"
|
||||||
figment = "0.10.8"
|
figment = "0.10.8"
|
||||||
#anyhow = "1.0.68"
|
anyhow = "1.0.68"
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
lto = "thin"
|
lto = "thin"
|
||||||
|
|
56
src/main.rs
56
src/main.rs
|
@ -43,19 +43,37 @@ enum Source {
|
||||||
Delft,
|
Delft,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Source {
|
||||||
|
fn from_url(url: &str) -> Option<Self> {
|
||||||
|
if url.starts_with("https://rooster.utwente.nl/ical") {
|
||||||
|
Some(Source::Twente)
|
||||||
|
} else if url.starts_with("https://mytimetable.tudelft.nl/ical") {
|
||||||
|
Some(Source::Delft)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_calendar(url: &str) -> anyhow::Result<Calendar> {
|
||||||
|
let body = reqwest::get(url).await?.text().await?;
|
||||||
|
let calendar = body.parse::<Calendar>().map_err(anyhow::Error::msg)?;
|
||||||
|
Ok(calendar)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_event_kind(description: &str) -> &str {
|
||||||
|
let first_line = description.split_once("\\n").unwrap().0;
|
||||||
|
first_line.split_once(": ").unwrap().1
|
||||||
|
}
|
||||||
|
|
||||||
#[get("/ical?<url>")]
|
#[get("/ical?<url>")]
|
||||||
async fn ics(url: &str) -> String {
|
async fn ics(url: &str) -> String {
|
||||||
let source = if url.starts_with("https://rooster.utwente.nl/ical") {
|
let source = match Source::from_url(url) {
|
||||||
Source::Twente
|
Some(source) => source,
|
||||||
} else if url.starts_with("https://mytimetable.tudelft.nl/ical") {
|
None => return "Must be a rooster.utwente.nl or mytimetable.tudelft.nl URL".to_string()
|
||||||
Source::Delft
|
|
||||||
} else {
|
|
||||||
return "Must be a rooster.utwente.nl or mytimetable.tudelft.nl URL".to_string();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let body = reqwest::get(url).await.unwrap().text().await.unwrap();
|
let calendar = get_calendar(url).await.unwrap();
|
||||||
|
|
||||||
let calendar = body.parse::<Calendar>().unwrap();
|
|
||||||
let mut new_calendar = Calendar::new();
|
let mut new_calendar = Calendar::new();
|
||||||
|
|
||||||
for component in calendar.iter() {
|
for component in calendar.iter() {
|
||||||
|
@ -66,23 +84,14 @@ async fn ics(url: &str) -> String {
|
||||||
|
|
||||||
let summary = event.get_summary().unwrap();
|
let summary = event.get_summary().unwrap();
|
||||||
let subject = match source {
|
let subject = match source {
|
||||||
Source::Twente => {
|
Source::Twente => summary.rsplit_once(' ').unwrap().0,
|
||||||
let (subject, _) = summary.rsplit_once(' ').unwrap();
|
Source::Delft => summary.split_once(" - ").unwrap().1,
|
||||||
subject
|
|
||||||
}
|
|
||||||
|
|
||||||
Source::Delft => {
|
|
||||||
let (_, subject) = summary.split_once(" - ").unwrap();
|
|
||||||
subject
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let description = event.get_description().unwrap();
|
|
||||||
let (kind, _) = description.split_once("\\n").unwrap();
|
|
||||||
let (_, kind) = kind.split_once(": ").unwrap();
|
|
||||||
|
|
||||||
let start = event.get_start().unwrap();
|
let start = event.get_start().unwrap();
|
||||||
let end = event.get_end().unwrap();
|
let end = event.get_end().unwrap();
|
||||||
|
let description = event.get_description().unwrap();
|
||||||
|
let kind = get_event_kind(&description);
|
||||||
|
|
||||||
let mut hasher = DefaultHasher::new();
|
let mut hasher = DefaultHasher::new();
|
||||||
url.hash(&mut hasher);
|
url.hash(&mut hasher);
|
||||||
|
@ -90,13 +99,14 @@ async fn ics(url: &str) -> String {
|
||||||
format!("{end:?}").hash(&mut hasher);
|
format!("{end:?}").hash(&mut hasher);
|
||||||
summary.hash(&mut hasher);
|
summary.hash(&mut hasher);
|
||||||
description.hash(&mut hasher);
|
description.hash(&mut hasher);
|
||||||
let mut new_event = Event::new();
|
|
||||||
|
|
||||||
|
let mut new_event = Event::new();
|
||||||
new_event
|
new_event
|
||||||
.summary(&format!("{kind} - {subject}"))
|
.summary(&format!("{kind} - {subject}"))
|
||||||
.description(description)
|
.description(description)
|
||||||
.starts(start)
|
.starts(start)
|
||||||
.ends(end);
|
.ends(end);
|
||||||
|
|
||||||
if let Some(location) = event.get_location() {
|
if let Some(location) = event.get_location() {
|
||||||
new_event.location(location);
|
new_event.location(location);
|
||||||
location.hash(&mut hasher);
|
location.hash(&mut hasher);
|
||||||
|
|
Loading…
Reference in a new issue