add command search mutual fund
This commit is contained in:
parent
8b6635f7ea
commit
923c0c1241
|
@ -0,0 +1,87 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/ananthakumaran/paisa/internal/model/mutualfund/scheme"
|
||||
"github.com/ananthakumaran/paisa/internal/scraper/mutualfund"
|
||||
"github.com/logrusorgru/aurora"
|
||||
"github.com/manifoldco/promptui"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var update bool
|
||||
|
||||
var mutualfundCmd = &cobra.Command{
|
||||
Use: "mutualfund",
|
||||
Short: "Search mutual fund",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
db, err := gorm.Open(sqlite.Open(viper.GetString("db_path")), &gorm.Config{})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
db.AutoMigrate(&scheme.Scheme{})
|
||||
count := scheme.Count(db)
|
||||
if update || count == 0 {
|
||||
schemes, err := mutualfund.GetSchemes()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
scheme.UpsertAll(db, schemes)
|
||||
} else {
|
||||
log.Info("Using cached results; pass '-u' to update the cache")
|
||||
}
|
||||
amc := promptAMC(db)
|
||||
name := promptName(db, amc)
|
||||
scheme := scheme.FindScheme(db, amc, name)
|
||||
log.Info("Mutual Fund Scheme Code: ", aurora.Bold(scheme.Code))
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
searchCmd.AddCommand(mutualfundCmd)
|
||||
mutualfundCmd.Flags().BoolVarP(&update, "update", "u", false, "update the Mutual Fund Scheme list")
|
||||
}
|
||||
|
||||
func promptAMC(db *gorm.DB) string {
|
||||
amcs := scheme.GetAMCs(db)
|
||||
return prompt("AMC", amcs)
|
||||
}
|
||||
|
||||
func promptName(db *gorm.DB, amc string) string {
|
||||
names := scheme.GetNAVNames(db, amc)
|
||||
return prompt("Fund Name", names)
|
||||
}
|
||||
|
||||
func prompt(label string, list []string) string {
|
||||
searcher := func(input string, index int) bool {
|
||||
item := list[index]
|
||||
item = strings.Replace(strings.ToLower(item), " ", "", -1)
|
||||
words := strings.Split(strings.ToLower(input), " ")
|
||||
for _, word := range words {
|
||||
if strings.TrimSpace(word) != "" && !strings.Contains(item, word) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
prompt := promptui.Select{
|
||||
Label: label,
|
||||
Items: list,
|
||||
Size: 10,
|
||||
Searcher: searcher,
|
||||
StartInSearchMode: true,
|
||||
}
|
||||
|
||||
_, item, err := prompt.Run()
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return item
|
||||
}
|
|
@ -11,7 +11,6 @@ var cfgFile string
|
|||
var rootCmd = &cobra.Command{
|
||||
Use: "paisa",
|
||||
Short: "A command line tool to manager personal finance",
|
||||
Long: "",
|
||||
}
|
||||
|
||||
func Execute() {
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var searchCmd = &cobra.Command{
|
||||
Use: "search",
|
||||
Short: "Search mutual fund",
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(searchCmd)
|
||||
}
|
|
@ -12,7 +12,6 @@ import (
|
|||
var serveCmd = &cobra.Command{
|
||||
Use: "serve",
|
||||
Short: "serve the WEB UI",
|
||||
Long: "",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
db, err := gorm.Open(sqlite.Open(viper.GetString("db_path")), &gorm.Config{})
|
||||
if err != nil {
|
||||
|
|
|
@ -12,7 +12,6 @@ import (
|
|||
var updateCmd = &cobra.Command{
|
||||
Use: "update",
|
||||
Short: "Sync journal data",
|
||||
Long: "",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
db, err := gorm.Open(sqlite.Open(viper.GetString("db_path")), &gorm.Config{})
|
||||
if err != nil {
|
||||
|
|
3
go.mod
3
go.mod
|
@ -4,6 +4,8 @@ go 1.18
|
|||
|
||||
require (
|
||||
github.com/gin-gonic/gin v1.7.7
|
||||
github.com/logrusorgru/aurora v2.0.3+incompatible
|
||||
github.com/manifoldco/promptui v0.9.0
|
||||
github.com/sirupsen/logrus v1.8.1
|
||||
github.com/spf13/cobra v1.4.0
|
||||
github.com/spf13/viper v1.10.1
|
||||
|
@ -12,6 +14,7 @@ require (
|
|||
)
|
||||
|
||||
require (
|
||||
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
|
||||
github.com/fsnotify/fsnotify v1.5.1 // indirect
|
||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||
github.com/go-playground/locales v0.13.0 // indirect
|
||||
|
|
11
go.sum
11
go.sum
|
@ -1,3 +1,9 @@
|
|||
github.com/chzyer/logex v1.1.10 h1:Swpa1K6QvQznwJRcfTfQJmTE72DqScAa40E+fbHEXEE=
|
||||
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
|
||||
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8=
|
||||
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
|
||||
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8=
|
||||
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
|
@ -37,8 +43,12 @@ github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHm
|
|||
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
|
||||
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
|
||||
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
|
||||
github.com/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczGlG91VSDkswnjF5A8=
|
||||
github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
|
||||
github.com/magiconair/properties v1.8.5 h1:b6kJs+EmPFMYGkow9GiUyCyOvIwYetYJ3fSaWak/Gls=
|
||||
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
|
||||
github.com/manifoldco/promptui v0.9.0 h1:3V4HzJk1TtXW1MTZMP7mdlwbBpIinw3HztaIlYthEiA=
|
||||
github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg=
|
||||
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
|
||||
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
|
||||
|
@ -91,6 +101,7 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh
|
|||
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 h1:HWj/xjIHfjYU5nVXpTM0s39J9CbLn7Cc5a7IC5rwsMQ=
|
||||
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
|
|
|
@ -0,0 +1,64 @@
|
|||
package scheme
|
||||
|
||||
import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Scheme struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
AMC string
|
||||
Code string
|
||||
Name string
|
||||
Type string
|
||||
Category string
|
||||
NAVName string
|
||||
}
|
||||
|
||||
func Count(db *gorm.DB) int64 {
|
||||
var count int64
|
||||
db.Model(&Scheme{}).Count(&count)
|
||||
return count
|
||||
}
|
||||
|
||||
func UpsertAll(db *gorm.DB, schemes []*Scheme) {
|
||||
err := db.Transaction(func(tx *gorm.DB) error {
|
||||
err := tx.Exec("DELETE FROM schemes").Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, scheme := range schemes {
|
||||
err := tx.Create(scheme).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func GetAMCs(db *gorm.DB) []string {
|
||||
var amcs []string
|
||||
db.Model(&Scheme{}).Distinct().Pluck("AMC", &amcs)
|
||||
return amcs
|
||||
}
|
||||
|
||||
func GetNAVNames(db *gorm.DB, amc string) []string {
|
||||
var navNames []string
|
||||
db.Model(&Scheme{}).Where("amc = ? and type = 'Open Ended'", amc).Pluck("NAVName", &navNames)
|
||||
return navNames
|
||||
}
|
||||
|
||||
func FindScheme(db *gorm.DB, amc string, NAVName string) Scheme {
|
||||
var scheme Scheme
|
||||
result := db.Where("amc = ? and nav_name = ?", amc, NAVName).First(&scheme)
|
||||
if result.Error != nil {
|
||||
log.Fatal(result)
|
||||
}
|
||||
return scheme
|
||||
}
|
|
@ -1,10 +1,9 @@
|
|||
package posting
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Posting struct {
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
package mutualfund
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"net/http"
|
||||
|
||||
"github.com/ananthakumaran/paisa/internal/model/mutualfund/scheme"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func GetSchemes() ([]*scheme.Scheme, error) {
|
||||
log.Info("Fetching Mutual Fund Scheme list from AMFI Website")
|
||||
resp, err := http.Get("https://portal.amfiindia.com/DownloadSchemeData_Po.aspx?mf=0")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
reader := csv.NewReader(resp.Body)
|
||||
reader.LazyQuotes = true
|
||||
records, err := reader.ReadAll()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var schemes []*scheme.Scheme
|
||||
for _, record := range records[1:] {
|
||||
scheme := scheme.Scheme{AMC: record[0], Code: record[1], Name: record[2], Type: record[3], Category: record[4], NAVName: record[5]}
|
||||
schemes = append(schemes, &scheme)
|
||||
|
||||
}
|
||||
return schemes, nil
|
||||
}
|
|
@ -36,7 +36,8 @@ export default async function () {
|
|||
groupKeys,
|
||||
_.map(groupKeys, () => 0)
|
||||
);
|
||||
const start = dayjs("01-Oct-2014", "DD-MMM-YYYY"),
|
||||
|
||||
const start = _.min(_.map(postings, (p) => p.timestamp)),
|
||||
end = dayjs().startOf("month");
|
||||
const ts = _.groupBy(postings, (p) => p.timestamp.format("YYYY-MM"));
|
||||
|
||||
|
|
Loading…
Reference in New Issue