add the imdb url for the movie

This commit is contained in:
Joe Ardent 2024-01-15 16:00:24 -08:00
parent 3caefad767
commit 785913fdba
2 changed files with 23 additions and 20 deletions

View file

@ -2,7 +2,7 @@ use std::{ffi::OsString, time::Duration};
use clap::Parser; use clap::Parser;
use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions}; use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions};
use what2watch::{get_db_pool, import_utils::add_omega_watches}; use what2watch::{get_db_pool, import_utils::add_imdb_movies};
#[derive(Debug, Parser)] #[derive(Debug, Parser)]
struct Cli { struct Cli {
@ -11,7 +11,7 @@ struct Cli {
#[clap( #[clap(
long, long,
short, short,
help = "Number of movies to add in the benchmark.", help = "Number of movies to add.",
default_value_t = 10_000 default_value_t = 10_000
)] )]
pub number: u32, pub number: u32,
@ -46,7 +46,7 @@ fn main() {
.unwrap(); .unwrap();
rt.block_on(async { rt.block_on(async {
let dur = add_omega_watches(&w2w_db, &movie_db, num).await.unwrap(); let dur = add_imdb_movies(&w2w_db, &movie_db, num).await.unwrap();
let rows: i32 = sqlx::query_scalar("select count(*) from watches") let rows: i32 = sqlx::query_scalar("select count(*) from watches")
.fetch_one(&w2w_db) .fetch_one(&w2w_db)
.await .await

View file

@ -7,8 +7,6 @@ use crate::{util::year_to_epoch, ShowKind, User, Watch, WatchQuest};
const USER_EXISTS_QUERY: &str = "select count(*) from users where id = $1"; const USER_EXISTS_QUERY: &str = "select count(*) from users where id = $1";
const MOVIE_QUERY: &str = "select * from movie_titles order by year, title asc limit ?";
//-************************************************************************ //-************************************************************************
// the omega user is the system ID, but has no actual power in the app // the omega user is the system ID, but has no actual power in the app
//-************************************************************************ //-************************************************************************
@ -16,35 +14,36 @@ const OMEGA_ID: Julid = Julid::omega();
const BULK_INSERT: usize = 2_000; const BULK_INSERT: usize = 2_000;
#[derive(Debug, sqlx::FromRow, Clone)] #[derive(Debug, sqlx::FromRow, Clone)]
pub struct ImportMovieOmega { pub struct ImportImdbMovie {
pub title: String, pub title: String,
pub year: Option<String>, pub year: Option<String>,
pub runtime: Option<String>, pub runtime: Option<String>,
pub aid: String,
} }
impl From<ImportMovieOmega> for Watch { impl From<ImportImdbMovie> for Watch {
fn from(value: ImportMovieOmega) -> Self { fn from(value: ImportImdbMovie) -> Self {
Watch { Watch {
id: OMEGA_ID, // this is ignored by the inserter id: OMEGA_ID, // this is ignored by the inserter
title: value.title, title: value.title,
release_date: year_to_epoch(value.year.as_deref()), release_date: year_to_epoch(value.year.as_deref()),
length: value.runtime.and_then(|v| v.parse::<i64>().ok()), length: value.runtime.and_then(|v| v.parse::<i64>().ok()),
kind: ShowKind::Movie, kind: ShowKind::Movie,
metadata_url: None, metadata_url: Some(format!("https://imdb.com/title/{}/", &value.aid)),
added_by: OMEGA_ID, added_by: OMEGA_ID,
} }
} }
} }
impl From<&ImportMovieOmega> for Watch { impl From<&ImportImdbMovie> for Watch {
fn from(value: &ImportMovieOmega) -> Self { fn from(value: &ImportImdbMovie) -> Self {
Watch { Watch {
id: OMEGA_ID, id: OMEGA_ID,
title: value.title.to_string(), title: value.title.to_string(),
release_date: year_to_epoch(value.year.as_deref()), release_date: year_to_epoch(value.year.as_deref()),
length: value.runtime.as_ref().and_then(|v| v.parse::<i64>().ok()), length: value.runtime.as_ref().and_then(|v| v.parse::<i64>().ok()),
kind: ShowKind::Movie, kind: ShowKind::Movie,
metadata_url: None, metadata_url: Some(format!("https://imdb.com/title/{}/", value.aid)),
added_by: OMEGA_ID, added_by: OMEGA_ID,
} }
} }
@ -89,14 +88,17 @@ pub async fn add_users(db_pool: &SqlitePool, users: &[User]) -> Result<Duration,
Ok(dur) Ok(dur)
} }
pub async fn add_omega_watches( pub async fn add_imdb_movies(
w2w_db: &SqlitePool, w2w_db: &SqlitePool,
movie_db: &SqlitePool, movie_db: &SqlitePool,
num: u32, num: u32,
) -> Result<Duration, ()> { ) -> Result<Duration, ()> {
const IMDB_MOVIE_QUERY: &str =
"select * from movie_titles where porn = 0 order by year, title asc limit ?";
let omega = ensure_omega(w2w_db).await; let omega = ensure_omega(w2w_db).await;
let movies: Vec<ImportMovieOmega> = sqlx::query_as(MOVIE_QUERY) let movies: Vec<ImportImdbMovie> = sqlx::query_as(IMDB_MOVIE_QUERY)
.bind(num) .bind(num)
.fetch_all(movie_db) .fetch_all(movie_db)
.await .await
@ -105,16 +107,17 @@ pub async fn add_omega_watches(
let start = Instant::now(); let start = Instant::now();
for movies in movies.as_slice().chunks(BULK_INSERT) { for movies in movies.as_slice().chunks(BULK_INSERT) {
let mut builder = sqlx::QueryBuilder::new( let mut builder = sqlx::QueryBuilder::new(
"insert into watches (kind, title, length, release_date, added_by) ", "insert into watches (kind, title, length, release_date, added_by, metadata_url) ",
); );
builder.push_values(movies, |mut b, movie| { builder.push_values(movies, |mut b, movie| {
let title = &movie.title; let movie: Watch = movie.into();
b.push_bind(ShowKind::Movie) b.push_bind(ShowKind::Movie)
.push_bind(title) .push_bind(movie.title)
.push_bind(movie.runtime.as_ref().and_then(|l| l.parse::<i64>().ok())) .push_bind(movie.length)
.push_bind(year_to_epoch(movie.year.as_deref())) .push_bind(movie.release_date)
.push_bind(omega); .push_bind(omega)
.push_bind(movie.metadata_url);
}); });
let q = builder.build(); let q = builder.build();
q.execute(w2w_db).await.map_err(|_| ())?; q.execute(w2w_db).await.map_err(|_| ())?;