2023-12-18 21:15:55 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2023-12-24 21:18:42 +00:00
|
|
|
"log"
|
2023-12-18 21:15:55 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
rumblelivestreamlib "github.com/tylertravisty/rumble-livestream-lib-go"
|
|
|
|
"github.com/wailsapp/wails/v2/pkg/runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Api struct {
|
2023-12-21 20:20:43 +00:00
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
cancelMu sync.Mutex
|
2023-12-24 21:18:42 +00:00
|
|
|
logError *log.Logger
|
|
|
|
logInfo *log.Logger
|
2023-12-21 20:20:43 +00:00
|
|
|
querying bool
|
|
|
|
queryingMu sync.Mutex
|
2023-12-18 21:15:55 +00:00
|
|
|
}
|
|
|
|
|
2023-12-24 21:18:42 +00:00
|
|
|
func NewApi(logError *log.Logger, logInfo *log.Logger) *Api {
|
|
|
|
return &Api{logError: logError, logInfo: logInfo}
|
2023-12-18 21:15:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Api) Startup(ctx context.Context) {
|
|
|
|
a.ctx = ctx
|
|
|
|
}
|
|
|
|
|
2023-12-21 20:20:43 +00:00
|
|
|
func (a *Api) Start(url string, interval time.Duration) error {
|
2023-12-24 21:18:42 +00:00
|
|
|
a.logInfo.Println("Api.Start")
|
2023-12-18 21:15:55 +00:00
|
|
|
if url == "" {
|
|
|
|
return fmt.Errorf("empty stream key")
|
|
|
|
}
|
|
|
|
|
|
|
|
a.queryingMu.Lock()
|
|
|
|
start := !a.querying
|
|
|
|
a.querying = true
|
|
|
|
a.queryingMu.Unlock()
|
|
|
|
|
|
|
|
if start {
|
2023-12-24 21:18:42 +00:00
|
|
|
a.logInfo.Println("Start querying")
|
2023-12-18 21:15:55 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
a.cancelMu.Lock()
|
|
|
|
a.cancel = cancel
|
|
|
|
a.cancelMu.Unlock()
|
2023-12-21 20:20:43 +00:00
|
|
|
go a.start(ctx, url, interval)
|
2023-12-18 21:15:55 +00:00
|
|
|
} else {
|
2023-12-24 21:18:42 +00:00
|
|
|
a.logInfo.Println("Querying already started")
|
2023-12-18 21:15:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Api) Stop() {
|
2023-12-24 21:18:42 +00:00
|
|
|
a.logInfo.Println("Stop querying")
|
2023-12-18 21:15:55 +00:00
|
|
|
a.cancelMu.Lock()
|
|
|
|
if a.cancel != nil {
|
|
|
|
a.cancel()
|
|
|
|
}
|
|
|
|
a.cancelMu.Unlock()
|
|
|
|
}
|
|
|
|
|
2023-12-21 20:20:43 +00:00
|
|
|
func (a *Api) start(ctx context.Context, url string, interval time.Duration) {
|
2023-12-18 21:15:55 +00:00
|
|
|
for {
|
|
|
|
a.query(url)
|
|
|
|
timer := time.NewTimer(interval)
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
a.queryingMu.Lock()
|
|
|
|
a.querying = false
|
|
|
|
a.queryingMu.Unlock()
|
|
|
|
timer.Stop()
|
|
|
|
return
|
|
|
|
case <-timer.C:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Api) query(url string) {
|
2024-01-30 17:24:07 +00:00
|
|
|
// a.logInfo.Println("QueryAPI")
|
2023-12-18 21:15:55 +00:00
|
|
|
client := rumblelivestreamlib.Client{StreamKey: url}
|
|
|
|
resp, err := client.Request()
|
|
|
|
if err != nil {
|
2023-12-24 21:18:42 +00:00
|
|
|
a.logError.Println("api: error executing client request:", err)
|
2023-12-19 21:26:11 +00:00
|
|
|
a.Stop()
|
|
|
|
runtime.EventsEmit(a.ctx, "QueryResponseError", "Failed to query API")
|
|
|
|
return
|
2023-12-18 21:15:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// resp := &rumblelivestreamlib.LivestreamResponse{}
|
|
|
|
|
|
|
|
// resp.Followers.RecentFollowers = append(resp.Followers.RecentFollowers, rumblelivestreamlib.Follower{"tyler-follow", "2023-12-12T21:53:34-04:00"})
|
|
|
|
// resp.Subscribers.RecentSubscribers = append(resp.Subscribers.RecentSubscribers, rumblelivestreamlib.Subscriber{"tyler-sub", "tyler-sub", 500, 5, "2023-12-14T21:53:34-04:00"})
|
|
|
|
// resp.Subscribers.RecentSubscribers = append(resp.Subscribers.RecentSubscribers, rumblelivestreamlib.Subscriber{"tyler-sub", "tyler-sub", 500, 5, "2023-12-13T21:53:34-04:00"})
|
|
|
|
// resp.Subscribers.RecentSubscribers = append(resp.Subscribers.RecentSubscribers, rumblelivestreamlib.Subscriber{"tyler-sub", "tyler-sub", 500, 5, "2023-11-13T21:53:34-04:00"})
|
|
|
|
// resp.Livestreams = []rumblelivestreamlib.Livestream{
|
|
|
|
// {
|
|
|
|
// CreatedOn: "2023-12-16T16:13:30+00:00",
|
|
|
|
// WatchingNow: 4},
|
|
|
|
// }
|
|
|
|
runtime.EventsEmit(a.ctx, "QueryResponse", &resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: if start errors, send event
|