Skip to content

Commit

Permalink
Merge pull request #43 from roremeol/roremeol-master
Browse files Browse the repository at this point in the history
  • Loading branch information
dude333 committed Oct 4, 2022
2 parents 9a0e2e2 + 62b9f95 commit 18ea32c
Show file tree
Hide file tree
Showing 15 changed files with 189 additions and 37 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ bin/**
*.yml
!.travis.yml
wiki/**
.DS_Store

# Test binary, build with `go test -c`
*.test
Expand All @@ -34,4 +35,4 @@ wiki/**
!.github/**

# Dependency Analytics
target/**
target/**
16 changes: 16 additions & 0 deletions README_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,22 @@ Some Linux distributions (e.g. Fedora 34) might face some issues regarding the s

`sudo update-ca-trust`

**Ubuntu**

1. Download the Issuer Root Cert

`curl https://secure.globalsign.net/cacert/Root-R1.crt > /tmp/GlobalSign_Root_CA.crt`
`curl https://secure.globalsign.net/cacert/Root-R2.crt > /tmp/GlobalSign_Root_CA_R2.crt`

2. Move the .crt files to the certificates folder

`sudo cp /tmp/GlobalSign_Root_CA.crt /usr/local/share/ca-certificates/`
`sudo cp /tmp/GlobalSign_Root_CA_R2.crt /usr/local/share/ca-certificates/`

3. Update the trusted certificates database

`sudo update-ca-trust`


# 4. How to compile

Expand Down
9 changes: 7 additions & 2 deletions cmd/rapina/cmdutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const yamlFile = "./setores.yml"
type Parms struct {
// Company name to be processed
Company string
// SpcfctnCd to indentify the ticker
SpcfctnCd string
// Report format (xlsx/stdout)
Format string
// OutputDir: path of the output xlsx
Expand Down Expand Up @@ -49,15 +51,18 @@ func openDatabase() (db *sql.DB, err error) {
//
// promptUser presents a navigable list to be selected on CLI
//
func promptUser(list []string) (result string) {
func promptUser(list []string, label string) (result string) {
if label == "" {
label = "Selecione a Empresa"
}
templates := &promptui.SelectTemplates{
Help: `{{ "Use estas teclas para navegar:" | faint }} {{ .NextKey | faint }} ` +
`{{ .PrevKey | faint }} {{ .PageDownKey | faint }} {{ .PageUpKey | faint }} ` +
`{{ if .Search }} {{ "and" | faint }} {{ .SearchKey | faint }} {{ "toggles search" | faint }}{{ end }}`,
}

prompt := promptui.Select{
Label: "Selecione a Empresa",
Label: label,
Items: list,
Templates: templates,
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/rapina/cmdutils_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package main

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
)

func TestFilename(t *testing.T) {
tempDir, _ := ioutil.TempDir("", "rapina-test")
tempDir, _ := os.MkdirTemp("", "rapina-test")

table := []struct {
path string
Expand Down
39 changes: 31 additions & 8 deletions cmd/rapina/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package main
import (
"fmt"
"sort"
"strings"

"github.com/dude333/rapina/reports"
"github.com/lithammer/fuzzysearch/fuzzy"
Expand Down Expand Up @@ -65,11 +66,17 @@ func init() {
}

func report(company string) {
var spcfctnCd string = "ON"
company = SelectCompany(company, scriptMode)
if company == "" {
fmt.Println("[x] Empresa não encontrada")
return
}
if strings.Contains(company, "@#") {
companyWithTicker := strings.Split(company, "@#")
company = companyWithTicker[0]
spcfctnCd = companyWithTicker[1]
}
fmt.Println()
fmt.Printf("[√] Criando relatório para %s ========\n", company)

Expand All @@ -87,6 +94,7 @@ func report(company string) {

parms := Parms{
Company: company,
SpcfctnCd: spcfctnCd,
Format: format,
OutputDir: outputDir,
YamlFile: yamlFile,
Expand Down Expand Up @@ -136,7 +144,21 @@ func SelectCompany(company string, scriptMode bool) string {

// Interactive menu
if len(matches) >= 1 {
result := promptUser(matches)
result := promptUser(matches, "Selecione a Empresa")

tickers, err := reports.ListTickers(db, result)
if err != nil {
fmt.Println("[x] Recuperando lista de tickers ", err)
return result
}

// Interactive menu
if len(tickers) > 0 {
ticker := promptUser(tickers, "Selecione o ticker")
resultWithTicker := fmt.Sprintf("%s@#%s", result, reports.GetSpcfctnCd(db, result, ticker))
return resultWithTicker
}

return result
}

Expand All @@ -163,13 +185,14 @@ func Report(p Parms) (err error) {
}

parms := map[string]interface{}{
"db": db,
"dataDir": dataDir,
"company": p.Company,
"format": p.Format,
"filename": file,
"yamlFile": p.YamlFile,
"reports": p.Reports,
"db": db,
"dataDir": dataDir,
"company": p.Company,
"SpcfctnCd": p.SpcfctnCd,
"format": p.Format,
"filename": file,
"yamlFile": p.YamlFile,
"reports": p.Reports,
}

if p.Format == "stdout" {
Expand Down
3 changes: 1 addition & 2 deletions fetch/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"database/sql"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path"
Expand Down Expand Up @@ -275,7 +274,7 @@ func downloadFile(url, filepath string, verbose bool) (err error) {
}

// Write the body to file
counter := ioutil.Discard
counter := io.Discard
if verbose {
counter = &WriteCounter{}
}
Expand Down
4 changes: 2 additions & 2 deletions fetch/fetch_fii.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"database/sql"
"encoding/base64"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"strconv"
Expand Down Expand Up @@ -322,7 +322,7 @@ func (fii *FII) Details(fiiCode string) (*rapina.FIIDetails, error) {
return details, fmt.Errorf("%s: %s", resp.Status, fundDetailURL)
}

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrapf(err, "FII Details(%s): reading body", fiiCode)
}
Expand Down
3 changes: 1 addition & 2 deletions fetch/unzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"archive/zip"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -66,7 +65,7 @@ func Unzip(src string, dest string, verbose bool) ([]string, error) {
return filenames, err
}

counter := ioutil.Discard
counter := io.Discard
if verbose {
fmt.Printf("[ ] Unziping %s", fpath)
counter = &WriteCounter{}
Expand Down
2 changes: 1 addition & 1 deletion parsers/fii.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func FetchFIIList(baseURL string) ([]string, error) {
return nil, errors.New(resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
Expand Down
5 changes: 2 additions & 3 deletions parsers/financial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package parsers

import (
"database/sql"
"io/ioutil"
"os"
"testing"

_ "github.com/mattn/go-sqlite3"
)

func tempFilename(t *testing.T) string {
f, err := ioutil.TempFile("", "rapina-test-")
f, err := os.CreateTemp("", "rapina-test-")
if err != nil {
t.Fatal(err)
}
Expand All @@ -32,7 +31,7 @@ CNPJ_CIA;DT_REFER;VERSAO;DENOM_CIA;CD_CVM;GRUPO_DFP;MOEDA;ESCALA_MOEDA;ORDEM_EXE
00.000.000/0001-91;2013-12-31;4;BANCO DO BRASIL S.A.;1023;DF Consolidado - Balan�o Patrimonial Ativo;REAL;MILHAR;�LTIMO;2013-12-31;1.03;Empr�stimos e Receb�veis;755821983.00
00.000.000/0001-91;2013-12-31;4;BANCO DO BRASIL S.A.;1023;DF Consolidado - Balan�o Patrimonial Ativo;REAL;MILHAR;�LTIMO;2013-12-31;1.04;Tributos Diferidos;21954460.00
`)
err := ioutil.WriteFile(filename, bpa, 0600)
err := os.WriteFile(filename, bpa, 0600)

return err
}
Expand Down
16 changes: 12 additions & 4 deletions parsers/sectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package parsers
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"strings"
"regexp"

"github.com/PuerkitoBio/goquery"
"github.com/dude333/rapina"
Expand Down Expand Up @@ -74,7 +74,7 @@ func SectorsToYaml(yamlFile string) (err error) {
fmt.Fprintln(w, " Segmentos:")
}
lastSub = subsectors[i]
fmt.Fprintln(w, " - Segmento:", elem.Text)
fmt.Fprintln(w, " - Segmento:", removeYamlInvalidChar(elem.Text))
fmt.Fprintln(w, " Empresas:")
_ = companies(w, "http://bvmf.bmfbovespa.com.br/cias-listadas/empresas-listadas/"+elem.Attr("href"))
}
Expand Down Expand Up @@ -116,7 +116,7 @@ func companies(w *bufio.Writer, url string) error {

e.ForEachWithBreak("a", func(_ int, elem *colly.HTMLElement) bool {
if strings.Contains(elem.Attr("href"), "ResumoEmpresaPrincipal.aspx") {
fmt.Fprintln(w, " -", elem.Text)
fmt.Fprintln(w, " -", removeYamlInvalidChar(elem.Text))
}
return false // get only the 1st elem
})
Expand Down Expand Up @@ -171,7 +171,7 @@ type Segment struct {
//
func FromSector(company, yamlFile string) (companies []string, sectorName string, err error) {

y, err := ioutil.ReadFile(yamlFile)
y, err := os.ReadFile(yamlFile)
if err != nil {
err = errors.Wrapf(err, "ReadFile: %v", err)
return
Expand All @@ -196,3 +196,11 @@ func FromSector(company, yamlFile string) (companies []string, sectorName string

return
}

//
// removeYamlInvalidChar removes yaml invalid characters
//
func removeYamlInvalidChar(text string) string {
yaml_invalid_chars := regexp.MustCompile(`[^/\s.A-zÀ-ú0-9&():-]`)
return yaml_invalid_chars.ReplaceAllString(text,"")
}
5 changes: 2 additions & 3 deletions parsers/sectors_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package parsers

import (
"io/ioutil"
"os"
"testing"
)

func TestFromSector(t *testing.T) {
tempDir, _ := ioutil.TempDir("", "rapina-test")
tempDir, _ := os.MkdirTemp("", "rapina-test")
filename := tempDir + "/test_sectors.yml"

createYaml(filename)
Expand Down Expand Up @@ -52,5 +51,5 @@ func createYaml(filename string) {
- GRENDENE S.A.
- VULCABRAS/AZALEIA S.A.`)

_ = ioutil.WriteFile(filename, yaml, 0644)
_ = os.WriteFile(filename, yaml, 0644)
}
49 changes: 46 additions & 3 deletions reports/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -687,10 +687,53 @@ func companies(db *sql.DB) ([]CompanyInfo, error) {
return list, nil
}

// TickerInfo contains the ticker name and SpcfctnCd
type TickerInfo struct {
name string
SpcfctnCd string
}

//
// tickers returns available tickers for a company name in the DB
//
func tickers(db *sql.DB, companyName string) ([]TickerInfo, error) {

selectTickers := `
SELECT trading_code, SpcfctnCd
FROM stock_codes
WHERE company_name LIKE ?
ORDER BY trading_code;`

rows, err := db.Query(selectTickers, "%"+companyName+"%")
if err != nil {
err = errors.Wrap(err, "falha ao ler banco de dados")
return nil, err
}
defer rows.Close()

var info TickerInfo
var list []TickerInfo
for rows.Next() {
err := rows.Scan(&info.name, &info.SpcfctnCd)
if err == nil {
list = append(list, info)
}
}

return list, nil
}

//
// setCompany sets the company ID, CNPJ and stock code based on it's name...
//
// func (r *Report) setCompany(company string) error {
// return r.setCompanyAndTicker(company,"ON")
// }

//
// setCompany sets the company ID, CNPJ and stock code based on it's name.
// setCompanyAndTicker sets the company ID, CNPJ and stock code based on it's name.
//
func (r *Report) setCompany(company string) error {
func (r *Report) setCompanyAndTicker(company string, spcfctnCd string) error {
if company == "" {
return errors.New("company name not set")
}
Expand All @@ -715,7 +758,7 @@ func (r *Report) setCompany(company string) error {
r.cnpj = cnpj

// Stock code
r.code, err = r.fetchStock.Code(r.company, "ON")
r.code, err = r.fetchStock.Code(r.company, spcfctnCd)
if err != nil {
fmt.Printf("\n[x] Erro obtendo código negociação: %v\n", err)
}
Expand Down
Loading

0 comments on commit 18ea32c

Please sign in to comment.