more code tidy-up
This commit is contained in:
parent
2190cac343
commit
04ca14163e
8 changed files with 195 additions and 94 deletions
|
|
@ -7,10 +7,10 @@ tmp_dir = "tmp"
|
||||||
|
|
||||||
[build]
|
[build]
|
||||||
args_bin = []
|
args_bin = []
|
||||||
bin = "./tmp/main"
|
bin = "./tmp/stream-tools"
|
||||||
cmd = "go build -o ./tmp/main ."
|
cmd = "go build -o ./tmp/stream-tools ./cmd/stream-tools"
|
||||||
delay = 1000
|
delay = 1000
|
||||||
entrypoint = ["./tmp/main"]
|
entrypoint = ["./tmp/stream-tools"]
|
||||||
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
|
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
|
||||||
exclude_file = []
|
exclude_file = []
|
||||||
exclude_regex = ["_test.go"]
|
exclude_regex = ["_test.go"]
|
||||||
|
|
|
||||||
5
cmd/satellite/main.go
Normal file
5
cmd/satellite/main.go
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// TODO: build satellite service for offline-fetching subscribers and cheers
|
||||||
|
}
|
||||||
|
|
@ -43,8 +43,6 @@ async function updateTitle() {
|
||||||
console.error("Connection lost, or an error has occurred.");
|
console.error("Connection lost, or an error has occurred.");
|
||||||
console.log("Attempting to reconnect...");
|
console.log("Attempting to reconnect...");
|
||||||
|
|
||||||
// TODO: stop reconnecting after 10 failed attempts
|
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
updateTitle();
|
updateTitle();
|
||||||
}, 1000);
|
}, 1000);
|
||||||
|
|
|
||||||
|
|
@ -37,8 +37,6 @@ async function sync() {
|
||||||
console.error("Connection lost, or an error has occurred.");
|
console.error("Connection lost, or an error has occurred.");
|
||||||
console.log("Attempting to reconnect...");
|
console.log("Attempting to reconnect...");
|
||||||
|
|
||||||
// TODO: stop reconnecting after 10 failed attempts
|
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
sync();
|
sync();
|
||||||
}, 1000);
|
}, 1000);
|
||||||
|
|
|
||||||
209
twitch/api.go
209
twitch/api.go
|
|
@ -8,25 +8,40 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strconv"
|
||||||
|
|
||||||
"codeberg.org/arimelody/ari-stream-tools/twitch/api"
|
"codeberg.org/arimelody/ari-stream-tools/twitch/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (srv *Service) userIDsFromNames(usernames []string) ([]string, error) {
|
type (
|
||||||
if len(usernames) == 0 { return []string{}, nil }
|
FullUser struct {
|
||||||
|
ID string `json:"id"`
|
||||||
url := strings.Builder{}
|
Login string `json:"login"`
|
||||||
url.WriteString(api.BASE_URL)
|
DisplayName string `json:"display_name"`
|
||||||
url.WriteString("/users?login=")
|
Type string `json:"type"`
|
||||||
url.WriteString(usernames[0])
|
BroadcasterType string `json:"broadcaster_type"`
|
||||||
for _, username := range usernames {
|
Description string `json:"description"`
|
||||||
url.WriteString("&login=")
|
ProfileImageURL string `json:"profile_image_url"`
|
||||||
url.WriteString(username)
|
OfflineImageURL string `json:"offline_image_url"`
|
||||||
|
ViewCount int `json:"view_count"`
|
||||||
|
Email string `json:"email"`
|
||||||
|
CreatedAt string `json:"created_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
GetUsersResponse struct {
|
||||||
|
Data []FullUser `json:"data"`
|
||||||
|
}
|
||||||
|
)
|
||||||
|
func (srv *Service) getUsers(ids []string, logins []string) (*GetUsersResponse, error) {
|
||||||
|
if len(ids) == 0 && len(logins) == 0 { return nil, nil }
|
||||||
|
|
||||||
|
url, err := url.Parse(api.BASE_URL + "/users?" + url.Values{
|
||||||
|
"id": ids, "login": logins,
|
||||||
|
}.Encode())
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", url.String(), nil)
|
req, err := http.NewRequest("GET", url.String(), nil)
|
||||||
if err != nil { return nil, err }
|
if err != nil { return nil, err }
|
||||||
|
|
||||||
|
|
@ -41,36 +56,12 @@ func (srv *Service) userIDsFromNames(usernames []string) ([]string, error) {
|
||||||
return nil, fmt.Errorf("%s: %s", res.Status, string(body))
|
return nil, fmt.Errorf("%s: %s", res.Status, string(body))
|
||||||
}
|
}
|
||||||
|
|
||||||
type (
|
data := &GetUsersResponse{}
|
||||||
UserData struct {
|
if err := json.NewDecoder(res.Body).Decode(data); err != nil {
|
||||||
ID string `json:"id"`
|
|
||||||
Login string `json:"login"`
|
|
||||||
DisplayName string `json:"display_name"`
|
|
||||||
Type string `json:"type"`
|
|
||||||
BroadcasterType string `json:"broadcaster_type"`
|
|
||||||
Description string `json:"description"`
|
|
||||||
ProfileImageURL string `json:"profile_image_url"`
|
|
||||||
OfflineImageURL string `json:"offline_image_url"`
|
|
||||||
ViewCount int `json:"view_count"`
|
|
||||||
Email string `json:"email"`
|
|
||||||
CreatedAt string `json:"created_at"`
|
|
||||||
}
|
|
||||||
|
|
||||||
Response struct {
|
|
||||||
Users []UserData `json:"data"`
|
|
||||||
}
|
|
||||||
)
|
|
||||||
data := Response{}
|
|
||||||
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
userIDs := []string{}
|
return data, nil
|
||||||
for _, user := range data.Users {
|
|
||||||
userIDs = append(userIDs, user.ID)
|
|
||||||
}
|
|
||||||
|
|
||||||
return userIDs, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (srv *Service) registerEventSubSession(session *api.EventSubSession) error {
|
func (srv *Service) registerEventSubSession(session *api.EventSubSession) error {
|
||||||
|
|
@ -89,13 +80,13 @@ func (srv *Service) handleNotification(payload *api.EventSubPayload) error {
|
||||||
var event api.FollowEvent
|
var event api.FollowEvent
|
||||||
err := json.Unmarshal(jsonData, &event)
|
err := json.Unmarshal(jsonData, &event)
|
||||||
if err != nil { return fmt.Errorf("Failed to cast to api.FollowEvent: %v", err) }
|
if err != nil { return fmt.Errorf("Failed to cast to api.FollowEvent: %v", err) }
|
||||||
log.Printf("New follow: %s", event.UserLogin)
|
log.Printf("New follow: %s", event.UserName)
|
||||||
srv.labels.LatestFollower.Text = event.UserLogin
|
srv.labels.LatestFollower.Text = event.UserName
|
||||||
srv.labels.LatestFollower.C <- event.UserLogin
|
srv.labels.LatestFollower.C <- event.UserName
|
||||||
|
|
||||||
if err := os.WriteFile(
|
if err := os.WriteFile(
|
||||||
path.Join(DATA_PATH, "state", LABEL_LATEST_FOLLOWER),
|
path.Join(DATA_PATH, "state", LABEL_LATEST_FOLLOWER),
|
||||||
[]byte(event.UserLogin), 0640,
|
[]byte(event.UserName), 0640,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return fmt.Errorf("Failed to write %s state: %v", LABEL_LATEST_FOLLOWER, err)
|
return fmt.Errorf("Failed to write %s state: %v", LABEL_LATEST_FOLLOWER, err)
|
||||||
}
|
}
|
||||||
|
|
@ -104,13 +95,13 @@ func (srv *Service) handleNotification(payload *api.EventSubPayload) error {
|
||||||
var event api.SubscribeEvent
|
var event api.SubscribeEvent
|
||||||
err := json.Unmarshal(jsonData, &event)
|
err := json.Unmarshal(jsonData, &event)
|
||||||
if err != nil { return fmt.Errorf("Failed to cast to api.SubscribeEvent: %v", err) }
|
if err != nil { return fmt.Errorf("Failed to cast to api.SubscribeEvent: %v", err) }
|
||||||
log.Printf("New subscription: %s", event.UserLogin)
|
log.Printf("New subscription: %s", event.UserName)
|
||||||
srv.labels.LatestSubscriber.Text = event.UserLogin
|
srv.labels.LatestSubscriber.Text = event.UserName
|
||||||
srv.labels.LatestSubscriber.C <- event.UserLogin
|
srv.labels.LatestSubscriber.C <- event.UserName
|
||||||
|
|
||||||
if err := os.WriteFile(
|
if err := os.WriteFile(
|
||||||
path.Join(DATA_PATH, "state", LABEL_LATEST_SUBSCRIBER),
|
path.Join(DATA_PATH, "state", LABEL_LATEST_SUBSCRIBER),
|
||||||
[]byte(event.UserLogin), 0640,
|
[]byte(event.UserName), 0640,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return fmt.Errorf("Failed to write %s state: %v", LABEL_LATEST_SUBSCRIBER, err)
|
return fmt.Errorf("Failed to write %s state: %v", LABEL_LATEST_SUBSCRIBER, err)
|
||||||
}
|
}
|
||||||
|
|
@ -119,13 +110,13 @@ func (srv *Service) handleNotification(payload *api.EventSubPayload) error {
|
||||||
var event api.CheerEvent
|
var event api.CheerEvent
|
||||||
err := json.Unmarshal(jsonData, &event)
|
err := json.Unmarshal(jsonData, &event)
|
||||||
if err != nil { return fmt.Errorf("Failed to cast to api.CheerEvent: %v", err) }
|
if err != nil { return fmt.Errorf("Failed to cast to api.CheerEvent: %v", err) }
|
||||||
log.Printf("%s cheered x%d bits: %s", event.UserLogin, event.Bits, event.Message)
|
log.Printf("%s cheered x%d bits: %s", event.UserName, event.Bits, event.Message)
|
||||||
srv.labels.LatestCheer.Text = event.UserLogin
|
srv.labels.LatestCheer.Text = event.UserName
|
||||||
srv.labels.LatestCheer.C <- event.UserLogin
|
srv.labels.LatestCheer.C <- event.UserName
|
||||||
|
|
||||||
if err := os.WriteFile(
|
if err := os.WriteFile(
|
||||||
path.Join(DATA_PATH, "state", LABEL_LATEST_CHEER),
|
path.Join(DATA_PATH, "state", LABEL_LATEST_CHEER),
|
||||||
[]byte(event.UserLogin), 0640,
|
[]byte(event.UserName), 0640,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return fmt.Errorf("Failed to write %s state: %v", LABEL_LATEST_CHEER, err)
|
return fmt.Errorf("Failed to write %s state: %v", LABEL_LATEST_CHEER, err)
|
||||||
}
|
}
|
||||||
|
|
@ -134,7 +125,7 @@ func (srv *Service) handleNotification(payload *api.EventSubPayload) error {
|
||||||
var event api.RaidEvent
|
var event api.RaidEvent
|
||||||
err := json.Unmarshal(jsonData, &event)
|
err := json.Unmarshal(jsonData, &event)
|
||||||
if err != nil { return fmt.Errorf("Failed to cast to api.RaidEvent: %v", err) }
|
if err != nil { return fmt.Errorf("Failed to cast to api.RaidEvent: %v", err) }
|
||||||
log.Printf("%s is now raiding with %d viewers!", event.FromUserLogin, event.Viewers)
|
log.Printf("%s is now raiding with %d viewers!", event.FromUserName, event.Viewers)
|
||||||
|
|
||||||
case "channel.channel_points_custom_reward_redemption.add":
|
case "channel.channel_points_custom_reward_redemption.add":
|
||||||
var event api.ChannelPointCustomRewardRedeemEvent
|
var event api.ChannelPointCustomRewardRedeemEvent
|
||||||
|
|
@ -142,7 +133,7 @@ func (srv *Service) handleNotification(payload *api.EventSubPayload) error {
|
||||||
if err != nil { return fmt.Errorf("Failed to cast to api.ChannelPointCustomRewardRedeemEvent: %v", err) }
|
if err != nil { return fmt.Errorf("Failed to cast to api.ChannelPointCustomRewardRedeemEvent: %v", err) }
|
||||||
log.Printf(
|
log.Printf(
|
||||||
"%s just redeemed %s for %d channel points.",
|
"%s just redeemed %s for %d channel points.",
|
||||||
event.UserLogin,
|
event.UserName,
|
||||||
event.Reward.Title,
|
event.Reward.Title,
|
||||||
event.Reward.Cost,
|
event.Reward.Cost,
|
||||||
)
|
)
|
||||||
|
|
@ -153,8 +144,8 @@ func (srv *Service) handleNotification(payload *api.EventSubPayload) error {
|
||||||
if err != nil { return fmt.Errorf("Failed to cast to api.ShoutoutCreate: %v", err) }
|
if err != nil { return fmt.Errorf("Failed to cast to api.ShoutoutCreate: %v", err) }
|
||||||
log.Printf(
|
log.Printf(
|
||||||
"%s gave a shoutout to %s.",
|
"%s gave a shoutout to %s.",
|
||||||
event.FromUserLogin,
|
event.FromUserName,
|
||||||
event.ToUserLogin,
|
event.ToUserName,
|
||||||
)
|
)
|
||||||
|
|
||||||
case "channel.chat.message":
|
case "channel.chat.message":
|
||||||
|
|
@ -341,3 +332,113 @@ func (srv *Service) subscribeToDefaultEvents(ctx context.Context) {
|
||||||
log.Printf("Failed to subscribe to channel.chat.message_delete: %v", err)
|
log.Printf("Failed to subscribe to channel.chat.message_delete: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type (
|
||||||
|
Follower struct {
|
||||||
|
FollowedAt string `json:"followed_at"`
|
||||||
|
UserID string `json:"user_id"`
|
||||||
|
UserLogin string `json:"user_login"`
|
||||||
|
UserName string `json:"user_name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
Pagination struct {
|
||||||
|
Cursor string `json:"cursor"`
|
||||||
|
}
|
||||||
|
|
||||||
|
GetFollowersResponse struct {
|
||||||
|
Data []Follower `json:"data"`
|
||||||
|
Pagination Pagination `json:"pagination"`
|
||||||
|
Total int `json:"total"`
|
||||||
|
Points int `json:"points"`
|
||||||
|
}
|
||||||
|
)
|
||||||
|
func (srv *Service) getFollowers(
|
||||||
|
ctx context.Context,
|
||||||
|
broadcasterID string,
|
||||||
|
limit int,
|
||||||
|
) (*GetFollowersResponse, error) {
|
||||||
|
client := srv.oauthConfig.Client(ctx, srv.oauthToken)
|
||||||
|
u, err := url.Parse(api.BASE_URL + "/channels/followers?" + url.Values{
|
||||||
|
"broadcaster_id": []string{ broadcasterID },
|
||||||
|
"first": []string { strconv.Itoa(limit) },
|
||||||
|
}.Encode())
|
||||||
|
req, err := http.NewRequest("GET", u.String(), nil)
|
||||||
|
if err != nil { return nil, err }
|
||||||
|
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
req.Header.Set("Client-Id", srv.clientID)
|
||||||
|
req.Header.Set("Authorization", "Bearer " + srv.oauthToken.AccessToken)
|
||||||
|
|
||||||
|
res, err := client.Do(req)
|
||||||
|
if err != nil { return nil, err }
|
||||||
|
|
||||||
|
if res.StatusCode != http.StatusOK {
|
||||||
|
body, _ := io.ReadAll(res.Body)
|
||||||
|
return nil, fmt.Errorf("%s: %s", res.Status, string(body))
|
||||||
|
}
|
||||||
|
|
||||||
|
resData := &GetFollowersResponse{}
|
||||||
|
if err := json.NewDecoder(res.Body).Decode(resData); err != nil { return nil, err }
|
||||||
|
|
||||||
|
return resData, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type (
|
||||||
|
Subscriber struct {
|
||||||
|
BroadcasterID string `json:"broadcaster_id"`
|
||||||
|
BroadcasterLogin string `json:"broadcaster_login"`
|
||||||
|
BroadcasterName string `json:"broadcaster_name"`
|
||||||
|
|
||||||
|
GifterID string `json:"gifter_id"`
|
||||||
|
GifterLogin string `json:"gifter_login"`
|
||||||
|
GifterName string `json:"gifter_name"`
|
||||||
|
|
||||||
|
IsGift bool `json:"is_gift"`
|
||||||
|
Tier string `json:"tier"`
|
||||||
|
PlanName string `json:"plan_name"`
|
||||||
|
|
||||||
|
UserID string `json:"user_id"`
|
||||||
|
UserLogin string `json:"user_login"`
|
||||||
|
UserName string `json:"user_name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
GetSubscribersResponse struct {
|
||||||
|
Data []Subscriber `json:"data"`
|
||||||
|
Pagination Pagination `json:"pagination"`
|
||||||
|
Total int `json:"total"`
|
||||||
|
Points int `json:"points"`
|
||||||
|
}
|
||||||
|
)
|
||||||
|
// Unfortunately, this function is very unreliable for pulling chronological
|
||||||
|
// subscription records. Twitch API does not currently provide a mechanism for
|
||||||
|
// fetching the latest subscriber; this will need to be tracked manually.
|
||||||
|
func (srv *Service) getSubscribers(
|
||||||
|
ctx context.Context,
|
||||||
|
broadcasterID string,
|
||||||
|
limit int,
|
||||||
|
) (*GetSubscribersResponse, error) {
|
||||||
|
client := srv.oauthConfig.Client(ctx, srv.oauthToken)
|
||||||
|
u, err := url.Parse(api.BASE_URL + "/subscriptions?" + url.Values{
|
||||||
|
"broadcaster_id": []string{ broadcasterID },
|
||||||
|
"first": []string { strconv.Itoa(limit) },
|
||||||
|
}.Encode())
|
||||||
|
req, err := http.NewRequest("GET", u.String(), nil)
|
||||||
|
if err != nil { return nil, err }
|
||||||
|
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
req.Header.Set("Client-Id", srv.clientID)
|
||||||
|
req.Header.Set("Authorization", "Bearer " + srv.oauthToken.AccessToken)
|
||||||
|
|
||||||
|
res, err := client.Do(req)
|
||||||
|
if err != nil { return nil, err }
|
||||||
|
|
||||||
|
if res.StatusCode != http.StatusOK {
|
||||||
|
body, _ := io.ReadAll(res.Body)
|
||||||
|
return nil, fmt.Errorf("%s: %s", res.Status, string(body))
|
||||||
|
}
|
||||||
|
|
||||||
|
resData := &GetSubscribersResponse{}
|
||||||
|
if err := json.NewDecoder(res.Body).Decode(resData); err != nil { return nil, err }
|
||||||
|
|
||||||
|
return resData, nil
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -40,8 +40,6 @@ async function sync() {
|
||||||
console.error("Connection lost, or an error has occurred.");
|
console.error("Connection lost, or an error has occurred.");
|
||||||
console.log("Attempting to reconnect...");
|
console.log("Attempting to reconnect...");
|
||||||
|
|
||||||
// TODO: stop reconnecting after 10 failed attempts
|
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
sync();
|
sync();
|
||||||
}, 1000);
|
}, 1000);
|
||||||
|
|
|
||||||
|
|
@ -124,17 +124,14 @@ func New(ctx context.Context, opts ServiceOptions) (*Service, error) {
|
||||||
port: opts.Port,
|
port: opts.Port,
|
||||||
labels: &twitchLabels{
|
labels: &twitchLabels{
|
||||||
LatestFollower: &twitchLabel{
|
LatestFollower: &twitchLabel{
|
||||||
Text: "some_follower",
|
|
||||||
C: latestFollowerC,
|
C: latestFollowerC,
|
||||||
Broadcast: latestFollowerBroadcast,
|
Broadcast: latestFollowerBroadcast,
|
||||||
},
|
},
|
||||||
LatestSubscriber: &twitchLabel{
|
LatestSubscriber: &twitchLabel{
|
||||||
Text: "some_subscriber",
|
|
||||||
C: latestSubscriberC,
|
C: latestSubscriberC,
|
||||||
Broadcast: latestSubscriberBroadcast,
|
Broadcast: latestSubscriberBroadcast,
|
||||||
},
|
},
|
||||||
LatestCheer: &twitchLabel{
|
LatestCheer: &twitchLabel{
|
||||||
Text: "some_cheer",
|
|
||||||
C: latestCheerC,
|
C: latestCheerC,
|
||||||
Broadcast: latestCheerBroadcast,
|
Broadcast: latestCheerBroadcast,
|
||||||
},
|
},
|
||||||
|
|
@ -176,19 +173,6 @@ func New(ctx context.Context, opts ServiceOptions) (*Service, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if data, err := os.ReadFile(path.Join(DATA_PATH, "state", LABEL_LATEST_FOLLOWER)); err == nil {
|
|
||||||
srv.labels.LatestFollower.Text = string(data)
|
|
||||||
log.Printf("Loaded latest follower: %s", string(data))
|
|
||||||
}
|
|
||||||
if data, err := os.ReadFile(path.Join(DATA_PATH, "state", LABEL_LATEST_SUBSCRIBER)); err == nil {
|
|
||||||
srv.labels.LatestSubscriber.Text = string(data)
|
|
||||||
log.Printf("Loaded latest subscriber: %s", string(data))
|
|
||||||
}
|
|
||||||
if data, err := os.ReadFile(path.Join(DATA_PATH, "state", LABEL_LATEST_CHEER)); err == nil {
|
|
||||||
srv.labels.LatestCheer.Text = string(data)
|
|
||||||
log.Printf("Loaded latest cheer: %s", string(data))
|
|
||||||
}
|
|
||||||
|
|
||||||
return srv, nil
|
return srv, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -282,27 +266,44 @@ func (srv *Service) Run(ctx context.Context) {
|
||||||
if srv.oauthToken == nil || !srv.oauthToken.Valid() {
|
if srv.oauthToken == nil || !srv.oauthToken.Valid() {
|
||||||
log.Printf("Log in with Twitch: http://localhost:%d/twitch/login", srv.port)
|
log.Printf("Log in with Twitch: http://localhost:%d/twitch/login", srv.port)
|
||||||
} else {
|
} else {
|
||||||
srv.start(ctx)
|
if err := srv.start(ctx); err != nil {
|
||||||
|
log.Printf("Failed to start Twitch service: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (srv *Service) start(ctx context.Context) {
|
func (srv *Service) start(ctx context.Context) error {
|
||||||
userIDs, err := srv.userIDsFromNames([]string{ srv.channelName })
|
if res, err := srv.getUsers(nil, []string{ srv.channelName }); err == nil {
|
||||||
if err != nil {
|
if len(res.Data) == 0 {
|
||||||
log.Printf(
|
return fmt.Errorf("resolve username \"%s\", it may not exist?", srv.channelName)
|
||||||
"Failed to resolve Twitch username \"%s\": %v",
|
}
|
||||||
srv.channelName,
|
srv.channelID = res.Data[0].ID
|
||||||
err,
|
} else { return fmt.Errorf("fetch Twitch users: %v", err) }
|
||||||
)
|
|
||||||
return
|
if res, err := srv.getFollowers(ctx, srv.channelID, 1); err == nil {
|
||||||
|
if len(res.Data) > 0 {
|
||||||
|
srv.labels.LatestFollower.Text = res.Data[0].UserName
|
||||||
|
log.Printf("Loaded latest follower: %s", res.Data[0].UserName)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.Printf("Failed to fetch latest follower: %v", err)
|
||||||
}
|
}
|
||||||
if len(userIDs) == 0 {
|
|
||||||
fmt.Printf("Failed to resolve username \"%s\", it may not exist?", srv.channelName)
|
// TODO: build satellite service for offline-fetching subscribers and cheers
|
||||||
return
|
|
||||||
|
if data, err := os.ReadFile(path.Join(DATA_PATH, "state", LABEL_LATEST_SUBSCRIBER)); err == nil {
|
||||||
|
srv.labels.LatestSubscriber.Text = string(data)
|
||||||
|
log.Printf("Loaded latest subscriber: %s", string(data))
|
||||||
|
}
|
||||||
|
|
||||||
|
if data, err := os.ReadFile(path.Join(DATA_PATH, "state", LABEL_LATEST_CHEER)); err == nil {
|
||||||
|
srv.labels.LatestCheer.Text = string(data)
|
||||||
|
log.Printf("Loaded latest cheer: %s", string(data))
|
||||||
}
|
}
|
||||||
srv.channelID = userIDs[0]
|
|
||||||
|
|
||||||
srv.startWebsocketListener(ctx)
|
srv.startWebsocketListener(ctx)
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (srv *Service) startWebsocketListener(ctx context.Context) {
|
func (srv *Service) startWebsocketListener(ctx context.Context) {
|
||||||
|
|
@ -341,7 +342,7 @@ func (srv *Service) startWebsocketListener(ctx context.Context) {
|
||||||
|
|
||||||
srv.subscribeToDefaultEvents(ctx)
|
srv.subscribeToDefaultEvents(ctx)
|
||||||
|
|
||||||
log.Printf("Connected to Twitch as %s.", srv.channelName)
|
log.Printf("Connected to Twitch for channel %s.", srv.channelName)
|
||||||
}
|
}
|
||||||
|
|
||||||
if message.Metadata.MessageType == string(api.NOTIFICATION) {
|
if message.Metadata.MessageType == string(api.NOTIFICATION) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue