add parameters for url and rss file

This commit is contained in:
Max Hohlfeld 2022-12-29 23:16:54 +01:00
parent 2e4851ae1c
commit 1db0031f4c
3 changed files with 38 additions and 9 deletions

7
Cargo.lock generated
View File

@ -524,6 +524,7 @@ name = "lfs_scraper"
version = "0.1.0"
dependencies = [
"clokwerk",
"pico-args",
"reqwest",
"rss",
"time",
@ -687,6 +688,12 @@ version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e"
[[package]]
name = "pico-args"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5be167a7af36ee22fe3115051bc51f6e6c7054c9348e28deb4f49bd6f705a315"
[[package]]
name = "pin-project-lite"
version = "0.2.9"

View File

@ -10,3 +10,4 @@ reqwest = { version = "0.11.13", features = ["blocking"] }
rss = "2"
time = "0.3"
clokwerk = "0.4"
pico-args = "0.5"

View File

@ -14,19 +14,40 @@ struct RemainingPlace {
free: usize,
}
// const URL: &str = "https://www.lfs.sachsen.de/restplatzboerse-5152.html";
const URL: &str = "http://127.0.0.1:8080/tip.html";
struct Args {
url: String,
rss_file: String,
}
fn parse_args() -> Result<Args, pico_args::Error> {
let mut pargs = pico_args::Arguments::from_env();
let args = Args {
url: pargs.value_from_str("--url")?,
rss_file: pargs.value_from_str("--rss-file")?,
};
Ok(args)
}
fn main() {
let args = match parse_args() {
Ok(v) => v,
Err(err) => {
eprintln!("Error: {err}");
std::process::exit(1);
}
};
let last_places: Arc<Mutex<Vec<RemainingPlace>>> = Arc::new(Mutex::new(Vec::new()));
let mut scheduler = Scheduler::new();
scheduler
.every(1.minutes())
.run(move || match get_current_places() {
.run(move || match get_current_places(&args.url) {
Ok(places) => {
if should_feed_be_updated(&places, &last_places) {
update_rss_file(places);
update_rss_file(places, &args.url, &args.rss_file);
println!("Updated feed.");
} else {
println!("No update.");
@ -43,8 +64,8 @@ fn main() {
}
}
fn get_current_places() -> Result<Vec<RemainingPlace>, reqwest::Error> {
let body = reqwest::blocking::get(URL)?.text()?;
fn get_current_places(url: &str) -> Result<Vec<RemainingPlace>, reqwest::Error> {
let body = reqwest::blocking::get(url)?.text()?;
let start = body.find("<tbody").unwrap();
let end = body.find("</tbody>").unwrap();
@ -74,10 +95,10 @@ fn get_current_places() -> Result<Vec<RemainingPlace>, reqwest::Error> {
Ok(places)
}
fn update_rss_file(places: Vec<RemainingPlace>) {
fn update_rss_file(places: Vec<RemainingPlace>, url: &str, rss_file: &str) {
let mut channel = ChannelBuilder::default()
.title(String::from("LFS Restplatzbörse"))
.link(URL.to_string())
.link(url.to_string())
.description(String::from(
"Ein RSS Feed der Restplatzbörse der Landesfeuerwehrschule Sachsen. Nicht offiziell.",
))
@ -103,7 +124,7 @@ fn update_rss_file(places: Vec<RemainingPlace>) {
channel.set_items(vec![item]);
let output = File::create("rss.txt").unwrap();
let output = File::create(rss_file).unwrap();
channel.pretty_write_to(output, ' ' as u8, 2).unwrap();
}