1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
use reqwest::{Client, Error};
use serde_json::Value;
use crate::emotes::{Emote, RetrieveEmoteAPI};
pub struct FrankerFaceZAPIClient {
client: Client,
base_url: String,
}
impl FrankerFaceZAPIClient {
pub fn new() -> Self {
Self {
client: Client::new(),
base_url: "https://api.frankerfacez.com/v1".into(),
}
}
fn parse_emoteset(&self, emotesets_json: &Value) -> Vec<Emote> {
let mut emotes = Vec::new();
for (_, set) in emotesets_json.as_object().unwrap() {
let emoticons = set.get("emoticons").unwrap().as_array().unwrap();
for emoticon in emoticons {
let emote = Emote {
id: emoticon.get("id").unwrap().to_string(),
code: emoticon.get("name").unwrap().to_string(),
original_code: None,
};
emotes.push(emote);
}
}
emotes
}
}
impl RetrieveEmoteAPI<Emote> for FrankerFaceZAPIClient {
async fn get_channel_emotes(&self, channel_id: &str) -> Result<Vec<Emote>, Error> {
let response = self
.client
.get(format!("{}/room/id/{}", self.base_url, channel_id))
.send()
.await?
.error_for_status()?;
let json: Value = response.json().await?;
let sets = json.get("sets").unwrap();
let emotes = self.parse_emoteset(sets);
Ok(emotes)
}
async fn get_global_emotes(&self) -> Result<Vec<Emote>, Error> {
let response = self
.client
.get(format!("{}/set/global", self.base_url))
.send()
.await?
.error_for_status()?;
let json: Value = response.json().await?;
let sets = json.get("sets").unwrap();
let emotes = self.parse_emoteset(sets);
Ok(emotes)
}
}
|