Skip to content

Commit

Permalink
Store FII dividends on db
Browse files Browse the repository at this point in the history
  • Loading branch information
dude333 committed Apr 23, 2021
1 parent 9b1a0ea commit d215961
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 20 deletions.
33 changes: 17 additions & 16 deletions cmd/fii_dividends.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"github.com/dude333/rapina/fetch"
"github.com/dude333/rapina/parsers"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// fiiDividendsCmd represents the rendimentos command
Expand Down Expand Up @@ -67,24 +66,26 @@ func FIIDividends(code string, n int) error {
}
code = strings.ToUpper(code)

stockStore, _ := parsers.NewStockStore(db)
srv, err := fetch.NewStockServer(stockStore, viper.GetString("apikey"))
if err != nil {
return err
}
err = srv.FetchStockQuote(fix(code))
if err != nil {
return err
}
/*
stockStore, _ := parsers.NewStockStore(db)
srv, err := fetch.NewStockServer(stockStore, viper.GetString("apikey"))
if err != nil {
return err
}
err = srv.FetchStockQuote(fix(code))
if err != nil {
return err
}
*/

fiiStore := parsers.NewFIIStore(db)
fii := fetch.NewFII(fiiStore)
return fii.FetchFIIDividends(code, n)
}

func fix(code string) string {
if len(code) == 4 {
return code + "11"
}
return code
}
// func fix(code string) string {
// if len(code) == 4 {
// return code + "11"
// }
// return code
// }
10 changes: 6 additions & 4 deletions fetch/fetch_fii.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"path"
Expand All @@ -20,6 +21,7 @@ import (
type FIIStore interface {
CNPJ(code string) (string, error)
StoreFIIDetails(stream []byte) error
StoreFIIDividends(stream map[string]string) error
}

// FII holds the infrastructure data.
Expand Down Expand Up @@ -141,7 +143,7 @@ func (fii FII) FetchFIIDividends(code string, n int) error {
if fieldName == "" {
fieldName = v
} else {
fmt.Printf("%-30s => %s\n", fieldName, v)
// fmt.Printf("%-30s => %s\n", fieldName, v)
yeld[fieldName] = v
fieldName = ""
}
Expand All @@ -155,9 +157,9 @@ func (fii FII) FetchFIIDividends(code string, n int) error {
if err := c.Visit(u); err != nil {
return err
}
fmt.Println("----------------------------")

// fmt.Printf("%+v\n", yeld)
if err := fii.store.StoreFIIDividends(yeld); err != nil {
log.Println("[x]", err)
}
}

return nil
Expand Down
48 changes: 48 additions & 0 deletions parsers/fiidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"database/sql"
"encoding/json"
"fmt"
"strconv"
"strings"

"github.com/pkg/errors"
Expand Down Expand Up @@ -98,6 +99,10 @@ func (fii FIIStore) StoreFIIDetails(stream []byte) error {
return err
}

//
// CNPJ returns the FII CNPJ for the 'code' or
// an empty string if not found in the db.
//
func (fii FIIStore) CNPJ(code string) (string, error) {
if fii.db == nil {
return "", ErrDBUnset
Expand All @@ -122,6 +127,31 @@ func (fii FIIStore) CNPJ(code string) (string, error) {
return cnpj, nil
}

//
// StoreFIIDividends stores the map into the db.
//
func (fii FIIStore) StoreFIIDividends(stream map[string]string) error {
// fmt.Println("----------------------------")
// fmt.Printf("%+v\n\n", stream)

if err := createTable(fii.db, "fii_dividends"); err != nil {
return err
}

code := mapFinder("Código de negociação da cota", stream)
baseDate := mapFinder("Data-base", stream)
pymtDate := mapFinder("Data do pagamento", stream)
val := mapFinder("Valor do provento por cota", stream)

const insert = `INSERT OR IGNORE INTO fii_dividends
(trading_code, base_date, payment_date, value) VALUES (?,?,?,?)`
_, err := fii.db.Exec(insert, code, baseDate, pymtDate, comma2dot(val))

// fmt.Println(insert, code, baseDate, pymtDate, comma2dot(val))

return errors.Wrap(err, "inserting data on fii_dividends")
}

func (fii FIIStore) SelectFIIDetails(code string) (*FIIDetails, error) {
if fii.db == nil {
return nil, ErrDBUnset
Expand Down Expand Up @@ -151,8 +181,26 @@ func (fii FIIStore) SelectFIIDetails(code string) (*FIIDetails, error) {
return &fiiDetail, nil
}

/* -------- Utils ----------- */

func trimFIIDetails(f *FIIDetails) {
f.DetailFund.CNPJ = strings.TrimSpace(f.DetailFund.CNPJ)
f.DetailFund.Acronym = strings.TrimSpace(f.DetailFund.Acronym)
f.DetailFund.TradingCode = strings.TrimSpace(f.DetailFund.TradingCode)
}

func mapFinder(key string, m map[string]string) string {
for k := range m {
if strings.Contains(k, key) {
return m[k]
}
}
return ""
}

func comma2dot(val string) float64 {
a := strings.ReplaceAll(val, ".", "")
b := strings.ReplaceAll(a, ",", ".")
n, _ := strconv.ParseFloat(b, 64)
return n
}
12 changes: 12 additions & 0 deletions parsers/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ var createTableMap = map[string]string{
volume real
);`,

"fii_dividends": `CREATE TABLE IF NOT EXISTS fii_dividends
(
trading_code varchar(12) NOT NULL,
base_date varchar(10) NOT NULL,
payment_date varchar(10),
value real
);`,

"status": `CREATE TABLE IF NOT EXISTS status
(
table_name TEXT NOT NULL PRIMARY KEY,
Expand Down Expand Up @@ -121,6 +129,8 @@ func whatTable(dataType string) (table string, err error) {
table = "companies"
case "fii_details":
table = dataType
case "fii_dividends":
table = dataType
case "stock_quotes":
table = dataType
default:
Expand Down Expand Up @@ -158,6 +168,8 @@ func createTable(db *sql.DB, dataType string) (err error) {
switch dataType {
case "fii_details":
version = currentFIIDbVersion
case "fii_dividends":
version = currentFIIDbVersion
case "stock_quotes":
version = currentStockQuotesVersion
}
Expand Down

0 comments on commit d215961

Please sign in to comment.