Implemented a downloaded/watched cleaner.
This commit is contained in:
@@ -16,6 +16,7 @@ diesel_codegen = { version = "0.16", features = ["sqlite"] }
|
||||
xdg = "2.1"
|
||||
lazy_static = "0.2"
|
||||
rss = "1.1"
|
||||
chrono = "0.4"
|
||||
# overwrite diesel dependancy that disables a feature rss depends upon
|
||||
dotenv = "*"
|
||||
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
use rayon::prelude::*;
|
||||
|
||||
use errors::*;
|
||||
use dbqueries;
|
||||
use index_feed::Database;
|
||||
use models::Episode;
|
||||
use chrono::prelude::*;
|
||||
|
||||
use std::path::Path;
|
||||
use std::fs;
|
||||
|
||||
// TODO: Write unit test.
|
||||
pub fn download_checker(db: Database) -> Result<()> {
|
||||
let mut episodes = {
|
||||
let tempdb = db.lock().unwrap();
|
||||
dbqueries::get_downloaded_episodes(&tempdb)?
|
||||
};
|
||||
|
||||
episodes.par_iter_mut().for_each(|ep| {
|
||||
if ep.local_uri().is_some() {
|
||||
if !Path::new(ep.local_uri().unwrap()).exists() {
|
||||
ep.set_local_uri(None);
|
||||
let res = ep.save(&db.clone());
|
||||
if let Err(err) = res {
|
||||
error!("Error while trying to update episode: {:#?}", ep);
|
||||
error!("Error: {}", err);
|
||||
};
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// TODO: Write unit test.
|
||||
pub fn watched_cleaner(db: &Database) -> Result<()> {
|
||||
let mut episodes = {
|
||||
let tempdb = db.lock().unwrap();
|
||||
dbqueries::get_watched_episodes(&tempdb)?
|
||||
};
|
||||
|
||||
let now_utc = Utc::now().timestamp() as i32;
|
||||
episodes.par_iter_mut().for_each(|mut ep| {
|
||||
if ep.local_uri().is_some() && ep.watched().is_some() {
|
||||
let watched = ep.watched().unwrap().clone();
|
||||
// TODO: expose a config and a user set option.
|
||||
let limit = watched + 172_800; // add 2days in seconds
|
||||
if now_utc > limit {
|
||||
let e = delete_local_content(db, &mut ep);
|
||||
if let Err(err) = e {
|
||||
error!("Error while trying to delete file: {:?}", ep.local_uri());
|
||||
error!("Error: {}", err);
|
||||
};
|
||||
}
|
||||
}
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// TODO: Write unit test.
|
||||
pub fn delete_local_content(db: &Database, ep: &mut Episode) -> Result<()> {
|
||||
if ep.local_uri().is_some() {
|
||||
let uri = ep.local_uri().unwrap().to_owned();
|
||||
if Path::new(&uri).exists() {
|
||||
let res = fs::remove_file(&uri);
|
||||
if res.is_ok() {
|
||||
ep.set_local_uri(None);
|
||||
ep.save(db)?;
|
||||
} else {
|
||||
error!("Error while trying to delete file: {}", uri);
|
||||
error!("Error: {}", res.unwrap_err());
|
||||
};
|
||||
}
|
||||
} else {
|
||||
error!(
|
||||
"Something went wrong evaluating the following path: {:?}",
|
||||
ep.local_uri(),
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -37,6 +37,20 @@ pub fn get_episodes(con: &SqliteConnection) -> QueryResult<Vec<Episode>> {
|
||||
eps
|
||||
}
|
||||
|
||||
pub fn get_downloaded_episodes(con: &SqliteConnection) -> QueryResult<Vec<Episode>> {
|
||||
use schema::episode::dsl::*;
|
||||
|
||||
let eps = episode.filter(local_uri.is_not_null()).load::<Episode>(con);
|
||||
eps
|
||||
}
|
||||
|
||||
pub fn get_watched_episodes(con: &SqliteConnection) -> QueryResult<Vec<Episode>> {
|
||||
use schema::episode::dsl::*;
|
||||
|
||||
let eps = episode.filter(watched.is_not_null()).load::<Episode>(con);
|
||||
eps
|
||||
}
|
||||
|
||||
pub fn get_episode(con: &SqliteConnection, ep_id: i32) -> QueryResult<Episode> {
|
||||
use schema::episode::dsl::*;
|
||||
|
||||
@@ -44,7 +58,10 @@ pub fn get_episode(con: &SqliteConnection, ep_id: i32) -> QueryResult<Episode> {
|
||||
ep
|
||||
}
|
||||
|
||||
pub fn get_episode_local_uri(con: &SqliteConnection, ep_id: i32) -> QueryResult<Option<String>> {
|
||||
pub fn get_episode_from_local_uri(
|
||||
con: &SqliteConnection,
|
||||
ep_id: i32,
|
||||
) -> QueryResult<Option<String>> {
|
||||
use schema::episode::dsl::*;
|
||||
|
||||
let ep = episode
|
||||
|
||||
@@ -16,6 +16,7 @@ extern crate diesel;
|
||||
#[macro_use]
|
||||
extern crate diesel_codegen;
|
||||
|
||||
extern crate chrono;
|
||||
extern crate hyper;
|
||||
extern crate rayon;
|
||||
extern crate reqwest;
|
||||
@@ -24,6 +25,7 @@ extern crate rss;
|
||||
extern crate xdg;
|
||||
|
||||
pub mod dbqueries;
|
||||
pub mod dbcheckup;
|
||||
pub mod models;
|
||||
pub mod index_feed;
|
||||
pub mod errors;
|
||||
|
||||
@@ -17,9 +17,11 @@ pub struct Episode {
|
||||
local_uri: Option<String>,
|
||||
description: Option<String>,
|
||||
published_date: Option<String>,
|
||||
/// Representation of system time. Should be in UTC.
|
||||
epoch: i32,
|
||||
length: Option<i32>,
|
||||
guid: Option<String>,
|
||||
/// Represent the epoch value of when the episode was last watched.
|
||||
watched: Option<i32>,
|
||||
podcast_id: i32,
|
||||
}
|
||||
@@ -94,6 +96,14 @@ impl Episode {
|
||||
self.length = value;
|
||||
}
|
||||
|
||||
pub fn watched(&self) -> Option<i32> {
|
||||
self.watched
|
||||
}
|
||||
|
||||
pub fn set_watched(&mut self, value: Option<i32>) {
|
||||
self.watched = value;
|
||||
}
|
||||
|
||||
pub fn save(&self, db: &Database) -> Result<()> {
|
||||
let tempdb = db.lock().unwrap();
|
||||
self.save_changes::<Episode>(&*tempdb)?;
|
||||
|
||||
Reference in New Issue
Block a user