use crate::html_parser::{parse_inner_node, replace_html_codes}; #[derive(Clone, PartialEq, Eq)] pub struct RemainingPlace { pub id: String, pub description: String, pub date: String, 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 { self.id.partial_cmp(&other.id) } } pub fn get_current_places(url: &str) -> Result, reqwest::Error> { let body = reqwest::blocking::get(url)?.text()?; let start = body.find("").unwrap(); let table = &body[start..=(end + 7)]; let mut places: Vec = Vec::new(); let mut lines: Vec = Vec::new(); let mut line = table.replace("\n", "").replace("\r", ""); while let Some(begin) = line.find("") { Some(end) => { let inner_node = &line[(begin + 5)..end]; let content = parse_inner_node(inner_node); let escaped_content = replace_html_codes(&content); lines.push(escaped_content); line.replace_range(begin..=end + 5, ""); } None => break, } } lines.chunks(5).for_each(|chunk| { let free = try_parse_free_slot(&chunk[3]); 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: id.to_string(), description: description.to_string(), date: chunk[2].clone(), free: free.unwrap() }; places.push(new_remaining_place); }); places.sort(); Ok(places) } fn try_parse_free_slot(content: &str) -> Option { if let Ok(slots) = content.parse::() { return Some(slots) } if let Some((left, right)) = content.split_once(' ') { if let Ok(slots) = left.parse::() { return Some(slots) } if let Ok(slots) = right.parse::() { return Some(slots) } } None }