123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- package afreeca
- import (
- "encoding/json"
- "fmt"
- "github.com/samber/lo"
- "github.com/tidwall/gjson"
- "strings"
- )
- type Preset struct {
- Label string `json:"label"`
- Resolution string `json:"label_resolution"`
- Name string `json:"name"`
- Bitrate uint64 `json:"bps"`
- }
- type Channel struct {
- Result int64
- GeoName string
- GeoCode uint64
- AcceptLanguage string
- ServiceLanguage string
- Id uint64
- Title string
- Category string
- BroadcasterId string
- BroadcasterName string
- PasswordRequired bool
- AudienceGrade uint64
- RMD string
- Resolution string
- Bitrate int64
- Presets []Preset
- Token string
- FanToken string
- GatewayIp string
- GatewayPort uint64
- CenterIp string
- CenterPort uint64
- ChatDomain string
- ChatIp string
- ChatPort uint64
- ChatId uint64
- }
- func (c *Channel) Unmarshall(b []byte) error {
- c.Result = gjson.GetBytes(b, "CHANNEL.RESULT").Int()
- c.GeoName = gjson.GetBytes(b, "CHANNEL.geo_cc").String()
- c.GeoCode = gjson.GetBytes(b, "CHANNEL.geo_rc").Uint()
- c.AcceptLanguage = gjson.GetBytes(b, "CHANNEL.acpt_lang").String()
- c.ServiceLanguage = gjson.GetBytes(b, "CHANNEL.svc_lang").String()
- c.Id = gjson.GetBytes(b, "CHANNEL.BNO").Uint()
- c.Title = gjson.GetBytes(b, "CHANNEL.TITLE").String()
- c.Category = gjson.GetBytes(b, "CHANNEL.CATE").String()
- c.BroadcasterId = gjson.GetBytes(b, "CHANNEL.BJID").String()
- c.BroadcasterName = gjson.GetBytes(b, "CHANNEL.BJNICK").String()
- c.PasswordRequired = gjson.GetBytes(b, "CHANNEL.BPWD").String() == "Y"
- c.AudienceGrade = gjson.GetBytes(b, "CHANNEL.GRADE").Uint()
- c.RMD = gjson.GetBytes(b, "CHANNEL.RMD").String()
- c.Resolution = gjson.GetBytes(b, "CHANNEL.RESOLUTION").String()
- c.Bitrate = gjson.GetBytes(b, "CHANNEL.BPS").Int()
- if gjson.GetBytes(b, "CHANNEL.VIEWPRESET").Exists() {
- if err := json.Unmarshal([]byte(gjson.GetBytes(b, "CHANNEL.VIEWPRESET").Raw), &c.Presets); err != nil {
- return err
- }
- }
- c.Token = gjson.GetBytes(b, "CHANNEL.TK").String()
- c.FanToken = gjson.GetBytes(b, "CHANNEL.FTK").String()
- c.GatewayIp = gjson.GetBytes(b, "CHANNEL.GWIP").String()
- c.GatewayPort = gjson.GetBytes(b, "CHANNEL.GWPT").Uint()
- c.CenterIp = gjson.GetBytes(b, "CHANNEL.CTIP").String()
- c.CenterPort = gjson.GetBytes(b, "CHANNEL.CTPT").Uint()
- c.ChatDomain = gjson.GetBytes(b, "CHANNEL.CHDOMAIN").String()
- c.ChatIp = gjson.GetBytes(b, "CHANNEL.CHIP").String()
- c.ChatPort = gjson.GetBytes(b, "CHANNEL.CHPT").Uint()
- c.ChatId = gjson.GetBytes(b, "CHANNEL.CHATNO").Uint()
- return nil
- }
- func (c Channel) IsLoginRequired() bool {
- return c.Result == -6
- }
- func (c Channel) IsGeoRestricted() bool {
- return c.Result == -2
- }
- func (c Channel) ChatServerUrl() string {
- var (
- h = c.ChatIp
- p = c.ChatPort
- )
- if c.ChatDomain != "" {
- h = c.ChatDomain
- }
- return fmt.Sprintf("ws://%s:%d/Websocket/%s", h, p, c.BroadcasterId)
- }
- func (c Channel) GetPreset(s string) *Preset {
- if s = strings.TrimSpace(s); s == "" {
- s = "auto"
- }
- presets := lo.Filter(c.Presets, func(item Preset, _ int) bool {
- return item.Name == s
- })
- if presets == nil {
- return nil
- }
- return &presets[0]
- }
|