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,10 +15,11 @@ categories = ["command-line-utilities"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
torrent-name-parser = "0.11.0" torrent-name-parser = "0.12.1"
tmdb = "3.0.0" tmdb-api = "0.4.0"
inquire = "0.5.3" inquire = "0.6.2"
load_file = "1.0.1" load_file = "1.0.1"
tokio = { version = "1.28.1", features = ["macros", "rt-multi-thread"] }
[profile.release] [profile.release]
strip = true strip = true

View file

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

View file

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

View file

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