summaryrefslogtreecommitdiff
path: root/internal/emotes.go
blob: d85eca2b2637b0b68d55fce82a19b414d352cf5f (plain)
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
package stats

import (
	"encoding/json"
	"fmt"
	"net/http"
)

type Emote struct {
	Id   string
	Name string
}

func GetBetterTTVEmotes(channelId string) (emotes []Emote, err error) {
	resp, err := http.Get(fmt.Sprintf("https://api.betterttv.net/3/cached/users/twitch/%s", channelId))
	if err != nil {
		return
	}
	defer resp.Body.Close()

	var data map[string]any
	if err = json.NewDecoder(resp.Body).Decode(&data); err != nil {
		return
	}

	if msg, ok := data["message"]; ok {
		err = fmt.Errorf("error while searching user: %s", msg)
		return
	}

	ejson := []map[string]any{}
	for _, v := range data["channelEmotes"].([]any) {
		ejson = append(ejson, v.(map[string]any))
	}
	for _, v := range data["sharedEmotes"].([]any) {
		ejson = append(ejson, v.(map[string]any))
	}

	for _, e := range ejson {
		emotes = append(emotes, Emote{
			Id:   e["id"].(string),
			Name: e["code"].(string),
		})
	}

	return
}