diff --git a/api/channel.go b/api/channel.go index 0e6cb64..8386616 100644 --- a/api/channel.go +++ b/api/channel.go @@ -19,6 +19,9 @@ func (s *Server) getCurrentChannelLogs(c echo.Context) error { day := time.Now().Day() redirectURL := fmt.Sprintf("/channelid/%s/%d/%d/%d", channelID, year, month, day) + if len(c.QueryString()) > 0 { + redirectURL += "?" + c.QueryString() + } return c.Redirect(http.StatusSeeOther, redirectURL) } @@ -29,6 +32,9 @@ func (s *Server) getCurrentChannelLogsByName(c echo.Context) error { day := time.Now().Day() redirectURL := fmt.Sprintf("/channel/%s/%d/%d/%d", channel, year, month, day) + if len(c.QueryString()) > 0 { + redirectURL += "?" + c.QueryString() + } return c.Redirect(http.StatusSeeOther, redirectURL) } diff --git a/api/user.go b/api/user.go index 3cc2a86..f8f943c 100644 --- a/api/user.go +++ b/api/user.go @@ -1,13 +1,8 @@ package api import ( - "bufio" - "compress/gzip" "fmt" - "io/ioutil" - "math/rand" "net/http" - "os" "strconv" "strings" "time" @@ -33,6 +28,9 @@ func (s *Server) getCurrentUserLogs(c echo.Context) error { month := int(time.Now().Month()) redirectURL := fmt.Sprintf("/channelid/%s/userid/%s/%d/%d", channelID, userID, year, month) + if len(c.QueryString()) > 0 { + redirectURL += "?" + c.QueryString() + } return c.Redirect(303, redirectURL) } @@ -44,6 +42,9 @@ func (s *Server) getCurrentUserLogsByName(c echo.Context) error { month := int(time.Now().Month()) redirectURL := fmt.Sprintf("/channel/%s/user/%s/%d/%d", channel, username, year, month) + if len(c.QueryString()) > 0 { + redirectURL += "?" + c.QueryString() + } return c.Redirect(303, redirectURL) } @@ -120,53 +121,14 @@ func (s *Server) getRandomQuoteByName(c echo.Context) error { } func (s *Server) getRandomQuote(c echo.Context) error { - userID := c.Param("userid") channelID := c.Param("channelid") + userID := c.Param("userid") - var userLogs []string - var lines []string - - if channelID == "" || userID == "" { - return c.JSON(http.StatusNotFound, "error finding logs") + rawMessage, err := s.fileLogger.ReadRandomMessageForUser(channelID, userID) + if err != nil { + return c.JSON(http.StatusInternalServerError, err.Error()) } - - years, _ := ioutil.ReadDir(s.logPath + "/" + channelID) - for _, yearDir := range years { - year := yearDir.Name() - months, _ := ioutil.ReadDir(s.logPath + "/" + channelID + "/" + year + "/") - for _, monthDir := range months { - month := monthDir.Name() - path := fmt.Sprintf("%s/%s/%s/%s/%s.txt", s.logPath, channelID, year, month, userID) - if _, err := os.Stat(path); err == nil { - userLogs = append(userLogs, path) - } else if _, err := os.Stat(path + ".gz"); err == nil { - userLogs = append(userLogs, path+".gz") - } - } - } - if len(userLogs) < 1 { - return c.JSON(http.StatusNotFound, "error finding logs") - } - - for _, logFile := range userLogs { - f, _ := os.Open(logFile) - - scanner := bufio.NewScanner(f) - - if strings.HasSuffix(logFile, ".gz") { - gz, _ := gzip.NewReader(f) - scanner = bufio.NewScanner(gz) - } - - for scanner.Scan() { - line := scanner.Text() - lines = append(lines, line) - } - f.Close() - } - - ranNum := rand.Intn(len(lines)) - channel, user, message := twitch.ParseMessage(lines[ranNum]) + channel, user, message := twitch.ParseMessage(rawMessage) if shouldRespondWithJson(c) { @@ -181,7 +143,7 @@ func (s *Server) getRandomQuote(c echo.Context) error { return c.JSON(http.StatusOK, randomQ) } - return c.String(http.StatusOK, fmt.Sprintf("%s: %s", user.DisplayName, message.Text)) + return c.String(http.StatusOK, message.Text) } func (s *Server) getUserLogs(c echo.Context) error { diff --git a/filelog/userlog.go b/filelog/userlog.go index cad33f6..dc20465 100644 --- a/filelog/userlog.go +++ b/filelog/userlog.go @@ -6,6 +6,8 @@ import ( "errors" "fmt" "io" + "io/ioutil" + "math/rand" "os" "strings" @@ -45,7 +47,7 @@ func (l *Logger) LogMessageForUser(channel string, user twitch.User, message twi return nil } -func (l *Logger) ReadLogForUser(channelID string, userID string, year int, month int) ([]string, error) { +func (l *Logger) ReadLogForUser(channelID, userID string, year int, month int) ([]string, error) { if channelID == "" || userID == "" { return []string{}, fmt.Errorf("Invalid channelID: %s or userID: %s", channelID, userID) } @@ -89,3 +91,52 @@ func (l *Logger) ReadLogForUser(channelID string, userID string, year int, month return content, nil } + +func (l *Logger) ReadRandomMessageForUser(channelID, userID string) (string, error) { + var userLogs []string + var lines []string + + if channelID == "" || userID == "" { + return "", errors.New("missing channelID or userID") + } + + 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 { + userLogs = append(userLogs, path) + } else if _, err := os.Stat(path + ".gz"); err == nil { + userLogs = append(userLogs, path+".gz") + } + } + } + + if len(userLogs) < 1 { + return "", errors.New("no log found") + } + + for _, logFile := range userLogs { + f, _ := os.Open(logFile) + + scanner := bufio.NewScanner(f) + + if strings.HasSuffix(logFile, ".gz") { + gz, _ := gzip.NewReader(f) + scanner = bufio.NewScanner(gz) + } + + for scanner.Scan() { + line := scanner.Text() + lines = append(lines, line) + } + f.Close() + } + + ranNum := rand.Intn(len(lines)) + + return lines[ranNum], nil +}