Update all libraries to the new database format

This commit is contained in:
2026-01-21 22:05:50 -07:00
parent 0e2a8e425b
commit 66168c120f
33 changed files with 1025 additions and 794 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "flix-cli"
version = "0.0.16"
version = "0.0.17"
edition.workspace = true
rust-version.workspace = true
description = "CLI for interacting with a flix database"
+19 -1
View File
@@ -1,12 +1,30 @@
use flix::model::numbers::{EpisodeNumber, SeasonNumber};
use chrono::NaiveDate;
use clap::Subcommand;
#[derive(Subcommand)]
pub enum AddCommand {
/// Process a flix collection
/// Add a flix collection
Collection {
#[arg(value_name = "TITLE")]
title: String,
#[arg(value_name = "OVERVIEW")]
overview: String,
},
/// Add a flix episode
Episode {
#[arg(value_name = "SHOW_WEB_SLUG")]
show_slug: String,
#[arg(value_name = "NUMBER")]
season_number: SeasonNumber,
#[arg(value_name = "NUMBER")]
episode_number: EpisodeNumber,
#[arg(value_name = "TITLE")]
title: String,
#[arg(value_name = "OVERVIEW")]
overview: String,
#[arg(value_name = "DATE")]
air_date: NaiveDate,
},
}
+25 -3
View File
@@ -1,7 +1,7 @@
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use anyhow::{Result, anyhow};
use clap::{Parser, Subcommand};
use clap::{Args, Parser, Subcommand};
pub mod flix;
pub mod tmdb;
@@ -13,8 +13,12 @@ pub struct Cli {
#[arg(short, long, value_name = "FILE", default_value = "~/.flix")]
config: PathBuf,
/// Use a custom cache file
#[arg(short = 'C', long, value_name = "FILE", default_value = "./flix.redb")]
cache: PathBuf,
/// Use a custom database file
#[arg(short, long, value_name = "DATABASE", default_value = "./flix.db")]
#[arg(short, long, value_name = "FILE", default_value = "./flix.db")]
database: PathBuf,
/// Enable tracing
@@ -38,6 +42,10 @@ impl Cli {
}
}
pub fn cache_path(&self) -> &Path {
&self.cache
}
pub fn database_path(&self) -> Result<String> {
self.database
.as_os_str()
@@ -51,12 +59,26 @@ impl Cli {
}
}
#[derive(Args)]
pub struct AddOverrides {
#[arg(long)]
pub title: Option<String>,
#[arg(long)]
pub sort_title: Option<String>,
#[arg(long)]
pub fs_slug: Option<String>,
#[arg(long)]
pub web_slug: Option<String>,
}
#[derive(Subcommand)]
pub enum Command {
/// Initialize a new database
Init,
/// Add new items to the database
Add {
#[command(flatten)]
overrides: AddOverrides,
#[command(subcommand)]
command: AddCommand,
},
+20 -6
View File
@@ -1,6 +1,7 @@
use std::path::PathBuf;
use std::rc::Rc;
use flix::tmdb::Client;
use flix::tmdb::{self, CachePolicy, Client, RedbCache};
use anyhow::{Context, Result};
use clap::Parser;
@@ -12,6 +13,8 @@ use cli::{AddCommand, Cli, Command, DeleteCommand, UpdateCommand};
mod config;
use config::Config;
use crate::cli::AddOverrides;
mod db;
mod run;
@@ -26,7 +29,11 @@ async fn main() -> Result<()> {
let database_path = cli.database_path()?;
let client = Client::new(config.tmdb().bearer_token().to_owned());
let client = Client::new(
tmdb::Config::new(config.tmdb().bearer_token().to_owned()),
Rc::new(RedbCache::new(cli.cache_path())?),
CachePolicy::Full,
);
if cli.trace {
tracing_subscriber::fmt()
@@ -37,7 +44,9 @@ async fn main() -> Result<()> {
match cli.command() {
Command::Init => exec_init(database_path).await?,
Command::Add { command } => exec_add(client, database_path, command).await?,
Command::Add { command, overrides } => {
exec_add(client, database_path, command, overrides).await?
}
Command::Update { command } => exec_update(client, database_path, command).await?,
Command::Delete { command } => exec_delete(client, database_path, command).await?,
Command::Backup { output } => exec_backup(database_path, output).await?,
@@ -53,15 +62,20 @@ async fn exec_init(database_path: String) -> Result<()> {
Ok(())
}
async fn exec_add(client: Client, database_path: String, command: AddCommand) -> Result<()> {
async fn exec_add(
client: Client,
database_path: String,
command: AddCommand,
overrides: AddOverrides,
) -> Result<()> {
let database = db::open(database_path).await?;
match command {
AddCommand::Flix { command } => {
run::flix::add(database.as_ref(), command).await?;
run::flix::add(database.as_ref(), command, overrides).await?;
}
AddCommand::Tmdb { command } => {
run::tmdb::add(client, database.as_ref(), command).await?;
run::tmdb::add(client, database.as_ref(), command, overrides).await?;
}
}
+69 -6
View File
@@ -1,23 +1,35 @@
use flix::db::entity;
use flix::model::id::CollectionId;
use flix::model::id::{CollectionId, ShowId};
use flix::model::numbers::{EpisodeNumber, SeasonNumber};
use flix::model::text;
use anyhow::Result;
use sea_orm::ActiveValue::{NotSet, Set};
use sea_orm::{ActiveModelTrait, DatabaseConnection, DbErr, TransactionError, TransactionTrait};
use crate::cli::AddOverrides;
use crate::cli::flix::AddCommand;
pub async fn add(db: &DatabaseConnection, command: AddCommand) -> Result<()> {
pub async fn add(
db: &DatabaseConnection,
command: AddCommand,
overrides: AddOverrides,
) -> Result<()> {
match command {
AddCommand::Collection { title, overview } => {
let result: Result<CollectionId, TransactionError<DbErr>> = db
.transaction(|txn| {
let title = title.clone();
let title = overrides.title.unwrap_or_else(|| title.clone());
let sort_title = text::make_sortable_title(&title);
let fs_slug = text::make_fs_slug(&title);
let web_slug = text::make_web_slug(&title);
let sort_title = overrides
.sort_title
.unwrap_or_else(|| text::make_sortable_title(&title));
let fs_slug = overrides
.fs_slug
.unwrap_or_else(|| text::make_fs_slug(&title));
let web_slug = overrides
.web_slug
.unwrap_or_else(|| text::make_web_slug(&title));
Box::pin(async move {
let flix = entity::info::collections::ActiveModel {
@@ -43,6 +55,57 @@ pub async fn add(db: &DatabaseConnection, command: AddCommand) -> Result<()> {
};
println!("Created Collection: {} [{}]", title, flix_id.into_raw());
Ok(())
}
AddCommand::Episode {
show_slug,
season_number,
episode_number,
title,
overview,
air_date,
} => {
let result: Result<(ShowId, SeasonNumber, EpisodeNumber), TransactionError<DbErr>> = db
.transaction(|txn| {
let title = overrides.title.unwrap_or_else(|| title.clone());
Box::pin(async move {
let show = entity::info::shows::Entity::find_by_web_slug(&show_slug)
.one(txn)
.await?
.ok_or_else(|| {
DbErr::Custom(format!("show '{}' does not exist", show_slug))
})?;
let flix = entity::info::episodes::ActiveModel {
show_id: Set(show.id),
season_number: Set(season_number),
episode_number: Set(episode_number),
title: Set(title),
overview: Set(overview),
date: Set(air_date),
}
.insert(txn)
.await?;
Ok((flix.show_id, flix.season_number, flix.episode_number))
})
})
.await;
let (flix_show, season_number, episode_number) = match result {
Ok(id) => id,
Err(TransactionError::Connection(err)) => Err(err)?,
Err(TransactionError::Transaction(err)) => Err(err)?,
};
println!(
"Created Episode: {} [{} S{} E{}]",
title,
flix_show.into_raw(),
season_number,
episode_number
);
Ok(())
}
}
+45 -18
View File
@@ -16,9 +16,15 @@ use sea_orm::{
ActiveModelTrait, DatabaseConnection, DbErr, EntityTrait, TransactionError, TransactionTrait,
};
use crate::cli::AddOverrides;
use crate::cli::tmdb::Command;
pub async fn add(client: Client, db: &DatabaseConnection, command: Command) -> Result<()> {
pub async fn add(
client: Client,
db: &DatabaseConnection,
command: Command,
overrides: AddOverrides,
) -> Result<()> {
match command {
Command::Collection { id } => {
let id = TmdbCollectionId::from_raw(id);
@@ -36,18 +42,25 @@ pub async fn add(client: Client, db: &DatabaseConnection, command: Command) -> R
.await
.with_context(|| format!("collections().get_details({})", id.into_raw()))?;
let title = collection.title.clone();
let title = overrides.title.unwrap_or(collection.title);
let sort_title = text::make_sortable_title(&title);
let fs_slug = text::make_fs_slug(&title);
let web_slug = text::make_web_slug(&title);
let sort_title = overrides
.sort_title
.unwrap_or_else(|| text::make_sortable_title(&title));
let fs_slug = overrides
.fs_slug
.unwrap_or_else(|| text::make_fs_slug(&title));
let web_slug = overrides
.web_slug
.unwrap_or_else(|| text::make_web_slug(&title));
let result: Result<CollectionId, TransactionError<DbErr>> = db
.transaction(|txn| {
let title = title.clone();
Box::pin(async move {
let flix = entity::info::collections::ActiveModel {
id: NotSet,
title: Set(collection.title),
title: Set(title),
overview: Set(collection.overview),
sort_title: Set(sort_title),
fs_slug: Set(fs_slug),
@@ -93,19 +106,26 @@ pub async fn add(client: Client, db: &DatabaseConnection, command: Command) -> R
.await
.with_context(|| format!("movies().get_details({})", id.into_raw()))?;
let title = movie.title.clone();
let title = overrides.title.unwrap_or(movie.title);
let year = movie.release_date.year();
let sort_title = text::make_sortable_title(&title);
let fs_slug = text::make_fs_slug_year(&title, year);
let web_slug = text::make_web_slug_year(&title, year);
let sort_title = overrides
.sort_title
.unwrap_or_else(|| text::make_sortable_title(&title));
let fs_slug = overrides
.fs_slug
.unwrap_or_else(|| text::make_fs_slug_year(&title, year));
let web_slug = overrides
.web_slug
.unwrap_or_else(|| text::make_web_slug_year(&title, year));
let result: Result<MovieId, TransactionError<DbErr>> = db
.transaction(|txn| {
let title = title.clone();
Box::pin(async move {
let flix = entity::info::movies::ActiveModel {
id: NotSet,
title: Set(movie.title),
title: Set(title),
tagline: Set(movie.tagline),
overview: Set(movie.overview),
date: Set(movie.release_date),
@@ -161,9 +181,6 @@ pub async fn add(client: Client, db: &DatabaseConnection, command: Command) -> R
let mut seasons = Vec::new();
let mut episodes = HashMap::new();
let title = show.title.clone();
let year = show.first_air_date.year();
for season in 1..=show.number_of_seasons {
let season = SeasonNumber::new(season);
let season = match client
@@ -218,16 +235,26 @@ pub async fn add(client: Client, db: &DatabaseConnection, command: Command) -> R
seasons.push(season);
}
let sort_title = text::make_sortable_title(&show.title);
let fs_slug = text::make_fs_slug_year(&show.title, show.first_air_date.year());
let web_slug = text::make_web_slug_year(&show.title, show.first_air_date.year());
let title = overrides.title.unwrap_or(show.title);
let year = show.first_air_date.year();
let sort_title = overrides
.sort_title
.unwrap_or_else(|| text::make_sortable_title(&title));
let fs_slug = overrides
.fs_slug
.unwrap_or_else(|| text::make_fs_slug_year(&title, year));
let web_slug = overrides
.web_slug
.unwrap_or_else(|| text::make_web_slug_year(&title, year));
let result: Result<ShowId, TransactionError<DbErr>> = db
.transaction(|txn| {
let title = title.clone();
Box::pin(async move {
let flix = entity::info::shows::ActiveModel {
id: NotSet,
title: Set(show.title),
title: Set(title),
tagline: Set(show.tagline),
overview: Set(show.overview),
date: Set(show.first_air_date),
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "flix-db"
version = "0.0.16"
version = "0.0.17"
edition.workspace = true
rust-version.workspace = true
description = "Types for storing persistent data about media"
+30
View File
@@ -7,6 +7,8 @@ pub mod collections {
use sea_orm::entity::prelude::*;
use crate::entity;
/// The database representation of a flix collection
#[sea_orm::model]
#[derive(Debug, Clone, DeriveEntityModel)]
@@ -29,6 +31,10 @@ pub mod collections {
/// The url-safe slug
#[sea_orm(indexed, unique)]
pub web_slug: String,
/// Potential content for this collection
#[sea_orm(has_one)]
pub content: HasOne<entity::content::collections::Entity>,
}
impl ActiveModelBehavior for ActiveModel {}
@@ -41,6 +47,8 @@ pub mod movies {
use chrono::NaiveDate;
use sea_orm::entity::prelude::*;
use crate::entity;
/// The database representation of a flix movie
#[sea_orm::model]
#[derive(Debug, Clone, DeriveEntityModel)]
@@ -68,6 +76,10 @@ pub mod movies {
/// The url-safe slug
#[sea_orm(indexed, unique)]
pub web_slug: String,
/// Potential content for this movie
#[sea_orm(has_one)]
pub content: HasOne<entity::content::movies::Entity>,
}
impl ActiveModelBehavior for ActiveModel {}
@@ -80,6 +92,8 @@ pub mod shows {
use chrono::NaiveDate;
use sea_orm::entity::prelude::*;
use crate::entity;
/// The database representation of a flix show
#[sea_orm::model]
#[derive(Debug, Clone, DeriveEntityModel)]
@@ -114,6 +128,10 @@ pub mod shows {
/// Episodes that are part of this show
#[sea_orm(has_many)]
pub episodes: HasMany<super::episodes::Entity>,
/// Potential content for this show
#[sea_orm(has_one)]
pub content: HasOne<entity::content::shows::Entity>,
}
impl ActiveModelBehavior for ActiveModel {}
@@ -127,6 +145,8 @@ pub mod seasons {
use chrono::NaiveDate;
use sea_orm::entity::prelude::*;
use crate::entity;
/// The database representation of a flix season
#[sea_orm::model]
#[derive(Debug, Clone, DeriveEntityModel)]
@@ -158,6 +178,10 @@ pub mod seasons {
/// Episodes that are part of this season
#[sea_orm(has_many)]
pub episodes: HasMany<super::episodes::Entity>,
/// Potential content for this season
#[sea_orm(has_one)]
pub content: HasOne<entity::content::seasons::Entity>,
}
impl ActiveModelBehavior for ActiveModel {}
@@ -171,6 +195,8 @@ pub mod episodes {
use chrono::NaiveDate;
use sea_orm::entity::prelude::*;
use crate::entity;
/// The database representation of a flix episode
#[sea_orm::model]
#[derive(Debug, Clone, DeriveEntityModel)]
@@ -211,6 +237,10 @@ pub mod episodes {
on_delete = "Cascade"
)]
pub season: HasOne<super::seasons::Entity>,
/// Potential content for this episode
#[sea_orm(has_one)]
pub content: HasOne<entity::content::episodes::Entity>,
}
impl ActiveModelBehavior for ActiveModel {}
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "flix"
version = "0.0.16"
version = "0.0.17"
edition.workspace = true
rust-version.workspace = true
description = "Mechanisms for interacting with flix media"
+2 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "flix-fs"
version = "0.0.16"
version = "0.0.17"
edition.workspace = true
rust-version.workspace = true
description = "Filesystem scanner for flix media"
@@ -14,6 +14,7 @@ rustdoc-args = ["--cfg", "docsrs"]
[dependencies]
async-stream = { workspace = true }
either = { workspace = true }
flix-model = { workspace = true }
regex = { workspace = true, features = ["perf", "std"] }
thiserror = { workspace = true }
+27 -151
View File
@@ -4,8 +4,7 @@ use core::pin::Pin;
use std::ffi::OsStr;
use std::path::Path;
use flix_model::id::{CollectionId, MovieId, ShowId};
use flix_model::numbers::{EpisodeNumbers, SeasonNumber};
use flix_model::id::CollectionId;
use async_stream::stream;
use tokio::fs;
@@ -14,7 +13,9 @@ use tokio_stream::wrappers::ReadDirStream;
use crate::Error;
use crate::macros::is_image_extension;
use crate::scanner::{generic, movie, show};
use crate::scanner::{
CollectionScan, EpisodeScan, MediaRef, MovieScan, SeasonScan, ShowScan, generic, movie, show,
};
/// A collection item
pub type Item = crate::Item<Scanner>;
@@ -22,74 +23,21 @@ pub type Item = crate::Item<Scanner>;
/// The scanner for collections
pub enum Scanner {
/// A scanned collection
Collection {
/// The ID of the parent collection (if any)
parent: Option<CollectionId>,
/// The ID of the collection
id: CollectionId,
/// The file name of the poster file
poster_file_name: Option<String>,
},
Collection(CollectionScan),
/// A scanned movie
Movie {
/// The ID of the parent collection (if any)
parent: Option<CollectionId>,
/// The ID of the movie
id: MovieId,
/// The file name of the media file
media_file_name: String,
/// The file name of the poster file
poster_file_name: Option<String>,
},
Movie(MovieScan),
/// A scanned show
Show {
/// The ID of the parent collection (if any)
parent: Option<CollectionId>,
/// The ID of the show
id: ShowId,
/// The file name of the poster file
poster_file_name: Option<String>,
},
Show(ShowScan),
/// A scanned episode
Season {
/// The ID of the show this season belongs to
show: ShowId,
/// The number of this season
season: SeasonNumber,
/// The file name of the poster file
poster_file_name: Option<String>,
},
Season(SeasonScan),
/// A scanned episode
Episode {
/// The ID of the show this episode belongs to
show: ShowId,
/// The season this episode belongs to
season: SeasonNumber,
/// The number(s) of this episode
episode: EpisodeNumbers,
/// The file name of the media file
media_file_name: String,
/// The file name of the poster file
poster_file_name: Option<String>,
},
Episode(EpisodeScan),
}
impl From<movie::Scanner> for Scanner {
fn from(value: movie::Scanner) -> Self {
match value {
movie::Scanner::Movie {
parent,
id,
media_file_name,
poster_file_name,
} => Self::Movie {
parent,
id,
media_file_name,
poster_file_name,
},
movie::Scanner::Movie(m) => Self::Movie(m),
}
}
}
@@ -97,37 +45,9 @@ impl From<movie::Scanner> for Scanner {
impl From<show::Scanner> for Scanner {
fn from(value: show::Scanner) -> Self {
match value {
show::Scanner::Show {
parent,
id,
poster_file_name,
} => Self::Show {
parent,
id,
poster_file_name,
},
show::Scanner::Season {
show,
season,
poster_file_name,
} => Self::Season {
show,
season,
poster_file_name,
},
show::Scanner::Episode {
show,
season,
episode,
media_file_name,
poster_file_name,
} => Self::Episode {
show,
season,
episode,
media_file_name,
poster_file_name,
},
show::Scanner::Show(s) => Self::Show(s),
show::Scanner::Season(s) => Self::Season(s),
show::Scanner::Episode(e) => Self::Episode(e),
}
}
}
@@ -135,57 +55,11 @@ impl From<show::Scanner> for Scanner {
impl From<generic::Scanner> for Scanner {
fn from(value: generic::Scanner) -> Self {
match value {
generic::Scanner::Collection {
parent,
id,
poster_file_name,
} => Self::Collection {
parent,
id,
poster_file_name,
},
generic::Scanner::Movie {
parent,
id,
media_file_name,
poster_file_name,
} => Self::Movie {
parent,
id,
media_file_name,
poster_file_name,
},
generic::Scanner::Show {
parent,
id,
poster_file_name,
} => Self::Show {
parent,
id,
poster_file_name,
},
generic::Scanner::Season {
show,
season,
poster_file_name,
} => Self::Season {
show,
season,
poster_file_name,
},
generic::Scanner::Episode {
show,
season,
episode,
media_file_name,
poster_file_name,
} => Self::Episode {
show,
season,
episode,
media_file_name,
poster_file_name,
},
generic::Scanner::Collection(c) => Self::Collection(c),
generic::Scanner::Movie(m) => Self::Movie(m),
generic::Scanner::Show(s) => Self::Show(s),
generic::Scanner::Season(s) => Self::Season(s),
generic::Scanner::Episode(e) => Self::Episode(e),
}
}
}
@@ -194,8 +68,8 @@ impl Scanner {
/// Scan a folder for a collection
pub fn scan_collection(
path: &Path,
parent: Option<CollectionId>,
id: CollectionId,
parent_ref: Option<MediaRef<CollectionId>>,
id_ref: MediaRef<CollectionId>,
) -> Pin<Box<impl Stream<Item = Item>>> {
Box::pin(stream!({
let dirs = match fs::read_dir(path).await {
@@ -266,15 +140,17 @@ impl Scanner {
yield Item {
path: path.to_owned(),
event: Ok(Self::Collection {
parent,
id,
event: Ok(Self::Collection(CollectionScan {
parent_ref,
id_ref: id_ref.clone(),
poster_file_name,
}),
})),
};
for subdir in subdirs_to_scan {
for await event in generic::Scanner::scan_detect_folder(&subdir, Some(id)) {
for await event in
generic::Scanner::scan_detect_folder(&subdir, Some(id_ref.clone()))
{
yield event.map(|e| e.into());
}
}
+6 -16
View File
@@ -13,6 +13,7 @@ use tokio_stream::wrappers::ReadDirStream;
use crate::Error;
use crate::macros::{is_image_extension, is_media_extension};
use crate::scanner::{EpisodeScan, MediaRef};
/// An episode item
pub type Item = crate::Item<Scanner>;
@@ -20,25 +21,14 @@ pub type Item = crate::Item<Scanner>;
/// The scanner for epispdes
pub enum Scanner {
/// A scanned episode
Episode {
/// The ID of the show this episode belongs to
show: ShowId,
/// The season this episode belongs to
season: SeasonNumber,
/// The number(s) of this episode
episode: EpisodeNumbers,
/// The file name of the media file
media_file_name: String,
/// The file name of the poster file
poster_file_name: Option<String>,
},
Episode(EpisodeScan),
}
impl Scanner {
/// Scan a folder for an episode
pub fn scan_episode(
path: &Path,
show: ShowId,
show_ref: MediaRef<ShowId>,
season: SeasonNumber,
episode: EpisodeNumbers,
) -> impl Stream<Item = Item> {
@@ -135,13 +125,13 @@ impl Scanner {
yield Item {
path: path.to_owned(),
event: Ok(Self::Episode {
show,
event: Ok(Self::Episode(EpisodeScan {
show_ref,
season,
episode,
media_file_name,
poster_file_name,
}),
})),
};
})
}
+61 -162
View File
@@ -6,16 +6,18 @@ use std::path::Path;
use std::sync::OnceLock;
use flix_model::id::{CollectionId, MovieId, RawId, ShowId};
use flix_model::numbers::{EpisodeNumbers, SeasonNumber};
use async_stream::stream;
use either::Either;
use regex::Regex;
use tokio::fs;
use tokio_stream::Stream;
use tokio_stream::wrappers::ReadDirStream;
use crate::Error;
use crate::scanner::{collection, movie, show};
use crate::scanner::{
CollectionScan, EpisodeScan, MediaRef, MovieScan, SeasonScan, ShowScan, collection, movie, show,
};
static MEDIA_FOLDER_REGEX: OnceLock<Regex> = OnceLock::new();
static SEASON_FOLDER_REGEX: OnceLock<Regex> = OnceLock::new();
@@ -24,116 +26,28 @@ static SEASON_FOLDER_REGEX: OnceLock<Regex> = OnceLock::new();
pub type Item = crate::Item<Scanner>;
/// The scanner for collections
#[derive(Debug)]
pub enum Scanner {
/// A scanned collection
Collection {
/// The ID of the parent collection (if any)
parent: Option<CollectionId>,
/// The ID of the collection
id: CollectionId,
/// The file name of the poster file
poster_file_name: Option<String>,
},
Collection(CollectionScan),
/// A scanned movie
Movie {
/// The ID of the parent collection (if any)
parent: Option<CollectionId>,
/// The ID of the movie
id: MovieId,
/// The file name of the media file
media_file_name: String,
/// The file name of the poster file
poster_file_name: Option<String>,
},
Movie(MovieScan),
/// A scanned show
Show {
/// The ID of the parent collection (if any)
parent: Option<CollectionId>,
/// The ID of the show
id: ShowId,
/// The file name of the poster file
poster_file_name: Option<String>,
},
Show(ShowScan),
/// A scanned episode
Season {
/// The ID of the show this season belongs to
show: ShowId,
/// The season this episode belongs to
season: SeasonNumber,
/// The file name of the poster file
poster_file_name: Option<String>,
},
Season(SeasonScan),
/// A scanned episode
Episode {
/// The ID of the show this episode belongs to
show: ShowId,
/// The season this episode belongs to
season: SeasonNumber,
/// The number(s) of this episode
episode: EpisodeNumbers,
/// The file name of the media file
media_file_name: String,
/// The file name of the poster file
poster_file_name: Option<String>,
},
Episode(EpisodeScan),
}
impl From<collection::Scanner> for Scanner {
fn from(value: collection::Scanner) -> Self {
match value {
collection::Scanner::Collection {
parent,
id,
poster_file_name,
} => Self::Collection {
parent,
id,
poster_file_name,
},
collection::Scanner::Movie {
parent,
id,
media_file_name,
poster_file_name,
} => Self::Movie {
parent,
id,
media_file_name,
poster_file_name,
},
collection::Scanner::Show {
parent,
id,
poster_file_name,
} => Self::Show {
parent,
id,
poster_file_name,
},
collection::Scanner::Season {
show,
season,
poster_file_name,
} => Self::Season {
show,
season,
poster_file_name,
},
collection::Scanner::Episode {
show,
season,
episode,
media_file_name,
poster_file_name,
} => Self::Episode {
show,
season,
episode,
media_file_name,
poster_file_name,
},
collection::Scanner::Collection(c) => Self::Collection(c),
collection::Scanner::Movie(m) => Self::Movie(m),
collection::Scanner::Show(s) => Self::Show(s),
collection::Scanner::Season(s) => Self::Season(s),
collection::Scanner::Episode(e) => Self::Episode(e),
}
}
}
@@ -141,17 +55,7 @@ impl From<collection::Scanner> for Scanner {
impl From<movie::Scanner> for Scanner {
fn from(value: movie::Scanner) -> Self {
match value {
movie::Scanner::Movie {
parent,
id,
media_file_name,
poster_file_name,
} => Self::Movie {
parent,
id,
media_file_name,
poster_file_name,
},
movie::Scanner::Movie(m) => Self::Movie(m),
}
}
}
@@ -159,42 +63,22 @@ impl From<movie::Scanner> for Scanner {
impl From<show::Scanner> for Scanner {
fn from(value: show::Scanner) -> Self {
match value {
show::Scanner::Show {
parent,
id,
poster_file_name,
} => Self::Show {
parent,
id,
poster_file_name,
},
show::Scanner::Season {
show,
season,
poster_file_name,
} => Self::Season {
show,
season,
poster_file_name,
},
show::Scanner::Episode {
show,
season,
episode,
media_file_name,
poster_file_name,
} => Self::Episode {
show,
season,
episode,
media_file_name,
poster_file_name,
},
show::Scanner::Show(s) => Self::Show(s),
show::Scanner::Season(s) => Self::Season(s),
show::Scanner::Episode(e) => Self::Episode(e),
}
}
}
impl Scanner {
/// Helper function for stripping allowed numerical prefixes for sorting ("01 - ")
fn strip_numeric_prefix(mut s: &str) -> &str {
while let Some('0'..='9') = s.chars().next() {
s = &s[1..]
}
s.strip_prefix(" - ").unwrap_or(s)
}
/// Detect the type of a folder and call the correct scanner. Use
/// this only for detecting possibly ambiguous media:
/// - Collections
@@ -202,7 +86,7 @@ impl Scanner {
/// - Shows
pub fn scan_detect_folder(
path: &Path,
parent: Option<CollectionId>,
parent: Option<MediaRef<CollectionId>>,
) -> impl Stream<Item = Item> {
enum MediaType {
Collection,
@@ -211,7 +95,7 @@ impl Scanner {
}
let media_folder_re = MEDIA_FOLDER_REGEX.get_or_init(|| {
Regex::new(r"^[[[:alnum:]]' -]+ \([[:digit:]]+\) \[[[:digit:]]+\]$")
Regex::new(r"^[[[:alnum:]]' -]+ \([[:digit:]]+\)( \[[[:digit:]]+\])?$")
.unwrap_or_else(|err| panic!("regex is invalid: {err}"))
});
let season_folder_re = SEASON_FOLDER_REGEX.get_or_init(|| {
@@ -227,16 +111,23 @@ impl Scanner {
return;
};
let Some(Ok(id)) = dir_name
let dir_name = Self::strip_numeric_prefix(dir_name);
// Use the explicit ID ("[X]") if it exists, otherwise parse the folder name
let media_id = if let Some((id_str, _)) = dir_name
.split_once('[')
.and_then(|(_, s)| s.split_once(']'))
.map(|(s, _)| s.parse::<RawId>())
else {
yield Item {
path: path.to_owned(),
event: Err(Error::UnexpectedFolder),
{
let Ok(id) = id_str.parse::<RawId>() else {
yield Item {
path: path.to_owned(),
event: Err(Error::UnexpectedFolder),
};
return;
};
return;
Either::Left(id)
} else {
Either::Right(flix_model::text::normalize_fs_name(dir_name))
};
let media_type: MediaType;
@@ -306,24 +197,32 @@ impl Scanner {
match media_type {
MediaType::Collection => {
for await event in collection::Scanner::scan_collection(
path,
parent,
CollectionId::from_raw(id),
) {
let id = match media_id {
Either::Left(raw) => MediaRef::Id(CollectionId::from_raw(raw)),
Either::Right(slug) => MediaRef::Slug(slug),
};
for await event in collection::Scanner::scan_collection(path, parent, id) {
yield event.map(|e| e.into());
}
}
MediaType::Movie => {
for await event in
movie::Scanner::scan_movie(path, parent, MovieId::from_raw(id))
{
let id = match media_id {
Either::Left(raw) => MediaRef::Id(MovieId::from_raw(raw)),
Either::Right(slug) => MediaRef::Slug(slug),
};
for await event in movie::Scanner::scan_movie(path, parent, id) {
yield event.map(|e| e.into());
}
}
MediaType::Show => {
for await event in show::Scanner::scan_show(path, parent, ShowId::from_raw(id))
{
let id = match media_id {
Either::Left(raw) => MediaRef::Id(ShowId::from_raw(raw)),
Either::Right(slug) => MediaRef::Slug(slug),
};
for await event in show::Scanner::scan_show(path, parent, id) {
yield event.map(|e| e.into());
}
}
+73
View File
@@ -3,6 +3,9 @@
//! The most common scanner to use is [generic::Scanner] which will
//! automatically detect and use the appropriate scanner.
use flix_model::id::{CollectionId, MovieId, ShowId};
use flix_model::numbers::{EpisodeNumbers, SeasonNumber};
pub mod library;
pub mod generic;
@@ -14,3 +17,73 @@ pub mod movie;
pub mod episode;
pub mod season;
pub mod show;
/// A reference to a piece of media
#[derive(Debug, Clone)]
pub enum MediaRef<ID> {
/// An explicit ID
Id(ID),
/// A filesystem slug
Slug(String),
}
/// A scanned collection
#[derive(Debug)]
pub struct CollectionScan {
/// The ID of the parent collection (if any)
pub parent_ref: Option<MediaRef<CollectionId>>,
/// The ID of the collection
pub id_ref: MediaRef<CollectionId>,
/// The file name of the poster file
pub poster_file_name: Option<String>,
}
/// A scanned movie
#[derive(Debug)]
pub struct MovieScan {
/// The ID of the parent collection (if any)
pub parent_ref: Option<MediaRef<CollectionId>>,
/// The ID of the movie
pub id_ref: MediaRef<MovieId>,
/// The file name of the media file
pub media_file_name: String,
/// The file name of the poster file
pub poster_file_name: Option<String>,
}
/// A scanned show
#[derive(Debug)]
pub struct ShowScan {
/// The ID of the parent collection (if any)
pub parent_ref: Option<MediaRef<CollectionId>>,
/// The ID of the show
pub id_ref: MediaRef<ShowId>,
/// The file name of the poster file
pub poster_file_name: Option<String>,
}
/// A scanned season
#[derive(Debug)]
pub struct SeasonScan {
/// The ID of the show this season belongs to
pub show_ref: MediaRef<ShowId>,
/// The season this episode belongs to
pub season: SeasonNumber,
/// The file name of the poster file
pub poster_file_name: Option<String>,
}
/// A scanned episode
#[derive(Debug)]
pub struct EpisodeScan {
/// The ID of the show this episode belongs to
pub show_ref: MediaRef<ShowId>,
/// The season this episode belongs to
pub season: SeasonNumber,
/// The number(s) of this episode
pub episode: EpisodeNumbers,
/// The file name of the media file
pub media_file_name: String,
/// The file name of the poster file
pub poster_file_name: Option<String>,
}
+8 -16
View File
@@ -12,6 +12,7 @@ use tokio_stream::wrappers::ReadDirStream;
use crate::Error;
use crate::macros::{is_image_extension, is_media_extension};
use crate::scanner::{MediaRef, MovieScan};
/// An movie item
pub type Item = crate::Item<Scanner>;
@@ -19,24 +20,15 @@ pub type Item = crate::Item<Scanner>;
/// The scanner for movies
pub enum Scanner {
/// A scanned movie
Movie {
/// The ID of the parent collection (if any)
parent: Option<CollectionId>,
/// The ID of the movie
id: MovieId,
/// The file name of the media file
media_file_name: String,
/// The file name of the poster file
poster_file_name: Option<String>,
},
Movie(MovieScan),
}
impl Scanner {
/// Scan a folder for a movie
pub fn scan_movie(
path: &Path,
parent: Option<CollectionId>,
id: MovieId,
parent_ref: Option<MediaRef<CollectionId>>,
id_ref: MediaRef<MovieId>,
) -> impl Stream<Item = Item> {
stream!({
let dirs = match fs::read_dir(path).await {
@@ -131,12 +123,12 @@ impl Scanner {
yield Item {
path: path.to_owned(),
event: Ok(Self::Movie {
parent,
id,
event: Ok(Self::Movie(MovieScan {
parent_ref,
id_ref,
media_file_name,
poster_file_name,
}),
})),
};
})
}
+10 -40
View File
@@ -13,53 +13,23 @@ use tokio_stream::wrappers::ReadDirStream;
use crate::Error;
use crate::macros::is_image_extension;
use crate::scanner::episode;
use crate::scanner::{EpisodeScan, MediaRef, SeasonScan, episode};
/// A season item
pub type Item = crate::Item<Scanner>;
/// The scanner for seasons
pub enum Scanner {
/// A scanned season
Season(SeasonScan),
/// A scanned episode
Season {
/// The ID of the show this season belongs to
show: ShowId,
/// The season this episode belongs to
season: SeasonNumber,
/// The file name of the poster file
poster_file_name: Option<String>,
},
/// A scanned episode
Episode {
/// The ID of the show this episode belongs to
show: ShowId,
/// The season this episode belongs to
season: SeasonNumber,
/// The number(s) of this episode
episode: EpisodeNumbers,
/// The file name of the media file
media_file_name: String,
/// The file name of the poster file
poster_file_name: Option<String>,
},
Episode(EpisodeScan),
}
impl From<episode::Scanner> for Scanner {
fn from(value: episode::Scanner) -> Self {
match value {
episode::Scanner::Episode {
show,
season,
episode,
media_file_name,
poster_file_name,
} => Self::Episode {
show,
season,
episode,
media_file_name,
poster_file_name,
},
episode::Scanner::Episode(e) => Self::Episode(e),
}
}
}
@@ -68,7 +38,7 @@ impl Scanner {
/// Scan a folder for a season and its episodes
pub fn scan_season(
path: &Path,
show: ShowId,
show_ref: MediaRef<ShowId>,
season: SeasonNumber,
) -> impl Stream<Item = Item> {
stream!({
@@ -140,11 +110,11 @@ impl Scanner {
yield Item {
path: path.to_owned(),
event: Ok(Self::Season {
show,
event: Ok(Self::Season(SeasonScan {
show_ref: show_ref.clone(),
season,
poster_file_name,
}),
})),
};
for episode_dir in episode_dirs_to_scan {
@@ -207,7 +177,7 @@ impl Scanner {
for await event in episode::Scanner::scan_episode(
&episode_dir,
show,
show_ref.clone(),
season_number,
episode_numbers,
) {
+17 -60
View File
@@ -4,7 +4,7 @@ use std::ffi::OsStr;
use std::path::Path;
use flix_model::id::{CollectionId, ShowId};
use flix_model::numbers::{EpisodeNumbers, SeasonNumber};
use flix_model::numbers::SeasonNumber;
use async_stream::stream;
use tokio::fs;
@@ -13,7 +13,7 @@ use tokio_stream::wrappers::ReadDirStream;
use crate::Error;
use crate::macros::is_image_extension;
use crate::scanner::season;
use crate::scanner::{EpisodeScan, MediaRef, SeasonScan, ShowScan, season};
/// A show item
pub type Item = crate::Item<Scanner>;
@@ -21,63 +21,18 @@ pub type Item = crate::Item<Scanner>;
/// The scanner for shows
pub enum Scanner {
/// A scanned show
Show {
/// The ID of the parent collection (if any)
parent: Option<CollectionId>,
/// The ID of the show
id: ShowId,
/// The file name of the poster file
poster_file_name: Option<String>,
},
Show(ShowScan),
/// A scanned season
Season(SeasonScan),
/// A scanned episode
Season {
/// The ID of the show this season belongs to
show: ShowId,
/// The season this episode belongs to
season: SeasonNumber,
/// The file name of the poster file
poster_file_name: Option<String>,
},
/// A scanned episode
Episode {
/// The ID of the show this episode belongs to
show: ShowId,
/// The season this episode belongs to
season: SeasonNumber,
/// The number(s) of this episode
episode: EpisodeNumbers,
/// The file name of the media file
media_file_name: String,
/// The file name of the poster file
poster_file_name: Option<String>,
},
Episode(EpisodeScan),
}
impl From<season::Scanner> for Scanner {
fn from(value: season::Scanner) -> Self {
match value {
season::Scanner::Season {
show,
season,
poster_file_name,
} => Self::Season {
show,
season,
poster_file_name,
},
season::Scanner::Episode {
show,
season,
episode,
media_file_name,
poster_file_name,
} => Self::Episode {
show,
season,
episode,
media_file_name,
poster_file_name,
},
season::Scanner::Season(s) => Self::Season(s),
season::Scanner::Episode(e) => Self::Episode(e),
}
}
}
@@ -86,8 +41,8 @@ impl Scanner {
/// Scan a folder for a show and its seasons/episodes
pub fn scan_show(
path: &Path,
parent: Option<CollectionId>,
id: ShowId,
parent_ref: Option<MediaRef<CollectionId>>,
id_ref: MediaRef<ShowId>,
) -> impl Stream<Item = Item> {
stream!({
let dirs = match fs::read_dir(path).await {
@@ -158,11 +113,11 @@ impl Scanner {
yield Item {
path: path.to_owned(),
event: Ok(Self::Show {
parent,
id,
event: Ok(Self::Show(ShowScan {
parent_ref,
id_ref: id_ref.clone(),
poster_file_name,
}),
})),
};
for season_dir in season_dirs_to_scan {
@@ -185,7 +140,9 @@ impl Scanner {
continue;
};
for await event in season::Scanner::scan_season(&season_dir, id, season_number) {
for await event in
season::Scanner::scan_season(&season_dir, id_ref.clone(), season_number)
{
yield event.map(|e| e.into());
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "flix-model"
version = "0.0.16"
version = "0.0.17"
edition.workspace = true
rust-version.workspace = true
description = "Core types for flix data"
+5 -2
View File
@@ -1,6 +1,6 @@
[package]
name = "flix-tmdb"
version = "0.0.16"
version = "0.0.17"
edition.workspace = true
rust-version.workspace = true
description = "Clients and models for fetching data from TMDB"
@@ -13,13 +13,16 @@ all-features = true
rustdoc-args = ["--cfg", "docsrs"]
[dependencies]
bytes = { workspace = true }
chrono = { workspace = true, features = ["serde"] }
flix-model = { workspace = true, features = ["serde"] }
governor = { workspace = true, features = ["jitter", "std"] }
nonzero_ext = { workspace = true }
reqwest = { workspace = true, features = ["json", "query", "rustls"] }
redb = { workspace = true }
reqwest = { workspace = true, features = ["query", "rustls"] }
sea-orm = { workspace = true, optional = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
thiserror = { workspace = true }
url = { workspace = true }
url-macro = { workspace = true }
+17 -26
View File
@@ -1,25 +1,30 @@
//! Collections API
use core::time::Duration;
use std::rc::Rc;
use std::sync::RwLock;
use governor::Jitter;
use crate::Config;
use crate::api::exec_request;
use crate::model::Collection;
use crate::model::id::CollectionId;
use crate::{Cache, CachePolicy, Config};
use super::{Error, make_request};
/// TMDB Collections API client
pub struct Client {
config: Rc<Config>,
cache: Rc<dyn Cache>,
policy: Rc<RwLock<CachePolicy>>,
}
impl Client {
/// Create a new client with the given configuration
pub fn new(config: Rc<Config>) -> Self {
Self { config }
pub fn new(config: Rc<Config>, cache: Rc<dyn Cache>, policy: Rc<RwLock<CachePolicy>>) -> Self {
Self {
config,
cache,
policy,
}
}
}
@@ -30,25 +35,11 @@ impl Client {
id: impl Into<CollectionId>,
language: Option<&str>,
) -> Result<Collection, Error> {
self.config
.limiter
.until_ready_with_jitter(Jitter::new(
Duration::from_millis(0),
Duration::from_millis(50),
))
.await;
Ok(self
.config
.client
.execute(make_request(
&self.config,
&format!("/3/collection/{}", id.into().into_raw()),
language,
)?)
.await?
.error_for_status()?
.json()
.await?)
let request = make_request(
&self.config,
&format!("/3/collection/{}", id.into().into_raw()),
language,
)?;
exec_request(&self.config, &*self.cache, &self.policy, request).await
}
}
+22 -31
View File
@@ -1,27 +1,32 @@
//! Episodes API
use core::time::Duration;
use std::rc::Rc;
use std::sync::RwLock;
use flix_model::numbers::{EpisodeNumber, SeasonNumber};
use governor::Jitter;
use crate::Config;
use crate::api::exec_request;
use crate::model::Episode;
use crate::model::id::ShowId;
use crate::{Cache, CachePolicy, Config};
use super::{Error, make_request};
/// TMDB Episodes API client
pub struct Client {
config: Rc<Config>,
cache: Rc<dyn Cache>,
policy: Rc<RwLock<CachePolicy>>,
}
impl Client {
/// Create a new client with the given configuration
pub fn new(config: Rc<Config>) -> Self {
Self { config }
pub fn new(config: Rc<Config>, cache: Rc<dyn Cache>, policy: Rc<RwLock<CachePolicy>>) -> Self {
Self {
config,
cache,
policy,
}
}
}
@@ -34,30 +39,16 @@ impl Client {
episode: impl Into<EpisodeNumber>,
language: Option<&str>,
) -> Result<Episode, Error> {
self.config
.limiter
.until_ready_with_jitter(Jitter::new(
Duration::from_millis(0),
Duration::from_millis(50),
))
.await;
Ok(self
.config
.client
.execute(make_request(
&self.config,
&format!(
"/3/tv/{}/season/{}/episode/{}",
id.into().into_raw(),
season.into(),
episode.into()
),
language,
)?)
.await?
.error_for_status()?
.json()
.await?)
let request = make_request(
&self.config,
&format!(
"/3/tv/{}/season/{}/episode/{}",
id.into().into_raw(),
season.into(),
episode.into()
),
language,
)?;
exec_request(&self.config, &*self.cache, &self.policy, request).await
}
}
+65 -1
View File
@@ -1,9 +1,15 @@
//! TMDB API clients
use core::ops::Deref;
use core::time::Duration;
use std::sync::RwLock;
use governor::Jitter;
use reqwest::Request;
use reqwest::header;
use serde::de::DeserializeOwned;
use crate::Config;
use crate::{Cache, CachePolicy, Config};
pub mod collections;
pub mod episodes;
@@ -20,6 +26,9 @@ pub enum Error {
/// Reqwest error wrapper
#[error("reqwest error: {0}")]
Reqwest(#[from] reqwest::Error),
/// Json error wrapper
#[error("json error: {0}")]
Json(#[from] serde_json::Error),
}
fn make_request(config: &Config, path: &str, language: Option<&str>) -> Result<Request, Error> {
@@ -38,3 +47,58 @@ fn make_request(config: &Config, path: &str, language: Option<&str>) -> Result<R
Ok(builder.build()?)
}
async fn exec_request<T: DeserializeOwned>(
config: &Config,
cache: &dyn Cache,
policy: &RwLock<CachePolicy>,
request: Request,
) -> Result<T, Error> {
let (read_cache, write_cache) = if let Ok(guard) = policy.read() {
match guard.deref() {
CachePolicy::None => (None, None),
CachePolicy::Full => (Some(cache), Some(cache)),
CachePolicy::Read => (Some(cache), None),
CachePolicy::Update => (None, Some(cache)),
}
} else {
(None, None)
};
let path = request.url().path().to_owned();
// read the cache and fall back to reqwest
let mut response = None;
if let Some(cache) = read_cache {
response = cache.get(&path);
}
let needs_cache_write = response.is_none();
let response = match response {
Some(response) => response,
None => {
config
.limiter
.until_ready_with_jitter(Jitter::new(
Duration::from_millis(0),
Duration::from_millis(50),
))
.await;
config
.client
.execute(request)
.await?
.error_for_status()?
.bytes()
.await?
}
};
// write to the cache if needed
if let Some(cache) = write_cache
&& needs_cache_write
{
cache.set(&path, &response);
}
Ok(serde_json::from_slice(&response)?)
}
+17 -26
View File
@@ -1,25 +1,30 @@
//! Movies API
use core::time::Duration;
use std::rc::Rc;
use std::sync::RwLock;
use governor::Jitter;
use crate::Config;
use crate::api::exec_request;
use crate::model::Movie;
use crate::model::id::MovieId;
use crate::{Cache, CachePolicy, Config};
use super::{Error, make_request};
/// TMDB Movies API client
pub struct Client {
config: Rc<Config>,
cache: Rc<dyn Cache>,
policy: Rc<RwLock<CachePolicy>>,
}
impl Client {
/// Create a new client with the given configuration
pub fn new(config: Rc<Config>) -> Self {
Self { config }
pub fn new(config: Rc<Config>, cache: Rc<dyn Cache>, policy: Rc<RwLock<CachePolicy>>) -> Self {
Self {
config,
cache,
policy,
}
}
}
@@ -30,25 +35,11 @@ impl Client {
id: impl Into<MovieId>,
language: Option<&str>,
) -> Result<Movie, Error> {
self.config
.limiter
.until_ready_with_jitter(Jitter::new(
Duration::from_millis(0),
Duration::from_millis(50),
))
.await;
Ok(self
.config
.client
.execute(make_request(
&self.config,
&format!("/3/movie/{}", id.into().into_raw()),
language,
)?)
.await?
.error_for_status()?
.json()
.await?)
let request = make_request(
&self.config,
&format!("/3/movie/{}", id.into().into_raw()),
language,
)?;
exec_request(&self.config, &*self.cache, &self.policy, request).await
}
}
+17 -26
View File
@@ -1,27 +1,32 @@
//! Seasons API
use core::time::Duration;
use std::rc::Rc;
use std::sync::RwLock;
use flix_model::numbers::SeasonNumber;
use governor::Jitter;
use crate::Config;
use crate::api::exec_request;
use crate::model::Season;
use crate::model::id::ShowId;
use crate::{Cache, CachePolicy, Config};
use super::{Error, make_request};
/// TMDB Seasons API client
pub struct Client {
config: Rc<Config>,
cache: Rc<dyn Cache>,
policy: Rc<RwLock<CachePolicy>>,
}
impl Client {
/// Create a new client with the given configuration
pub fn new(config: Rc<Config>) -> Self {
Self { config }
pub fn new(config: Rc<Config>, cache: Rc<dyn Cache>, policy: Rc<RwLock<CachePolicy>>) -> Self {
Self {
config,
cache,
policy,
}
}
}
@@ -33,25 +38,11 @@ impl Client {
season: impl Into<SeasonNumber>,
language: Option<&str>,
) -> Result<Season, Error> {
self.config
.limiter
.until_ready_with_jitter(Jitter::new(
Duration::from_millis(0),
Duration::from_millis(50),
))
.await;
Ok(self
.config
.client
.execute(make_request(
&self.config,
&format!("/3/tv/{}/season/{}", id.into().into_raw(), season.into()),
language,
)?)
.await?
.error_for_status()?
.json()
.await?)
let request = make_request(
&self.config,
&format!("/3/tv/{}/season/{}", id.into().into_raw(), season.into()),
language,
)?;
exec_request(&self.config, &*self.cache, &self.policy, request).await
}
}
+17 -26
View File
@@ -1,25 +1,30 @@
//! Shows API
use core::time::Duration;
use std::rc::Rc;
use std::sync::RwLock;
use governor::Jitter;
use crate::Config;
use crate::api::exec_request;
use crate::model::Show;
use crate::model::id::ShowId;
use crate::{Cache, CachePolicy, Config};
use super::{Error, make_request};
/// TMDB Shows API client
pub struct Client {
config: Rc<Config>,
cache: Rc<dyn Cache>,
policy: Rc<RwLock<CachePolicy>>,
}
impl Client {
/// Create a new client with the given configuration
pub fn new(config: Rc<Config>) -> Self {
Self { config }
pub fn new(config: Rc<Config>, cache: Rc<dyn Cache>, policy: Rc<RwLock<CachePolicy>>) -> Self {
Self {
config,
cache,
policy,
}
}
}
@@ -30,25 +35,11 @@ impl Client {
id: impl Into<ShowId>,
language: Option<&str>,
) -> Result<Show, Error> {
self.config
.limiter
.until_ready_with_jitter(Jitter::new(
Duration::from_millis(0),
Duration::from_millis(50),
))
.await;
Ok(self
.config
.client
.execute(make_request(
&self.config,
&format!("/3/tv/{}", id.into().into_raw()),
language,
)?)
.await?
.error_for_status()?
.json()
.await?)
let request = make_request(
&self.config,
&format!("/3/tv/{}", id.into().into_raw()),
language,
)?;
exec_request(&self.config, &*self.cache, &self.policy, request).await
}
}
+83
View File
@@ -0,0 +1,83 @@
use std::path::Path;
use std::time::{SystemTime, UNIX_EPOCH};
use bytes::Bytes;
use redb::{Database, DatabaseError, ReadableDatabase, TableDefinition};
/// The client cache policy
pub enum CachePolicy {
/// Do not use a cache
None,
/// Use and update the cache
Full,
/// Use the cache but don't update it
Read,
/// Ignore the cache but update it
Update,
}
/// The trait representing a caching backend
pub trait Cache {
/// Get a cached value, or None
fn get(&self, query: &str) -> Option<Bytes>;
/// Set a value in the cache
fn set(&self, query: &str, response: &Bytes);
}
const TABLE: TableDefinition<&str, (u64, &[u8])> = TableDefinition::new("tmdb_responses");
/// A [Cache] implementation using [redb] as the backend
pub struct RedbCache {
db: Database,
}
impl RedbCache {
/// Create/open a [redb] database at the path
pub fn new(path: &Path) -> Result<Self, DatabaseError> {
Ok(Self {
db: Database::create(path)?,
})
}
/// Helper function allowing for `.ok()?`
fn write(&self, timestamp: u64, query: &str, response: &Bytes) -> Option<()> {
let write_txn = self.db.begin_write().ok()?;
{
let mut table = write_txn.open_table(TABLE).ok()?;
table
.insert(query, (timestamp, response.iter().as_slice()))
.ok()?;
}
write_txn.commit().ok()
}
}
impl Cache for RedbCache {
fn get(&self, query: &str) -> Option<Bytes> {
let read_txn = self.db.begin_read().ok()?;
let table = read_txn.open_table(TABLE).ok()?;
let result = table.get(query).ok()??;
let (timestamp, data) = result.value();
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0);
if now.saturating_sub(timestamp) >= 60 * 60 * 24 * 30 * 6 {
None
} else {
Some(Bytes::copy_from_slice(data))
}
}
fn set(&self, query: &str, response: &Bytes) {
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0);
self.write(now, query, response);
}
}
+33 -13
View File
@@ -1,6 +1,7 @@
use std::rc::Rc;
use std::sync::RwLock;
use crate::{Config, api};
use crate::{Cache, CachePolicy, Config, api};
/// The primary client that references all other clients
pub struct Client {
@@ -9,23 +10,42 @@ pub struct Client {
shows: api::shows::Client,
seasons: api::seasons::Client,
episodes: api::episodes::Client,
cache_policy: Rc<RwLock<CachePolicy>>,
}
impl Client {
/// Create a new client from a default configuration using the bearer token
pub fn new(bearer_token: String) -> Self {
Self::new_with_config(Config::new(bearer_token))
/// Create a new client with the given configuration
pub fn new(config: Config, cache: Rc<dyn Cache>, cache_policy: CachePolicy) -> Self {
let config = Rc::new(config);
let cache_policy = Rc::new(RwLock::new(cache_policy));
Self {
collections: api::collections::Client::new(
config.clone(),
cache.clone(),
cache_policy.clone(),
),
movies: api::movies::Client::new(config.clone(), cache.clone(), cache_policy.clone()),
shows: api::shows::Client::new(config.clone(), cache.clone(), cache_policy.clone()),
seasons: api::seasons::Client::new(config.clone(), cache.clone(), cache_policy.clone()),
episodes: api::episodes::Client::new(
config.clone(),
cache.clone(),
cache_policy.clone(),
),
cache_policy,
}
}
/// Create a new client with the given configuration
pub fn new_with_config(config: Config) -> Self {
let config = Rc::new(config);
Self {
collections: api::collections::Client::new(config.clone()),
movies: api::movies::Client::new(config.clone()),
shows: api::shows::Client::new(config.clone()),
seasons: api::seasons::Client::new(config.clone()),
episodes: api::episodes::Client::new(config.clone()),
/// Modify the [CachePolicy]
pub fn set_cache_policy(&self, new_policy: CachePolicy) {
match self.cache_policy.write() {
Ok(mut policy) => *policy = new_policy,
Err(mut poison) => {
**poison.get_mut() = new_policy;
self.cache_policy.clear_poison();
}
}
}
}
+3
View File
@@ -5,6 +5,9 @@
pub mod api;
pub mod model;
mod cache;
pub use cache::{Cache, CachePolicy, RedbCache};
mod client;
pub use client::Client;