diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/betterttv.rs | 73 | ||||
| -rw-r--r-- | src/lib.rs | 30 |
2 files changed, 97 insertions, 6 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()) + } +} @@ -1,14 +1,32 @@ mod emotes; -} +pub mod betterttv; #[cfg(test)] mod tests { - use super::*; + use crate::{betterttv::BetterTTVAPIClient, emotes::RetrieveEmoteAPI}; + + #[tokio::test] + async fn get_betterttv_channel_emotes() { + let bttv = BetterTTVAPIClient::new(); + let emotes = bttv.get_channel_emotes("555579413").await; + + assert_eq!(emotes.is_ok(), true); + + let emotes = emotes.unwrap(); + + assert_eq!(emotes.len(), 1); + } + + #[tokio::test] + async fn get_betterttv_global_emotes() { + let bttv = BetterTTVAPIClient::new(); + let emotes = bttv.get_global_emotes().await; + + assert_eq!(emotes.is_ok(), true); + + let emotes = emotes.unwrap(); - #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); + assert_eq!(emotes.len() >= 1, true); } } |
