4 Commits

21 changed files with 547 additions and 373 deletions
Generated
+220 -280
View File
File diff suppressed because it is too large Load Diff
+3 -2
View File
@@ -34,12 +34,13 @@ overflow-checks = true
strip = "debuginfo"
[workspace.dependencies]
flix = { path = "crates/flix", version = "=0.0.4", default-features = false }
flix-tmdb = { path = "crates/tmdb", version = "=0.0.4", default-features = false }
flix = { path = "crates/flix", version = "=0.0.8", default-features = false }
flix-tmdb = { path = "crates/tmdb", version = "=0.0.8", default-features = false }
anyhow = { version = "^1", default-features = false }
chrono = { version = "^0.4", default-features = false }
clap = { version = "^4", default-features = false, features = ["std"] }
futures = { version = "^0.3", default-features = false }
home = { version = "^0.5", default-features = false }
reqwest = { version = "^0.12", default-features = false }
serde = { version = "^1", default-features = false }
+2 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "flix-cli"
version = "0.0.4"
version = "0.0.8"
categories = ["command-line-utilities"]
description = "CLI for interacting with flix media"
@@ -48,5 +48,6 @@ clap = { workspace = true, features = [
] }
home = { workspace = true }
serde = { workspace = true, features = ["derive"] }
futures = { workspace = true }
tokio = { workspace = true, features = ["rt", "fs", "macros"] }
toml = { workspace = true, features = ["display", "parse"] }
+22 -13
View File
@@ -7,9 +7,9 @@ pub mod tmdb;
#[derive(Parser)]
#[command(version, about, long_about = None)]
pub struct Cli {
/// Use a custom config file [default: ~/.flix]
#[arg(short, long, value_name = "FILE")]
config: Option<PathBuf>,
/// Use a custom config file
#[arg(short, long, value_name = "FILE", default_value = "~/.flix")]
config: PathBuf,
#[command(subcommand)]
command: Command,
@@ -17,17 +17,14 @@ pub struct Cli {
impl Cli {
pub fn config_path(&self) -> PathBuf {
match self.config.as_ref() {
Some(path) => match path.strip_prefix("~/") {
Ok(path) => expect_home_dir().join(path),
Err(_) => path.to_owned(),
},
None => expect_home_dir().join(".flix"),
match self.config.strip_prefix("~/") {
Ok(path) => expect_home_dir().join(path),
Err(_) => self.config.to_owned(),
}
}
pub fn command(&self) -> &Command {
&self.command
pub fn command(self) -> Command {
self.command
}
}
@@ -45,12 +42,18 @@ pub enum Command {
force: bool,
/// Change the destination
#[arg(short, long, value_name = "FILE")]
output: Option<PathBuf>,
#[arg(short, long, value_name = "FILE", default_value = "flix.toml")]
output: PathBuf,
#[command(subcommand)]
command: BackendCommand,
},
/// Update a flix manifest
Update {
/// Change the destination
#[arg(short, long, value_name = "FILE", default_value = "flix.toml")]
output: PathBuf,
},
}
#[derive(Subcommand)]
@@ -62,6 +65,12 @@ pub enum BackendCommand {
},
}
impl From<tmdb::Command> for BackendCommand {
fn from(value: tmdb::Command) -> Self {
Self::Tmdb { command: value }
}
}
fn expect_home_dir() -> PathBuf {
#[allow(clippy::expect_used)]
home::home_dir().expect("you do not have a home directory")
+3
View File
@@ -25,6 +25,7 @@ pub enum Command {
season: u32,
},
/// Process a TMDB episode
#[command(trailing_var_arg = true)]
Episode {
#[arg(value_name = "TMDB_ID")]
id: u32,
@@ -32,5 +33,7 @@ pub enum Command {
season: u32,
#[arg(value_name = "EPISODE_NUM")]
episode: u32,
#[arg(value_name = "...")]
episodes: Vec<u32>,
},
}
+55 -31
View File
@@ -5,15 +5,16 @@ use flix_tmdb::Client;
use anyhow::{Context, Result};
use clap::Parser;
use tokio::fs;
use tokio::io::AsyncWriteExt;
mod cli;
use cli::{BackendCommand, Cli, Command};
mod config;
use config::Config;
use tokio::io::AsyncWriteExt;
mod run;
use run::flix::FlixObject;
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<()> {
@@ -27,41 +28,64 @@ async fn main() -> Result<()> {
let client = Client::new(config.tmdb().bearer_token().to_owned());
match cli.command() {
Command::Print { command } => match command {
BackendCommand::Tmdb { command } => {
let object = run::tmdb::TmdbObject::fetch(&client, command).await?;
println!("{}", object.serialize().context("failed to serialize")?)
}
},
Command::Print { command } => exec_print(client, command).await?,
Command::Write {
force,
output,
command,
} => match command {
BackendCommand::Tmdb { command } => {
let object = run::tmdb::TmdbObject::fetch(&client, command).await?;
let output = output
.as_ref()
.map(|p| p.as_path())
.unwrap_or(Path::new("flix.toml"));
let mut file = if *force {
fs::File::create(output).await
} else {
fs::File::create_new(output).await
}
.with_context(|| format!("could not create file at path {}", output.display()))?;
file.write_all(
object
.serialize()
.context("failed to serialize tmdb object")?
.as_bytes(),
)
.await
.with_context(|| format!("could not write to file at path {}", output.display()))?;
}
},
} => exec_write(client, force, &output, command).await?,
Command::Update { output } => exec_update(client, &output).await?,
}
Ok(())
}
async fn exec_print(client: Client, command: BackendCommand) -> Result<()> {
match command {
BackendCommand::Tmdb { command } => {
let object = run::tmdb::TmdbObject::fetch(&client, command).await?;
println!("{}", object.serialize().context("failed to serialize")?)
}
}
Ok(())
}
async fn exec_write(
client: Client,
force: bool,
output: &Path,
command: BackendCommand,
) -> Result<()> {
match command {
BackendCommand::Tmdb { command } => {
let object = run::tmdb::TmdbObject::fetch(&client, command).await?;
let mut file = if force {
fs::File::create(output).await
} else {
fs::File::create_new(output).await
}
.with_context(|| format!("could not create file at path {}", output.display()))?;
file.write_all(
object
.serialize()
.context("failed to serialize tmdb object")?
.as_bytes(),
)
.await
.with_context(|| format!("could not write to file at path {}", output.display()))?;
}
}
Ok(())
}
async fn exec_update(client: Client, output: &Path) -> Result<()> {
let content = fs::read_to_string(output)
.await
.with_context(|| format!("failed to read file at path: {}", output.display()))?;
let object: FlixObject = toml::from_str(&content)
.with_context(|| format!("failed to deserialize flix file: {}", output.display()))?;
let command = object.backend_command()?;
exec_write(client, true, output, command).await
}
+75
View File
@@ -0,0 +1,75 @@
use flix::model::{Collection, Episode, Movie, Season, Show, Verse};
use anyhow::{Result, anyhow, bail};
use crate::cli::BackendCommand;
use crate::cli::tmdb;
#[derive(Debug, serde::Deserialize)]
#[serde(untagged)]
pub enum FlixObject {
Collection(Collection),
Movie(Movie),
Show(Show),
Season(Season),
Episode(Episode),
// DEAD CODE: The TMDB backend does not support Verses
#[allow(dead_code)]
Verse(Verse),
}
impl FlixObject {
pub fn backend_command(&self) -> Result<BackendCommand> {
Ok(match self {
FlixObject::Collection(collection) => {
let Some(ref tmdb) = collection.tmdb else {
bail!("missing tmdb data")
};
tmdb::Command::Collection { id: tmdb.id.into() }.into()
}
FlixObject::Movie(movie) => {
let Some(ref tmdb) = movie.tmdb else {
bail!("missing tmdb data")
};
tmdb::Command::Movie { id: tmdb.id.into() }.into()
}
FlixObject::Show(show) => {
let Some(ref tmdb) = show.tmdb else {
bail!("missing tmdb data")
};
tmdb::Command::Show { id: tmdb.id.into() }.into()
}
FlixObject::Season(season) => {
let Some(ref tmdb) = season.tmdb else {
bail!("missing tmdb data")
};
tmdb::Command::Season {
id: tmdb.show_id.into(),
season: season.season.number,
}
.into()
}
FlixObject::Episode(episode) => {
let Some(ref tmdb) = episode.tmdb else {
bail!("missing tmdb data")
};
tmdb::Command::Episode {
id: tmdb.show_id.into(),
season: episode.episode.season,
episode: episode
.episode
.number
.primary_episode_number()
.ok_or_else(|| {
anyhow!("the episode does not have a primary episode number")
})?,
episodes: episode.episode.number.additional_episode_numbers(),
}
.into()
}
FlixObject::Verse(_) => {
bail!("verses are not handled")
}
})
}
}
+1
View File
@@ -1 +1,2 @@
pub mod flix;
pub mod tmdb;
+57 -23
View File
@@ -1,14 +1,16 @@
use flix::model::{
Collection, Episode, GenericCollection, GenericEpisode, GenericMovie, GenericSeason,
GenericShow, Movie, Season, Show, TmdbCollection, TmdbMovie, TmdbShow,
Collection, Episode, EpisodeNumber, GenericCollection, GenericEpisode, GenericMovie,
GenericSeason, GenericShow, Movie, Season, Show, TmdbCollection, TmdbEpisode, TmdbMovie,
TmdbSeason, TmdbShow,
};
use flix_tmdb::Client;
use flix_tmdb::model::{
Collection as TCollection, Episode as TEpisode, Movie as TMovie, Season as TSeason,
Show as TShow,
Show as TShow, ShowId,
};
use anyhow::{Context, Result};
use futures::{StreamExt, TryStreamExt, stream};
use crate::cli::tmdb::Command;
@@ -16,8 +18,8 @@ pub enum TmdbObject {
Collection(TCollection),
Movie(TMovie),
Show(TShow),
Season(TSeason),
Episode(TEpisode),
Season(TSeason, ShowId),
Episode(TEpisode, Vec<u32>, u32, ShowId),
}
impl TmdbObject {
@@ -25,7 +27,7 @@ impl TmdbObject {
Ok(match self {
TmdbObject::Collection(tmdb) => toml::to_string(&Collection {
collection: GenericCollection {
name: tmdb.name,
title: tmdb.title,
overview: tmdb.overview,
},
tmdb: Some(TmdbCollection { id: tmdb.id }),
@@ -44,7 +46,7 @@ impl TmdbObject {
})?,
TmdbObject::Show(tmdb) => toml::to_string(&Show {
show: GenericShow {
name: tmdb.name,
title: tmdb.title,
overview: tmdb.overview,
genres: tmdb.genres.iter().cloned().map(|g| g.name).collect(),
air_date: tmdb.first_air_date,
@@ -54,29 +56,43 @@ impl TmdbObject {
genres: tmdb.genres.iter().map(|g| g.id).collect(),
}),
})?,
TmdbObject::Season(tmdb) => toml::to_string(&Season {
TmdbObject::Season(tmdb, show_id) => toml::to_string(&Season {
season: GenericSeason {
number: tmdb.season_number,
name: tmdb.name,
overview: tmdb.overview,
air_date: tmdb.air_date,
},
})?,
TmdbObject::Episode(tmdb) => toml::to_string(&Episode {
episode: GenericEpisode {
number: tmdb.episode_number,
name: tmdb.name,
title: tmdb.title,
overview: tmdb.overview,
air_date: tmdb.air_date,
},
tmdb: Some(TmdbSeason { show_id }),
})?,
TmdbObject::Episode(tmdb, mut episode_numbers, season_number, show_id) => {
toml::to_string(&Episode {
episode: GenericEpisode {
number: if episode_numbers.is_empty() {
EpisodeNumber::Single {
number: tmdb.episode_number,
}
} else {
episode_numbers.insert(0, tmdb.episode_number);
EpisodeNumber::Multiple {
numbers: episode_numbers,
}
},
season: season_number,
title: tmdb.title,
overview: tmdb.overview,
air_date: tmdb.air_date,
},
tmdb: Some(TmdbEpisode { show_id }),
})?
}
})
}
}
impl TmdbObject {
pub async fn fetch(client: &Client, command: &Command) -> Result<Self> {
Ok(match *command {
pub async fn fetch(client: &Client, command: Command) -> Result<Self> {
Ok(match command {
Command::Collection { id } => Self::Collection(
client
.collections()
@@ -104,20 +120,38 @@ impl TmdbObject {
.get_details(id, season, None)
.await
.with_context(|| format!("could not get show details for '{id}' S{season}"))?,
id.into(),
),
Command::Episode {
id,
season,
episode,
} => Self::Episode(
client
episodes,
} => {
let mut episode = client
.episodes()
.get_details(id, season, episode, None)
.await
.with_context(|| {
format!("could not get show details for '{id}' S{season}E{episode}")
})?,
),
})?;
let title = stream::once(async { Ok(episode.title) })
.chain(stream::iter(episodes.clone()).then(|episode| async move {
client
.episodes()
.get_details(id, season, episode, None)
.await
.with_context(|| {
format!("could not get show details for '{id}' S{season}E{episode}")
})
.map(|episode| episode.title)
}))
.try_collect::<Vec<_>>()
.await?
.join(" + ");
episode.title = title;
Self::Episode(episode, episodes, season, id.into())
}
})
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "flix"
version = "0.0.4"
version = "0.0.8"
categories = []
description = "Types for storing persistent data about media"
+2 -2
View File
@@ -15,8 +15,8 @@ pub struct Collection {
/// The generic collection data
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct GenericCollection {
/// The collection's name
pub name: String,
/// The collection's title
pub title: String,
/// The collection's overview
pub overview: String,
}
+56 -4
View File
@@ -1,3 +1,6 @@
#[cfg(feature = "tmdb")]
use flix_tmdb::model::ShowId;
use chrono::NaiveDate;
/// An Episode container
@@ -5,17 +8,66 @@ use chrono::NaiveDate;
pub struct Episode {
/// The generic episode data
pub episode: GenericEpisode,
/// The TMDB episode data
#[cfg(feature = "tmdb")]
pub tmdb: Option<TmdbEpisode>,
}
/// A wrapper for handling single and multi-episode entries
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[serde(untagged)]
pub enum EpisodeNumber {
/// The entry contains a single episode
Single {
/// The episode's number
number: u32,
},
/// The entry contains multiple episodes
Multiple {
/// The list of episode numbers
numbers: Vec<u32>,
},
}
impl EpisodeNumber {
/// Get the primary episode number of this episode
pub fn primary_episode_number(&self) -> Option<u32> {
match self {
EpisodeNumber::Single { number } => Some(*number),
EpisodeNumber::Multiple { numbers } => numbers.first().copied(),
}
}
/// Get additional episode numbers of this episode
pub fn additional_episode_numbers(&self) -> Vec<u32> {
match self {
EpisodeNumber::Single { number: _ } => vec![],
EpisodeNumber::Multiple { numbers } => numbers.iter().skip(1).copied().collect(),
}
}
}
/// The generic episode data
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct GenericEpisode {
/// The episode's number
pub number: u32,
/// The episode's name
pub name: String,
/// The episode's number(s)
#[serde(flatten)]
pub number: EpisodeNumber,
/// The episode's season's number
pub season: u32,
/// The episode's title
pub title: String,
/// The episode's overview
pub overview: String,
/// The episode's air date
pub air_date: NaiveDate,
}
/// The TMDB show data
#[cfg(feature = "tmdb")]
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct TmdbEpisode {
/// The episodes's show's TMDB ID
pub show_id: ShowId,
}
+17 -2
View File
@@ -1,3 +1,6 @@
#[cfg(feature = "tmdb")]
use flix_tmdb::model::ShowId;
use chrono::NaiveDate;
/// A Season container
@@ -5,6 +8,10 @@ use chrono::NaiveDate;
pub struct Season {
/// The generic season data
pub season: GenericSeason,
/// The TMDB season data
#[cfg(feature = "tmdb")]
pub tmdb: Option<TmdbSeason>,
}
/// The generic season data
@@ -12,10 +19,18 @@ pub struct Season {
pub struct GenericSeason {
/// The season's number
pub number: u32,
/// The season's name
pub name: String,
/// The season's title
pub title: String,
/// The season's overview
pub overview: String,
/// The season's air date
pub air_date: NaiveDate,
}
/// The TMDB show data
#[cfg(feature = "tmdb")]
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct TmdbSeason {
/// The season's show's TMDB ID
pub show_id: ShowId,
}
+2 -2
View File
@@ -17,8 +17,8 @@ pub struct Show {
/// The generic show data
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct GenericShow {
/// The show's name
pub name: String,
/// The show's title
pub title: String,
/// The show's overview
pub overview: String,
/// The show's genres
+11 -3
View File
@@ -1,5 +1,5 @@
#[cfg(feature = "tmdb")]
use flix_tmdb::model::{MovieId, ShowId};
use flix_tmdb::model::{CollectionId, MovieId, ShowId};
/// A Verse container
#[derive(Debug, serde::Serialize, serde::Deserialize)]
@@ -15,8 +15,8 @@ pub struct Verse {
/// The generic verse data
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct GenericVerse {
/// The verse's name
pub name: String,
/// The verse's title
pub title: String,
/// The verse's overview
pub overview: String,
}
@@ -25,8 +25,16 @@ pub struct GenericVerse {
#[cfg(feature = "tmdb")]
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct TmdbVerse {
/// The list of collection TMDB IDs in the verse
#[serde(default)]
pub collections: Vec<CollectionId>,
/// The list of movie TMDB IDs in the verse
#[serde(default)]
pub movies: Vec<MovieId>,
/// The list of show TMDB IDs in the verse
#[serde(default)]
pub shows: Vec<ShowId>,
/// The list of sub-verse names
#[serde(default)]
pub verses: Vec<String>,
}
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "flix-tmdb"
version = "0.0.4"
version = "0.0.8"
categories = []
description = "Clients and models for fetching data from TMDB"
+3 -2
View File
@@ -5,8 +5,9 @@ use super::{CollectionId, MovieId};
pub struct Collection {
/// The collection's TMDB ID
pub id: CollectionId,
/// The collection's name
pub name: String,
/// The collection's title
#[serde(rename = "name")]
pub title: String,
/// The collection's overview
pub overview: String,
/// The list of movies that are part of this collection
+3 -2
View File
@@ -5,8 +5,9 @@ use chrono::NaiveDate;
pub struct Episode {
/// The episode's number
pub episode_number: u32,
/// The episode's name
pub name: String,
/// The episode's title
#[serde(rename = "name")]
pub title: String,
/// The episode's overview
pub overview: String,
/// The episode's air date
+7
View File
@@ -1,4 +1,5 @@
use core::fmt;
use core::hash::{Hash, Hasher};
use core::marker::PhantomData;
/// The TMDB ID type of a movie genre
@@ -81,3 +82,9 @@ impl<T> PartialEq for TmdbId<T> {
}
impl<T> Eq for TmdbId<T> {}
impl<T> Hash for TmdbId<T> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.inner.hash(state);
}
}
+3 -2
View File
@@ -7,8 +7,9 @@ use super::Episode;
pub struct Season {
/// The season's number
pub season_number: u32,
/// The season's name
pub name: String,
/// The season's title
#[serde(rename = "name")]
pub title: String,
/// The season's overview
pub overview: String,
/// The season's air date
+3 -2
View File
@@ -7,8 +7,9 @@ use super::{ShowGenre, ShowId};
pub struct Show {
/// The show's TMDB ID
pub id: ShowId,
/// The show's name
pub name: String,
/// The show's title
#[serde(rename = "name")]
pub title: String,
/// The show's overview
pub overview: String,
/// The list of genres this show belongs to