使用反向代理访问yahoo,增加情绪指数

master
yg070520@sina.com 1 year ago
parent 844b5f33ad
commit 45fd0aba2e
  1. 9
      cmd/mop/main.go
  2. 4
      yahoo_crumb.go
  3. 11
      yahoo_market.go
  4. 68
      yahoo_quotes.go

@ -16,7 +16,7 @@ import (
"bufio" "bufio"
"encoding/json" "encoding/json"
"strconv" "strconv"
//"log" "log"
//"io/ioutil" //"io/ioutil"
"easyquotation" "easyquotation"
@ -252,6 +252,7 @@ func mainLoop(screen *mop.Screen, profile *mop.Profile, mode string) {
market := mop.NewMarket(stock.G_STOCK_MANAGER.StockList, &watchlist) market := mop.NewMarket(stock.G_STOCK_MANAGER.StockList, &watchlist)
quotes := mop.NewQuotes(market, profile, stock.G_STOCK_MANAGER.StockList, &watchlist, client) quotes := mop.NewQuotes(market, profile, stock.G_STOCK_MANAGER.StockList, &watchlist, client)
quotes.Setquotes()
codestoadd := []string{"sh000001", "sz399001", "sz399006"} codestoadd := []string{"sh000001", "sz399001", "sz399006"}
quotes.Addstockcodetofile(codestoadd) quotes.Addstockcodetofile(codestoadd)
screen.Draw(market) screen.Draw(market)
@ -280,6 +281,10 @@ loop:
lineEditor.Prompt(event.Ch) lineEditor.Prompt(event.Ch)
} else if event.Ch == 'F' { } else if event.Ch == 'F' {
profile.SetFilter("") profile.SetFilter("")
} else if event.Ch == 'T' {
watchlist := getwatchlist()
log.Println("watchlist update.", watchlist)
quotes.ResetforNewday(&watchlist)
} else if event.Ch == 'o' || event.Ch == 'O' { } else if event.Ch == 'o' || event.Ch == 'O' {
columnEditor = mop.NewColumnEditor(screen, quotes) columnEditor = mop.NewColumnEditor(screen, quotes)
} else if event.Ch == 'g' || event.Ch == 'G' { } else if event.Ch == 'g' || event.Ch == 'G' {
@ -451,8 +456,8 @@ loop:
} }
if hour == 9 && minute == 11 && second == 0 && mode == "standby" { if hour == 9 && minute == 11 && second == 0 && mode == "standby" {
//log.Println("It's a test")
watchlist := getwatchlist() watchlist := getwatchlist()
log.Println("watchlist update enter.")
quotes.ResetforNewday(&watchlist) quotes.ResetforNewday(&watchlist)
paused = false paused = false
} }

@ -15,7 +15,7 @@ import (
const crumbURL = "https://query1.finance.yahoo.com/v1/test/getcrumb" const crumbURL = "https://query1.finance.yahoo.com/v1/test/getcrumb"
const cookieURL = "https://login.yahoo.com" const cookieURL = "https://login.yahoo.com"
const userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/113.0" const userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/113.0"
const substitute = "http://racknerd.jatus.top" const substitute = "http://racknerd.jatus.top:8000"
func fetchCrumb(cookies string) (string, string) { func fetchCrumb(cookies string) (string, string) {
substituteUrl := crumbURL substituteUrl := crumbURL
@ -71,8 +71,10 @@ func fetchCrumb(cookies string) (string, string) {
panic(err) panic(err)
} }
log.Println("Crumb:", string(body[:])) log.Println("Crumb:", string(body[:]))
//log.Println("body:", string(body[:]))
return string(body[:]),substitute return string(body[:]),substitute
} }
//log.Println("Originl Crumb:", string(body[:]))
return string(body[:]),"" return string(body[:]),""
} }

@ -9,7 +9,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"log" //"log"
"strings" "strings"
"easyquotation/stock" "easyquotation/stock"
@ -36,6 +36,7 @@ type Market struct {
Gold map[string]string Gold map[string]string
errors string // Error(s), if any. errors string // Error(s), if any.
url string // URL with symbols to fetch data url string // URL with symbols to fetch data
quotes *Quotes
res map[string]*stock.Stock res map[string]*stock.Stock
watchlist *Watchlist watchlist *Watchlist
@ -63,14 +64,15 @@ func NewMarket(res map[string]*stock.Stock, watchlist *Watchlist) *Market {
market.Gold = make(map[string]string) market.Gold = make(map[string]string)
market.cookies = fetchCookies() market.cookies = fetchCookies()
//market.quotes = quotes
substitute := "" substitute := ""
market.crumb, substitute = fetchCrumb(market.cookies) market.crumb, substitute = fetchCrumb(market.cookies)
if substitute != "" { if substitute != "" {
newURL := strings.Replace(marketURL, "https://query1.finance.yahoo.com", substitute, -1) newURL := strings.Replace(marketURL, "https://query1.finance.yahoo.com", substitute, -1)
log.Println("URL is ", newURL) //log.Println("URL is ", newURL)
market.url = fmt.Sprintf(newURL, market.crumb, `^DJI,^IXIC,^GSPC,^N225,^HSI,^FTSE,^GDAXI,^TNX,CL=F,CNH=X,EUR=X,GC=F`) + marketURLQueryParts market.url = fmt.Sprintf(newURL, market.crumb, `^DJI,^IXIC,^GSPC,^N225,^HSI,^FTSE,^GDAXI,^TNX,CL=F,CNH=X,EUR=X,GC=F`) + marketURLQueryParts
log.Println("full URL is ", market.url) //log.Println("full URL is ", market.url)
}else{ }else{
market.url = fmt.Sprintf(marketURL, market.crumb, `^DJI,^IXIC,^GSPC,^N225,^HSI,^FTSE,^GDAXI,^TNX,CL=F,CNH=X,EUR=X,GC=F`) + marketURLQueryParts market.url = fmt.Sprintf(marketURL, market.crumb, `^DJI,^IXIC,^GSPC,^N225,^HSI,^FTSE,^GDAXI,^TNX,CL=F,CNH=X,EUR=X,GC=F`) + marketURLQueryParts
} }
@ -202,7 +204,8 @@ func (market *Market) extract(body []byte) *Market {
}else{ }else{
market.Yield[`change`] = q.Time//market.watchlist.Baseon market.Yield[`change`] = q.Time//market.watchlist.Baseon
} }
market.Yield[`latest`] = "" avgper, _ := market.quotes.getemotionindex(market.res)
market.Yield[`latest`] = float2Str(avgper)
//log.Println("here we update the market ,time is", q.Time) //log.Println("here we update the market ,time is", q.Time)
market.Oil = assign(results, 8, true) market.Oil = assign(results, 8, true)

@ -128,6 +128,10 @@ type Watchlist struct {
Watchlist []WatchlistItem `json:"watchlist"` Watchlist []WatchlistItem `json:"watchlist"`
} }
func (quotes *Quotes) Setquotes(){
quotes.market.quotes = quotes
}
// Fetch the latest stock quotes and parse raw fetched data into array of // Fetch the latest stock quotes and parse raw fetched data into array of
// []Stock structs. // []Stock structs.
func (quotes *Quotes) Fetch() (self *Quotes) { func (quotes *Quotes) Fetch() (self *Quotes) {
@ -618,6 +622,42 @@ func (quotes* Quotes) ResetforNewday(watchlist *Watchlist) {
quotes.totalstocks = quotes.totalstocks[:0] quotes.totalstocks = quotes.totalstocks[:0]
quotes.watchlist = watchlist quotes.watchlist = watchlist
quotes.upstocks = map[string]string{} quotes.upstocks = map[string]string{}
log.Println("here we retrieve data :", quotes.watchlist.Baseon)
}
func (quotes* Quotes) getemotionindex(res map[string]*stock.Stock) (float64, int) {
avgpercent := 100.0
avgcount := 0
array_percent := []float64{}
wamap := make(map[string]WatchlistItem, len(quotes.watchlist.Watchlist))
for _, item := range quotes.watchlist.Watchlist {
if _, ok := res[item.Scode]; ok {
wamap[item.Scode] = item
}
//fmt.Println(scodes)
}
for _, sitem := range quotes.totalstocks {
if inlist(quotes.profile.Tickers, sitem.Scode) == false {
scode := sitem.Scode
q := res[scode].Market
if _, ok := wamap[scode]; ok {
strprice, _ := strconv.ParseFloat(wamap[scode].Enterprice, 64)
stdchangepercent := q.LastPrice*100 / strprice
avgcount++
avgpercent += stdchangepercent
array_percent = append(array_percent, stdchangepercent)
}
}
}
if avgcount > 0 {
log.Println(avgpercent, avgcount)
//log.Println(array_percent)
avgpercent = avgpercent / float64(avgcount)
//quotes.stocks[0].High52 = float2Str(avgpercent)
}
return avgpercent, avgcount
} }
// this will parse the json objects // this will parse the json objects
@ -646,23 +686,12 @@ func (quotes *Quotes) parse2(body []byte, res map[string]*stock.Stock) (*Quotes,
//fmt.Println(scodes) //fmt.Println(scodes)
} }
for _, item := range quotes.addedstocks {//special stock to trace
if _, ok := res[item]; ok {
q := res[item].Market
if q.LastPrice>=q.PreClose*1.02 || q.LastPrice<=q.PreClose*0.98 {
scodes = append(scodes, item)
}
}
}
for _, item := range quotes.profile.Tickers {//profile stock to trace for _, item := range quotes.profile.Tickers {//profile stock to trace
if _, ok := res[item]; ok { if _, ok := res[item]; ok {
scodes = append(scodes, item) scodes = append(scodes, item)
} }
} }
//scodes = append(scodes, quotes.profile.Tickers...)
//fmt.Println(scodes)
//
var snames []string var snames []string
quotes.stocks = make([]Stock, len(scodes)) quotes.stocks = make([]Stock, len(scodes))
//fmt.Println(res["sh600000"]) //fmt.Println(res["sh600000"])
@ -746,21 +775,6 @@ func (quotes *Quotes) parse2(body []byte, res map[string]*stock.Stock) (*Quotes,
} }
} }
//quotes.stocks[0].PeRatio = result["trailingPE"]
/*
// TODO calculate rt
quotes.stocks[0].PeRatioX = result["trailingPE"]
quotes.stocks[0].Dividend = result["trailingAnnualDividendRate"]
quotes.stocks[0].Yield = result["trailingAnnualDividendYield"]
quotes.stocks[0].MarketCap = result["marketCap"]
// TODO calculate rt?
quotes.stocks[0].MarketCapX = result["marketCap"]
quotes.stocks[0].Currency = result["currency"]
quotes.stocks[0].PreOpen = result["preMarketChangePercent"]
quotes.stocks[0].AfterHours = result["postMarketChangePercent"]
*/
//fmt.Println(quotes)
return quotes, nil return quotes, nil
} }

Loading…
Cancel
Save