From f9ac3ec9343db9ad8ef3cda11bc743de3feb6858 Mon Sep 17 00:00:00 2001 From: RobViren Date: Tue, 2 Nov 2021 16:12:44 -0500 Subject: [PATCH] Fixed edge case --- main.go | 69 +-------------------------------------------- runner/instagram.go | 3 ++ runner/logger.go | 32 ++++++++++++++++++--- 3 files changed, 32 insertions(+), 72 deletions(-) diff --git a/main.go b/main.go index 9d6d313..5fd65b3 100644 --- a/main.go +++ b/main.go @@ -1,80 +1,13 @@ package main import ( - "log" - "os" - "path" - "path/filepath" - "strconv" - "time" - - "github.com/xuri/excelize/v2" "rodbiren.ddns.net/git/robviren/datahop/runner" ) func main() { - exe_path, err := os.Executable() - if err != nil { - log.Fatalln(err) - } - work_dir := filepath.Dir(exe_path) - datahop := runner.NewRunner() - data := datahop.RunAll() + datahop.RunAll() defer datahop.Close() - var f *excelize.File - _, err = os.Stat(path.Join(work_dir, "data.xlsx")) - if err == nil { - f, err = excelize.OpenFile(path.Join(work_dir, "data.xlsx")) - if err != nil { - log.Println(err) - } - } else { - f = excelize.NewFile() - f.NewSheet("Facebook") - f.NewSheet("Instagram") - } - - f.DeleteSheet("Sheet1") - - f.SetCellValue("Facebook", "A1", "Brand") - f.SetCellValue("Facebook", "B1", "Followers") - f.SetCellValue("Facebook", "C1", "Engagement") - f.SetCellValue("Facebook", "D1", "Posts") - f.SetCellValue("Facebook", "E1", "Posts") - - f.SetCellValue("Instagram", "A1", "Brand") - f.SetCellValue("Instagram", "B1", "Followers") - f.SetCellValue("Instagram", "C1", "Engagement") - f.SetCellValue("Instagram", "D1", "Posts") - f.SetCellValue("Instagram", "E1", "Posts") - - for _, elm := range data { - rows, err := f.GetRows(elm.Site) - if err != nil { - log.Println(err) - } - f.SetCellValue(elm.Site, "A"+strconv.Itoa(1+len(rows)), elm.Name) - f.SetCellValue(elm.Site, "B"+strconv.Itoa(1+len(rows)), elm.Followers) - f.SetCellValue(elm.Site, "C"+strconv.Itoa(1+len(rows)), elm.Engagement) - f.SetCellValue(elm.Site, "D"+strconv.Itoa(1+len(rows)), elm.Posts) - f.SetCellValue(elm.Site, "E"+strconv.Itoa(1+len(rows)), time.Now().Format("01-02-2006")) - } - - current_day := time.Now() - - if err := f.SaveAs(path.Join(work_dir, "data-"+current_day.Format("01-02-2006")+".xlsx")); err != nil { - log.Println(err) - } - - if err := f.SaveAs(path.Join(work_dir, "data.xlsx")); err != nil { - log.Println(err) - } - - // signal_channel := make(chan os.Signal, 1) - // signal.Notify(signal_channel, os.Interrupt) - // <-signal_channel - } diff --git a/runner/instagram.go b/runner/instagram.go index ed95d8a..8d3e8bb 100644 --- a/runner/instagram.go +++ b/runner/instagram.go @@ -24,6 +24,7 @@ type InstaPost struct { Likes int `json:"likes"` Views int `json:"views"` URL string `json:"url"` + Type string `json:"type"` } //go:embed js/instagram_followers_urls.js @@ -139,8 +140,10 @@ func (r *Runner) GetIntsaEgagement(urls []string) []InstaPost { } if res.Views != 0 { + res.Type = "Video" res.Engagement = res.Views + res.Comments } else { + res.Type = "Image" res.Engagement = res.Likes + res.Comments } diff --git a/runner/logger.go b/runner/logger.go index bffdd9f..56351f4 100644 --- a/runner/logger.go +++ b/runner/logger.go @@ -23,6 +23,28 @@ func NewLogger(work_dir string, weekago time.Time, current_time time.Time) *Logg 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 @@ -41,9 +63,10 @@ func (l *Logger) LogFacebookRes(data FacebookRes, target string) { 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) + 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 } } @@ -59,6 +82,7 @@ func (l *Logger) LogInstaRes(posts []InstaPost, target string) { 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 } } @@ -72,10 +96,10 @@ func (l *Logger) LogResult(data Result, sheet string) { 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")) + // 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")) + err := l.log.SaveAs(path.Join(l.work_dir, "data-"+time.Now().Format("01-02-2006")+".xlsx")) checkErr(err) }