diff options
Diffstat (limited to 'src/betterttv.rs')
| -rw-r--r-- | src/betterttv.rs | 73 |
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()) + } +} |
