summaryrefslogtreecommitdiff
path: root/src/seventv.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/seventv.rs')
-rw-r--r--src/seventv.rs102
1 files changed, 102 insertions, 0 deletions
diff --git a/src/seventv.rs b/src/seventv.rs
index 4ca225a..dd4a939 100644
--- a/src/seventv.rs
+++ b/src/seventv.rs
@@ -2,6 +2,7 @@ use std::collections::HashSet;
use futures::SinkExt;
use reqwest::{Client, Error};
+use serde::{Deserialize, Serialize};
use serde_json::Value;
use tokio::net::TcpStream;
use tokio_tungstenite::{
@@ -11,6 +12,21 @@ use tungstenite::{Message, Result, protocol::WebSocketConfig};
use crate::emotes::{Emote, RetrieveEmoteAPI, RetrieveEmoteWS};
+#[derive(Deserialize, Serialize, Clone, Debug)]
+pub struct User {
+ pub id: String,
+ pub alias_id: usize,
+ pub username: String,
+ pub emote_set_id: String,
+}
+
+#[derive(Deserialize, Serialize, Clone, Debug)]
+pub struct EmoteSet {
+ pub id: String,
+ pub name: String,
+ pub owner: User,
+}
+
pub struct SevenTVAPIClient {
client: Client,
base_url: String,
@@ -24,6 +40,92 @@ impl SevenTVAPIClient {
}
}
+ pub async fn get_user_by_twitch_id(&self, twitch_id: usize) -> Option<User> {
+ let client = Client::new();
+ let response: serde_json::Value = client
+ .get(format!("{}/users/twitch/{}", self.base_url, twitch_id))
+ .send()
+ .await
+ .ok()?
+ .error_for_status()
+ .ok()?
+ .json()
+ .await
+ .ok()?;
+
+ let alias_id = response["id"].as_str()?.parse::<usize>().ok()?;
+ let username = response["username"].as_str()?;
+ let emote_set_id = response["emote_set_id"].as_str()?;
+
+ let id = response["user"]["id"].as_str()?;
+
+ Some(User {
+ id: id.to_string(),
+ alias_id,
+ username: username.to_string(),
+ emote_set_id: emote_set_id.to_string(),
+ })
+ }
+
+ pub async fn get_user_by_id(&self, id: &str) -> Option<User> {
+ let client = Client::new();
+ let response: serde_json::Value = client
+ .get(format!("{}/users/{}", self.base_url, id))
+ .send()
+ .await
+ .ok()?
+ .error_for_status()
+ .ok()?
+ .json()
+ .await
+ .ok()?;
+
+ self.parse_user_json(&response)
+ }
+
+ pub async fn get_emote_set(&self, emote_set_id: &str) -> Option<EmoteSet> {
+ let client = Client::new();
+ let response: serde_json::Value = client
+ .get(format!("{}/emote-sets/{}", self.base_url, emote_set_id))
+ .send()
+ .await
+ .ok()?
+ .error_for_status()
+ .ok()?
+ .json()
+ .await
+ .ok()?;
+
+ let id = response["id"].as_str()?;
+ let name = response["name"].as_str()?;
+ let owner = self.parse_user_json(&response["owner"])?;
+
+ Some(EmoteSet {
+ id: id.to_string(),
+ name: name.to_string(),
+ owner,
+ })
+ }
+
+ fn parse_user_json(&self, json: &Value) -> Option<User> {
+ let id = json["id"].as_str()?;
+
+ let connections = json["connections"].as_array()?;
+ let twitch_connection = connections.iter().find(|x| {
+ let Some(platform) = x["platform"].as_str() else {
+ return false;
+ };
+ platform.eq("TWITCH")
+ })?;
+
+ Some(User {
+ id: id.to_string(),
+ alias_id: twitch_connection["id"].as_str()?.parse().ok()?,
+ username: twitch_connection["username"].as_str()?.to_string(),
+ emote_set_id: twitch_connection["emote_set_id"].as_str()?.to_string(),
+ })
+ }
+
fn parse_emoteset(&self, emotesets_json: &Value) -> Vec<Emote> {
let mut emotes = Vec::new();