diff --git a/frontend b/frontend index a85a87ac..6b214a2f 160000 --- a/frontend +++ b/frontend @@ -1 +1 @@ -Subproject commit a85a87acdbb8e391b316f3acab5c89344eeed38e +Subproject commit 6b214a2fa283eba46f5df3ec7ef73af285d8f3a7 diff --git a/migrations/20180215141312_add_image_path.down.sql b/migrations/20180215141312_add_image_path.down.sql new file mode 100644 index 00000000..74ddeeca --- /dev/null +++ b/migrations/20180215141312_add_image_path.down.sql @@ -0,0 +1 @@ +ALTER TABLE MarketEvents DROP COLUMN imagePath; \ No newline at end of file diff --git a/migrations/20180215141312_add_image_path.up.sql b/migrations/20180215141312_add_image_path.up.sql new file mode 100644 index 00000000..da1e3888 --- /dev/null +++ b/migrations/20180215141312_add_image_path.up.sql @@ -0,0 +1 @@ +ALTER TABLE MarketEvents ADD COLUMN imagePath varchar(255); \ No newline at end of file diff --git a/models/MarketEvent.go b/models/MarketEvent.go index 2fa8ab6f..31c001e5 100644 --- a/models/MarketEvent.go +++ b/models/MarketEvent.go @@ -1,6 +1,11 @@ package models import ( + "io" + "net/http" + "os" + "strings" + "github.com/Sirupsen/logrus" "github.com/thakkarparth007/dalal-street-server/proto_build/models" "github.com/thakkarparth007/dalal-street-server/utils" @@ -13,6 +18,7 @@ type MarketEvent struct { Headline string `gorm:"column:headline;not null" json:"headline"` Text string `gorm:"column:text" json:"text"` IsGlobal bool `gorm:"column:isGlobal" json:"is_global"` + ImagePath string `gorm:"column:imagePath" json:"image_path"` CreatedAt string `gorm:"column:createdAt;not null" json:"created_at"` } @@ -28,6 +34,7 @@ func (gMarketEvent *MarketEvent) ToProto() *models_pb.MarketEvent { Text: gMarketEvent.Text, EmotionScore: gMarketEvent.EmotionScore, IsGlobal: gMarketEvent.IsGlobal, + ImagePath: gMarketEvent.ImagePath, CreatedAt: gMarketEvent.CreatedAt, } return pMarketEvent @@ -66,27 +73,58 @@ func GetMarketEvents(lastId, count uint32) (bool, []*MarketEvent, error) { return moreExists, marketEvents, nil } -func AddMarketEvent(stockId uint32, headline, text string, isGlobal bool) error { +func AddMarketEvent(stockId uint32, headline, text string, isGlobal bool, imageURL string) error { var l = logger.WithFields(logrus.Fields{ "method": "AddMarketEvent", "param_stockId": stockId, "param_headline": headline, "param_text": text, "param_isGlobal": isGlobal, + "param_imageURL": imageURL, }) l.Infof("Attempting") + // Try downloading image first + response, err := http.Get(imageURL) + if err != nil { + l.Error(err) + return err + } + defer response.Body.Close() + + var splitURL = strings.Split(imageURL, "/") + var basename = splitURL[len(splitURL)-1] + l.Debugf("strings : %v ImageURL : %s Basename : %s", splitURL, imageURL, basename) + + // open file for saving image + file, err := os.Create(utils.GetImageBasePath() + basename) + + if err != nil { + l.Error(err) + return err + } + defer file.Close() + + // copy image to file + _, err = io.Copy(file, response.Body) + if err != nil { + l.Error(err) + return err + } + db := getDB() me := &MarketEvent{ - StockId: stockId, - Headline: headline, - Text: text, - IsGlobal: isGlobal, + StockId: stockId, + Headline: headline, + Text: text, + IsGlobal: isGlobal, + ImagePath: basename, + CreatedAt: utils.GetCurrentTimeISO8601(), } - if err := db.Save(me).Error; err != nil { + if err = db.Save(me).Error; err != nil { l.Error(err) return err } diff --git a/models/MarketEvent_test.go b/models/MarketEvent_test.go index 0b980b5d..97bc5e44 100644 --- a/models/MarketEvent_test.go +++ b/models/MarketEvent_test.go @@ -14,6 +14,7 @@ func TestMarketEventToProto(t *testing.T) { Text: "Hello World", IsGlobal: true, EmotionScore: -54, + ImagePath: "bitcoin_1516197589.jpg", CreatedAt: "2017-02-09T00:00:00", } @@ -32,6 +33,7 @@ func Test_GetMarketEvents(t *testing.T) { Text: "Hello World", IsGlobal: true, EmotionScore: -54, + ImagePath: "bitcoin_1516197589.jpg", CreatedAt: "2017-02-09T00:00:00", } db := getDB() @@ -71,18 +73,19 @@ func Test_GetMarketEvents(t *testing.T) { func Test_AddMarketEvent(t *testing.T) { marketEvent := &MarketEvent{ - Id: 1, - StockId: 3, - Headline: "Hello", - Text: "Hello World", - IsGlobal: true, + Id: 1, + StockId: 3, + Headline: "Hello", + Text: "Hello World", + IsGlobal: true, + ImagePath: "bitcoin_1516197589.jpg", } db := getDB() defer func() { db.Exec("DELETE FROM MarketEvents") }() - err := AddMarketEvent(3, "Hello", "Hello World", true) + err := AddMarketEvent(3, "Hello", "Hello World", true, "http://www.valuewalk.com/wp-content/uploads/2018/01/bitcoin_1516197589.jpg") if err != nil { t.Fatalf("AddMarketEvent failed with error: %+v", err) } diff --git a/proto b/proto index 68e6c561..3a9f7429 160000 --- a/proto +++ b/proto @@ -1 +1 @@ -Subproject commit 68e6c5618eeb700b92ea358cb418da0fc4af331a +Subproject commit 3a9f7429a897e01dc72179a2b8d601693a9f6708 diff --git a/socketapi/repl/repl.go b/socketapi/repl/repl.go index c11c4169..d7f3cfa3 100644 --- a/socketapi/repl/repl.go +++ b/socketapi/repl/repl.go @@ -234,6 +234,7 @@ var replCmds = map[string]replCmdFn{ var headline string var text string var isGlobal bool + var imageURL string aun, _ := userSess.Get("repl_Username") @@ -243,6 +244,9 @@ var replCmds = map[string]replCmdFn{ s.print("Enter brief text:") s.read("%q", &text) + s.print("Enter image URL:") + s.read("%q", &imageURL) + if stockId == 0 { isGlobal = true } else { @@ -257,7 +261,7 @@ var replCmds = map[string]replCmdFn{ c := 'N' s.read("%c", &c) if c == 'Y' { - err := models.AddMarketEvent(stockId, headline, text, isGlobal) + err := models.AddMarketEvent(stockId, headline, text, isGlobal, imageURL) if err != nil { models.AdminLog(aun, fmt.Sprintf("Adding market event '%s'[%s] for %d failed due to '%+v'", headline, text, stockId, err)) s.error(err) diff --git a/utils/utils.go b/utils/utils.go index db9677ad..f6c6663d 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -3,6 +3,7 @@ package utils import ( "math/rand" "net/http" + "os" "strings" "time" ) @@ -45,5 +46,9 @@ func IsGrpcRequest(req *http.Request) bool { } func GetCurrentTimeISO8601() string { - return time.Now().Format(time.RFC3339); + return time.Now().Format(time.RFC3339) +} + +func GetImageBasePath() string { + return os.Getenv("GOPATH") + "/src/github.com/thakkarparth007/dalal-street-server/frontend/public/src/images/news/" }