2022-02-15 13:32:46 +01:00
|
|
|
use crate::{
|
|
|
|
addressing::Address,
|
|
|
|
database::{entry::Entry, UpEndConnection},
|
|
|
|
util::jobs::JobContainer,
|
|
|
|
};
|
|
|
|
use anyhow::Result;
|
|
|
|
use std::sync::{Arc, RwLock};
|
2022-02-10 11:38:45 +01:00
|
|
|
|
|
|
|
#[cfg(feature = "extractors-web")]
|
|
|
|
pub mod web;
|
2022-02-15 13:32:46 +01:00
|
|
|
|
2022-02-28 21:36:55 +01:00
|
|
|
#[cfg(feature = "extractors-audio")]
|
|
|
|
pub mod audio;
|
|
|
|
|
2022-02-15 13:32:46 +01:00
|
|
|
pub trait Extractor {
|
2022-02-28 20:36:16 +01:00
|
|
|
fn get(
|
|
|
|
&self,
|
|
|
|
address: &Address,
|
2022-02-28 21:36:55 +01:00
|
|
|
connection: &UpEndConnection,
|
2022-02-28 20:36:16 +01:00
|
|
|
job_container: Arc<RwLock<JobContainer>>,
|
|
|
|
) -> Result<Vec<Entry>>;
|
|
|
|
|
|
|
|
fn is_needed(&self, _address: &Address, _connection: &UpEndConnection) -> Result<bool> {
|
|
|
|
Ok(true)
|
|
|
|
}
|
2022-02-15 13:32:46 +01:00
|
|
|
|
2022-02-28 19:49:42 +01:00
|
|
|
fn insert_info(
|
2022-02-15 13:32:46 +01:00
|
|
|
&self,
|
2022-02-28 20:36:16 +01:00
|
|
|
address: &Address,
|
|
|
|
connection: &UpEndConnection,
|
2022-02-15 13:32:46 +01:00
|
|
|
job_container: Arc<RwLock<JobContainer>>,
|
2022-02-28 21:36:55 +01:00
|
|
|
) -> Result<usize> {
|
2022-02-28 20:36:16 +01:00
|
|
|
if self.is_needed(address, connection)? {
|
2022-02-28 21:36:55 +01:00
|
|
|
let entries = self.get(address, connection, job_container)?;
|
2022-02-15 13:32:46 +01:00
|
|
|
|
2022-02-28 20:36:16 +01:00
|
|
|
connection.transaction(|| {
|
2022-02-28 21:36:55 +01:00
|
|
|
let len = entries.len();
|
2022-02-28 20:36:16 +01:00
|
|
|
for entry in entries {
|
|
|
|
connection.insert_entry(entry)?;
|
|
|
|
}
|
2022-02-28 21:36:55 +01:00
|
|
|
Ok(len)
|
2022-02-28 20:36:16 +01:00
|
|
|
})
|
|
|
|
} else {
|
2022-02-28 21:36:55 +01:00
|
|
|
Ok(0)
|
2022-02-28 20:36:16 +01:00
|
|
|
}
|
2022-02-15 13:32:46 +01:00
|
|
|
}
|
2022-02-28 22:43:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|