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

70
src/pages/components.rs Normal file
View file

@ -0,0 +1,70 @@
use maud::{html, PreEscaped};
pub fn shell(content: PreEscaped<String>, title: &str) -> PreEscaped<String> {
html! {
html {
head {
title=(title)
};
body {
(content)
}
}
}
}
pub fn loading_spinner() -> PreEscaped<String> {
html! {
style {
".spinner { display: flex;justify-content: center;align-items: center;height: 100vh;}
.spinner-border { border: 2px solid #007bff;border-top: 2px solid transparent;border-radius: 50%;width: 40px;height: 40px;animation: spin 1s linear infinite;}
@keyframes spin {0% { transform: rotate(0deg); }100% { transform: rotate(360deg); }}"
};
div class="spinner" {
div class="spinner-border" {};
};
}
}
pub fn search_bar(query: &str) -> PreEscaped<String> {
html! {
form hx-get="/search" action="/search" hx-push-url="true" hx-target="#main-view" hx-swap="innerHTML" {
input style="width: 100%;" value=(query) name="query" type="search" placeholder="Search...";
};
}
}
pub fn video_element(video: &mut Video) -> PreEscaped<String> {
html!(
@let desc = video.description().unwrap_or_default().to_owned();
@let video_hash = video.hash();
article class="container-fluid" style="margin: 50px; cursor: pointer;" {
a href=(format!("/watch?v={video_hash}")) style="text-decoration:none !important;" {
img style="width: 350px;" width="480" src=(format!("/video/thumbnail?v={video_hash}"));
div style="padding: 10px;" {
h2 style="margin: 0; font-size: 18px;" { (video.title().unwrap()) };
@if !desc.is_empty() {
p style="margin: 0; color: grey; font-size: 14px;margin-top: 10px;" { (desc.chars().take(200).chain("...".to_string().chars()).take(203).collect::<String>()) };
};
};
};
};
)
}
pub fn header(query: &str) -> PreEscaped<String> {
html!(
header style="padding: 10px 0; display: flex; justify-content: space-between;" {
a href="/" style="text-decoration: none; margin-left: 20px;" {
div style="margin-right: 20px;display:flex;align-items: center" {
img src="/icon" width="64" style="margin-top: -25px;margin-right: 15px;border-radius: 20%;";
p style="font-size: 42px;" { "WatchDogs" };
};
};
div style="width: 35px;" {};
div style="flex-grow: 1; text-align: center;" {
(search_bar(query));
};
};
)
}

View file

@ -1,4 +1,5 @@
pub mod assets;
pub mod components;
pub mod index;
pub mod yt;

View file

@ -25,6 +25,32 @@ pub fn get_youtube_metadata(path: &PathBuf) -> Option<YouTubeMeta> {
Some(YouTubeMeta { inner: val })
}
pub fn get_vid_duration(path: &PathBuf) -> Option<f64> {
// ffprobe -v error -show_entries format=duration -of csv=p=0 input_video.mp4
let cmd = std::process::Command::new("ffprobe")
.arg("-v")
.arg("error")
.arg("-show_entries")
.arg("format=duration")
.arg("-of")
.arg("csv=p=0")
.arg(path.to_str().unwrap())
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.unwrap();
let out = cmd.wait_with_output().unwrap();
let stdout = String::from_utf8_lossy(&out.stdout).to_string();
//let stderr = String::from_utf8_lossy(&out.stderr).to_string();
let val: f64 = stdout.trim().parse().unwrap();
log::info!("Extracted Duration from {path:?}");
Some(val)
}
pub struct YouTubeMeta {
inner: serde_json::Value,
}