initial batch of changes
This commit is contained in:
parent
119b6808d1
commit
9927d7041a
4 changed files with 100 additions and 1 deletions
|
@ -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
|
||||
|
|
|
@ -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())
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue