Recover last value from rss channel when creating thread jobs
This commit is contained in:
parent
4b36b75d9f
commit
cad05a6609
51
src/job.rs
51
src/job.rs
|
@ -6,6 +6,7 @@ use std::time::Instant;
|
||||||
use ansi_to_html;
|
use ansi_to_html;
|
||||||
use chrono;
|
use chrono;
|
||||||
use rss;
|
use rss;
|
||||||
|
use scraper;
|
||||||
|
|
||||||
use crate::conf::Conf;
|
use crate::conf::Conf;
|
||||||
|
|
||||||
|
@ -56,6 +57,7 @@ impl Job {
|
||||||
self.channel.as_mut().unwrap().set_generator("Haunter".to_string());
|
self.channel.as_mut().unwrap().set_generator("Haunter".to_string());
|
||||||
},
|
},
|
||||||
Ok(file) => {
|
Ok(file) => {
|
||||||
|
println!("Reading channel from '{}'", self.output_file.as_ref().unwrap().display());
|
||||||
self.channel = Some(
|
self.channel = Some(
|
||||||
rss::Channel::read_from(std::io::BufReader::new(file)).unwrap()
|
rss::Channel::read_from(std::io::BufReader::new(file)).unwrap()
|
||||||
);
|
);
|
||||||
|
@ -112,9 +114,16 @@ impl Job {
|
||||||
job.every = Duration::new(converted_value, 0);
|
job.every = Duration::new(converted_value, 0);
|
||||||
},
|
},
|
||||||
"output_file" => {
|
"output_file" => {
|
||||||
job.output_file = Some(
|
if item.1.starts_with("/") || item.1.starts_with("./") {
|
||||||
conf.output_dir.join(PathBuf::from_str(item.1.as_str()).unwrap())
|
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());
|
println!("Unknown key '{}' in job file '{}'", key, path.display());
|
||||||
|
@ -130,6 +139,29 @@ impl Job {
|
||||||
return Ok(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) {
|
pub fn update(&mut self, value: &str, diff: &str) {
|
||||||
if self.channel.is_none() {
|
if self.channel.is_none() {
|
||||||
println!("Skipping update of channel: no channel set");
|
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());
|
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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -113,10 +113,11 @@ fn main() {
|
||||||
},
|
},
|
||||||
Ok(value) => value,
|
Ok(value) => value,
|
||||||
};
|
};
|
||||||
|
let last_result = job.last_value().clone();
|
||||||
jobs.push(ThreadJob {
|
jobs.push(ThreadJob {
|
||||||
job: job,
|
job: job,
|
||||||
handle: None,
|
handle: None,
|
||||||
last_result: None,
|
last_result: last_result,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -199,6 +200,7 @@ fn main() {
|
||||||
let events = inotify.read_events(&mut notify_buffer);
|
let events = inotify.read_events(&mut notify_buffer);
|
||||||
if events.is_ok() {
|
if events.is_ok() {
|
||||||
for event in events.unwrap() {
|
for event in events.unwrap() {
|
||||||
|
println!("Event: {:?}", event);
|
||||||
if event.name.is_none() {
|
if event.name.is_none() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -229,10 +231,11 @@ fn main() {
|
||||||
},
|
},
|
||||||
Ok(value) => value,
|
Ok(value) => value,
|
||||||
};
|
};
|
||||||
|
let last_result = job.last_value().clone();
|
||||||
jobs.push(ThreadJob {
|
jobs.push(ThreadJob {
|
||||||
job: job,
|
job: job,
|
||||||
handle: None,
|
handle: None,
|
||||||
last_result: None,
|
last_result: last_result,
|
||||||
});
|
});
|
||||||
println!("Added job from '{}' being created", path.display());
|
println!("Added job from '{}' being created", path.display());
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue