From 17903f1ac917018959d0fd24f3f9b09c36963923 Mon Sep 17 00:00:00 2001 From: Victoria Fischer Date: Fri, 5 Jun 2026 12:22:36 +0200 Subject: [PATCH] main: make bandcamp command use bandcamp library to avoid panics --- src/main.rs | 19 ++++++++++--------- src/prediction.rs | 2 +- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index 556abc7..9ba8037 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,7 @@ use futures::{StreamExt, future::FutureExt}; 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 events; @@ -364,7 +364,7 @@ impl App { } else { self.sys_message_sink.send("Invalid timer format. Use /timer [minutes]".into()).await.unwrap(); } - } + }, "/clear" => { match arg.trim() { "playlist" => { @@ -415,12 +415,14 @@ impl App { } 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. - let body = reqwest::get(url).await.unwrap().text().await.unwrap(); - let fragment = Html::parse_document(&body); - let selector = Selector::parse("script[type=\"application/ld+json\"]").unwrap(); - let json_ld = fragment.select(&selector).next().unwrap().inner_html(); - self.direction.artifacts.push(json_ld.trim().to_string()); + if let Ok(album) = bandcamp::album_from_url(url).await { + let result: BandcampResult = album.into(); + let json = serde_json::to_string(&result).unwrap(); + self.direction.artifacts.push(json); + self.sys_message_sink.send("Added bandcamp album".into()).await.unwrap(); + } 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) { @@ -436,7 +438,6 @@ impl App { self.scene.conversation_mut().append(&mut actions.additions.clone()); self.prediction_request_sink.send(actions).unwrap(); self.is_requesting = true; - panic!("at the disco"); } fn reload_mixxx_playlist(&mut self) { diff --git a/src/prediction.rs b/src/prediction.rs index fd1d004..dfb3e1b 100644 --- a/src/prediction.rs +++ b/src/prediction.rs @@ -51,7 +51,7 @@ struct BandcampQueryArgs { } #[derive(Debug, Serialize, Deserialize, Clone)] -enum BandcampResult { +pub enum BandcampResult { Artist { name: String, bio: Option, location: Option }, Album { title: String, about: Option, credits: Option, release_date: DateTime, artist: String } }