chaturbate-dvr/handler/list_channels.go
J0nDoe 3bdae1b872 Updates and refactors
Channel_file.go - fix issue with segments not correctly ending when they were supposed to

Log_type.go - moved log type to it's own file, setup global logging (touches on issue #47)

Main.go - added update_log_level handler, setting global log level

Channel.go, channel_internal.go, channel_util.go - updated to use new log_type

Manager.go - updated to use new log_type, update from .com to .global (issue #74)

Channel_update.go, create_channel.go, delete_channel.go, get_channel.go, get_settings.go, listen_update.go, pause_channel.go, resume_channel.go, terminal_program.go - go fmt / go vet

Chaturbate_channels.json.sample - added sample json of the channels file, for mapping in docker config

List_channels.go - refactored to sort by online status, so online is always at the first ones you see

Script.js - adjust default settings, added pagination, added global log logic

Index.html - updated to use online version of tocas ui, added pagination, added global log logic, visual improvements

Removal of local tocas folder since using online version
2024-10-22 15:04:53 -04:00

99 lines
2.8 KiB
Go

package handler
import (
"net/http"
"sort"
"github.com/gin-gonic/gin"
"github.com/teacat/chaturbate-dvr/chaturbate"
"github.com/urfave/cli/v2"
)
//=======================================================
// Request & Response
//=======================================================
type ListChannelsRequest struct {
}
type ListChannelsResponse struct {
Channels []*ListChannelsResponseChannel `json:"channels"`
}
type ListChannelsResponseChannel struct {
Username string `json:"username"`
ChannelURL string `json:"channel_url"`
Filename string `json:"filename"`
LastStreamedAt string `json:"last_streamed_at"`
SegmentDuration string `json:"segment_duration"`
SplitDuration string `json:"split_duration"`
SegmentFilesize string `json:"segment_filesize"`
SplitFilesize string `json:"split_filesize"`
Interval int `json:"interval"`
IsOnline bool `json:"is_online"`
IsPaused bool `json:"is_paused"`
Logs []string `json:"logs"`
}
//=======================================================
// Factory
//=======================================================
type ListChannelsHandler struct {
chaturbate *chaturbate.Manager
cli *cli.Context
}
func NewListChannelsHandler(c *chaturbate.Manager, cli *cli.Context) *ListChannelsHandler {
return &ListChannelsHandler{c, cli}
}
//=======================================================
// Handle
//=======================================================
// Handle processes the request to list channels, sorting by IsOnline.
func (h *ListChannelsHandler) Handle(c *gin.Context) {
var req *ListChannelsRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
// Fetch channels
channels, err := h.chaturbate.ListChannels()
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
// Sort by IsOnline: online channels first, then offline
sort.SliceStable(channels, func(i, j int) bool {
return channels[i].IsOnline && !channels[j].IsOnline
})
// Populate response
resp := &ListChannelsResponse{
Channels: make([]*ListChannelsResponseChannel, len(channels)),
}
for i, channel := range channels {
resp.Channels[i] = &ListChannelsResponseChannel{
Username: channel.Username,
ChannelURL: channel.ChannelURL,
Filename: channel.Filename(),
LastStreamedAt: channel.LastStreamedAt,
SegmentDuration: channel.SegmentDurationStr(),
SplitDuration: channel.SplitDurationStr(),
SegmentFilesize: channel.SegmentFilesizeStr(),
SplitFilesize: channel.SplitFilesizeStr(),
Interval: channel.Interval,
IsOnline: channel.IsOnline,
IsPaused: channel.IsPaused,
Logs: channel.Logs,
}
}
// Send the response
c.JSON(http.StatusOK, resp)
}