From d1fc65f6284dd61c894458b29a79b3fe41185a9a Mon Sep 17 00:00:00 2001 From: metamuffin Date: Tue, 13 Jun 2023 22:20:09 +0200 Subject: tmdb import --- tools/src/tmdb.rs | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 tools/src/tmdb.rs (limited to 'tools/src/tmdb.rs') diff --git a/tools/src/tmdb.rs b/tools/src/tmdb.rs new file mode 100644 index 0000000..c17adab --- /dev/null +++ b/tools/src/tmdb.rs @@ -0,0 +1,91 @@ +use std::io::Write; + +use log::info; +use serde::Deserialize; + +#[derive(Debug, Clone, Deserialize)] +pub struct TmdbQuery { + pub page: usize, + pub results: Vec, + pub total_pages: usize, + pub total_results: usize, +} + +#[derive(Debug, Clone, Deserialize)] +pub struct TmdbQueryResult { + pub adult: bool, + pub backdrop_path: String, + pub genre_ids: Vec, + pub id: u64, + pub original_language: String, + pub original_title: String, + pub overview: String, + pub popularity: f64, + pub poster_path: String, + pub release_date: String, + pub title: String, + pub video: bool, + pub vote_average: f64, + pub vote_count: usize, +} + +#[derive(Debug, Clone, Deserialize)] +pub struct TmdbDetails { + pub adult: bool, + pub backdrop_path: Option, + pub genres: Vec, + pub id: u64, + pub original_language: String, + pub original_title: String, + pub overview: String, + pub popularity: f64, + pub poster_path: Option, + pub release_date: String, + pub title: String, + pub video: bool, + pub vote_average: f64, + pub vote_count: usize, + pub budget: usize, + pub homepage: String, + pub imdb_id: String, + pub production_companies: Vec, + pub revenue: usize, + pub tagline: String, +} + +#[derive(Debug, Clone, Deserialize)] +pub struct TmdbGenre { + pub id: u64, + pub name: String, +} + +#[derive(Debug, Clone, Deserialize)] +pub struct TmdbProductionCompany { + pub id: u64, + pub name: String, + pub logo_path: Option, +} + +pub fn tmdb_search(query: &str, key: &str) -> anyhow::Result { + info!("searching tmdb: {query:?}"); + Ok(reqwest::blocking::get(&format!( + "https://api.themoviedb.org/3/search/movie?query={}&api_key={key}", + query.replace(" ", "+") + ))? + .json::()?) +} + +pub fn tmdb_details(id: u64, key: &str) -> anyhow::Result { + info!("fetching details: {id:?}"); + Ok(reqwest::blocking::get(&format!( + "https://api.themoviedb.org/3/movie/{id}?api_key={key}" + ))? + .json()?) +} + +pub fn tmdb_image(path: &str, out: &mut impl Write) -> anyhow::Result<()> { + info!("downloading image {path:?}"); + let mut res = reqwest::blocking::get(&format!("https://image.tmdb.org/t/p/original{path}"))?; + res.copy_to(out)?; + Ok(()) +} -- cgit v1.2.3-70-g09d2