Switched to tmdb_api

This commit is contained in:
Sayantan Santra 2023-05-15 17:10:51 -05:00
parent 14e5899f36
commit 1a83f88c0b
5 changed files with 535 additions and 1093 deletions

1530
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -15,14 +15,15 @@ categories = ["command-line-utilities"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
torrent-name-parser = "0.11.0"
tmdb = "3.0.0"
inquire = "0.5.3"
torrent-name-parser = "0.12.1"
tmdb-api = "0.4.0"
inquire = "0.6.2"
load_file = "1.0.1"
tokio = { version = "1.28.1", features = ["macros", "rt-multi-thread"] }
[profile.release]
strip = true
opt-level = "z"
lto = true
codegen-units = 1
panic = "abort"
panic = "abort"

View file

@ -3,7 +3,11 @@ use inquire::{
Select,
};
use std::{collections::HashMap, fs, path::Path, process::exit};
use tmdb::{model::*, themoviedb::*};
use tmdb_api::{
movie::{credits::MovieCredits, search::MovieSearch},
prelude::Command,
Client,
};
use torrent_name_parser::Metadata;
use crate::structs::{get_long_lang, Language, MovieEntry};
@ -12,9 +16,9 @@ use crate::structs::{get_long_lang, Language, MovieEntry};
const VERSION: &str = env!("CARGO_PKG_VERSION");
// Function to process movie entries
pub fn process_file(
pub async fn process_file(
filename: &String,
tmdb: &TMDb,
tmdb: &Client,
pattern: &str,
dry_run: bool,
) -> (String, bool) {
@ -43,40 +47,37 @@ pub fn process_file(
}
// Search using the TMDb API
let mut search = tmdb.search();
search.title(metadata.title());
let year = metadata.year().map(|y| y as u16);
let search = MovieSearch::new(metadata.title().to_string()).with_year(year);
let reply = search.execute(tmdb).await;
// Check if year is present in filename
if let Some(year) = metadata.year() {
search.year(year as u64);
}
let mut results = Vec::new();
if let Ok(search_results) = search.execute() {
results = search_results.results;
} else {
eprintln!(" There was an error while searching {}!", file_base);
}
let results = match reply {
Ok(res) => Ok(res.results),
Err(e) => {
eprintln!(" There was an error while searching {}!", file_base);
Err(e)
}
};
let mut movie_list: Vec<MovieEntry> = Vec::new();
// Create movie entry from the result
for result in results {
let mut movie_details = MovieEntry::from(result);
// Get director's name, if needed
if pattern.contains("{director}") {
let with_credits: Result<Movie, _> =
tmdb.fetch().id(movie_details.id).append_credits().execute();
if let Ok(movie) = with_credits {
if let Some(cre) = movie.credits {
let mut directors = cre.crew;
directors.retain(|x| x.job == "Director");
for person in directors {
movie_details.director = person.name;
if results.is_ok() {
for result in results.unwrap() {
let mut movie_details = MovieEntry::from(result);
// Get director's name, if needed
if pattern.contains("{director}") {
let credits_search = MovieCredits::new(movie_details.id);
let credits_reply = credits_search.execute(tmdb).await;
if credits_reply.is_ok() {
let mut crew = credits_reply.unwrap().crew;
crew.retain(|x| x.job == *"Director");
for person in crew {
movie_details.director = person.person.name;
}
}
}
movie_list.push(movie_details);
}
movie_list.push(movie_details);
}
// If nothing is found, skip

View file

@ -1,13 +1,14 @@
use load_file::{self, load_str};
use std::{env, fs, path::Path, process::exit};
use tmdb::themoviedb::*;
use tmdb_api::Client;
// Import all the modules
mod functions;
use functions::{process_args, process_file};
mod structs;
fn main() {
#[tokio::main]
async fn main() {
// Read arguments from commandline
let args: Vec<String> = env::args().collect();
@ -37,10 +38,7 @@ fn main() {
}
// Create TMDb object for API calls
let tmdb = TMDb {
api_key,
language: "en",
};
let tmdb = Client::new(api_key.to_string());
// Iterate over entries
for entry in entries {
@ -51,7 +49,7 @@ fn main() {
false => {
if Path::new(entry.as_str()).is_file() {
// Process the filename for movie entries
process_file(&entry, &tmdb, pattern, settings["dry_run"]);
process_file(&entry, &tmdb, pattern, settings["dry_run"]).await;
} else {
eprintln!("The file {} wasn't found on disk, skipping...", entry);
continue;
@ -71,7 +69,8 @@ fn main() {
&tmdb,
pattern,
settings["dry_run"],
);
)
.await;
if movie_name_temp == *"n/a" {
continue;

View file

@ -1,5 +1,5 @@
use std::fmt;
use tmdb::model::*;
use tmdb_api::movie::MovieShort;
// Struct for movie entries
pub struct MovieEntry {
@ -12,13 +12,16 @@ pub struct MovieEntry {
impl MovieEntry {
// Create movie entry from results
pub fn from(movie: SearchMovie) -> MovieEntry {
pub fn from(movie: MovieShort) -> MovieEntry {
MovieEntry {
title: movie.title,
id: movie.id,
title: movie.inner.title,
id: movie.inner.id,
director: String::from("N/A"),
year: String::from(movie.release_date.split('-').next().unwrap_or("N/A")),
language: get_long_lang(movie.original_language.as_str()),
year: match movie.inner.release_date {
Some(date) => date.format("%Y").to_string(),
_ => "N/A".to_string(),
},
language: get_long_lang(movie.inner.original_language.as_str()),
}
}