2018-12-03 22:58:45 +01:00
|
|
|
package api
|
|
|
|
|
2020-02-25 21:14:05 +01:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strconv"
|
|
|
|
|
2022-01-12 19:44:06 +01:00
|
|
|
"github.com/gempir/go-twitch-irc/v3"
|
2020-02-25 21:14:05 +01:00
|
|
|
)
|
|
|
|
|
2022-06-21 18:25:32 +02:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-12-03 20:57:35 +01:00
|
|
|
// swagger:route GET /channel/{channel} logs channelLogs
|
|
|
|
//
|
|
|
|
// Get entire channel logs of current day
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// - application/json
|
|
|
|
// - text/plain
|
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: chatLog
|
|
|
|
|
|
|
|
// swagger:route GET /channel/{channel}/{year}/{month}/{day} logs channelLogsYearMonthDay
|
|
|
|
//
|
|
|
|
// Get entire channel logs of given day
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// - application/json
|
|
|
|
// - text/plain
|
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: chatLog
|
2020-02-25 21:44:25 +01:00
|
|
|
func (s *Server) getChannelLogs(request logRequest) (*chatLog, error) {
|
2020-02-25 21:14:05 +01:00
|
|
|
yearStr := request.time.year
|
|
|
|
monthStr := request.time.month
|
|
|
|
dayStr := request.time.day
|
|
|
|
|
|
|
|
year, err := strconv.Atoi(yearStr)
|
|
|
|
if err != nil {
|
|
|
|
return &chatLog{}, errors.New("invalid year")
|
|
|
|
}
|
|
|
|
month, err := strconv.Atoi(monthStr)
|
|
|
|
if err != nil {
|
|
|
|
return &chatLog{}, errors.New("invalid month")
|
|
|
|
}
|
|
|
|
day, err := strconv.Atoi(dayStr)
|
|
|
|
if err != nil {
|
|
|
|
return &chatLog{}, errors.New("invalid day")
|
|
|
|
}
|
|
|
|
|
|
|
|
logMessages, err := s.fileLogger.ReadLogForChannel(request.channelid, year, month, day)
|
|
|
|
if err != nil {
|
|
|
|
return &chatLog{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if request.reverse {
|
|
|
|
reverseSlice(logMessages)
|
|
|
|
}
|
|
|
|
|
2020-11-19 21:22:26 +01:00
|
|
|
logResult := createLogResult()
|
2020-02-25 21:14:05 +01:00
|
|
|
|
|
|
|
for _, rawMessage := range logMessages {
|
2020-11-19 21:22:26 +01:00
|
|
|
logResult.Messages = append(logResult.Messages, createChatMessage(twitch.ParseMessage(rawMessage)))
|
2020-02-25 21:14:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return &logResult, nil
|
|
|
|
}
|
|
|
|
|
2020-02-25 21:44:25 +01:00
|
|
|
func (s *Server) getChannelLogsRange(request logRequest) (*chatLog, error) {
|
2020-12-02 18:35:44 +01:00
|
|
|
fromTime, toTime, err := parseFromTo(request.time.from, request.time.to, channelHourLimit)
|
2020-02-25 21:14:05 +01:00
|
|
|
if err != nil {
|
|
|
|
return &chatLog{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var logMessages []string
|
|
|
|
|
|
|
|
logMessages, _ = s.fileLogger.ReadLogForChannel(request.channelid, fromTime.Year(), int(fromTime.Month()), fromTime.Day())
|
|
|
|
|
|
|
|
if fromTime.Month() != toTime.Month() {
|
|
|
|
additionalMessages, _ := s.fileLogger.ReadLogForChannel(request.channelid, toTime.Year(), int(toTime.Month()), toTime.Day())
|
|
|
|
|
|
|
|
logMessages = append(logMessages, additionalMessages...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if request.reverse {
|
|
|
|
reverseSlice(logMessages)
|
|
|
|
}
|
|
|
|
|
2020-11-19 21:22:26 +01:00
|
|
|
logResult := createLogResult()
|
2020-02-25 21:14:05 +01:00
|
|
|
|
|
|
|
for _, rawMessage := range logMessages {
|
|
|
|
parsedMessage := twitch.ParseMessage(rawMessage)
|
|
|
|
|
2020-11-19 21:22:26 +01:00
|
|
|
switch message := parsedMessage.(type) {
|
2020-02-25 21:14:05 +01:00
|
|
|
case *twitch.PrivateMessage:
|
|
|
|
if message.Time.Unix() < fromTime.Unix() || message.Time.Unix() > toTime.Unix() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case *twitch.ClearChatMessage:
|
|
|
|
if message.Time.Unix() < fromTime.Unix() || message.Time.Unix() > toTime.Unix() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case *twitch.UserNoticeMessage:
|
2020-11-19 21:22:26 +01:00
|
|
|
if message.Time.Unix() < fromTime.Unix() || message.Time.Unix() > toTime.Unix() {
|
|
|
|
continue
|
2020-02-25 21:14:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-19 21:22:26 +01:00
|
|
|
logResult.Messages = append(logResult.Messages, createChatMessage(parsedMessage))
|
2020-02-25 21:14:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return &logResult, nil
|
|
|
|
}
|