Compare commits
1 Commits
renovate/a
...
database
| Author | SHA1 | Date | |
|---|---|---|---|
|
7edd14c628
|
@@ -1,4 +1,3 @@
|
|||||||
-- Add migration script here
|
|
||||||
CREATE TABLE IF NOT EXISTS tracks
|
CREATE TABLE IF NOT EXISTS tracks
|
||||||
(
|
(
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
|||||||
5
migrations/20240627193022_save_tracks.sql
Normal file
5
migrations/20240627193022_save_tracks.sql
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
CREATE TABLE IF NOT EXISTS blobs
|
||||||
|
(
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
data BLOB
|
||||||
|
);
|
||||||
@@ -1,14 +1,18 @@
|
|||||||
use crate::commands::join::join_channel;
|
use crate::commands::join::join_channel;
|
||||||
|
use crate::db::track::{insert_blob, Blob};
|
||||||
use crate::metadata::{Metadata, MetadataMap};
|
use crate::metadata::{Metadata, MetadataMap};
|
||||||
use crate::state::State;
|
use crate::state::{State, StateRef};
|
||||||
use crate::{colors, db};
|
use crate::{colors, db};
|
||||||
|
|
||||||
|
use async_trait::async_trait;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use songbird::input::cached::Memory;
|
use songbird::input::cached::Memory;
|
||||||
use songbird::input::{Compose, YoutubeDl};
|
use songbird::input::{Compose, YoutubeDl};
|
||||||
use songbird::tracks::Track;
|
use songbird::tracks::Track;
|
||||||
use std::io::{BufRead, BufReader};
|
use songbird::{Event, EventContext, EventHandler, TrackEvent};
|
||||||
|
use std::io::{BufRead, BufReader, Read};
|
||||||
use std::ops::Sub;
|
use std::ops::Sub;
|
||||||
|
use std::sync::Arc;
|
||||||
use std::{error::Error, time::Duration};
|
use std::{error::Error, time::Duration};
|
||||||
use tokio::process::Command;
|
use tokio::process::Command;
|
||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
@@ -183,10 +187,8 @@ pub(crate) async fn play(
|
|||||||
.ok_or("Could not find url")?;
|
.ok_or("Could not find url")?;
|
||||||
|
|
||||||
let mut src = YoutubeDl::new(reqwest::Client::new(), url.clone());
|
let mut src = YoutubeDl::new(reqwest::Client::new(), url.clone());
|
||||||
let src_copy = src.clone();
|
let memory = Memory::new(src.clone().into()).await.unwrap();
|
||||||
let src_copy2 = src.clone();
|
let track: Track = memory.new_handle().into();
|
||||||
let _m = Memory::new(src_copy2.into());
|
|
||||||
let track: Track = src_copy.into();
|
|
||||||
|
|
||||||
if let Ok(metadata) = src.aux_metadata().await {
|
if let Ok(metadata) = src.aux_metadata().await {
|
||||||
debug!("metadata: {:?}", metadata);
|
debug!("metadata: {:?}", metadata);
|
||||||
@@ -255,6 +257,17 @@ pub(crate) async fn play(
|
|||||||
url,
|
url,
|
||||||
src,
|
src,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
handle
|
||||||
|
.add_event(
|
||||||
|
Event::Track(TrackEvent::Preparing),
|
||||||
|
TrackPreparingNotifier {
|
||||||
|
memory,
|
||||||
|
track_id,
|
||||||
|
state: Arc::clone(&state),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.expect("could not add event");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -350,6 +363,31 @@ pub(crate) async fn play(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct TrackPreparingNotifier {
|
||||||
|
memory: Memory,
|
||||||
|
track_id: i64,
|
||||||
|
state: Arc<StateRef>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl EventHandler for TrackPreparingNotifier {
|
||||||
|
async fn act(&self, _ctx: &EventContext<'_>) -> Option<Event> {
|
||||||
|
tracing::info!("Build buffer");
|
||||||
|
let mut reader = BufReader::new(self.memory.new_handle());
|
||||||
|
let mut bytes = Vec::new();
|
||||||
|
reader
|
||||||
|
.read_to_end(&mut bytes)
|
||||||
|
.expect("could not read track in memory");
|
||||||
|
|
||||||
|
tracing::info!("Saving track");
|
||||||
|
insert_blob(&self.state.pool, Blob::new(self.track_id, bytes))
|
||||||
|
.await
|
||||||
|
.expect("could not insert blob");
|
||||||
|
tracing::info!("Saved");
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|||||||
@@ -144,3 +144,25 @@ pub(crate) async fn insert_guild(
|
|||||||
.await?;
|
.await?;
|
||||||
Ok(res.last_insert_rowid())
|
Ok(res.last_insert_rowid())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, FromRow)]
|
||||||
|
pub(crate) struct Blob {
|
||||||
|
pub(crate) id: i64,
|
||||||
|
pub(crate) data: Vec<u8>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Blob {
|
||||||
|
pub(crate) fn new(id: i64, data: Vec<u8>) -> Self {
|
||||||
|
Self { id, data }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn insert_blob(pool: &sqlx::SqlitePool, blob: Blob) -> Result<(), sqlx::Error> {
|
||||||
|
let query = "INSERT OR REPLACE INTO blobs (id, data) VALUES ($1, $2)";
|
||||||
|
sqlx::query(query)
|
||||||
|
.bind(blob.id)
|
||||||
|
.bind(blob.data)
|
||||||
|
.execute(pool)
|
||||||
|
.await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ use futures::StreamExt;
|
|||||||
use signal::signal_handler;
|
use signal::signal_handler;
|
||||||
use songbird::{shards::TwilightMap, Songbird};
|
use songbird::{shards::TwilightMap, Songbird};
|
||||||
use state::StateRef;
|
use state::StateRef;
|
||||||
use std::{env, error::Error, sync::Arc};
|
use std::{env, error::Error, str::FromStr, sync::Arc};
|
||||||
use tokio::select;
|
use tokio::select;
|
||||||
use tracing::{debug, info};
|
use tracing::{debug, info};
|
||||||
use twilight_cache_inmemory::InMemoryCache;
|
use twilight_cache_inmemory::InMemoryCache;
|
||||||
@@ -41,9 +41,9 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
|
|||||||
|
|
||||||
let (mut shards, state) = {
|
let (mut shards, state) = {
|
||||||
let db = env::var("DATABASE_URL").map_err(|_| "DATABASE_URL is not set")?;
|
let db = env::var("DATABASE_URL").map_err(|_| "DATABASE_URL is not set")?;
|
||||||
let options = SqliteConnectOptions::new()
|
let options = SqliteConnectOptions::from_str(&db)
|
||||||
.create_if_missing(true)
|
.expect("could not create options")
|
||||||
.filename(&db);
|
.create_if_missing(true);
|
||||||
let pool = SqlitePoolOptions::new()
|
let pool = SqlitePoolOptions::new()
|
||||||
.max_connections(5)
|
.max_connections(5)
|
||||||
.connect_with(options)
|
.connect_with(options)
|
||||||
|
|||||||
Reference in New Issue
Block a user