This repository has been archived by the owner on Mar 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from gaius-qi/feat/stock
Feat/stock
- Loading branch information
Showing
9 changed files
with
456 additions
and
298 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package config | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestConfig(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
cfg := New() | ||
|
||
assert.NotNil(cfg) | ||
|
||
assert.Equal(cfg.LogLevel, DefaultLogLevel) | ||
assert.Equal(cfg.LogFormat, DefaultLogFormat) | ||
assert.Equal(cfg.Debug, DefaultDebug) | ||
assert.Equal(cfg.Index, DefaultIndex) | ||
assert.Equal(cfg.Platform, DefaultPlatform) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package stock | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"regexp" | ||
"time" | ||
|
||
"github.com/gaius-qi/honk/internal/config" | ||
) | ||
|
||
const ( | ||
defaultSinaHost = "hq.sinajs.cn" | ||
defaultSinaProtocol = "https:" | ||
sinaTimeLayout = "2006-01-02 15:04:05" | ||
) | ||
|
||
type sinaStock struct { | ||
number string | ||
index config.IndexType | ||
} | ||
|
||
func newSinaStock(cfg *config.Config) sinaStock { | ||
return sinaStock{ | ||
number: cfg.Number, | ||
index: cfg.Index, | ||
} | ||
} | ||
|
||
func (s sinaStock) Get() (*Stock, error) { | ||
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s//%s", defaultSinaProtocol, defaultSinaHost), nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Add query params | ||
q := req.URL.Query() | ||
q.Add("list", fmt.Sprintf("%s%s", s.index, s.number)) | ||
req.URL.RawQuery = q.Encode() | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
statusCode := resp.StatusCode | ||
if statusCode >= http.StatusInternalServerError { | ||
return nil, fmt.Errorf("Sina Server Error, status: %d", statusCode) | ||
} else if statusCode >= http.StatusBadRequest { | ||
return nil, fmt.Errorf("Honk Client Error, status: %d", statusCode) | ||
} | ||
|
||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Match useful data | ||
data := regexp.MustCompile(`[\\"\\,]+`).Split(string(body), -1) | ||
if len(data) < 35 { | ||
return nil, errors.New("Sina platform returns wrong data") | ||
} | ||
|
||
// Time parse | ||
t, err := time.Parse(sinaTimeLayout, fmt.Sprintf("%s %s", data[31], data[32])) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Stock{ | ||
Name: data[1], | ||
Number: s.number, | ||
OpeningPrice: data[2], | ||
PreviousClosingPrice: data[3], | ||
CurrentPrice: data[4], | ||
HighPrice: data[5], | ||
LowPrice: data[6], | ||
Date: t, | ||
}, nil | ||
} |
Oops, something went wrong.