Added arbitrary spotify search

This commit is contained in:
2025-07-31 07:03:24 -05:00
parent d3ad04bc33
commit 6ee36cb65a
5 changed files with 93 additions and 51 deletions

View File

@ -6,5 +6,5 @@ DATABASE_URL=sqlite:watcat.db
RSPOTIFY_CLIENT_ID=
RSPOTIFY_CLIENT_SECRET=
# optional, to set cache size, set to 0 to disable
# optional, to set color cache size. set to 0 to disable
MAX_CACHE_SIZE=

2
Cargo.lock generated
View File

@ -4270,7 +4270,7 @@ dependencies = [
[[package]]
name = "watcat"
version = "0.6.0"
version = "0.6.1"
dependencies = [
"arabic_reshaper",
"dotenvy",

View File

@ -1,6 +1,6 @@
[package]
name = "watcat"
version = "0.6.0"
version = "0.6.1"
edition = "2024"
[dependencies]

View File

@ -31,9 +31,10 @@ TODO
+ ~~add art and lastfm url fetching~~
+ ~~add help command~~
+ ~~refactor to combine repeated parts of art, url, and fmi.~~
+ use macro for commands
+ use Option instead of empty string sentinel value
+ ~~use macro for commands~~
+ ~~use Option instead of empty string sentinel value~~
+ ~~add cache for album art colors~~
+ ~~add spotify link fetching~~
+ ~~add arbitrary spotify searching~~
+ make various parameters user-configurable
+ use tiny-skia instead of magick-rust?

View File

@ -124,12 +124,12 @@ fn validate_color(col: &PixelWand) -> Option<Lab> {
async fn get_track(
ctx: &Context,
arg: &str,
arg: Option<&str>,
id: UserId,
) -> Result<lastfm::track::NowPlayingTrack, Reply> {
let lastfm_user = match arg {
"" => get_lastfm_username(ctx, id).await,
_ => Some(arg.to_owned()),
None => get_lastfm_username(ctx, id).await,
Some(user) => Some(user.to_owned()),
};
let lastfm_client = match lastfm_user {
Some(s) => lastfm::Client::<String, String>::from_env(s),
@ -150,7 +150,7 @@ async fn get_track(
Ok(track)
}
async fn art(ctx: &Context, arg: &str, id: UserId) -> Reply {
async fn art(ctx: &Context, arg: Option<&str>, id: UserId) -> Reply {
let track = match get_track(ctx, arg, id).await {
Ok(track) => track,
Err(e) => return e,
@ -162,7 +162,7 @@ async fn art(ctx: &Context, arg: &str, id: UserId) -> Reply {
Reply::Text(track_art.to_owned())
}
async fn url(ctx: &Context, arg: &str, id: UserId) -> Reply {
async fn url(ctx: &Context, arg: Option<&str>, id: UserId) -> Reply {
let track = match get_track(ctx, arg, id).await {
Ok(track) => track,
Err(e) => return e,
@ -170,7 +170,7 @@ async fn url(ctx: &Context, arg: &str, id: UserId) -> Reply {
Reply::Text(track.url)
}
async fn spot(ctx: &Context, arg: &str, id: UserId) -> Reply {
async fn spot(ctx: &Context, arg: Option<&str>, id: UserId) -> Reply {
let track = match get_track(ctx, arg, id).await {
Ok(track) => track,
Err(e) => return e,
@ -178,7 +178,7 @@ async fn spot(ctx: &Context, arg: &str, id: UserId) -> Reply {
let mut data = ctx.data.write().await;
let spotify_option = data.get_mut::<SpotifyHaver>().expect("Failed to have spotify option");
if spotify_option.is_none() {
return Reply::Text("Unable to use Spotify command: Contact bot Administrator.".to_owned())
return Reply::Text("Unable to use Spotify commands: Contact bot Administrator.".to_owned())
}
let spotify_arc_mutex = spotify_option.as_mut().unwrap();
let spotify_client = spotify_arc_mutex.lock().await;
@ -195,7 +195,7 @@ async fn spot(ctx: &Context, arg: &str, id: UserId) -> Reply {
}
}
async fn fmi(ctx: &Context, arg: &str, id: UserId, avatar: Option<String>) -> Reply {
async fn fmi(ctx: &Context, arg: Option<&str>, id: UserId, avatar: Option<String>) -> Reply {
let track = match get_track(ctx, arg, id).await {
Ok(track) => track,
Err(e) => return e,
@ -434,29 +434,31 @@ async fn fmi(ctx: &Context, arg: &str, id: UserId, avatar: Option<String>) -> Re
Reply::Image(main_wand.write_image_blob("png").unwrap())
}
fn help(arg: &str) -> Reply {
fn help(arg: Option<&str>) -> Reply {
Reply::Text(format!(
"```{}```",
match arg {
"" =>
"usage: .k <img|set|art|url|spotify|help> [lastfm user]\n .k img [lastfm user]: get now playing\n .k set <lastfm user>: sets your username for the future\n .k art [lastfm user]: get now playing album art\n .k url [lastfm user]: get now playing lastfm url\n .k spot [lastfm user]: get now playing spotify link\n .k help [command]: displays a help message"
None =>
"usage: .k <img|set|art|url|spotify|help> [lastfm user]\n .k img [lastfm user]: get now playing\n .k set <lastfm user>: sets your username for the future\n .k art [lastfm user]: get now playing album art\n .k url [lastfm user]: get now playing lastfm url\n .k spot [lastfm user]: get now playing spotify link\n .k search <search query>: searches spotify\n .k help [command]: displays a help message"
.to_owned(),
"img" | "fmi" | "fm" | "get" =>
Some("img" | "fmi" | "fm" | "get") =>
"usage: .k img [lastfm user]\nreturns a pretty-printed image of the user's now playing track on last.fm, using the album art's colors to theme the image. see also: .k set\naliases: .kfmi, .k <fmi|get|img>, .kf, .ki, or even just .k"
.to_owned(),
"set" =>
Some("set") =>
"usage: .k set <lastfm user>\nties a lastfm username to your account. use this to not require your username for .k fmi every time, for example\naliases: .kset"
.to_owned(),
"art" =>
Some("art") =>
"usage: .k art [lastfm user]\nfetches user's now playing track's album art\naliases: .k art, .kart, .ka"
.to_owned(),
"url" | "link" =>
Some("url" | "link") =>
"usage: .k url [lastfm user]\nfetches a lastfm link to user's now playing track\naliases: .k url, .k link, .kl, .ku, .kurl"
.to_owned(),
"spot" | "spotify" =>
"usage: .k spotify [lastfm user]\nsearches spotify for the user's now playing track and returns a link to that track\naliases: .k <spot|spotify>, .ks, .kspot"
Some("spot" | "spotify") =>
"usage: .k spotify [lastfm user]\nsearches spotify for the user's now playing track and returns a link to that track\naliases: .k <spot|spotify>, .kx, .kspot"
.to_owned(),
_ => format!("unknown command: {arg}"),
Some("search") =>
"usage: .k search <search query>\nsearches spotify with an arbitrary query and returns the first result\naliases: .k search, .ks".to_owned(),
Some(cmd) => format!("unknown command: {cmd}"),
}
))
}
@ -466,42 +468,81 @@ async fn set(ctx: &Context, arg: &str, id: UserId) -> Reply {
Reply::Text(format!("set user {arg}"))
}
async fn search(ctx: &Context, arg: &str) -> Reply {
let mut data = ctx.data.write().await;
let spotify_option = data.get_mut::<SpotifyHaver>().expect("Failed to have spotify option");
if spotify_option.is_none() {
return Reply::Text("Unable to use Spotify commands: Contact bot Administrator.".to_owned())
}
let spotify_arc_mutex = spotify_option.as_mut().unwrap();
let spotify_client = spotify_arc_mutex.lock().await;
spotify_client.request_token().await.unwrap();
let search = spotify_client.search(arg, SearchType::Track, None, None, None, None).await;
let tracks = match search {
Ok(SearchResult::Tracks(track_page)) => track_page,
Err(e) => return Reply::Text(format!("Failed to get track {e}")),
_ => return Reply::Text("Spotify search failed".to_owned()),
};
match &tracks.items.first().map(|x| x.external_urls.get("spotify")) {
Some(Some(url)) => Reply::Text(url.to_owned().to_owned()),
_ => Reply::Text("Unable to get spotify url".to_owned()),
}
}
macro_rules! cmd_pattern {
($long_cmd:pat, $short_pattern:pat, $arg_name:pat) => {
(Some(".k"), Some($long_cmd), $arg_name) | (Some(".k"), $arg_name, Some($long_cmd)) | (Some($short_pattern), $arg_name, None)
}
}
#[async_trait]
impl EventHandler for Handler {
async fn message(&self, ctx: Context, msg: Message) {
let mut cmd_iter = msg.content.split(" ");
let cmd = cmd_iter.next().unwrap_or("");
let arg1 = cmd_iter.next().unwrap_or("");
let arg2 = cmd_iter.next().unwrap_or("");
let cmd = cmd_iter.next();
let arg1 = cmd_iter.next();
let arg2 = cmd_iter.next();
let resp = match (cmd, arg1, arg2) {
(".set", arg, "") | (".k", "set", arg) | (".kset", arg, "") => {
log!("{} received", msg.content);
Some(set(&ctx, arg, msg.author.id).await)
}
(".k", "art", arg) | (".k", arg, "art") | (".ka" | ".kart", arg, "") => {
log!("{} received", msg.content);
Some(art(&ctx, arg, msg.author.id).await)
}
(".k", "url" | "uri" | "link", arg)
| (".k", arg, "url" | "uri" | "link")
| (".ku" | ".kl" | ".kurl", arg, "") => {
log!("{} received", msg.content);
Some(url(&ctx, arg, msg.author.id).await)
}
(".k", "spot" | "spotify", arg)
| (".k", arg, "spot" | "spotify")
| (".ks" | ".kspot", arg, "") => {
log!("{} received", msg.content);
Some(spot(&ctx, arg, msg.author.id).await)
}
(".k", "help" | "h" | "?", arg)
| (".k", arg, "help" | "h" | "?")
| (".kh" | ".k?", arg, "") => {
(Some(".k"), Some("help" | "h" | "?"), arg) // skip the ".k <command> help" syntax
| (Some(".kh") | Some(".k?"), arg, None) => {
log!("{} received", msg.content);
Some(help(arg))
}
(".fmi" | ".k" | ".kf" | ".ki" | ".kfmi", arg, "")
| (".k", "fm" | "fmi" | "get" | "img", arg) => {
cmd_pattern!("set", ".kset" | ".set", Some(arg)) =>
/*(".set", arg, "") | (".k", "set", arg) | (".kset", arg, "") =>*/ {
log!("{} received", msg.content);
Some(set(&ctx, arg, msg.author.id).await)
},
cmd_pattern!("art", ".ka" | ".kart", arg) =>
/*(".k", "art", arg) | (".k", arg, "art") | (".ka" | ".kart", arg, "") =>*/ {
log!("{} received", msg.content);
Some(art(&ctx, arg, msg.author.id).await)
},
cmd_pattern!("url" | "uri" | "link", ".ku" | ".kl", arg) =>
/*(".k", "url" | "uri" | "link", arg)
| (".k", arg, "url" | "uri" | "link")
| (".ku" | ".kl" | ".kurl", arg, "") =>*/ {
log!("{} received", msg.content);
Some(url(&ctx, arg, msg.author.id).await)
}
cmd_pattern!("spot" | "spotify", ".kx", arg) =>
/*(".k", "spot" | "spotify", arg)
| (".k", arg, "spot" | "spotify")
| (".ks" | ".kspot", arg, "") =>*/ {
log!("{} received", msg.content);
Some(spot(&ctx, arg, msg.author.id).await)
}
(Some(".ks"), Some(_), _) => {
log!("{} received", msg.content);
Some(search(&ctx, &msg.content[4..]).await)
},
(Some(".k"), Some("search"), Some(_)) => {
log!("{} received", msg.content);
Some(search(&ctx, &msg.content[10..]).await)
},
cmd_pattern!("fm" | "fmi" | "get" | "img", ".fmi" | ".k" | ".kf" | ".ki" | ".kfmi", arg) =>
/*(".fmi" | ".k" | ".kf" | ".ki" | ".kfmi", arg, "")
| (".k", "fm" | "fmi" | "get" | "img", arg) =>*/ {
log!("{} received", msg.content);
Some(fmi(&ctx, arg, msg.author.id, msg.author.avatar_url()).await)
}