rewrite into a better scheduler
This commit is contained in:
parent
cd2dbf7c12
commit
2e4851ae1c
93
src/main.rs
93
src/main.rs
@ -1,8 +1,11 @@
|
|||||||
use std::{collections::VecDeque, fs::File};
|
use clokwerk::{Scheduler, TimeUnits};
|
||||||
|
use rss::{ChannelBuilder, ItemBuilder};
|
||||||
|
use std::fs::File;
|
||||||
|
use std::sync::{Arc, Mutex};
|
||||||
|
use std::thread;
|
||||||
|
use std::time::Duration;
|
||||||
use time::OffsetDateTime;
|
use time::OffsetDateTime;
|
||||||
|
|
||||||
use rss::{ChannelBuilder, Item, ItemBuilder};
|
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq)]
|
#[derive(Clone, PartialEq, Eq)]
|
||||||
struct RemainingPlace {
|
struct RemainingPlace {
|
||||||
id: String,
|
id: String,
|
||||||
@ -11,22 +14,37 @@ struct RemainingPlace {
|
|||||||
free: usize,
|
free: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<(), reqwest::Error> {
|
|
||||||
// const URL: &str = "https://www.lfs.sachsen.de/restplatzboerse-5152.html";
|
// const URL: &str = "https://www.lfs.sachsen.de/restplatzboerse-5152.html";
|
||||||
const URL: &str = "http://127.0.0.1:8080/tip.html";
|
const URL: &str = "http://127.0.0.1:8080/tip.html";
|
||||||
|
|
||||||
let mut channel = ChannelBuilder::default()
|
fn main() {
|
||||||
.title(String::from("LFS Restplatzbörse"))
|
let last_places: Arc<Mutex<Vec<RemainingPlace>>> = Arc::new(Mutex::new(Vec::new()));
|
||||||
.link(URL.to_string())
|
let mut scheduler = Scheduler::new();
|
||||||
.description(String::from("Ein RSS Feed der Restplatzbörse der Landesfeuerwehrschule Sachsen. Nicht offiziell."))
|
|
||||||
.language(Some("de-DE".to_string()))
|
|
||||||
.build();
|
|
||||||
|
|
||||||
let mut last_places: Vec<RemainingPlace> = Vec::new();
|
scheduler
|
||||||
let mut items: VecDeque<Item> = VecDeque::new();
|
.every(1.minutes())
|
||||||
|
.run(move || match get_current_places() {
|
||||||
|
Ok(places) => {
|
||||||
|
if should_feed_be_updated(&places, &last_places) {
|
||||||
|
update_rss_file(places);
|
||||||
|
println!("Updated feed.");
|
||||||
|
} else {
|
||||||
|
println!("No update.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(error) => {
|
||||||
|
println!("Error: {}", error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let body = reqwest::blocking::get(URL).unwrap().text().unwrap();
|
scheduler.run_pending();
|
||||||
|
thread::sleep(Duration::from_millis(10));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_current_places() -> Result<Vec<RemainingPlace>, reqwest::Error> {
|
||||||
|
let body = reqwest::blocking::get(URL)?.text()?;
|
||||||
|
|
||||||
let start = body.find("<tbody").unwrap();
|
let start = body.find("<tbody").unwrap();
|
||||||
let end = body.find("</tbody>").unwrap();
|
let end = body.find("</tbody>").unwrap();
|
||||||
@ -53,14 +71,18 @@ fn main() -> Result<(), reqwest::Error> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let condition = places.len() == last_places.len()
|
Ok(places)
|
||||||
&& places
|
}
|
||||||
.iter()
|
|
||||||
.zip(last_places.iter())
|
|
||||||
.all(|(one, two)| one == two);
|
|
||||||
|
|
||||||
if !condition {
|
fn update_rss_file(places: Vec<RemainingPlace>) {
|
||||||
last_places = places.clone();
|
let mut channel = ChannelBuilder::default()
|
||||||
|
.title(String::from("LFS Restplatzbörse"))
|
||||||
|
.link(URL.to_string())
|
||||||
|
.description(String::from(
|
||||||
|
"Ein RSS Feed der Restplatzbörse der Landesfeuerwehrschule Sachsen. Nicht offiziell.",
|
||||||
|
))
|
||||||
|
.language(Some("de-DE".to_string()))
|
||||||
|
.build();
|
||||||
|
|
||||||
let title = format!("Restplatzbörse Update - {}", OffsetDateTime::now_utc());
|
let title = format!("Restplatzbörse Update - {}", OffsetDateTime::now_utc());
|
||||||
let content = places
|
let content = places
|
||||||
@ -79,23 +101,30 @@ fn main() -> Result<(), reqwest::Error> {
|
|||||||
.content(Some(content))
|
.content(Some(content))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
items.push_back(item);
|
channel.set_items(vec![item]);
|
||||||
|
|
||||||
if items.len() > 10 {
|
|
||||||
items.pop_front().unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
channel.set_items(items.clone());
|
|
||||||
|
|
||||||
let output = File::create("rss.txt").unwrap();
|
let output = File::create("rss.txt").unwrap();
|
||||||
channel.pretty_write_to(output, ' ' as u8, 2).unwrap();
|
channel.pretty_write_to(output, ' ' as u8, 2).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
println!("Got Update and wrote to file");
|
fn should_feed_be_updated(
|
||||||
} else {
|
new_places: &Vec<RemainingPlace>,
|
||||||
println!("No update");
|
last_places: &Mutex<Vec<RemainingPlace>>,
|
||||||
}
|
) -> bool {
|
||||||
std::thread::sleep(std::time::Duration::from_secs_f64(60.0));
|
let mut last_places = last_places.lock().unwrap();
|
||||||
|
|
||||||
|
let are_the_same_places = new_places.len() == last_places.len()
|
||||||
|
&& new_places
|
||||||
|
.iter()
|
||||||
|
.zip(last_places.iter())
|
||||||
|
.all(|(one, two)| one == two);
|
||||||
|
|
||||||
|
if !are_the_same_places {
|
||||||
|
last_places.clear();
|
||||||
|
last_places.append(&mut new_places.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
!are_the_same_places
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_node(input: &str) -> String {
|
fn parse_node(input: &str) -> String {
|
||||||
|
Loading…
Reference in New Issue
Block a user