61 lines
1.1 KiB
Go
61 lines
1.1 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"io/ioutil"
|
||
|
"log"
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
|
type Vtuber struct {
|
||
|
Name string
|
||
|
ChannelID string
|
||
|
CurrentStream string
|
||
|
LiveMsg string
|
||
|
}
|
||
|
|
||
|
func NewVtuber(name, channelID, liveMsg string) *Vtuber {
|
||
|
return &Vtuber{
|
||
|
Name: name,
|
||
|
ChannelID: channelID,
|
||
|
CurrentStream: "",
|
||
|
LiveMsg: liveMsg,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (v *Vtuber) IsLive() bool {
|
||
|
return v.CurrentStream != ""
|
||
|
}
|
||
|
|
||
|
func (v *Vtuber) Update() error {
|
||
|
url := fmt.Sprintf("https://holodex.net/api/v2/live?channel_id=%s&lang=all&sort=available_at&order=desc&limit=25&offset=0&paginated=%3Cempty%3E", v.ChannelID)
|
||
|
req, err := http.NewRequest("GET", url, nil)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
res, err := http.DefaultClient.Do(req)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
defer res.Body.Close()
|
||
|
jsonBody, _ := ioutil.ReadAll(res.Body)
|
||
|
var sl StreamList
|
||
|
err = json.Unmarshal(jsonBody, &sl)
|
||
|
if err != nil {
|
||
|
log.Println("error parsing json for vtuber")
|
||
|
return err
|
||
|
}
|
||
|
found := false
|
||
|
for _, s := range sl.Streams {
|
||
|
if s.Status == "live" {
|
||
|
v.CurrentStream = s.ID
|
||
|
found = true
|
||
|
}
|
||
|
}
|
||
|
if !found {
|
||
|
v.CurrentStream = ""
|
||
|
}
|
||
|
return nil
|
||
|
}
|