Fix unavailable stored videos causing songs not to be played

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/17/head
trivernis 3 years ago
parent 26cc07abec
commit 0300966e56
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -1,5 +1,5 @@
use diesel::insert_into;
use diesel::prelude::*;
use diesel::{delete, insert_into};
use tokio_diesel::*;
use crate::error::DatabaseResult;
@ -57,4 +57,15 @@ impl Database {
Ok(songs.into_iter().next())
}
/// Deletes a song from the database
pub async fn delete_song(&self, id: i64) -> DatabaseResult<()> {
use youtube_songs::dsl;
delete(dsl::youtube_songs)
.filter(dsl::id.eq(id))
.execute_async(&self.pool)
.await?;
Ok(())
}
}

@ -439,6 +439,15 @@ async fn get_youtube_song_for_track(database: &Database, track: Track) -> BotRes
log::debug!("Trying to find track in database.");
if let Some(id) = track.id {
let entry = database.get_song(&id).await?;
if let Some(song) = &entry {
// check if the video is still available
if youtube_dl::get_video_information(&song.url).await.is_err() {
log::debug!("Video '{}' is not available. Deleting entry", song.url);
database.delete_song(song.id).await?;
return Ok(None);
}
}
log::trace!("Found entry is {:?}", entry);
Ok(entry.map(Song::from))
} else {

Loading…
Cancel
Save