Skip to content

Commit

Permalink
Add GetRoomInfo method, fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Davincible committed Jun 7, 2022
1 parent dda499f commit 9d1ed19
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 8 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,21 @@ func NewTikTok() *TikTok {}
// TrackUser will start to track the livestream of a user, if live.
// To listen to events emitted by the livestream, such as comments and viewer
// count, listen to the Live.Events channel.
// It will start a go routine and connect to the tiktok websocket.
func (t *TikTok) TrackUser(username string) (*Live, error) {}

// TrackRoom will start to track a room by room ID.
// It will start a go routine and connect to the tiktok websocket.
func (t *TikTok) TrackRoom(roomId string) (*Live, error) {}

// GetUserInfo will fetch information about the user, such as follwers stats,
// their user ID, as well as the RoomID, with which you can tell if they are live.
func (t *TikTok) GetUserInfo(user string) (*UserInfo, error) {}

// GetRoomInfo will only fetch the room info, normally available with Live.Info
// but not start tracking a live stream.
func (t *TikTok) GetRoomInfo(username string) (*RoomInfo, error) {}

// GetPriceList fetches the price list of tiktok coins. Prices will be given in
// USD cents and the cents equivalent of the local currency of the IP location.
// To fetch a different currency, use a VPN or proxy to change your IP to a
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require github.com/gobwas/ws v1.1.0
require (
github.com/gobwas/httphead v0.1.0 // indirect
github.com/gobwas/pool v0.2.1 // indirect
github.com/pkg/errors v0.9.1
golang.org/x/net v0.0.0-20220225172249-27dd8689420f
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
google.golang.org/protobuf v1.27.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ github.com/gobwas/ws v1.1.0/go.mod h1:nzvNcVha5eUziGrbxFCo6qFIojQHjJV5cLYIbezhfL
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
golang.org/x/net v0.0.0-20220225172249-27dd8689420f h1:oA4XRj0qtSt8Yo1Zms0CUlsT3KG69V2UGQWPBxujDmc=
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/sys v0.0.0-20201207223542-d4d67f95c62d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
35 changes: 30 additions & 5 deletions live.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gotiktoklive

import (
"encoding/json"
"errors"
"fmt"
"io"
"net"
Expand All @@ -13,6 +12,7 @@ import (
"time"

pb "github.com/Davincible/gotiktoklive/proto"
"github.com/pkg/errors"
"golang.org/x/net/context"

"google.golang.org/protobuf/proto"
Expand Down Expand Up @@ -54,10 +54,13 @@ func (t *TikTok) newLive(roomId string) *Live {

ctx, cancel := context.WithCancel(context.Background())
live.done = ctx.Done
o := sync.Once{}
live.close = func() {
cancel()
t.wg.Wait()
close(live.Events)
o.Do(func() {
cancel()
t.wg.Wait()
close(live.Events)
})
}

return &live
Expand Down Expand Up @@ -88,9 +91,30 @@ func (l *Live) fetchRoom() error {
return nil
}

// GetRoomInfo will only fetch the room info, normally available with live.Info
// but not start tracking a live stream.
func (t *TikTok) GetRoomInfo(username string) (*RoomInfo, error) {
id, err := t.getRoomID(username)
if err != nil {
return nil, errors.Wrap(err, "Failed to fetch room ID by username")
}

l := Live{
t: t,
ID: id,
}

roomInfo, err := l.getRoomInfo()
if err != nil {
return nil, errors.Wrap(err, "Failed to fetch room info")
}
return roomInfo, nil
}

// TrackUser will start to track the livestream of a user, if live.
// To listen to events emitted by the livestream, such as comments and viewer
// count, listen to the Live.Events channel.
// It will start a go routine and connect to the tiktok websocket.
func (t *TikTok) TrackUser(username string) (*Live, error) {
id, err := t.getRoomID(username)
if err != nil {
Expand All @@ -100,7 +124,8 @@ func (t *TikTok) TrackUser(username string) (*Live, error) {
return t.TrackRoom(id)
}

// TrackRoom will start to track a room by room ID
// TrackRoom will start to track a room by room ID.
// It will start a go routine and connect to the tiktok websocket.
func (t *TikTok) TrackRoom(roomId string) (*Live, error) {
live := t.newLive(roomId)

Expand Down
2 changes: 1 addition & 1 deletion tests/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package tests

const (
USERNAME = "promobot.robots"
USERNAME = "sp.cosplay"

)
11 changes: 11 additions & 0 deletions tests/tiktok_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,14 @@ func TestUserInfo(t *testing.T) {

t.Logf("Got user info: %+v", info)
}

func TestRoomInfo(t *testing.T) {
tiktok := gotiktoklive.NewTikTok()

info, err := tiktok.GetRoomInfo(USERNAME)
if err != nil {
t.Fatal(err)
}

t.Logf("Got user info: %+v", info)
}
2 changes: 1 addition & 1 deletion wss.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (l *Live) readSocket() {
for {
hdr, err := rd.NextFrame()
if err != nil {
l.t.errHandler(fmt.Errorf("Failed to read websocket message, atempting to reconnect: %w", err))
l.t.warnHandler(fmt.Errorf("Failed to read websocket message, attempting to reconnect: %w", err))
l.wss.Close()
if !l.reconnectWebsocket() {
return
Expand Down
2 changes: 1 addition & 1 deletion wss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestWebsocket(t *testing.T) {

ctx, cancel := context.WithCancel(context.Background())
live.done = ctx.Done
live.Close = func() {
live.close = func() {
cancel()
close(live.Events)
}
Expand Down

0 comments on commit 9d1ed19

Please sign in to comment.