Recover last value from rss channel when creating thread jobs

This commit is contained in:
Kienan Stewart 2022-09-25 19:49:22 -04:00
parent 4b36b75d9f
commit cad05a6609
2 changed files with 53 additions and 5 deletions

View File

@ -6,6 +6,7 @@ use std::time::Instant;
use ansi_to_html;
use chrono;
use rss;
use scraper;
use crate::conf::Conf;
@ -56,6 +57,7 @@ impl Job {
self.channel.as_mut().unwrap().set_generator("Haunter".to_string());
},
Ok(file) => {
println!("Reading channel from '{}'", self.output_file.as_ref().unwrap().display());
self.channel = Some(
rss::Channel::read_from(std::io::BufReader::new(file)).unwrap()
);
@ -112,10 +114,17 @@ impl Job {
job.every = Duration::new(converted_value, 0);
},
"output_file" => {
if item.1.starts_with("/") || item.1.starts_with("./") {
job.output_file = Some(
PathBuf::from_str(item.1.as_str()).unwrap()
);
}
else {
job.output_file = Some(
conf.output_dir.join(PathBuf::from_str(item.1.as_str()).unwrap())
);
}
}
_ => {
println!("Unknown key '{}' in job file '{}'", key, path.display());
return Err("Unknown key");
@ -130,6 +139,29 @@ impl Job {
return Ok(job);
}
pub fn last_value(&self) -> Option<String> {
if self.channel.is_none() {
return None;
}
let channel = self.channel.as_ref().unwrap();
if channel.items.len() == 0 {
return None;
}
let last = &channel.items[channel.items.len()-1];
if last.content.is_none() {
return None;
}
let fragment = scraper::Html::parse_fragment(&last.content.as_ref().unwrap().as_str());
let selector = scraper::Selector::parse("pre.new-value").unwrap();
let value = match fragment.select(&selector).next() {
Some(value) => value,
_ => {
return None;
},
};
return Some(value.inner_html().trim().to_string());
}
pub fn update(&mut self, value: &str, diff: &str) {
if self.channel.is_none() {
println!("Skipping update of channel: no channel set");
@ -237,4 +269,17 @@ selector = section.listing:nth-child(2) > ul:nth-child(1) > li:nth-child(3) > he
assert!(job.channel.is_some());
}
#[test]
fn recover_value_from_channel() {
let conf = Conf::get_default_conf();
let mut job_file = NamedTempFile::new().unwrap();
let job_conf = format!(r#"
url = http://example.com/test
output_file = ./src/job_example.rss
"#);
job_file.write_all(job_conf.as_bytes()).expect("Failed to write job test content");
let job = Job::from_file(job_file.path(), &conf).expect("Failed to read configuration file");
assert_eq!(job.output_file.as_ref().unwrap().to_str().unwrap(), "./src/job_example.rss");
assert_eq!(job.last_value().unwrap(), "Version 1.64.0");
}
}

View File

@ -113,10 +113,11 @@ fn main() {
},
Ok(value) => value,
};
let last_result = job.last_value().clone();
jobs.push(ThreadJob {
job: job,
handle: None,
last_result: None,
last_result: last_result,
});
}
@ -199,6 +200,7 @@ fn main() {
let events = inotify.read_events(&mut notify_buffer);
if events.is_ok() {
for event in events.unwrap() {
println!("Event: {:?}", event);
if event.name.is_none() {
continue;
}
@ -229,10 +231,11 @@ fn main() {
},
Ok(value) => value,
};
let last_result = job.last_value().clone();
jobs.push(ThreadJob {
job: job,
handle: None,
last_result: None,
last_result: last_result,
});
println!("Added job from '{}' being created", path.display());
},