main: make bandcamp command use bandcamp library to avoid panics

This commit is contained in:
2026-06-05 12:22:36 +02:00
parent 39973c333c
commit 17903f1ac9
2 changed files with 11 additions and 10 deletions
+10 -9
View File
@@ -14,7 +14,7 @@ use futures::{StreamExt, future::FutureExt};
use ratatui::prelude::*; use ratatui::prelude::*;
use crate::{events::AudioRecordRequest, prediction::PossibleResponse, scene::{ConversationEntry, PlaylistEntry, Scene, StageActions, StageDirection}, tts::start_tts}; use crate::{events::AudioRecordRequest, prediction::{BandcampResult, PossibleResponse}, scene::{ConversationEntry, PlaylistEntry, Scene, StageActions, StageDirection}, tts::start_tts};
mod scene; mod scene;
mod events; mod events;
@@ -364,7 +364,7 @@ impl App {
} else { } else {
self.sys_message_sink.send("Invalid timer format. Use /timer [minutes]".into()).await.unwrap(); self.sys_message_sink.send("Invalid timer format. Use /timer [minutes]".into()).await.unwrap();
} }
} },
"/clear" => { "/clear" => {
match arg.trim() { match arg.trim() {
"playlist" => { "playlist" => {
@@ -415,12 +415,14 @@ impl App {
} }
async fn add_bandcamp_artifact(&mut self, url: &str) { async fn add_bandcamp_artifact(&mut self, url: &str) {
// FIXME: This can crash if the page doesn't load properly, or if the structure of the Bandcamp page changes. We should add some error handling here. if let Ok(album) = bandcamp::album_from_url(url).await {
let body = reqwest::get(url).await.unwrap().text().await.unwrap(); let result: BandcampResult = album.into();
let fragment = Html::parse_document(&body); let json = serde_json::to_string(&result).unwrap();
let selector = Selector::parse("script[type=\"application/ld+json\"]").unwrap(); self.direction.artifacts.push(json);
let json_ld = fragment.select(&selector).next().unwrap().inner_html(); self.sys_message_sink.send("Added bandcamp album".into()).await.unwrap();
self.direction.artifacts.push(json_ld.trim().to_string()); } else {
self.sys_message_sink.send("Could not fetch bandcamp data! Is that a proper URL?".into()).await.unwrap();
}
} }
async fn speak(&mut self, text: String) { async fn speak(&mut self, text: String) {
@@ -436,7 +438,6 @@ impl App {
self.scene.conversation_mut().append(&mut actions.additions.clone()); self.scene.conversation_mut().append(&mut actions.additions.clone());
self.prediction_request_sink.send(actions).unwrap(); self.prediction_request_sink.send(actions).unwrap();
self.is_requesting = true; self.is_requesting = true;
panic!("at the disco");
} }
fn reload_mixxx_playlist(&mut self) { fn reload_mixxx_playlist(&mut self) {
+1 -1
View File
@@ -51,7 +51,7 @@ struct BandcampQueryArgs {
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
enum BandcampResult { pub enum BandcampResult {
Artist { name: String, bio: Option<String>, location: Option<String> }, Artist { name: String, bio: Option<String>, location: Option<String> },
Album { title: String, about: Option<String>, credits: Option<String>, release_date: DateTime<Utc>, artist: String } Album { title: String, about: Option<String>, credits: Option<String>, release_date: DateTime<Utc>, artist: String }
} }