|
|
|
@ -12,11 +12,34 @@ use diesel::{Connection, pg::PgConnection, prelude::*};
|
|
|
|
|
mod config; |
|
|
|
|
use config::{Config, CalendarOptions}; |
|
|
|
|
mod schema; |
|
|
|
|
use schema::{calendars::dsl::calendars, events}; |
|
|
|
|
use schema::{calendars::dsl::calendars}; |
|
|
|
|
mod model; |
|
|
|
|
use model::{Calendar, NewCalendar}; |
|
|
|
|
use model::{Calendar, NewCalendar, Event}; |
|
|
|
|
mod ics; |
|
|
|
|
use ics::Parser; |
|
|
|
|
use ics::{Parser, Object, GetValue}; |
|
|
|
|
|
|
|
|
|
fn obj_to_event(calendar: String, obj: Object) -> Option<Event> { |
|
|
|
|
if obj.name != "VEVENT" { |
|
|
|
|
return None; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let dtstart = obj.get("DTSTART")?; |
|
|
|
|
let summary = obj.get("SUMMARY")?; |
|
|
|
|
let id = format!("{}{}{}{}", |
|
|
|
|
obj.get("UID").unwrap_or(""), |
|
|
|
|
dtstart, |
|
|
|
|
obj.get("DTSTAMP").unwrap_or(""), |
|
|
|
|
obj.get("RECURRENCE-ID").unwrap_or("")); |
|
|
|
|
// TODO: DESCRIPTION
|
|
|
|
|
Some(Event { |
|
|
|
|
calendar, id, |
|
|
|
|
dtstart, |
|
|
|
|
dtend: obj.get("DTEND"), |
|
|
|
|
summary, |
|
|
|
|
location: obj.get("LOCATION"), |
|
|
|
|
url: obj.get("URL"), |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub struct Resources { |
|
|
|
|
db_url: String, |
|
|
|
@ -84,9 +107,9 @@ impl Resources {
|
|
|
|
|
} |
|
|
|
|
let mut res = req.send()?; |
|
|
|
|
|
|
|
|
|
println!("{} {}", res.status(), cal_opts.url); |
|
|
|
|
if res.status() != 200 { |
|
|
|
|
let msg = format!("HTTP {}", res.status()); |
|
|
|
|
println!("{} {}", res.status(), cal_opts.url); |
|
|
|
|
diesel::update(calendars) |
|
|
|
|
.filter(schema::calendars::dsl::id.eq(id.clone())) |
|
|
|
|
.set((schema::calendars::dsl::last_success.eq(Utc::now().naive_utc()), |
|
|
|
@ -112,20 +135,32 @@ impl Resources {
|
|
|
|
|
|
|
|
|
|
let mut p = Parser::new(); |
|
|
|
|
let mut buf = [0; 1024]; |
|
|
|
|
let mut events = vec![]; |
|
|
|
|
loop { |
|
|
|
|
match res.read(&mut buf)? { |
|
|
|
|
len if len > 0 => { |
|
|
|
|
let data = &buf[..len]; |
|
|
|
|
p.feed(data, |obj| { |
|
|
|
|
println!("- [{}] {}", obj.get("DTSTART").unwrap_or("?"), obj.get("SUMMARY").unwrap_or("?")); |
|
|
|
|
print!(" {}", obj.get("LOCATION").unwrap_or("?")); |
|
|
|
|
obj.get("URL").map(|url| print!(" <{}>", url)); |
|
|
|
|
println!(""); |
|
|
|
|
if let Some(event) = obj_to_event(cal_opts.url.clone(), obj) { |
|
|
|
|
events.push(event); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
_ => break, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
drop(res); |
|
|
|
|
|
|
|
|
|
println!("{} events {}", events.len(), cal_opts.url); |
|
|
|
|
diesel::delete(schema::events::dsl::events) |
|
|
|
|
.filter(schema::events::dsl::calendar.eq(cal_opts.url)) |
|
|
|
|
.execute(&db)?; |
|
|
|
|
for event in events { |
|
|
|
|
// println!("insert {:?}", event);
|
|
|
|
|
diesel::insert_into(schema::events::dsl::events) |
|
|
|
|
.values(&event) |
|
|
|
|
.execute(&db)?; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Ok(()) |
|
|
|
|
})?; |
|
|
|
|