This commit is contained in:
JMARyA 2024-12-12 18:54:40 +01:00
parent 31a5271e6e
commit b62fb0e719
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
8 changed files with 160 additions and 10 deletions

View file

@ -42,7 +42,7 @@ impl Library {
}
pub async fn get_directory_videos(&self, dir: &str) -> Vec<Video> {
sqlx::query_as("SELECT * FROM videos WHERE directory = ?1")
sqlx::query_as("SELECT * FROM videos WHERE directory = $1")
.bind(dir)
.fetch_all(&self.conn)
.await

View file

@ -1,4 +1,8 @@
use crate::{get_pg, pages::ToAPI, yt_meta};
use crate::{
get_pg,
pages::ToAPI,
yt_meta::{self, get_vid_duration},
};
use serde::{Deserialize, Serialize};
use serde_json::json;
use sqlx::prelude::FromRow;
@ -10,7 +14,6 @@ pub struct YouTubeMeta {
pub title: String,
pub uploader_name: String,
pub uploader_id: String,
pub duration: i64,
pub views: i64,
pub upload_date: chrono::NaiveDate,
}
@ -52,7 +55,7 @@ pub struct Video {
pub id: uuid::Uuid,
pub directory: String,
pub path: String,
pub duration: i64,
pub duration: f64,
pub title: String,
youtube_id: Option<String>,
}
@ -92,6 +95,8 @@ impl Video {
let mut tx = db.begin().await.unwrap();
let vid_duration = get_vid_duration(v).unwrap();
if let Some(meta) = yt_meta::get_youtube_metadata(v) {
sqlx::query("INSERT INTO youtube_meta (id, title, description, uploader_name, uploader_id, duration, views, upload_date) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)")
.bind(&meta.youtube_id().unwrap())
@ -104,9 +109,10 @@ impl Video {
.bind(&meta.upload_date())
.execute(&mut *tx).await.unwrap();
let vid = sqlx::query_as("INSERT INTO videos (directory, path, title, youtube_id) VALUES ($1, $2, $3, $4, $5)")
let vid = sqlx::query_as("INSERT INTO videos (directory, path, duration, title, youtube_id) VALUES ($1, $2, $3, $4, $5)")
.bind(&dir)
.bind(v.to_str().unwrap())
.bind(vid_duration)
.bind(meta.title())
.bind(meta.youtube_id().unwrap())
.fetch_one(&mut *tx).await.unwrap();
@ -139,10 +145,11 @@ impl Video {
}
let vid = sqlx::query_as(
"INSERT INTO videos (directory, path, title) VALUES ($1, $2, $3, $4) RETURNING *",
"INSERT INTO videos (directory, path, duration, title) VALUES ($1, $2, $3, $4) RETURNING *",
)
.bind(dir)
.bind(v.to_str().unwrap())
.bind(vid_duration)
.bind(file_name)
.fetch_one(&mut *tx)
.await