|
|
@ -5,95 +5,131 @@ package mop |
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"bytes" |
|
|
|
"bytes" |
|
|
|
"encoding/json" |
|
|
|
"encoding/json" |
|
|
|
// "io/ioutil"
|
|
|
|
"io/ioutil" |
|
|
|
// "net/http"
|
|
|
|
"net/http" |
|
|
|
|
|
|
|
"strings" |
|
|
|
|
|
|
|
"time" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
const real_time_url = "http://finance.google.com/finance/info?client=ig&q=" |
|
|
|
const real_time_url = "http://finance.google.com/finance/info?client=ig&q=" |
|
|
|
const body = ` |
|
|
|
|
|
|
|
// [
|
|
|
|
// const body = `
|
|
|
|
{ |
|
|
|
// // [
|
|
|
|
"id": "665300" |
|
|
|
// {
|
|
|
|
,"t" : "COH" |
|
|
|
// "id": "22144"
|
|
|
|
,"e" : "NYSE" |
|
|
|
// ,"t" : "AAPL"
|
|
|
|
,"l" : "56.54" |
|
|
|
// ,"e" : "NASDAQ"
|
|
|
|
,"l_cur" : "56.54" |
|
|
|
// ,"l" : "393.78"
|
|
|
|
,"s": "0" |
|
|
|
// ,"l_cur" : "393.78"
|
|
|
|
,"ltt":"4:01PM EDT" |
|
|
|
// ,"s": "2"
|
|
|
|
,"lt" : "Jun 21, 4:01PM EDT" |
|
|
|
// ,"ltt":"4:00PM EDT"
|
|
|
|
,"c" : "-0.75" |
|
|
|
// ,"lt" : "Jun 27, 4:00PM EDT"
|
|
|
|
,"cp" : "-1.31" |
|
|
|
// ,"c" : "-4.29"
|
|
|
|
,"ccol" : "chr" |
|
|
|
// ,"cp" : "-1.08"
|
|
|
|
} |
|
|
|
// ,"ccol" : "chr"
|
|
|
|
,{ |
|
|
|
// ,"el": "393.40"
|
|
|
|
"id": "353353" |
|
|
|
// ,"el_cur": "393.40"
|
|
|
|
,"t" : "ATVI" |
|
|
|
// ,"elt" : "Jun 27, 5:04PM EDT"
|
|
|
|
,"e" : "NASDAQ" |
|
|
|
// ,"ec" : "-0.38"
|
|
|
|
,"l" : "13.55" |
|
|
|
// ,"ecp" : "-0.10"
|
|
|
|
,"l_cur" : "13.55" |
|
|
|
// ,"eccol" : "chr"
|
|
|
|
,"s": "0" |
|
|
|
// ,"div" : "3.05"
|
|
|
|
,"ltt":"3:59PM EDT" |
|
|
|
// ,"yld" : "3.10"
|
|
|
|
,"lt" : "Jun 21, 3:59PM EDT" |
|
|
|
// }
|
|
|
|
,"c" : "-0.33" |
|
|
|
// ,{
|
|
|
|
,"cp" : "-2.38" |
|
|
|
// "id": "353353"
|
|
|
|
,"ccol" : "chr" |
|
|
|
// ,"t" : "ATVI"
|
|
|
|
} |
|
|
|
// ,"e" : "NASDAQ"
|
|
|
|
,{ |
|
|
|
// ,"l" : "13.55"
|
|
|
|
"id": "17154" |
|
|
|
// ,"l_cur" : "13.55"
|
|
|
|
,"t" : "HPQ" |
|
|
|
// ,"s": "0"
|
|
|
|
,"e" : "NYSE" |
|
|
|
// ,"ltt":"3:59PM EDT"
|
|
|
|
,"l" : "24.15" |
|
|
|
// ,"lt" : "Jun 21, 3:59PM EDT"
|
|
|
|
,"l_cur" : "24.15" |
|
|
|
// ,"c" : "-0.33"
|
|
|
|
,"s": "0" |
|
|
|
// ,"cp" : "-2.38"
|
|
|
|
,"ltt":"4:01PM EDT" |
|
|
|
// ,"ccol" : "chr"
|
|
|
|
,"lt" : "Jun 21, 4:01PM EDT" |
|
|
|
// }
|
|
|
|
,"c" : "-0.57" |
|
|
|
// ,{
|
|
|
|
,"cp" : "-2.31" |
|
|
|
// "id": "17154"
|
|
|
|
,"ccol" : "chr" |
|
|
|
// ,"t" : "HPQ"
|
|
|
|
} |
|
|
|
// ,"e" : "NYSE"
|
|
|
|
,{ |
|
|
|
// ,"l" : "24.15"
|
|
|
|
"id": "18241" |
|
|
|
// ,"l_cur" : "24.15"
|
|
|
|
,"t" : "IBM" |
|
|
|
// ,"s": "0"
|
|
|
|
,"e" : "NYSE" |
|
|
|
// ,"ltt":"4:01PM EDT"
|
|
|
|
,"l" : "195.46" |
|
|
|
// ,"lt" : "Jun 21, 4:01PM EDT"
|
|
|
|
,"l_cur" : "195.46" |
|
|
|
// ,"c" : "0.57"
|
|
|
|
,"s": "0" |
|
|
|
// ,"cp" : "2.31"
|
|
|
|
,"ltt":"4:02PM EDT" |
|
|
|
// ,"ccol" : "chr"
|
|
|
|
,"lt" : "Jun 21, 4:02PM EDT" |
|
|
|
// }
|
|
|
|
,"c" : "-1.89" |
|
|
|
// ,{
|
|
|
|
,"cp" : "-0.96" |
|
|
|
// "id": "18241"
|
|
|
|
,"ccol" : "chr" |
|
|
|
// ,"t" : "IBM"
|
|
|
|
} |
|
|
|
// ,"e" : "NYSE"
|
|
|
|
]` |
|
|
|
// ,"l" : "195.46"
|
|
|
|
|
|
|
|
// ,"l_cur" : "195.46"
|
|
|
|
|
|
|
|
// ,"s": "0"
|
|
|
|
|
|
|
|
// ,"ltt":"4:02PM EDT"
|
|
|
|
|
|
|
|
// ,"lt" : "Jun 21, 4:02PM EDT"
|
|
|
|
|
|
|
|
// ,"c" : "-1.89"
|
|
|
|
|
|
|
|
// ,"cp" : "-0.96"
|
|
|
|
|
|
|
|
// ,"ccol" : "chr"
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// ]`
|
|
|
|
|
|
|
|
|
|
|
|
type Message struct { |
|
|
|
type Message struct { |
|
|
|
Ticker string `json:"t"` |
|
|
|
Ticker string `json:"t"` |
|
|
|
LastTrade string `json:"l"` |
|
|
|
Exchange string `json:"e"` |
|
|
|
Change string `json:"c"` |
|
|
|
LastTrade string `json:"l"` |
|
|
|
|
|
|
|
CurrentPrice string `json:"l_cur"` |
|
|
|
|
|
|
|
LastTradeTime string `json:"ltt"` |
|
|
|
|
|
|
|
LastTradeDateTime string `json:"lt"` |
|
|
|
|
|
|
|
Change string `json:"c"` |
|
|
|
|
|
|
|
ChangePercent string `json:"cp"` |
|
|
|
|
|
|
|
ExLastTrade string `json:"el"` |
|
|
|
|
|
|
|
ExCurrentPrice string `json:"el_cur"` |
|
|
|
|
|
|
|
ExLastTradeDateTime string `json:"elt"` |
|
|
|
|
|
|
|
ExChange string `json:"ec"` |
|
|
|
|
|
|
|
ExChangePercent string `json:"ecp"` |
|
|
|
|
|
|
|
Dividend string `json:"div"` |
|
|
|
|
|
|
|
Yield string `json:"yld"` |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var message []Message |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (m *Message) Color() string { |
|
|
|
|
|
|
|
if strings.Index(m.Change, "-") == -1 { |
|
|
|
|
|
|
|
return "</green><green>" |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
return "</red><red>" |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
func Quote(ticker string) []Message { |
|
|
|
func Quote(ticker string) []Message { |
|
|
|
// // Send the request.
|
|
|
|
if len(message) > 0 && time.Now().Second()%5 != 0 { // Fetch quotes every 5 seconds.
|
|
|
|
// response, err := http.Get(real_time_url + ticker)
|
|
|
|
return message |
|
|
|
// if err != nil {
|
|
|
|
} |
|
|
|
// panic(err)
|
|
|
|
|
|
|
|
// }
|
|
|
|
// Send the request.
|
|
|
|
//
|
|
|
|
response, err := http.Get(real_time_url + ticker) |
|
|
|
// // Fetch response and get its body.
|
|
|
|
if err != nil { |
|
|
|
// defer response.Body.Close()
|
|
|
|
panic(err) |
|
|
|
// body, err := ioutil.ReadAll(response.Body)
|
|
|
|
} |
|
|
|
//
|
|
|
|
|
|
|
|
// // Parse JSON.
|
|
|
|
// Fetch response and get its body.
|
|
|
|
// var message []Message
|
|
|
|
defer response.Body.Close() |
|
|
|
// err = json.Unmarshal(sanitize(body), &message)
|
|
|
|
body, err := ioutil.ReadAll(response.Body) |
|
|
|
|
|
|
|
|
|
|
|
// Parse JSON.
|
|
|
|
// Parse JSON.
|
|
|
|
var message []Message |
|
|
|
err = json.Unmarshal(sanitize(body), &message) |
|
|
|
err := json.Unmarshal(sanitize([]byte(body)), &message) |
|
|
|
|
|
|
|
|
|
|
|
// Parse JSON.
|
|
|
|
|
|
|
|
// err := json.Unmarshal(sanitize([]byte(body)), &message)
|
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
panic(err) |
|
|
|
panic(err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return message |
|
|
|
return message |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|