Skip to content

Commit

Permalink
Transfer ownership of project, update deps, fix bugs
Browse files Browse the repository at this point in the history
Fix for #2 #3 #5
  • Loading branch information
shayne committed Feb 28, 2021
1 parent e9a3885 commit ea6db6b
Show file tree
Hide file tree
Showing 39 changed files with 166 additions and 68 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.exe
/com.shayne.stocks.sdPlugin/sdplugin-stock*
/vendor
16 changes: 16 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Connect to server",
"type": "go",
"request": "attach",
"mode": "remote",
"port": 2345,
"host": "127.0.0.1",
}
]
}
16 changes: 8 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
plugin:
-@kill-streamdeck.bat
@go build -o com.exension.stocks.sdPlugin\\sdplugin-stocks.exe github.com/exension/stock-ticker-stream-deck-plugin/cmd/stock_ticker_stream_deck_plugin
@env GOOS=darwin GOARCH=amd64 go build -o com.exension.stocks.sdPlugin\\sdplugin-stocks github.com/exension/stock-ticker-stream-deck-plugin/cmd/stock_ticker_stream_deck_plugin
@xcopy com.exension.stocks.sdPlugin $(APPDATA)\\Elgato\\StreamDeck\\Plugins\\com.exension.stocks.sdPlugin\\ /E /Q /Y
@go build -o com.shayne.stocks.sdPlugin\\sdplugin-stocks.exe github.com/shayne/stock-ticker-stream-deck-plugin/cmd/stock_ticker_stream_deck_plugin
@env GOOS=darwin GOARCH=amd64 go build -o com.shayne.stocks.sdPlugin\\sdplugin-stocks github.com/shayne/stock-ticker-stream-deck-plugin/cmd/stock_ticker_stream_deck_plugin
@xcopy com.shayne.stocks.sdPlugin $(APPDATA)\\Elgato\\StreamDeck\\Plugins\\com.shayne.stocks.sdPlugin\\ /E /Q /Y
@start-streamdeck.bat

debug:
@go build -o com.exension.stocks.sdPlugin\\sdplugin-stocks.exe github.com/exension/stock-ticker-stream-deck-plugin/cmd/stock_ticker_stream_deck_plugin
@env GOOS=darwin GOARCH=amd64 go build -o com.exension.stocks.sdPlugin\\sdplugin-stocks github.com/exension/stock-ticker-stream-deck-plugin/cmd/stock_ticker_stream_deck_plugin
@xcopy com.exension.stocks.sdPlugin $(APPDATA)\\Elgato\\StreamDeck\\Plugins\\com.exension.stocks.sdPlugin\\ /E /Q /Y
@go build -o com.shayne.stocks.sdPlugin\\sdplugin-stocks.exe github.com/shayne/stock-ticker-stream-deck-plugin/cmd/stock_ticker_stream_deck_plugin
@env GOOS=darwin GOARCH=amd64 go build -o com.shayne.stocks.sdPlugin\\sdplugin-stocks github.com/shayne/stock-ticker-stream-deck-plugin/cmd/stock_ticker_stream_deck_plugin
@xcopy com.shayne.stocks.sdPlugin $(APPDATA)\\Elgato\\StreamDeck\\Plugins\\com.shayne.stocks.sdPlugin\\ /E /Q /Y

release:
-@rm release\com.exension.stocks.streamDeckPlugin
@DistributionTool.exe com.exension.stocks.sdPlugin release
-@rm release\com.shayne.stocks.streamDeckPlugin
@DistributionTool.exe com.shayne.stocks.sdPlugin release

.PHONY: release
87 changes: 62 additions & 25 deletions cmd/stock_ticker_stream_deck_plugin/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,64 @@ import (

const apiURL = "https://query1.finance.yahoo.com/v7/finance/quote?lang=en-US&region=US&corsDomain=finance.yahoo.com&fields=symbol,marketState,regularMarketPrice,regularMarketChange,regularMarketChangePercent,preMarketPrice,preMarketChange,preMarketChangePercent,postMarketPrice,postMarketChange,postMarketChangePercent"

// StockData contains price data for stock symbol
type StockData struct {
Symbol string `json:"symbol"`
Price float64 `json:"regularMarketPrice"`
Change float64 `json:"regularMarketChange"`
ChangePercent float64 `json:"regularMarketChangePercent"`
PostPrice float64 `json:"postMarketPrice"`
PostChange float64 `json:"postMarketChange"`
PostChangePercent float64 `json:"postMarketChangePercent"`
PrePrice float64 `json:"preMarketPrice"`
PreChange float64 `json:"preMarketChange"`
PreChangePercent float64 `json:"preMarketChangePercent"`
MarketState string `json:"marketState"`
// AutoGenerated struct from API response
// https://mholt.github.io/json-to-go/
type AutoGenerated struct {
QuoteResponse QuoteResponse `json:"quoteResponse"`
}

// Result autogenerated
type Result struct {
Language string `json:"language"`
Region string `json:"region"`
QuoteType string `json:"quoteType"`
QuoteSourceName string `json:"quoteSourceName"`
Triggerable bool `json:"triggerable"`
SourceInterval int `json:"sourceInterval"`
ExchangeDataDelayedBy int `json:"exchangeDataDelayedBy"`
FirstTradeDateMilliseconds int64 `json:"firstTradeDateMilliseconds"`
PriceHint int `json:"priceHint"`
PreMarketChangePercent float64 `json:"preMarketChangePercent"`
PreMarketTime int `json:"preMarketTime"`
PreMarketPrice float64 `json:"preMarketPrice"`
PreMarketChange float64 `json:"preMarketChange"`
PostMarketChangePercent float64 `json:"postMarketChangePercent"`
PostMarketTime int `json:"postMarketTime"`
PostMarketPrice float64 `json:"postMarketPrice"`
PostMarketChange float64 `json:"postMarketChange"`
RegularMarketChange float64 `json:"regularMarketChange"`
RegularMarketChangePercent float64 `json:"regularMarketChangePercent"`
RegularMarketTime int `json:"regularMarketTime"`
RegularMarketPrice float64 `json:"regularMarketPrice"`
RegularMarketPreviousClose float64 `json:"regularMarketPreviousClose"`
FullExchangeName string `json:"fullExchangeName"`
ExchangeTimezoneName string `json:"exchangeTimezoneName"`
ExchangeTimezoneShortName string `json:"exchangeTimezoneShortName"`
GmtOffSetMilliseconds int `json:"gmtOffSetMilliseconds"`
Market string `json:"market"`
EsgPopulated bool `json:"esgPopulated"`
MarketState string `json:"marketState"`
Exchange string `json:"exchange"`
Tradeable bool `json:"tradeable"`
Symbol string `json:"symbol"`
}

// QuoteResponse autogenerated
type QuoteResponse struct {
Result []Result `json:"result"`
Error interface{} `json:"error"`
}

type apiResult struct {
Result []StockData `json:"result"`
Result []Result `json:"result"`
}

// CallAPI makes HTTP request for stock data
func CallAPI(symbols []string) map[string]StockData {
func CallAPI(symbols []string) map[string]Result {
if len(symbols) == 0 {
return nil
}

client := &http.Client{}

url := apiURL + "&symbols=" + strings.Join(symbols, ",")
Expand All @@ -41,12 +78,17 @@ func CallAPI(symbols []string) map[string]StockData {
log.Fatalln(err)
}

req.Header.Set("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
req.Header.Set("accept-encoding", "gzip, deflate, br")
req.Header.Set("accept-language", "en-US,en;q=0.9")
req.Header.Set("cache-control", "max-age=0")
req.Header.Set("dnt", "1")
req.Header.Set("sec-fetch-dest", "document")
req.Header.Set("sec-fetch-mode", "navigate")
req.Header.Set("sec-fetch-site", "none")
req.Header.Set("sec-fetch-user", "?1")
req.Header.Set("upgrade-insecure-requests", "1")
req.Header.Set("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.90 Safari/537.36")
req.Header.Set("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36 Edg/88.0.705.81")

resp, err := client.Do(req)
if err != nil {
Expand All @@ -60,19 +102,14 @@ func CallAPI(symbols []string) map[string]StockData {
log.Fatalln(err)
}

var objmap map[string]*json.RawMessage
err = json.Unmarshal(body, &objmap)
var autogen AutoGenerated
err = json.Unmarshal(body, &autogen)
if err != nil {
log.Fatal("body unmarshal", err)
}
var results apiResult
err = json.Unmarshal(*objmap["quoteResponse"], &results)
if err != nil {
log.Fatal("results unmarshal", err)
}

ret := make(map[string]StockData)
for _, v := range results.Result {
ret := make(map[string]Result)
for _, v := range autogen.QuoteResponse.Result {
ret[v.Symbol] = v
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/stock_ticker_stream_deck_plugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ func main() {
var logpath string
if runtime.GOOS == "windows" {
appdata = os.Getenv("APPDATA")
logpath = filepath.Join(appdata, "Elgato/StreamDeck/Plugins/com.exension.stocks.sdPlugin/stocks.log")
logpath = filepath.Join(appdata, "Elgato/StreamDeck/Plugins/com.shayne.stocks.sdPlugin/stocks.log")
} else {
appdata = os.Getenv("HOME")
logpath = filepath.Join(appdata, "Library/Application Support/com.elgato.StreamDeck/Plugins/com.exension.stocks.sdPlugin/stocks.log")
logpath = filepath.Join(appdata, "Library/Application Support/com.elgato.StreamDeck/Plugins/com.shayne.stocks.sdPlugin/stocks.log")
}
f, err := os.OpenFile(logpath, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
Expand Down
39 changes: 20 additions & 19 deletions cmd/stock_ticker_stream_deck_plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"strings"
"time"

"github.com/exension/go-streamdeck-sdk"
"github.com/gorilla/websocket"
"github.com/shayne/go-streamdeck-sdk"
)

type tile struct {
Expand Down Expand Up @@ -38,32 +38,32 @@ func newPlugin(port, uuid, event, info string) *plugin {
return p
}

func (p *plugin) renderTile(t *tile, data StockData) *[]byte {
func (p *plugin) renderTile(t *tile, data Result) *[]byte {
var price, change, changePercent float64
var status string
statusColor := orange // regular/pre
switch data.MarketState {
case "REGULAR":
status = ""
price = data.Price
change = data.Change
changePercent = data.ChangePercent
price = data.RegularMarketPrice
change = data.RegularMarketChange
changePercent = data.RegularMarketChangePercent
case "POST", "POSTPOST", "PREPRE", "CLOSED":
statusColor = blue
status = ""
price = data.PostPrice
change = data.PostChange
changePercent = data.PostChangePercent
price = data.PostMarketPrice
change = data.PostMarketChange
changePercent = data.PostMarketChangePercent
case "PRE":
status = ""
if data.PrePrice > 0 {
price = data.PrePrice
change = data.PreChange
if data.PreMarketPrice > 0 {
price = data.PreMarketPrice
change = data.PreMarketChange
} else {
price = data.PostPrice
change = data.PostChange
price = data.PostMarketPrice
change = data.PostMarketChange
}
changePercent = data.PreChangePercent
changePercent = data.PreMarketChangePercent
}
arrow := ""
arrowColor := red
Expand All @@ -88,6 +88,9 @@ func (p *plugin) updateTiles(tiles []*tile) {
}
}
stocks := CallAPI(symbols)
if stocks == nil {
return
}
for _, t := range tiles {
b := p.renderTile(t, stocks[t.symbol])
err := p.sd.SetImage(t.context, *b)
Expand All @@ -98,15 +101,13 @@ func (p *plugin) updateTiles(tiles []*tile) {
}

func (p *plugin) startUpdateLoop() {
tick := time.Tick(5 * time.Minute)
tick := time.Tick(10 * time.Second)
for {
select {
case <-tick:
tiles := make([]*tile, 0, len(p.tiles))
i := 0
var tiles []*tile
for _, t := range p.tiles {
tiles[i] = t
i++
tiles = append(tiles, t)
}
p.updateTiles(tiles)
}
Expand Down
Binary file removed com.exension.stocks.sdPlugin/sdplugin-stocks
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions com.shayne.stocks.sdPlugin/debug.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dlv.exe exec --headless --listen=:2345 --log --api-version=2 sdplugin-stocks.exe -- %*
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
39 changes: 39 additions & 0 deletions com.shayne.stocks.sdPlugin/manifest copy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"Actions": [
{
"Icon": "icon",
"Name": "Stocks",
"States": [
{
"Image": "defaultImage",
"TitleAlignment": "top",
"FontSize": "9",
"TitleColor": "#b7b7b7",
"ShowTitle": false
}
],
"SupportedInMultiActions": false,
"Tooltip": "Display auto-updating stock ticker prices.",
"UUID": "com.shayne.stocks.symbol"
}
],
"Author": "shayne",
"CodePathWin": "sdplugin-stocks.exe",
"CodePathMac": "sdplugin-stocks",
"PropertyInspectorPath": "index_pi.html",
"Description": "Display auto-updating stock ticker prices.",
"Name": "Stocks",
"Icon": "pluginIcon",
"URL": "https://github.com/shayne/stock-ticker-stream-deck-plugin",
"Version": "1.0.0",
"OS": [
{
"Platform": "windows",
"MinimumVersion": "10"
},
{
"Platform": "mac",
"MinimumVersion": "10.11"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
],
"SupportedInMultiActions": false,
"Tooltip": "Display auto-updating stock ticker prices.",
"UUID": "com.exension.stocks.symbol"
"UUID": "com.shayne.stocks.symbol"
}
],
"Author": "exension",
"Author": "shayne",
"CodePathWin": "sdplugin-stocks.exe",
"CodePathMac": "sdplugin-stocks",
"PropertyInspectorPath": "index_pi.html",
"Description": "Display auto-updating stock ticker prices.",
"Name": "Stocks",
"Icon": "pluginIcon",
"URL": "https://github.com/exension/stock-ticker-stream-deck-plugin",
"URL": "https://github.com/shayne/stock-ticker-stream-deck-plugin",
"Version": "1.0.0",
"OS": [
{
Expand Down
File renamed without changes
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module github.com/exension/stock-ticker-stream-deck-plugin
module github.com/shayne/stock-ticker-stream-deck-plugin

go 1.12
go 1.16

require (
github.com/exension/go-streamdeck-sdk v0.0.0-20190626182848-2c0344c59b5b
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
github.com/gorilla/websocket v1.4.0
golang.org/x/image v0.0.0-20181116024801-cd38e8056d9b
github.com/gorilla/websocket v1.4.2
github.com/shayne/go-streamdeck-sdk v0.0.0-20210227145409-6f2347bf9170
golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb
)
13 changes: 8 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
github.com/exension/go-streamdeck-sdk v0.0.0-20190626182848-2c0344c59b5b h1:/TgPsTvXOjPXycmJRLmq4ZOFW4R+KbQpQLDPVCCDmSY=
github.com/exension/go-streamdeck-sdk v0.0.0-20190626182848-2c0344c59b5b/go.mod h1:t2Zt30LYKUTTZwA7bmhXlpi4rgNVwrLLtlXBcMGrfq4=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
golang.org/x/image v0.0.0-20181116024801-cd38e8056d9b h1:VHyIDlv3XkfCa5/a81uzaoDkHH4rr81Z62g+xlnO8uM=
golang.org/x/image v0.0.0-20181116024801-cd38e8056d9b/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/shayne/go-streamdeck-sdk v0.0.0-20210227145409-6f2347bf9170 h1:nI5RDk6ZTzpqmMx7YxOAj/43clthruPMavXkQvKdo1o=
github.com/shayne/go-streamdeck-sdk v0.0.0-20210227145409-6f2347bf9170/go.mod h1:EHXOXjKBYpNUnaZB7bVoscBAPaeT0sicOhoWzqzBucc=
golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb h1:fqpd0EBDzlHRCjiphRR5Zo/RSWWQlWv34418dnEixWk=
golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

0 comments on commit ea6db6b

Please sign in to comment.