rename misc -> lib, change navbar to use state over url match, init potential wiki
This commit is contained in:
parent
803863d273
commit
db1c07a3cf
19 changed files with 2207 additions and 27 deletions
lib
126
lib/7TVAPI.ts
Normal file
126
lib/7TVAPI.ts
Normal file
|
@ -0,0 +1,126 @@
|
|||
import type RedisInstance from "ioredis";
|
||||
|
||||
async function applyCache(
|
||||
redis: RedisInstance,
|
||||
key: string,
|
||||
query: string,
|
||||
gql: boolean,
|
||||
cacheTime: number
|
||||
) {
|
||||
if (await redis.get(key)) {
|
||||
return JSON.parse((await redis.get(key)) as string);
|
||||
} else {
|
||||
const response = await fetchEndpoint(redis, query, gql);
|
||||
if (response != null) {
|
||||
await redis.set(key, JSON.stringify(response), "EX", cacheTime);
|
||||
}
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchEndpoint(
|
||||
redis: RedisInstance,
|
||||
query: string,
|
||||
gql: boolean = false
|
||||
) {
|
||||
if (await redis.get("7TV.RATE_LIMIT")) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
} else {
|
||||
await redis.set("7TV.RATE_LIMIT", "1", "EX", 1);
|
||||
}
|
||||
if (gql) {
|
||||
const response = await fetchGQL(query);
|
||||
const json = response.data;
|
||||
return json;
|
||||
} else {
|
||||
const response = await fetch(query);
|
||||
const json = await response.json();
|
||||
return json;
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchGQL(query: string) {
|
||||
const response = await fetch("https://7tv.io/v3/gql", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
query: query,
|
||||
}),
|
||||
});
|
||||
const json = await response.json();
|
||||
return json;
|
||||
}
|
||||
|
||||
async function getGlobalEmotes(redis: RedisInstance) {
|
||||
const gqlQuery = `query {
|
||||
namedEmoteSet(name: GLOBAL) {
|
||||
emote_count
|
||||
emotes {
|
||||
flags
|
||||
id
|
||||
name
|
||||
data {
|
||||
animated
|
||||
flags
|
||||
id
|
||||
name
|
||||
tags
|
||||
host {
|
||||
files {
|
||||
format
|
||||
height
|
||||
width
|
||||
name
|
||||
size
|
||||
}
|
||||
url
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}`;
|
||||
return await applyCache(redis, "7TV.GLOBAL_EMOTES", gqlQuery, true, 3600);
|
||||
}
|
||||
|
||||
async function getChannelEmotes(redis: RedisInstance, channelID: string) {
|
||||
const gqlQuery = `query {
|
||||
user(id: "${channelID}") {
|
||||
emote_sets {
|
||||
emote_count
|
||||
emotes(origins: true) {
|
||||
flags
|
||||
id
|
||||
name
|
||||
data {
|
||||
animated
|
||||
flags
|
||||
id
|
||||
name
|
||||
tags
|
||||
host {
|
||||
files {
|
||||
format
|
||||
height
|
||||
width
|
||||
name
|
||||
size
|
||||
}
|
||||
url
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}`;
|
||||
return await applyCache(
|
||||
redis,
|
||||
"7TV.CHANNEL_EMOTES_" + channelID,
|
||||
gqlQuery,
|
||||
true,
|
||||
1200
|
||||
);
|
||||
}
|
||||
|
||||
export { getGlobalEmotes, getChannelEmotes };
|
Loading…
Add table
Add a link
Reference in a new issue