0
Fork 0
mirror of https://codeberg.org/SafeTwitch/safetwitch-backend.git synced 2025-01-24 21:28:44 -05:00

Add badges typse

This commit is contained in:
dragongoose 2023-03-31 07:56:56 -04:00
parent 685645ddac
commit 79301bdf37
2 changed files with 95 additions and 1 deletions

View file

@ -25,4 +25,12 @@ export interface StreamerData {
isPartner: boolean
colorHex: string
id: number
}
export interface Badge {
id: string,
title: string,
setId: string,
version: string,
images: { [k:string]: string }
}

View file

@ -1,5 +1,5 @@
import { LooseObject } from "../../../types/looseTypes"
import { StreamerData, StreamData, Social } from "../../../types/scraping/Streamer"
import { StreamerData, StreamData, Social, Badge } from "../../../types/scraping/Streamer"
const base64 = (data: String) => {
return Buffer.from(data).toString('base64url')
@ -417,4 +417,90 @@ export class TwitchAPI {
return formatedGameData
}
public getTwitchBadges = async () => {
const payload = [
{
"operationName": "ChannelPointsPredictionBadges",
"variables": {},
"extensions": {
"persistedQuery": {
"sha256Hash": "36995b30b22c31d1cd0aa329987ac9b5368bb7e6e1ab1df42808bdaa80a6dbf9",
"version": 1
}
},
}
]
const res = await fetch(this.twitchUrl, {
method: 'POST',
body: JSON.stringify(payload),
headers: this.headers
})
const data = await res.json()
if(!data[0].data.badges) return null;
let formatedBadges: Badge[] = []
for (let badge of data[0].data.badges) {
let formatedBadge: Badge = {
id: Buffer.from(badge.id, 'base64').toString(),
setId: badge.setID,
title: badge.title,
version: badge.version,
images: {
image1x: badge.image1x,
image2x: badge.image2x,
image4x: badge.image4x,
}
}
formatedBadges.push(formatedBadge)
}
return formatedBadges
}
public getStreamerBadges = async (streamerName: string) => {
const payload = {
"extensions": {
"persistedQuery": {
"sha256Hash": "86f43113c04606e6476e39dcd432dee47c994d77a83e54b732e11d4935f0cd08",
"version": 1
}
},
"operationName": "ChatList_Badges",
"variables": {
"channelLogin": streamerName
}
}
const res = await fetch(this.twitchUrl, {
method: 'POST',
body: JSON.stringify(payload),
headers: this.headers
})
const data = await res.json()
if(!data.data.user.broadcastBadges) return null;
let formatedBadges: Badge[] = []
for (let badge of data.data.user.broadcastBadges) {
let formatedBadge: Badge = {
id: Buffer.from(badge.id, 'base64').toString(),
setId: badge.setID,
title: badge.title,
version: badge.version,
images: {
image1x: badge.image1x,
image2x: badge.image2x,
image4x: badge.image4x,
}
}
formatedBadges.push(formatedBadge)
}
return formatedBadges
}
}