adapt to new layout of data source

This commit is contained in:
Max Hohlfeld 2023-04-25 14:41:06 +02:00
parent 6e3e3e6321
commit ecd188ecfe
2 changed files with 48 additions and 8 deletions

View File

@ -10,7 +10,7 @@ pub fn parse_inner_node(inner_node: &str) -> String {
}
if let Some(end) = end {
let new_end = end - 1;
let new_end = end.saturating_sub(1);
if new_end >= start_index {
end_index = new_end

View File

@ -8,6 +8,18 @@ pub struct RemainingPlace {
pub free: usize,
}
impl Ord for RemainingPlace {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.id.cmp(&other.id)
}
}
impl PartialOrd for RemainingPlace {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.id.partial_cmp(&other.id)
}
}
pub fn get_current_places(url: &str) -> Result<Vec<RemainingPlace>, reqwest::Error> {
let body = reqwest::blocking::get(url)?.text()?;
@ -35,19 +47,47 @@ pub fn get_current_places(url: &str) -> Result<Vec<RemainingPlace>, reqwest::Err
}
}
lines.chunks(4).for_each(|chunk| {
lines.chunks(6).for_each(|chunk| {
let free = try_parse_free_slot(&chunk[4]);
let (id, description) = if let Some((id, description)) = chunk[0].split_once(' ') {
(id, description)
} else {
return;
};
if free.is_none() {
return;
}
let new_remaining_place = RemainingPlace {
id: chunk[0].clone(),
description: chunk[1].clone(),
id: id.to_string(),
description: description.to_string(),
date: chunk[2].clone(),
free: match chunk[3].parse() {
Ok(value) => value,
Err(_) => 0,
},
free: free.unwrap()
};
places.push(new_remaining_place);
});
places.sort();
Ok(places)
}
fn try_parse_free_slot(content: &str) -> Option<usize> {
if let Ok(slots) = content.parse::<usize>() {
return Some(slots)
}
if let Some((left, right)) = content.split_once(' ') {
if let Ok(slots) = left.parse::<usize>() {
return Some(slots)
}
if let Ok(slots) = right.parse::<usize>() {
return Some(slots)
}
}
None
}