twitchapon

[Twi]tch [Cha]nnel [Po]ints Rewards Redemption Listener
git clone git://bsandro.tech/twitchapon
Log | Files | Refs | README | LICENSE

commit 58b96b9c07a5015ecdbc676efe3c66cb76c3a908
parent b9f1276d665b96339fc27f736a5db3e3cf5114b8
Author: bsandro <brian.drosan@gmail.com>
Date:   Fri, 14 May 2021 02:36:54 +0300

obs-websocket integration has begun (config):
- removed unnecessary fields from config, moved corresponding settings into nested nodes
- config file now includes a section for the obs-websocket
- print sample configuration if config file does not exists

Diffstat:
MTODO | 2+-
Mauth.go | 8+++++++-
Mclient.go | 12+++---------
Mconfig.go | 43++++++++++++++++++++++++++++++++++---------
Mconfig_sample.json | 16+++++++++-------
Muser.go | 4++--
6 files changed, 56 insertions(+), 29 deletions(-)

diff --git a/TODO b/TODO @@ -2,6 +2,7 @@ Brief TODO list of features to implement. Symbols as I use them: "-" is "planned", "?" is "questionable", "+" is "done" - https://github.com/Palakis/obs-websocket compatibility +- put some comments in the code - unmarshal nested json received in messages and forward it properly - cache authorization token till it expires - authorization token renewal process @@ -10,4 +11,3 @@ Symbols as I use them: "-" is "planned", "?" is "questionable", "+" is "done" - global feature: listen to bits and channel subscription events ? open system default browser with an oauth link instead of just printing its - delay between ping messages should be configurable -- put some comments in the code diff --git a/auth.go b/auth.go @@ -19,7 +19,13 @@ type TAuth struct { func (auth *TAuth) Init(auth_code string) error { const auth_request_url string = "https://id.twitch.tv/oauth2/token?client_id=%s&client_secret=%s&grant_type=authorization_code&code=%s&redirect_uri=http://%s" - response, err := http.Post(fmt.Sprintf(auth_request_url, App.Config.ClientId, App.Config.ClientSecret, auth_code, App.Config.LocalServer), "", nil) + response, err := http.Post(fmt.Sprintf( + auth_request_url, + App.Config.Twitch.ClientId, + App.Config.Twitch.ClientSecret, + auth_code, + App.Config.LocalServer), "", nil) + if err != nil { return err } diff --git a/client.go b/client.go @@ -4,7 +4,6 @@ import ( "fmt" "golang.org/x/net/websocket" "log" - "net/http" ) type TMessage struct { @@ -27,11 +26,7 @@ func ListenJob() { if msg.Type != "MESSAGE" { continue } - if resp, err := http.Get(App.Config.AnimServer); err == nil { - resp.Body.Close() - } else { - fmt.Println("Error while making the query to the animation service") - } + fmt.Println("Message received!") } App.WaitGroup.Done() @@ -52,10 +47,9 @@ func main() { log.Fatal(err) } - fmt.Printf(code_request_url, App.Config.ClientId, App.Config.LocalServer) + fmt.Printf(code_request_url, App.Config.Twitch.ClientId, App.Config.LocalServer) - url := fmt.Sprintf("wss://%s:%s/", App.Config.WssServer, App.Config.WssPort) - App.Connection, err = websocket.Dial(url, "", App.Config.WssOrigin) + App.Connection, err = websocket.Dial("wss://pubsub-edge.twitch.tv:443/", "", "http://localhost") if err != nil { log.Fatal(err) } diff --git a/config.go b/config.go @@ -3,17 +3,20 @@ package main import ( "encoding/json" "os" + "fmt" ) type TConfig struct { - LocalServer string `json:"local_server"` - WssServer string `json:"wss_server"` - WssPort string `json:"wss_port"` - WssOrigin string `json:"wss_origin"` - AnimServer string `json:"anim_server"` - UserName string `json:"user_name"` - ClientId string `json:"client_id"` - ClientSecret string `json:"client_secret"` + LocalServer string `json:"local_server"` + Twitch struct { + UserName string `json:"user_name"` + ClientId string `json:"client_id"` + ClientSecret string `json:"client_secret"` + } `json:"twitch"` + ObsWebsocket struct { + Port string `json:"port"` + Password string `json:"password"` + } `json:"obs_websocket"` } func (conf *TConfig) Init() error { @@ -23,6 +26,12 @@ func (conf *TConfig) Init() error { } config_data, err := os.ReadFile(config_name) if err != nil { + if (os.IsNotExist(err)) { + fmt.Println("Config file does not exist! Sample:") + conf.InitSample() + conf.Print() + } + return err } err = json.Unmarshal(config_data, conf) @@ -34,5 +43,21 @@ func (conf *TConfig) Init() error { } func (conf *TConfig) IsInited() bool { - return conf != nil && len(conf.ClientId) > 0 + return conf != nil && len(conf.LocalServer) > 0 +} + +func (conf *TConfig) Print() { + fmt.Println("------------------------8<----------------------") + json, _ := json.MarshalIndent(conf, "", "\t") + fmt.Println(string(json)) + fmt.Println("------------------------>8----------------------") +} + +func (conf *TConfig) InitSample() { + conf.LocalServer = "localhost:8081" + conf.Twitch.UserName = "twitch-username" + conf.Twitch.ClientId = "xxxxxxxxxxxxxxx" + conf.Twitch.ClientSecret = "xxxxxxxxxxx" + conf.ObsWebsocket.Port = "4444" + conf.ObsWebsocket.Password = "xxxxxxxxx" } diff --git a/config_sample.json b/config_sample.json @@ -1,10 +1,12 @@ { "local_server": "localhost:8081", - "wss_server": "pubsub-edge.twitch.tv", - "wss_port": "443", - "wss_origin": "http://localhost:8081", - "anim_server": "http://localhost:2345", - "user_name": "bsandro", - "client_id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx", - "client_secret": "xxxxxxxxxxxxxxxxxxxxxxx" + "twitch": { + "user_name": "bsandro", + "client_id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + "client_secret": "zzzzzzzzzzzzzzzzzzzzzzzzzzzz" + }, + "obs_websocket": { + "port": "4444", + "password": "xxxxxxxx" + } } diff --git a/user.go b/user.go @@ -33,7 +33,7 @@ func getUser(user_name string, user *TUser) error { if err != nil { return err } - request.Header.Add("Client-Id", App.Config.ClientId) + request.Header.Add("Client-Id", App.Config.Twitch.ClientId) request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", App.Auth.AccessToken)) response, err := client.Do(request) if err != nil { @@ -58,7 +58,7 @@ func getUser(user_name string, user *TUser) error { } func (user *TUser) Init() error { - err := getUser(App.Config.UserName, user) + err := getUser(App.Config.Twitch.UserName, user) return err }