aboutsummaryrefslogtreecommitdiff
path: root/import
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2025-01-29 23:09:54 +0100
committermetamuffin <metamuffin@disroot.org>2025-01-29 23:09:54 +0100
commit59825d68efa1077382fd6acac73f75ae9dc3680a (patch)
tree5f948eddf7b4c85f245359c35c653ab8768ba8f4 /import
parent8099c51e56b6d253c05cac9c235f52027ad736fa (diff)
downloadjellything-59825d68efa1077382fd6acac73f75ae9dc3680a.tar
jellything-59825d68efa1077382fd6acac73f75ae9dc3680a.tar.bz2
jellything-59825d68efa1077382fd6acac73f75ae9dc3680a.tar.zst
mtime based incremental import
Diffstat (limited to 'import')
-rw-r--r--import/src/lib.rs30
1 files changed, 26 insertions, 4 deletions
diff --git a/import/src/lib.rs b/import/src/lib.rs
index ab410eb..34780a8 100644
--- a/import/src/lib.rs
+++ b/import/src/lib.rs
@@ -11,7 +11,8 @@ use ebml_struct::{
};
use jellybase::{assetfed::AssetInner, cache::cache_file, database::Database, CONF, SECRETS};
use jellycommon::{
- Chapter, LocalTrack, MediaInfo, NodeID, NodeKind, Rating, SourceTrack, SourceTrackKind, TrackSource,
+ Chapter, LocalTrack, MediaInfo, NodeID, NodeKind, Rating, SourceTrack, SourceTrackKind,
+ TrackSource,
};
use log::info;
use regex::Regex;
@@ -21,6 +22,7 @@ use std::{
io::{BufReader, ErrorKind, Read, Write},
path::Path,
sync::LazyLock,
+ time::UNIX_EPOCH,
};
use tmdb::Tmdb;
use tokio::{
@@ -48,11 +50,11 @@ pub fn is_importing() -> bool {
IMPORT_SEM.available_permits() == 0
}
-pub async fn import_wrap(db: Database) -> Result<()> {
+pub async fn import_wrap(db: Database, incremental: bool) -> Result<()> {
let _sem = IMPORT_SEM.try_acquire()?;
let jh = spawn_blocking(move || {
- let errs = match import(&db) {
+ let errs = match import(&db, incremental) {
Err(e) => vec![format!("{e:#}")],
Ok(e) => e,
};
@@ -64,7 +66,7 @@ pub async fn import_wrap(db: Database) -> Result<()> {
Ok(())
}
-fn import(db: &Database) -> Result<Vec<String>> {
+fn import(db: &Database, incremental: bool) -> Result<Vec<String>> {
let mut queue = VecDeque::from_iter(Some(CONF.media_path.clone()));
let mut errors = Vec::new();
@@ -72,6 +74,8 @@ fn import(db: &Database) -> Result<Vec<String>> {
trakt: SECRETS.api.trakt.as_ref().map(|key| Trakt::new(key)),
tmdb: SECRETS.api.tmdb.as_ref().map(|key| Tmdb::new(key)),
};
+ let mut num_skipped = 0;
+ let mut num_imported = 0;
while let Some(path) = queue.pop_front() {
if path.is_dir() {
@@ -80,11 +84,29 @@ fn import(db: &Database) -> Result<Vec<String>> {
}
}
if path.is_file() {
+ let meta = path.metadata()?;
+ let mtime = meta
+ .modified()?
+ .duration_since(UNIX_EPOCH)
+ .unwrap()
+ .as_secs();
+
+ if incremental {
+ if let Some(last_mtime) = db.get_import_file_mtime(&path)? {
+ if last_mtime >= mtime {
+ num_skipped += 1;
+ continue;
+ }
+ }
+ }
+ num_imported += 1;
if let Err(e) = import_file(db, &path).context(anyhow!("{path:?}")) {
errors.push(format!("{e:#}"));
}
+ db.set_import_file_mtime(&path, mtime)?;
}
}
+ info!("import finished. skipped={num_skipped} imported={num_imported}");
Ok(errors)
}