Files
datahop/runner/logger.go
2021-11-01 18:38:58 -05:00

82 lines
2.7 KiB
Go

package runner
import (
"path"
"strconv"
"time"
"github.com/xuri/excelize/v2"
)
type Logger struct {
log *excelize.File
work_dir string
weekago time.Time
current_time time.Time
}
func NewLogger(work_dir string, weekago time.Time, current_time time.Time) *Logger {
logfile := excelize.NewFile()
logfile.NewSheet("FacebookPosts")
logfile.NewSheet("InstaPosts")
logfile.NewSheet("Facebook")
logfile.NewSheet("Insta")
logfile.DeleteSheet("Sheet1")
var logger = new(Logger)
logger.log = logfile
logger.work_dir = work_dir
logger.weekago = weekago
logger.current_time = current_time
return logger
}
const log_facebook_sheet = "FacebookPosts"
const log_insta_sheet = "InstaPosts"
func (l *Logger) LogFacebookRes(data FacebookRes, target string) {
rows, err := l.log.GetRows(log_facebook_sheet)
checkErr(err)
current_index := len(rows)
for _, elm := range data.Posts {
if elm.Timestamp.After(l.weekago) && elm.Timestamp.Before(l.current_time) {
l.log.SetCellValue(log_facebook_sheet, "A"+strconv.Itoa(current_index+1), target)
l.log.SetCellValue(log_facebook_sheet, "D"+strconv.Itoa(current_index+1), elm.Timestamp.Format("01-02-2006"))
l.log.SetCellValue(log_facebook_sheet, "C"+strconv.Itoa(current_index+1), elm.Engagement)
l.log.SetCellValue(log_facebook_sheet, "B"+strconv.Itoa(current_index+1), elm.URL)
current_index += 1
}
}
}
func (l *Logger) LogInstaRes(posts []InstaPost, target string) {
rows, err := l.log.GetRows(log_insta_sheet)
checkErr(err)
current_index := len(rows)
for _, elm := range posts {
if elm.Timestamp.After(l.weekago) && elm.Timestamp.Before(l.current_time) {
l.log.SetCellValue(log_insta_sheet, "A"+strconv.Itoa(current_index+1), target)
l.log.SetCellValue(log_insta_sheet, "B"+strconv.Itoa(current_index+1), elm.URL)
l.log.SetCellValue(log_insta_sheet, "C"+strconv.Itoa(current_index+1), elm.Engagement)
l.log.SetCellValue(log_insta_sheet, "D"+strconv.Itoa(current_index+1), elm.Timestamp.Format("01-02-2006"))
current_index += 1
}
}
}
func (l *Logger) LogResult(data Result, sheet string) {
rows, err := l.log.GetRows(sheet)
checkErr(err)
current_index := len(rows)
l.log.SetCellValue(sheet, "A"+strconv.Itoa(current_index+1), data.Name)
l.log.SetCellValue(sheet, "B"+strconv.Itoa(current_index+1), data.Followers)
l.log.SetCellValue(sheet, "C"+strconv.Itoa(current_index+1), data.Engagement)
l.log.SetCellValue(sheet, "D"+strconv.Itoa(current_index+1), data.Posts)
l.log.SetCellValue(sheet, "E"+strconv.Itoa(current_index+1), time.Now().Format("01-02-2006"))
}
func (l *Logger) Close() {
err := l.log.SaveAs(path.Join(l.work_dir, "log-"+time.Now().Format("01-02-2006")+".xlsx"))
checkErr(err)
}