Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

News images #166

Merged
merged 7 commits into from
Feb 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frontend
1 change: 1 addition & 0 deletions migrations/20180215141312_add_image_path.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE MarketEvents DROP COLUMN imagePath;
1 change: 1 addition & 0 deletions migrations/20180215141312_add_image_path.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE MarketEvents ADD COLUMN imagePath varchar(255);
50 changes: 44 additions & 6 deletions models/MarketEvent.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"`
}

Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
15 changes: 9 additions & 6 deletions models/MarketEvent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}

Expand All @@ -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()
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion proto
6 changes: 5 additions & 1 deletion socketapi/repl/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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 {
Expand All @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"math/rand"
"net/http"
"os"
"strings"
"time"
)
Expand Down Expand Up @@ -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/"
}