randomquote implemented

This commit is contained in:
gempir 2018-12-03 21:52:15 +01:00
parent f18567ee4b
commit bb9812378c
2 changed files with 45 additions and 38 deletions

View file

@ -18,10 +18,11 @@ import (
)
type RandomQuoteJSON struct {
Channel string `json:"channel"`
Username string `json:"username"`
Message string `json:"message"`
Timestamp string `json:"timestamp"`
Channel string `json:"channel"`
Username string `json:"username"`
DisplayName string `json:"displayName"`
Message string `json:"message"`
Timestamp timestamp `json:"timestamp"`
}
type AllChannelsJSON struct {
@ -78,21 +79,19 @@ func (s *Server) getCurrentChannelLogsByName(c echo.Context) error {
}
func (s *Server) getRandomQuote(c echo.Context) error {
username := c.Param("username")
username = strings.ToLower(strings.TrimSpace(username))
channel := strings.ToLower(c.Param("channel"))
channel = strings.TrimSpace(channel)
userID := c.Param("userid")
channelID := c.Param("channelid")
var userLogs []string
var lines []string
years, _ := ioutil.ReadDir(s.logPath + "/" + channel)
years, _ := ioutil.ReadDir(s.logPath + "/" + channelID)
for _, yearDir := range years {
year := yearDir.Name()
months, _ := ioutil.ReadDir(s.logPath + "/" + channel + "/" + year + "/")
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, channel, year, month, username)
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 {
@ -122,22 +121,22 @@ func (s *Server) getRandomQuote(c echo.Context) error {
}
ranNum := rand.Intn(len(lines))
line := lines[ranNum]
lineSplit := strings.SplitN(line, "]", 2)
channel, user, message := twitch.ParseMessage(lines[ranNum])
if c.Request().Header.Get("Content-Type") == "application/json" {
if shouldRespondWithJson(c) {
randomQ := RandomQuoteJSON{
Channel: channel,
Username: username,
Message: strings.TrimPrefix(lineSplit[1], " "+username+": "),
Timestamp: strings.TrimPrefix(lineSplit[0], "["),
Channel: channel,
Username: user.Username,
DisplayName: user.DisplayName,
Message: message.Text,
Timestamp: timestamp{message.Time},
}
return c.JSON(http.StatusOK, randomQ)
}
return c.String(http.StatusOK, lineSplit[1])
return c.String(http.StatusOK, fmt.Sprintf("%s: %s", user.DisplayName, message.Text))
}
func (s *Server) getChannelLogsByName(c echo.Context) error {
@ -215,17 +214,18 @@ func (s *Server) getUserLogs(c echo.Context) error {
channel, user, parsedMessage := twitch.ParseMessage(rawMessage)
message := chatMessage{
Timestamp: timestamp{parsedMessage.Time},
Username: user.Username,
Text: parsedMessage.Text,
Type: parsedMessage.Type,
Channel: channel,
Timestamp: timestamp{parsedMessage.Time},
Username: user.Username,
DisplayName: user.DisplayName,
Text: parsedMessage.Text,
Type: parsedMessage.Type,
Channel: channel,
}
logResult.Messages = append(logResult.Messages, message)
}
if c.Request().Header.Get("Content-Type") == "application/json" || c.QueryParam("type") == "json" {
if shouldRespondWithJson(c) {
return writeJSONResponse(c, &logResult)
}
@ -267,17 +267,18 @@ func (s *Server) getChannelLogs(c echo.Context) error {
channel, user, parsedMessage := twitch.ParseMessage(rawMessage)
message := chatMessage{
Timestamp: timestamp{parsedMessage.Time},
Username: user.Username,
Text: parsedMessage.Text,
Type: parsedMessage.Type,
Channel: channel,
Timestamp: timestamp{parsedMessage.Time},
Username: user.Username,
DisplayName: user.DisplayName,
Text: parsedMessage.Text,
Type: parsedMessage.Type,
Channel: channel,
}
logResult.Messages = append(logResult.Messages, message)
}
if c.Request().Header.Get("Content-Type") == "application/json" || c.QueryParam("type") == "json" {
if shouldRespondWithJson(c) {
return writeJSONResponse(c, &logResult)
}

View file

@ -61,14 +61,13 @@ func (s *Server) Init() {
e.GET("/channel/:channel/user/:username/:year/:month", s.getUserLogsByName)
e.GET("/channelid/:channelid/user/:userid", s.getCurrentUserLogs)
e.GET("/channelid/:channelid/userid/:userid/:year/:month", s.getUserLogs)
e.GET("/channelid/:channelid/userid/:userid/random", s.getRandomQuote)
e.GET("/channel/:channel", s.getCurrentChannelLogsByName)
e.GET("/channel/:channel/:year/:month/:day", s.getChannelLogsByName)
e.GET("/channelid/:channelid", s.getCurrentChannelLogs)
e.GET("/channelid/:channelid/:year/:month/:day", s.getChannelLogs)
e.GET("/channelid/:channelid/userid/:userid/random", s.getRandomQuote)
e.Logger.Fatal(e.Start(s.listenAddress))
}
@ -84,11 +83,12 @@ type chatLog struct {
}
type chatMessage struct {
Text string `json:"text"`
Username string `json:"username"`
Channel string `json:"channel"`
Timestamp timestamp `json:"timestamp"`
Type twitch.MessageType `json:"type"`
Text string `json:"text"`
Username string `json:"username"`
DisplayName string `json:"displayName"`
Channel string `json:"channel"`
Timestamp timestamp `json:"timestamp"`
Type twitch.MessageType `json:"type"`
}
type timestamp struct {
@ -202,3 +202,9 @@ func buildOrder(c echo.Context) order {
return dataOrder
}
func shouldRespondWithJson(c echo.Context) bool {
_, ok := c.QueryParams()["json"]
return c.Request().Header.Get("Content-Type") == "application/json" || c.QueryParam("type") == "json" || ok
}