From 45fd0aba2e7073f025c29ac1220b88a3047b3dce Mon Sep 17 00:00:00 2001 From: "yg070520@sina.com" Date: Thu, 29 Jun 2023 15:24:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E5=8F=8D=E5=90=91=E4=BB=A3?= =?UTF-8?q?=E7=90=86=E8=AE=BF=E9=97=AEyahoo=EF=BC=8C=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=83=85=E7=BB=AA=E6=8C=87=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/mop/main.go | 9 +++++-- yahoo_crumb.go | 4 ++- yahoo_market.go | 11 +++++--- yahoo_quotes.go | 68 +++++++++++++++++++++++++++++-------------------- 4 files changed, 58 insertions(+), 34 deletions(-) diff --git a/cmd/mop/main.go b/cmd/mop/main.go index 69a2ce9..00b3898 100644 --- a/cmd/mop/main.go +++ b/cmd/mop/main.go @@ -16,7 +16,7 @@ import ( "bufio" "encoding/json" "strconv" - //"log" + "log" //"io/ioutil" "easyquotation" @@ -252,6 +252,7 @@ func mainLoop(screen *mop.Screen, profile *mop.Profile, mode string) { market := mop.NewMarket(stock.G_STOCK_MANAGER.StockList, &watchlist) quotes := mop.NewQuotes(market, profile, stock.G_STOCK_MANAGER.StockList, &watchlist, client) + quotes.Setquotes() codestoadd := []string{"sh000001", "sz399001", "sz399006"} quotes.Addstockcodetofile(codestoadd) screen.Draw(market) @@ -280,6 +281,10 @@ loop: lineEditor.Prompt(event.Ch) } else if event.Ch == 'F' { 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' { columnEditor = mop.NewColumnEditor(screen, quotes) } else if event.Ch == 'g' || event.Ch == 'G' { @@ -451,8 +456,8 @@ loop: } if hour == 9 && minute == 11 && second == 0 && mode == "standby" { - //log.Println("It's a test") watchlist := getwatchlist() + log.Println("watchlist update enter.") quotes.ResetforNewday(&watchlist) paused = false } diff --git a/yahoo_crumb.go b/yahoo_crumb.go index a19b62a..e8e3d60 100644 --- a/yahoo_crumb.go +++ b/yahoo_crumb.go @@ -15,7 +15,7 @@ import ( const crumbURL = "https://query1.finance.yahoo.com/v1/test/getcrumb" 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 substitute = "http://racknerd.jatus.top" +const substitute = "http://racknerd.jatus.top:8000" func fetchCrumb(cookies string) (string, string) { substituteUrl := crumbURL @@ -71,8 +71,10 @@ func fetchCrumb(cookies string) (string, string) { panic(err) } log.Println("Crumb:", string(body[:])) + //log.Println("body:", string(body[:])) return string(body[:]),substitute } + //log.Println("Originl Crumb:", string(body[:])) return string(body[:]),"" } diff --git a/yahoo_market.go b/yahoo_market.go index 065ef22..65f6fd4 100644 --- a/yahoo_market.go +++ b/yahoo_market.go @@ -9,7 +9,7 @@ import ( "fmt" "io/ioutil" "net/http" - "log" + //"log" "strings" "easyquotation/stock" @@ -36,6 +36,7 @@ type Market struct { Gold map[string]string errors string // Error(s), if any. url string // URL with symbols to fetch data + quotes *Quotes res map[string]*stock.Stock watchlist *Watchlist @@ -63,14 +64,15 @@ func NewMarket(res map[string]*stock.Stock, watchlist *Watchlist) *Market { market.Gold = make(map[string]string) market.cookies = fetchCookies() + //market.quotes = quotes substitute := "" market.crumb, substitute = fetchCrumb(market.cookies) if substitute != "" { 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 - log.Println("full URL is ", market.url) + //log.Println("full URL is ", market.url) }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 } @@ -202,7 +204,8 @@ func (market *Market) extract(body []byte) *Market { }else{ 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) market.Oil = assign(results, 8, true) diff --git a/yahoo_quotes.go b/yahoo_quotes.go index bc596bc..60892c0 100644 --- a/yahoo_quotes.go +++ b/yahoo_quotes.go @@ -128,6 +128,10 @@ type Watchlist struct { 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 // []Stock structs. func (quotes *Quotes) Fetch() (self *Quotes) { @@ -618,6 +622,42 @@ func (quotes* Quotes) ResetforNewday(watchlist *Watchlist) { quotes.totalstocks = quotes.totalstocks[:0] quotes.watchlist = watchlist 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 @@ -646,23 +686,12 @@ func (quotes *Quotes) parse2(body []byte, res map[string]*stock.Stock) (*Quotes, //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 if _, ok := res[item]; ok { scodes = append(scodes, item) } } - //scodes = append(scodes, quotes.profile.Tickers...) - //fmt.Println(scodes) - // + var snames []string quotes.stocks = make([]Stock, len(scodes)) //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 }