channel.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package afreeca
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/samber/lo"
  6. "github.com/tidwall/gjson"
  7. "strings"
  8. )
  9. type Preset struct {
  10. Label string `json:"label"`
  11. Resolution string `json:"label_resolution"`
  12. Name string `json:"name"`
  13. Bitrate uint64 `json:"bps"`
  14. }
  15. type Channel struct {
  16. Result int64
  17. GeoName string
  18. GeoCode uint64
  19. AcceptLanguage string
  20. ServiceLanguage string
  21. Id uint64
  22. Title string
  23. Category string
  24. BroadcasterId string
  25. BroadcasterName string
  26. PasswordRequired bool
  27. AudienceGrade uint64
  28. RMD string
  29. Resolution string
  30. Bitrate int64
  31. Presets []Preset
  32. Token string
  33. FanToken string
  34. GatewayIp string
  35. GatewayPort uint64
  36. CenterIp string
  37. CenterPort uint64
  38. ChatDomain string
  39. ChatIp string
  40. ChatPort uint64
  41. ChatId uint64
  42. }
  43. func (c *Channel) Unmarshall(b []byte) error {
  44. c.Result = gjson.GetBytes(b, "CHANNEL.RESULT").Int()
  45. c.GeoName = gjson.GetBytes(b, "CHANNEL.geo_cc").String()
  46. c.GeoCode = gjson.GetBytes(b, "CHANNEL.geo_rc").Uint()
  47. c.AcceptLanguage = gjson.GetBytes(b, "CHANNEL.acpt_lang").String()
  48. c.ServiceLanguage = gjson.GetBytes(b, "CHANNEL.svc_lang").String()
  49. c.Id = gjson.GetBytes(b, "CHANNEL.BNO").Uint()
  50. c.Title = gjson.GetBytes(b, "CHANNEL.TITLE").String()
  51. c.Category = gjson.GetBytes(b, "CHANNEL.CATE").String()
  52. c.BroadcasterId = gjson.GetBytes(b, "CHANNEL.BJID").String()
  53. c.BroadcasterName = gjson.GetBytes(b, "CHANNEL.BJNICK").String()
  54. c.PasswordRequired = gjson.GetBytes(b, "CHANNEL.BPWD").String() == "Y"
  55. c.AudienceGrade = gjson.GetBytes(b, "CHANNEL.GRADE").Uint()
  56. c.RMD = gjson.GetBytes(b, "CHANNEL.RMD").String()
  57. c.Resolution = gjson.GetBytes(b, "CHANNEL.RESOLUTION").String()
  58. c.Bitrate = gjson.GetBytes(b, "CHANNEL.BPS").Int()
  59. if gjson.GetBytes(b, "CHANNEL.VIEWPRESET").Exists() {
  60. if err := json.Unmarshal([]byte(gjson.GetBytes(b, "CHANNEL.VIEWPRESET").Raw), &c.Presets); err != nil {
  61. return err
  62. }
  63. }
  64. c.Token = gjson.GetBytes(b, "CHANNEL.TK").String()
  65. c.FanToken = gjson.GetBytes(b, "CHANNEL.FTK").String()
  66. c.GatewayIp = gjson.GetBytes(b, "CHANNEL.GWIP").String()
  67. c.GatewayPort = gjson.GetBytes(b, "CHANNEL.GWPT").Uint()
  68. c.CenterIp = gjson.GetBytes(b, "CHANNEL.CTIP").String()
  69. c.CenterPort = gjson.GetBytes(b, "CHANNEL.CTPT").Uint()
  70. c.ChatDomain = gjson.GetBytes(b, "CHANNEL.CHDOMAIN").String()
  71. c.ChatIp = gjson.GetBytes(b, "CHANNEL.CHIP").String()
  72. c.ChatPort = gjson.GetBytes(b, "CHANNEL.CHPT").Uint()
  73. c.ChatId = gjson.GetBytes(b, "CHANNEL.CHATNO").Uint()
  74. return nil
  75. }
  76. func (c Channel) IsLoginRequired() bool {
  77. return c.Result == -6
  78. }
  79. func (c Channel) IsGeoRestricted() bool {
  80. return c.Result == -2
  81. }
  82. func (c Channel) ChatServerUrl() string {
  83. var (
  84. h = c.ChatIp
  85. p = c.ChatPort
  86. )
  87. if c.ChatDomain != "" {
  88. h = c.ChatDomain
  89. }
  90. return fmt.Sprintf("ws://%s:%d/Websocket/%s", h, p, c.BroadcasterId)
  91. }
  92. func (c Channel) GetPreset(s string) *Preset {
  93. if s = strings.TrimSpace(s); s == "" {
  94. s = "auto"
  95. }
  96. presets := lo.Filter(c.Presets, func(item Preset, _ int) bool {
  97. return item.Name == s
  98. })
  99. if presets == nil {
  100. return nil
  101. }
  102. return &presets[0]
  103. }