This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
||||
/target
|
||||
muell.ics
|
||||
|
40
src/main.rs
40
src/main.rs
@@ -1,7 +1,7 @@
|
||||
use regex::Regex;
|
||||
use chrono::{NaiveDate, Utc};
|
||||
use uuid::Uuid;
|
||||
use regex::Regex;
|
||||
use std::process;
|
||||
use uuid::Uuid;
|
||||
|
||||
fn parse_german_date(date_str: &str) -> Option<NaiveDate> {
|
||||
NaiveDate::parse_from_str(date_str, "%d.%m.%Y").ok()
|
||||
@@ -11,7 +11,7 @@ fn create_ics_event(date: NaiveDate, title: &str, event_type: &str) -> String {
|
||||
let uid = Uuid::new_v4();
|
||||
let date_str = date.format("%Y%m%d");
|
||||
let timestamp = Utc::now().format("%Y%m%dT%H%M%SZ");
|
||||
|
||||
|
||||
format!(
|
||||
"BEGIN:VEVENT\n\
|
||||
DTSTART;VALUE=DATE:{date_str}\n\
|
||||
@@ -32,20 +32,20 @@ fn create_ics_event(date: NaiveDate, title: &str, event_type: &str) -> String {
|
||||
|
||||
fn fetch_waste_dates() -> Result<String, Box<dyn std::error::Error>> {
|
||||
let client = reqwest::blocking::Client::new();
|
||||
|
||||
|
||||
let response = client
|
||||
.get("https://www.luftenberg.at/system/web/kalender.aspx?sprache=1&menuonr=226723085&typids=227710683,227710684")
|
||||
.header("Cookie", "riscms_muellkalender=11443990_985180")
|
||||
.send()?;
|
||||
|
||||
|
||||
Ok(response.text()?)
|
||||
}
|
||||
|
||||
fn html_to_ics(html_content: &str) -> Result<String, &'static str> {
|
||||
let pattern = Regex::new(
|
||||
r#"td_kal.*?(\d{2}\.\d{2}\.\d{4}).*?<a[^>]*>([^<]+)</a>.*?<span>([^<]+)</span>"#
|
||||
).unwrap();
|
||||
|
||||
let pattern =
|
||||
Regex::new(r"td_kal.*?(\d{2}\.\d{2}\.\d{4}).*?<a[^>]*>([^<]+)</a>.*?<span>([^<]+)</span>")
|
||||
.unwrap();
|
||||
|
||||
let mut ics_content = String::from(
|
||||
"BEGIN:VCALENDAR\n\
|
||||
VERSION:2.0\n\
|
||||
@@ -54,16 +54,16 @@ fn html_to_ics(html_content: &str) -> Result<String, &'static str> {
|
||||
METHOD:PUBLISH\n\
|
||||
X-WR-CALNAME:Abfalltermine Luftenberg\n\
|
||||
X-WR-CALDESC:Waste collection dates for Luftenberg an der Donau\n\
|
||||
X-WR-TIMEZONE:Europe/Vienna\n"
|
||||
X-WR-TIMEZONE:Europe/Vienna\n",
|
||||
);
|
||||
|
||||
|
||||
let mut event_count = 0;
|
||||
|
||||
|
||||
for cap in pattern.captures_iter(html_content) {
|
||||
let date_str = &cap[1];
|
||||
let title = &cap[2];
|
||||
let event_type = &cap[3];
|
||||
|
||||
|
||||
if let Some(date) = parse_german_date(date_str) {
|
||||
let event = create_ics_event(date, title, event_type);
|
||||
ics_content.push_str(&event);
|
||||
@@ -71,11 +71,11 @@ fn html_to_ics(html_content: &str) -> Result<String, &'static str> {
|
||||
event_count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if event_count == 0 {
|
||||
return Err("No waste collection events found");
|
||||
}
|
||||
|
||||
|
||||
ics_content.push_str("END:VCALENDAR");
|
||||
Ok(ics_content)
|
||||
}
|
||||
@@ -84,18 +84,18 @@ fn main() {
|
||||
let html_content = match fetch_waste_dates() {
|
||||
Ok(content) => content,
|
||||
Err(e) => {
|
||||
eprintln!("Error fetching waste dates: {}", e);
|
||||
eprintln!("Error fetching waste dates: {e}");
|
||||
process::exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
let ics_output = match html_to_ics(&html_content) {
|
||||
Ok(content) => content,
|
||||
Err(e) => {
|
||||
eprintln!("Error: {}", e);
|
||||
eprintln!("Error: {e}");
|
||||
process::exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
println!("{}", ics_output);
|
||||
|
||||
println!("{ics_output}");
|
||||
}
|
||||
|
Reference in New Issue
Block a user