summaryrefslogtreecommitdiff
path: root/src/betterttv.rs
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2025-04-02 18:04:09 +0500
committerilotterytea <iltsu@alright.party>2025-04-02 18:04:09 +0500
commitcf2dd7c5f6293df675d88f1867015ad0730e0cfe (patch)
tree32023e3accc666ac846658e66a934444dd092c49 /src/betterttv.rs
parentf8fc21903b0ac7bbdcbfdb95b295b9b98af4b1fa (diff)
feat: get betterttv emotes
Diffstat (limited to 'src/betterttv.rs')
-rw-r--r--src/betterttv.rs73
1 files changed, 73 insertions, 0 deletions
diff --git a/src/betterttv.rs b/src/betterttv.rs
new file mode 100644
index 0000000..23ae608
--- /dev/null
+++ b/src/betterttv.rs
@@ -0,0 +1,73 @@
+use reqwest::{Client, Error};
+use serde::Deserialize;
+use serde_json::Value;
+
+use crate::emotes::RetrieveEmoteAPI;
+
+#[derive(Debug, Deserialize)]
+pub struct BetterTTVEmote {
+ pub id: String,
+ pub code: String,
+ #[serde(rename = "imageType")]
+ pub image_type: String,
+ pub animated: bool,
+}
+
+pub struct BetterTTVAPIClient {
+ client: Client,
+ base_url: String,
+}
+
+impl BetterTTVAPIClient {
+ pub fn new() -> Self {
+ Self {
+ client: Client::new(),
+ base_url: "https://api.betterttv.net/3".into(),
+ }
+ }
+}
+
+impl RetrieveEmoteAPI<BetterTTVEmote> for BetterTTVAPIClient {
+ async fn get_channel_emotes(&self, channel_id: &str) -> Result<Vec<BetterTTVEmote>, Error> {
+ let response = self
+ .client
+ .get(format!(
+ "{}/cached/users/twitch/{}",
+ self.base_url, channel_id
+ ))
+ .send()
+ .await?
+ .error_for_status()?;
+
+ let json: Value = response.json().await?;
+
+ let mut emotes = Vec::new();
+
+ if let Some(shared_emotes) = json.get("sharedEmotes") {
+ let shared_emotes: Vec<BetterTTVEmote> =
+ serde_json::from_value(shared_emotes.clone()).unwrap();
+ emotes.extend(shared_emotes);
+ }
+
+ if let Some(channel_emotes) = json.get("channelEmotes") {
+ let channel_emotes: Vec<BetterTTVEmote> =
+ serde_json::from_value(channel_emotes.clone()).unwrap();
+ emotes.extend(channel_emotes);
+ }
+
+ Ok(emotes)
+ }
+
+ async fn get_global_emotes(&self) -> Result<Vec<BetterTTVEmote>, Error> {
+ let response = self
+ .client
+ .get(format!("{}/cached/emotes/global", self.base_url))
+ .send()
+ .await?
+ .error_for_status()?;
+
+ let json: Value = response.json().await?;
+
+ Ok(serde_json::from_value(json).unwrap())
+ }
+}