Create channel when creating job from file

This commit is contained in:
Kienan Stewart 2022-09-25 17:49:26 -04:00
parent d2866c3f37
commit 82bd7e2d5c
1 changed files with 29 additions and 20 deletions

View File

@ -41,31 +41,34 @@ impl Job {
self.output_file = Some(output_file); self.output_file = Some(output_file);
} }
fn set_default_channel(&mut self) {
match std::fs::File::open(self.output_file.as_ref().unwrap()) {
Err(why) => {
println!("Failed to open '{}': {}", self.output_file.as_ref().unwrap().display(), why);
println!("Creating empty RSS channel for job '{}'", self.url);
self.channel = Some(
rss::ChannelBuilder::default()
.title(self.url.as_str())
.link(self.url.as_str())
.description("haunting")
.build()
);
self.channel.as_mut().unwrap().set_generator("Haunter".to_string());
},
Ok(file) => {
self.channel = Some(
rss::Channel::read_from(std::io::BufReader::new(file)).unwrap()
);
},
};
}
pub fn new(url: &str, selector: &str, conf: &Conf) -> Job { pub fn new(url: &str, selector: &str, conf: &Conf) -> Job {
let mut job = Job::default(conf); let mut job = Job::default(conf);
job.url = url.to_string(); job.url = url.to_string();
job.set_default_output_file(conf); job.set_default_output_file(conf);
job.selector = selector.to_string(); job.selector = selector.to_string();
job.set_default_channel();
match std::fs::File::open(job.output_file.as_ref().unwrap()) {
Err(why) => {
println!("Failed to open '{}': {}", job.output_file.as_ref().unwrap().display(), why);
println!("Creating empty RSS channel for job '{}'", job.url);
job.channel = Some(
rss::ChannelBuilder::default()
.title(url)
.link(url)
.description("haunting")
.build()
);
job.channel.as_mut().unwrap().set_generator("Haunter".to_string());
},
Ok(file) => {
job.channel = Some(
rss::Channel::read_from(std::io::BufReader::new(file)).unwrap()
);
},
};
return job; return job;
} }
@ -108,6 +111,7 @@ impl Job {
if job.output_file.is_none() { if job.output_file.is_none() {
job.set_default_output_file(conf); job.set_default_output_file(conf);
} }
job.set_default_channel();
job.source_file = Some(PathBuf::from(path)); job.source_file = Some(PathBuf::from(path));
return Ok(job); return Ok(job);
} }
@ -172,6 +176,8 @@ mod tests {
assert_eq!(job.url, "my/url"); assert_eq!(job.url, "my/url");
assert_eq!(job.output_file.unwrap().to_str().unwrap(), "results.d/my-url.rss"); assert_eq!(job.output_file.unwrap().to_str().unwrap(), "results.d/my-url.rss");
assert_eq!(job.selector, "myselector"); assert_eq!(job.selector, "myselector");
assert!(job.source_file.is_none());
assert!(job.channel.is_some());
} }
#[test] #[test]
@ -192,6 +198,8 @@ selector = section.listing:nth-child(2) > ul:nth-child(1) > li:nth-child(3) > he
assert_eq!(job.output_file.unwrap().to_str().unwrap(), "results.d/example_output.atom"); assert_eq!(job.output_file.unwrap().to_str().unwrap(), "results.d/example_output.atom");
assert_eq!(job.every.as_secs(), 7200); assert_eq!(job.every.as_secs(), 7200);
assert_eq!(job.selector, "section.listing:nth-child(2) > ul:nth-child(1) > li:nth-child(3) > header:nth-child(3) > h2:nth-child(1) > a:nth-child(1)"); assert_eq!(job.selector, "section.listing:nth-child(2) > ul:nth-child(1) > li:nth-child(3) > header:nth-child(3) > h2:nth-child(1) > a:nth-child(1)");
assert_eq!(job.source_file.unwrap().to_str(), tf.path().to_str());
assert!(job.channel.is_some());
} }
#[test] #[test]
@ -212,6 +220,7 @@ selector = section.listing:nth-child(2) > ul:nth-child(1) > li:nth-child(3) > he
assert_eq!(job.every.as_secs(), 7200); assert_eq!(job.every.as_secs(), 7200);
assert_eq!(job.selector, "section.listing:nth-child(2) > ul:nth-child(1) > li:nth-child(3) > header:nth-child(3) > h2:nth-child(1) > a:nth-child(1)"); assert_eq!(job.selector, "section.listing:nth-child(2) > ul:nth-child(1) > li:nth-child(3) > header:nth-child(3) > h2:nth-child(1) > a:nth-child(1)");
assert_eq!(job.source_file.unwrap().to_str(), tf.path().to_str()); assert_eq!(job.source_file.unwrap().to_str(), tf.path().to_str());
assert!(job.channel.is_some());
} }
} }