Skip to content

Commit

Permalink
adding a progress bar.
Browse files Browse the repository at this point in the history
  • Loading branch information
AnalogJ committed Jan 20, 2022
1 parent 921cc82 commit 4ec7496
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/emersion/go-imap v1.1.0
github.com/emersion/go-message v0.15.0
github.com/fatih/color v1.13.0
github.com/schollz/progressbar/v3 v3.8.5
github.com/sirupsen/logrus v1.8.1
github.com/spf13/viper v1.10.1
github.com/urfave/cli v1.22.5
Expand Down
41 changes: 24 additions & 17 deletions pkg/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/emersion/go-message"
"github.com/emersion/go-message/charset"
"github.com/emersion/go-message/mail"
"github.com/schollz/progressbar/v3"
"github.com/sirupsen/logrus"
"io"
"io/ioutil"
Expand All @@ -30,7 +31,8 @@ type EmailEngine struct {
client *client.Client
configuration config.Interface

report map[string]*model.SenderReport
progressBar *progressbar.ProgressBar
report map[string]*model.SenderReport
}

func New(logger *logrus.Entry, configuration config.Interface) (EmailEngine, error) {
Expand Down Expand Up @@ -73,21 +75,25 @@ func (ee *EmailEngine) Start() error {
totalMessages = 0
page := 0

for {
// get latest mailbox information
//https://bitmapcake.blogspot.com/2018/07/gmail-mailbox-names-for-imap-connections.html
mbox, err := ee.client.Select(ee.configuration.GetString("imap-mailbox-name"), false)
if err != nil {
ee.logger.Fatal(err)
}
// Get all messages
totalMessages = mbox.Messages
// get latest mailbox information
//https://bitmapcake.blogspot.com/2018/07/gmail-mailbox-names-for-imap-connections.html
mbox, err := ee.client.Select(ee.configuration.GetString("imap-mailbox-name"), false)
if err != nil {
ee.logger.Fatal(err)
}
// Get count of all messages
totalMessages = mbox.Messages

if totalMessages == 0 {
//if theres no messages to process, break out of the loop
ee.logger.Printf("No messages to process")
break
}
//set a progress bar
ee.progressBar = progressbar.Default(int64(totalMessages))

if totalMessages == 0 {
//if theres no messages to process, break out of the loop
ee.logger.Printf("No messages to process")
return errors.New("No messages to process")
}

for {
// 1-500, 501-1000
from := uint32((page * BATCH_SIZE) + 1)
to := uint32((page + 1) * BATCH_SIZE)
Expand All @@ -99,7 +105,7 @@ func (ee *EmailEngine) Start() error {
seqset := new(imap.SeqSet)
seqset.AddRange(from, to)

ee.logger.Printf("Retrieving messages (%d-%d, page: %d, total: %d)", from, to, page, totalMessages)
ee.logger.Debugf("Retrieving messages (%d-%d, page: %d, total: %d)", from, to, page, totalMessages)
ee.retrieveMessages(seqset)

if totalMessages <= to {
Expand Down Expand Up @@ -173,6 +179,7 @@ func (ee *EmailEngine) retrieveMessages(seqset *imap.SeqSet) {
if err != nil {
ee.logger.Errorf("error processing message: %v", err)
}
ee.progressBar.Add(1)
}

if err := <-done; err != nil {
Expand Down Expand Up @@ -333,7 +340,7 @@ func (ee *EmailEngine) extractBodyUnsubscribe(msg *imap.Message) ([]string, erro
linkTextCompare := strings.ToLower(link.Text())

if strings.Contains(linkTextCompare, "subscribe") {
ee.logger.Infof("body unsubscribe link -> [%s](%s)\n", link.Text(), link.Attrs()["href"])
ee.logger.Debugf("body unsubscribe link -> [%s](%s)\n", link.Text(), link.Attrs()["href"])
unsubscribeUris = append(unsubscribeUris, link.Attrs()["href"])
}
}
Expand Down

0 comments on commit 4ec7496

Please sign in to comment.