upend/src/extractors/mod.rs

68 lines
1.6 KiB
Rust

use crate::{
addressing::Address,
database::{entry::Entry, UpEndConnection},
util::jobs::JobContainer,
};
use anyhow::Result;
use std::sync::{Arc, RwLock};
#[cfg(feature = "extractors-web")]
pub mod web;
#[cfg(feature = "extractors-audio")]
pub mod audio;
pub trait Extractor {
fn get(
&self,
address: &Address,
connection: &UpEndConnection,
job_container: Arc<RwLock<JobContainer>>,
) -> Result<Vec<Entry>>;
fn is_needed(&self, _address: &Address, _connection: &UpEndConnection) -> Result<bool> {
Ok(true)
}
fn insert_info(
&self,
address: &Address,
connection: &UpEndConnection,
job_container: Arc<RwLock<JobContainer>>,
) -> Result<usize> {
if self.is_needed(address, connection)? {
let entries = self.get(address, connection, job_container)?;
connection.transaction(|| {
let len = entries.len();
for entry in entries {
connection.insert_entry(entry)?;
}
Ok(len)
})
} else {
Ok(0)
}
}
}
pub fn extract_all(
address: &Address,
connection: &UpEndConnection,
job_container: Arc<RwLock<JobContainer>>,
) -> Result<usize> {
let mut entry_count = 0;
#[cfg(feature = "extractors-web")]
{
entry_count += web::WebExtractor.insert_info(address, connection, job_container.clone())?;
}
#[cfg(feature = "extractors-audio")]
{
entry_count += audio::ID3Extractor.insert_info(address, connection, job_container)?;
}
Ok(entry_count)
}