diff --git a/api/channel.go b/api/channel.go index db4388c..83250e5 100644 --- a/api/channel.go +++ b/api/channel.go @@ -7,6 +7,48 @@ import ( "github.com/gempir/go-twitch-irc/v3" ) +// RandomQuoteJSON response when request a random message +type RandomChannelQuoteJSON struct { + Channel string `json:"channel"` + Username string `json:"username"` + DisplayName string `json:"displayName"` + Message string `json:"message"` + Timestamp timestamp `json:"timestamp"` +} + +// swagger:route GET /channel/{channel}/random logs randomChannelLog +// +// Get a random line from the entire channel log's history +// +// Produces: +// - application/json +// - text/plain +// +// Responses: +// 200: chatLog + +// swagger:route GET /channelid/{channelid}/random logs randomChannelLog +// +// Get a random line from the entire channel log's history +// +// Produces: +// - application/json +// - text/plain +// +// Responses: +// 200: chatLog +func (s *Server) getChannelRandomQuote(request logRequest) (*chatLog, error) { + rawMessage, err := s.fileLogger.ReadRandomMessageForChannel(request.channelid) + if err != nil { + return &chatLog{}, err + } + parsedMessage := twitch.ParseMessage(rawMessage) + + chatMsg := createChatMessage(parsedMessage) + + return &chatLog{Messages: []chatMessage{chatMsg}}, nil +} + // swagger:route GET /channel/{channel} logs channelLogs // // Get entire channel logs of current day diff --git a/api/logrequest.go b/api/logrequest.go index 92649b7..3a35a84 100644 --- a/api/logrequest.go +++ b/api/logrequest.go @@ -103,6 +103,8 @@ func (s *Server) newLogRequestFromURL(r *http.Request) (logRequest, error) { request.time.day = params[5] } else if request.isUserRequest && len(params) == 6 && params[5] == "random" { request.time.random = true + } else if request.isChannelRequest && len(params) == 4 && params[3] == "random" { + request.time.random = true } else { if request.isChannelRequest { request.time.year = fmt.Sprintf("%d", time.Now().Year()) diff --git a/api/server.go b/api/server.go index c4436e9..a8bf251 100644 --- a/api/server.go +++ b/api/server.go @@ -195,7 +195,12 @@ func (s *Server) routeLogs(w http.ResponseWriter, r *http.Request) bool { var logs *chatLog if request.time.random { - logs, err = s.getRandomQuote(request) + if requst.isUserRequest { + logs, err = s.getRandomQuote(request) + } + else { + logs, err = s.getChannelRandomQuote(request) + } } else if request.time.from != "" && request.time.to != "" { if request.isUserRequest { logs, err = s.getUserLogsRange(request) diff --git a/filelog/channellog.go b/filelog/channellog.go index c125970..463ed03 100644 --- a/filelog/channellog.go +++ b/filelog/channellog.go @@ -123,3 +123,53 @@ func (l *Logger) ReadLogForChannel(channelID string, year int, month int, day in return content, nil } + +func (l *Logger) ReadRandomMessageForChannel(channelID) (string, error) { + var days []string + var lines []string + + if channelID == "" { + return "", errors.New("missing channelID") + } + + years, _ := ioutil.ReadDir(l.logPath + "/" + channelID) + + for _, yearDir := range years { + year := yearDir.Name() + months, _ := ioutil.ReadDir(l.logPath + "/" + channelID + "/" + year + "/") + for _, monthDir := range months { + month := monthDir.Name() + path := fmt.Sprintf("%s/%s/%s/%s/%s.txt", l.logPath, channelID, year, month, userID) + if _, err := os.Stat(path); err == nil { + days = append(days, path) + } else if _, err := os.Stat(path + ".gz"); err == nil { + days = append(days, path+".gz") + } + } + } + + if len(days) < 1 { + return "", errors.New("no log found") + } + + randomDayIndex := rand.Intn(len(days)) + randomDayPath := days[randomDayIndex] + + f, _ := os.Open(randomDayPath) + scanner := bufio.NewScanner(f) + + if strings.HasSuffix(randomDayPath, ".gz") { + gz, _ := gzip.NewReader(f) + scanner = bufio.NewScanner(gz) + } + + for scanner.Scan() { + line := scanner.Text() + lines = append(lines, line) + } + + f.Close() + + randomLineNumber := rand.Intn(len(lines)) + return lines[randomLineNumber], nil +}