Created
February 8, 2020 12:34
-
-
Save knabben/c57ef1d5e6a6bf2be9c1b6308928d2e2 to your computer and use it in GitHub Desktop.
Fundamental analysis scrapper for BMF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package company | |
| import ( | |
| "github.com/jinzhu/gorm" | |
| _ "github.com/jinzhu/gorm/dialects/postgres" | |
| ) | |
| var Companies []Company | |
| type Company struct { | |
| gorm.Model | |
| Symbol string `gorm:"type:varchar(10)"` | |
| Name string `gorm:"type:varchar(50)"` | |
| Segment string `gorm:"type:varchar(200)"` | |
| Ibovespa bool | |
| } | |
| func (Company) TableName() string { | |
| return "companies" | |
| } | |
| func StartDatabase() *gorm.DB { | |
| dbUrl := "host=localhost port=5432 dbname=bmf user=postgres password=postgres sslmode=disable" | |
| db, err := gorm.Open("postgres", dbUrl) | |
| if err != nil { | |
| panic(err) | |
| } | |
| db.AutoMigrate(&Company{}) | |
| return db | |
| } | |
| func ListCompanies(db *gorm.DB) *gorm.DB { | |
| return db.Where("ibovespa = true").Order("name asc").Limit(10).Find(&Companies) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package cmd | |
| import ( | |
| "fmt" | |
| "io/ioutil" | |
| "os" | |
| "strings" | |
| "text/tabwriter" | |
| "net/http" | |
| "net/url" | |
| "github.com/knabben/bov-stream/producer/company" | |
| "github.com/spf13/cobra" | |
| "github.com/tidwall/gjson" | |
| ) | |
| var fundamentalCmd = &cobra.Command{ | |
| Use: "fundamental", | |
| Short: "Fundamental data from Yahoo for BVSP stocks", | |
| Long: ``, | |
| Run: func(cmd *cobra.Command, args []string) { | |
| fetchFundamental() | |
| }, | |
| } | |
| func init() { | |
| RootCmd.AddCommand(fundamentalCmd) | |
| } | |
| func fetchFundamental() { | |
| db := company.StartDatabase() | |
| w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, '-', | |
| tabwriter.AlignRight|tabwriter.Debug) | |
| fmt.Fprintln(w, | |
| "Name", "\t", | |
| "Symbol", "\t", | |
| "Segment", "\t", | |
| "Current Price", "\t", | |
| "Beta", "\t", | |
| "Ebitda", "\t", | |
| "Enterprise Value", "\t", | |
| "Price/Book", "\t", | |
| "TotalRevenue", "\t", | |
| "Revenue Per Share", "\t", | |
| "Total Debt", "\t", | |
| "Total Cash", "\t", | |
| ) | |
| defaultKeyStats := "quoteSummary.result.0.defaultKeyStatistics" | |
| financialData := "quoteSummary.result.0.financialData" | |
| for _, company := range company.Companies { | |
| v := url.Values{} | |
| v.Set("formatted", "true") | |
| v.Set("modules", "defaultKeyStatistics,financialData,calendarEvents") | |
| var url string = fmt.Sprintf( | |
| "https://query2.finance.yahoo.com/v10/finance/quoteSummary/%s.SA?%s", | |
| company.Symbol, v.Encode()) | |
| resp, err := http.Get(url) | |
| if err != nil { | |
| panic(err) | |
| } | |
| b, err := ioutil.ReadAll(resp.Body) | |
| currentPrice := gjson.Get(string(b), financialData+".currentPrice.fmt") | |
| beta := gjson.Get(string(b), defaultKeyStats+".beta.fmt") | |
| enpValue := gjson.Get(string(b), defaultKeyStats+".enterpriseValue.fmt") | |
| priceToBook := gjson.Get(string(b), defaultKeyStats+".priceToBook.fmt") | |
| revenuePerShare := gjson.Get(string(b), financialData+".revenuePerShare.fmt") | |
| totalCash := gjson.Get(string(b), financialData+".totalCash.fmt") | |
| totalRevenue := gjson.Get(string(b), financialData+".totalRevenue.fmt") | |
| totalDebt := gjson.Get(string(b), financialData+".totalDebt.fmt") | |
| ebitda := gjson.Get(string(b), financialData+".ebitda.fmt") | |
| fmt.Fprintln(w, | |
| company.Name, "\t", | |
| company.Symbol, "\t", | |
| strings.Split(company.Segment, "/")[0], "\t", | |
| currentPrice, "\t", | |
| beta, "\t", | |
| ebitda, "\t", | |
| enpValue, "\t", | |
| priceToBook, "\t", | |
| totalRevenue, "\t", | |
| revenuePerShare, "\t", | |
| totalDebt, "\t", | |
| totalCash, "\t", | |
| ) | |
| resp.Body.Close() | |
| } | |
| defer db.Close() | |
| w.Flush() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment