Skip to content

Commit

Permalink
change how cooltext styles are loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
trueharuu committed Jun 28, 2024
1 parent 2448339 commit 4778963
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 220 deletions.
18 changes: 6 additions & 12 deletions assyst-core/src/command/services/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::str::FromStr;
use std::time::Duration;

use anyhow::bail;
Expand All @@ -10,7 +9,7 @@ use super::flags::DownloadFlags;
use super::CommandCtxt;

use crate::command::{Availability, Category};
use crate::rest::cooltext::{burn_text, Style};
use crate::rest::cooltext::{burn_text, STYLES};
use crate::rest::r34::get_random_r34;
use crate::rest::web_media_download::{download_web_media, WebDownloadOpts};

Expand All @@ -37,24 +36,19 @@ pub async fn burntext(ctxt: CommandCtxt<'_>, text: Rest) -> anyhow::Result<()> {
access = Availability::Public,
cooldown = Duration::from_secs(2),
category = Category::Services,
usage = "[style] [text]",
// usage = "[style] [text]",
examples = ["burning hello", "saint fancy"],
send_processing = true
)]
pub async fn cooltext(ctxt: CommandCtxt<'_>, style: Word, text: Rest) -> anyhow::Result<()> {
let s = Style::from_str(style.0.as_str());
if let Ok(v) = s {
let result = crate::rest::cooltext::cooltext(v, text.0.as_str()).await?;
ctxt.reply(result).await?;
let result = crate::rest::cooltext::cooltext(&style.0, text.0.as_str()).await;
if let Ok(r) = result {
ctxt.reply(r).await?;
} else {
bail!(
"unknown style {}, available styles are: {}",
style.0,
Style::list()
.iter()
.map(|x| x.to_string())
.collect::<Vec<_>>()
.join(", ")
STYLES.iter().map(|(v, _)| *v).collect::<Vec<_>>().join(", ")
)
}

Expand Down
237 changes: 29 additions & 208 deletions assyst-core/src/rest/cooltext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,24 @@ pub async fn burn_text(text: &str) -> anyhow::Result<Vec<u8>> {
Ok(content.to_vec())
}

pub async fn cooltext(style: Style, text: &str) -> anyhow::Result<Vec<u8>> {
pub async fn cooltext(style: &str, text: &str) -> anyhow::Result<Vec<u8>> {
let client = ClientBuilder::new().danger_accept_invalid_certs(true).build().unwrap();
let styled = STYLES
.iter()
.find_map(|(x, y)| if *x == style { Some(y) } else { None })
.ok_or(anyhow::anyhow!("unknown style {style}"))?;

let cool_text_response = client
.post(COOLTEXT_URL)
.query(query_for_style(style, text, "70").as_slice())
.query(&[
("LogoID", *styled),
("Text", text),
("FontSize", "70"),
("FileFormat", "6"),
("Integer13", "on"),
("Integer12", "on"),
("BackgroundColor_color", "#FFFFFF"),
])
.header("content-length", "0")
.send()
.await?
Expand All @@ -86,209 +98,18 @@ pub async fn cooltext(style: Style, text: &str) -> anyhow::Result<Vec<u8>> {
Ok(content.to_vec())
}

// generates a url query for a given style, using the default parameters
fn query_for_style<'a>(style: Style, text: &'a str, font_size: &'a str) -> Vec<(&'a str, &'a str)> {
let mut v = vec![
("Text", text),
("FontSize", font_size),
("FileFormat", "6"), // .PNG w/ Transparency
("Integer13", "on"), // auto width
("Integer12", "on"), // auto height
("BackgroundColor_color", "#FFFFFF"), // white background
];
v.extend(match style {
Style::Skate => vec![
("LogoID", "4610356863"),
("Color1_color", "#83D6FC"),
("Color2_color", "#0088FF"),
("Color3_color", "#000000"),
("Integer1", "5"),
("Integer5", "2"),
("Integer7", "1"),
("Integer8", "1"),
("Integer14_color", "#000000"),
("Integer6", "75"),
("Integer9", "0"),
],
Style::SuperScripted => vec![
("LogoID", "4610363770"),
("Color1_color", "#000000"),
("Integer5", "4"),
("Integer7", "0"),
("Integer8", "0"),
("Integer14_color", "#000000"),
("Integer6", "70"),
("Integer9", "0"),
],
Style::Tough => vec![
("LogoID", "758282876"),
("Color1_color", "#0A213D"),
("Integer1", "5"),
("Integer5", "0"),
("Integer7", "0"),
("Integer8", "0"),
("Integer14_color", "#000000"),
("Integer6", "75"),
("Integer9", "0"),
],
Style::White => vec![
("LogoID", "4610365972"),
("Color1_color", "#000000"),
("Color2_color", "#FFFFFF"),
("Color3_color", "#FFFFFF"),
("Integer5", "0"),
("Integer7", "0"),
("Integer8", "0"),
("Integer14_color", "#000000"),
("Integer6", "75"),
("Integer9", "0"),
],
Style::IText => vec![
("LogoID", "37"),
("Color1_color", "#669900"),
("Integer5", "0"),
("Integer7", "0"),
("Integer8", "0"),
("Integer14_color", "#000000"),
("Integer6", "75"),
("Integer9", "0"),
],
Style::Easy => vec![
("LogoID", "791030843"),
("Color1_color", "#004A99"),
("Integer5", "4"),
("Integer7", "0"),
("Integer8", "0"),
("Integer14_color", "#000000"),
("Integer6", "60"),
("Integer9", "0"),
],
Style::Textured => vec![
("LogoID", "23"),
("Color2_color", "#206A00"),
("Color3_color", "#00006A"),
("Integer9", "0"),
],
Style::Stranger => vec![
("LogoID", "4610366723"),
("Color1_color", "#F5FAFF"),
("Color2_color", "#16877A"),
("Color3_color", "#000000"),
("Integer1", "5"),
("Integer5", "2"),
("Integer7", "1"),
("Integer8", "1"),
("Integer14_color", "#000000"),
("Integer6", "75"),
("Integer9", "0"),
],
Style::Burning => vec![
("LogoID", "4"),
("Color1_color", "#FF0000"),
("Integer1", "15"),
("Boolean1", "on"),
("Integer9", "0"),
],
Style::Neon => vec![
("LogoID", "18"),
("Color1_color", "#23D3FF"),
("Integer5", "1"),
("Integer7", "3"),
("Integer8", "3"),
("Integer14_color", "#000000"),
("Integer6", "85"),
("Integer9", "0"),
],
Style::Candy => vec![
("LogoID", "732431452"),
("Color1_color", "#FF00FF"),
("Integer5", "2"),
("Integer7", "2"),
("Integer8", "2"),
("Integer14_color", "#000000"),
("Integer6", "25"),
("Integer9", "0"),
],
Style::Saint => vec![("LogoID", "4516516448")],
});
v
}

pub enum Style {
Skate,
SuperScripted,
Tough,
White,
IText,
Easy,
Textured,
Stranger,
Burning,
Neon,
Candy,
Saint,
}

// this sucks, replace with some proc macro that does the following 3 impls for free
impl Style {
pub fn list() -> &'static [Self] {
&[
Self::Skate,
Self::SuperScripted,
Self::Tough,
Self::White,
Self::IText,
Self::Easy,
Self::Textured,
Self::Stranger,
Self::Burning,
Self::Neon,
Self::Candy,
Self::Saint,
]
}
}

impl Display for Style {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
Self::Skate => "skate",
Self::SuperScripted => "super_scripted",
Self::Tough => "tough",
Self::White => "white",
Self::IText => "itext",
Self::Easy => "easy",
Self::Textured => "textured",
Self::Stranger => "stranger",
Self::Burning => "burning",
Self::Neon => "neon",
Self::Candy => "candy",
Self::Saint => "saint",
}
)
}
}

impl FromStr for Style {
type Err = &'static str; // FIXME: this should not be a string.
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"skate" => Ok(Self::Skate),
"super_scripted" => Ok(Self::SuperScripted),
"tough" => Ok(Self::Tough),
"white" => Ok(Self::White),
"itext" => Ok(Self::IText),
"easy" => Ok(Self::Easy),
"textured" => Ok(Self::Textured),
"stranger" => Ok(Self::Stranger),
"burning" => Ok(Self::Burning),
"neon" => Ok(Self::Neon),
"candy" => Ok(Self::Candy),
"saint" => Ok(Self::Saint),
_ => Err("unknown style"),
}
}
}
pub const STYLES: &[(&str, &str)] = &[
("skate", "4610356863"),
("super_scripted", "4610363770"),
("tough", "758282876"),
("white", "4610365972"),
("itext", "37"),
("easy", "791030843"),
("textured", "23"),
("stranger", "4610366723"),
("burning", "4"),
("neon", "18"),
("candy", "732431452"),
("saint", "4516516448"),
("3d_outline", "4611483973"),
];

0 comments on commit 4778963

Please sign in to comment.