2017-03-08 20:08:44 +01:00
|
|
|
package filelog
|
|
|
|
|
|
|
|
import (
|
2018-12-02 14:53:01 +01:00
|
|
|
"bufio"
|
|
|
|
"compress/gzip"
|
|
|
|
"errors"
|
2017-03-11 21:51:08 +01:00
|
|
|
"fmt"
|
2018-12-02 14:53:01 +01:00
|
|
|
"io"
|
2017-04-15 09:46:39 +02:00
|
|
|
"os"
|
2018-12-02 14:53:01 +01:00
|
|
|
"strings"
|
2017-09-11 19:51:29 +02:00
|
|
|
|
2022-01-12 19:44:06 +01:00
|
|
|
"github.com/gempir/go-twitch-irc/v3"
|
2021-01-09 13:34:31 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
2017-03-08 20:08:44 +01:00
|
|
|
)
|
|
|
|
|
2019-07-13 15:05:29 +02:00
|
|
|
func (l *Logger) LogPrivateMessageForChannel(message twitch.PrivateMessage) error {
|
2019-03-11 21:41:12 +01:00
|
|
|
year := message.Time.Year()
|
|
|
|
month := int(message.Time.Month())
|
|
|
|
day := message.Time.Day()
|
2019-07-13 15:05:29 +02:00
|
|
|
err := os.MkdirAll(fmt.Sprintf(l.logPath+"/%s/%d/%d/%d", message.RoomID, year, month, day), 0750)
|
2019-03-11 21:41:12 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-13 15:05:29 +02:00
|
|
|
filename := fmt.Sprintf(l.logPath+"/%s/%d/%d/%d/channel.txt", message.RoomID, year, month, day)
|
2019-03-11 21:41:12 +01:00
|
|
|
|
|
|
|
file, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0640)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
if _, err = file.WriteString(message.Raw + "\n"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-13 15:05:29 +02:00
|
|
|
func (l *Logger) LogClearchatMessageForChannel(message twitch.ClearChatMessage) error {
|
2017-04-14 17:43:12 +02:00
|
|
|
year := message.Time.Year()
|
2018-12-02 14:53:01 +01:00
|
|
|
month := int(message.Time.Month())
|
2017-04-14 17:43:12 +02:00
|
|
|
day := message.Time.Day()
|
2019-07-13 15:05:29 +02:00
|
|
|
err := os.MkdirAll(fmt.Sprintf(l.logPath+"/%s/%d/%d/%d", message.RoomID, year, month, day), 0750)
|
2017-03-08 20:08:44 +01:00
|
|
|
if err != nil {
|
2017-03-10 18:09:13 +01:00
|
|
|
return err
|
2017-03-08 20:08:44 +01:00
|
|
|
}
|
2019-07-13 15:05:29 +02:00
|
|
|
filename := fmt.Sprintf(l.logPath+"/%s/%d/%d/%d/channel.txt", message.RoomID, year, month, day)
|
|
|
|
|
|
|
|
file, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0640)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
if _, err = file.WriteString(message.Raw + "\n"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) LogUserNoticeMessageForChannel(message twitch.UserNoticeMessage) error {
|
|
|
|
year := message.Time.Year()
|
|
|
|
month := int(message.Time.Month())
|
|
|
|
day := message.Time.Day()
|
|
|
|
err := os.MkdirAll(fmt.Sprintf(l.logPath+"/%s/%d/%d/%d", message.RoomID, year, month, day), 0750)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
filename := fmt.Sprintf(l.logPath+"/%s/%d/%d/%d/channel.txt", message.RoomID, year, month, day)
|
2017-03-08 20:08:44 +01:00
|
|
|
|
2018-12-02 14:53:01 +01:00
|
|
|
file, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0640)
|
2017-03-08 20:08:44 +01:00
|
|
|
if err != nil {
|
2017-03-10 18:09:13 +01:00
|
|
|
return err
|
2017-03-08 20:08:44 +01:00
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
2018-12-05 22:41:31 +01:00
|
|
|
if _, err = file.WriteString(message.Raw + "\n"); err != nil {
|
2017-03-10 18:09:13 +01:00
|
|
|
return err
|
2017-03-08 20:08:44 +01:00
|
|
|
}
|
2017-03-11 11:06:50 +01:00
|
|
|
|
2017-03-10 18:09:13 +01:00
|
|
|
return nil
|
2017-03-11 11:06:50 +01:00
|
|
|
}
|
2018-12-02 14:53:01 +01:00
|
|
|
|
|
|
|
func (l *Logger) ReadLogForChannel(channelID string, year int, month int, day int) ([]string, error) {
|
|
|
|
filename := fmt.Sprintf(l.logPath+"/%s/%d/%d/%d/channel.txt", channelID, year, month, day)
|
|
|
|
|
|
|
|
if _, err := os.Stat(filename); err != nil {
|
|
|
|
filename = filename + ".gz"
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(filename)
|
|
|
|
if err != nil {
|
2018-12-02 19:23:54 +01:00
|
|
|
return []string{}, errors.New("file not found: " + filename)
|
2018-12-02 14:53:01 +01:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
var reader io.Reader
|
|
|
|
|
|
|
|
if strings.HasSuffix(filename, ".gz") {
|
|
|
|
gz, err := gzip.NewReader(f)
|
|
|
|
if err != nil {
|
2021-01-09 13:34:31 +01:00
|
|
|
log.Error(err)
|
2018-12-02 14:53:01 +01:00
|
|
|
return []string{}, errors.New("file gzip not readable")
|
|
|
|
}
|
|
|
|
reader = gz
|
|
|
|
} else {
|
|
|
|
reader = f
|
|
|
|
}
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(reader)
|
|
|
|
if err != nil {
|
2021-01-09 13:34:31 +01:00
|
|
|
log.Error(err)
|
2018-12-02 14:53:01 +01:00
|
|
|
return []string{}, errors.New("file not readable")
|
|
|
|
}
|
|
|
|
|
|
|
|
content := []string{}
|
|
|
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
content = append(content, line)
|
|
|
|
}
|
|
|
|
|
|
|
|
return content, nil
|
|
|
|
}
|