func (fii *FII) dividendsFromServer(code string, n int) (*[]rapina.Dividend, error) {
	if n > MAX_N {
		n = MAX_N
	}
	nn := n
	lastLen := -1
	dividends := &[]rapina.Dividend{}
	for len(*dividends) < n && nn <= 200 {
		progress.Status("Relatórios de dividendos: %s", code)
		ids, err := fii.reportIDs(repDividends, code, nn)
		if err != nil {
			return nil, err
		}
		progress.Debug("Report IDs: %v", ids)
		dividends, err = fii.dividendReport(code, ids)
		if err != nil {
			return nil, err
		}
		progress.Debug("Dividends (%d): %v", len(*dividends), *dividends)
		if lastLen == len(*dividends) {
			break
		}
		lastLen = len(*dividends)
		nn += 2 * (n - len(*dividends))
	}
	return dividends, nil
}
Dividends gets the report IDs for one company ('cnpj') and then the
yeld montlhy report for 'n' months, starting from the latest released.

If the number of reports does not match n, it'll retry with a bigger n as
sometimes reports from follow-on offerings (FPO).
package fetch
import (
	"crypto/tls"
	"database/sql"
	"encoding/base64"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"strconv"
	"strings"
	"time"
	"github.com/dude333/rapina"
	"github.com/dude333/rapina/parsers"
	"github.com/dude333/rapina/progress"
	"github.com/gocolly/colly"
	"github.com/pkg/errors"
)
const MAX_N = 200
type FII struct {
	storage rapina.FIIStorage
}
func NewFII(db *sql.DB, log rapina.Logger) (*FII, error) {
	storage, err := parsers.NewFII(db, log)
	if err != nil {
		return nil, err
	}
	fii := &FII{
		storage: storage,
	}
	return fii, nil
}
type id int
type Report struct {
	Data []docID `json:"data"`
}
type docID struct {
	ID          id     `json:"id"`
	Description string `json:"descricaoFundo"`
	DocType     string `json:"tipoDocumento"`
	Status      string `json:"situacaoDocumento"`
}
func (fii FII) Dividends(code string, n int) (*[]rapina.Dividend, error) {
	dividends, months, err := fii.dividendsFromDB(code, n)
	if err == nil {
		if months >= n {
			return dividends, err
		}
	}
	if _, err := fii.dividendsFromServer(code, n); err != nil {
		return nil, err
	}
	dividends, _, err = fii.dividendsFromDB(code, n)
	return dividends, err
}
func (fii FII) dividendsFromDB(code string, n int) (*[]rapina.Dividend, int, error) {
	var dividends []rapina.Dividend
	var months int
	for _, monthYear := range rapina.MonthsFromToday(n + 2) {
		d, err := fii.storage.Dividends(code, monthYear)
		if err == nil {  
			dividends = append(dividends, *d...)
			months++
		}
		if months == n {
			break
		}
	}
	if len(dividends) == 0 {
		return nil, 0, errors.New("dividendos não encontrados")
	}
	return &dividends, months, nil
}