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") logfile.SetCellValue("Facebook", "A1", "Brand") logfile.SetCellValue("Facebook", "B1", "Followers") logfile.SetCellValue("Facebook", "C1", "Engagement") logfile.SetCellValue("Facebook", "D1", "Posts") logfile.SetCellValue("Insta", "A1", "Brand") logfile.SetCellValue("Insta", "B1", "Followers") logfile.SetCellValue("Insta", "C1", "Engagement") logfile.SetCellValue("Insta", "D1", "Posts") logfile.SetCellValue("InstaPosts", "A1", "Brand") logfile.SetCellValue("InstaPosts", "B1", "URL") logfile.SetCellValue("InstaPosts", "C1", "Engagement") logfile.SetCellValue("InstaPosts", "D1", "Date") logfile.SetCellValue("InstaPosts", "E1", "Type") logfile.SetCellValue("FacebookPosts", "A1", "Brand") logfile.SetCellValue("FacebookPosts", "B1", "URL") logfile.SetCellValue("FacebookPosts", "C1", "Engagement") logfile.SetCellValue("FacebookPosts", "D1", "Date") 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, "B"+strconv.Itoa(current_index+1), elm.URL) l.log.SetCellValue(log_facebook_sheet, "C"+strconv.Itoa(current_index+1), elm.Engagement) l.log.SetCellValue(log_facebook_sheet, "D"+strconv.Itoa(current_index+1), elm.Timestamp.Format("01-02-2006")) 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")) l.log.SetCellValue(log_insta_sheet, "E"+strconv.Itoa(current_index+1), elm.Type) 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, "data-"+time.Now().Format("01-02-2006")+".xlsx")) checkErr(err) }