-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
456 lines (400 loc) · 11.6 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
package main
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"log"
"math"
"math/rand"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
)
var isRunning = false
var isPaused = false
var appCode = ""
var hopper = 0
var costPerShot = 1.00
var minTimePerShotInSec = 30
var maxTimePerShotInSec = 300
func logMessage(message string) {
currentTime := time.Now()
fmt.Println("{" + currentTime.Format(time.RFC1123) + "} " + message)
}
func logError(err error) {
currentTime := time.Now()
log.Fatalln("{" + currentTime.Format(time.RFC1123) + "} " + err.Error())
log.Fatalln(err)
}
func logErrorMessage(err string) {
currentTime := time.Now()
fmt.Println("{" + currentTime.Format(time.RFC1123) + "} " + err)
}
func getLifeLeftOfAccessToken(accessToken *AccessToken) time.Duration {
return (time.Duration(accessToken.LifeTime) * time.Second) - time.Since(accessToken.TimeBorn)
}
func handleFireing() {
for {
if !isRunning {
break
}
if hopper > 0 {
fire()
hopper--
}
time.Sleep(time.Duration(randomValue(minTimePerShotInSec, maxTimePerShotInSec)) * time.Second)
}
}
func listenAndHandleDonations(accessToken *AccessToken) {
lastDonationID := getLastDonationID(accessToken)
remainder := 0.00
for {
if !isRunning {
break
}
refreshAccessToken(accessToken)
logMessage("checking for donations")
donations := getStreamlabsDonations(accessToken, nil, lastDonationID)
var donation Donation
for _, donation = range *donations {
fmt.Printf("%+v\n", donation)
//This should give us 2 parts full balls and a part of a ball from the user
fullShots, partalShot := math.Modf(donation.Amount / costPerShot)
hopper += int(fullShots)
remainder += partalShot
if remainder > 1 {
remainder--
hopper++
}
logMessage("Hopper: " + strconv.Itoa(hopper) + ", Remainder: " + fmt.Sprintf("%f", remainder))
}
if len(*donations) != 0 {
*lastDonationID = donation.DonationID //TODO: This shouldent be in the loop
}
time.Sleep(5 * time.Second)
}
}
func getStreamlabsDonations(accessToken *AccessToken, numDonations *int, afterDonationID *int) *[]Donation {
urlParams := url.Values{}
urlParams.Add("access_token", accessToken.Val)
urlParams.Add("currency", "USD")
if numDonations != nil {
urlParams.Add("limit", strconv.Itoa(*numDonations))
}
if afterDonationID != nil {
urlParams.Add("after", strconv.Itoa(*afterDonationID))
}
finalURL := "https://streamlabs.com/api/v1.0/donations?" + urlParams.Encode()
logMessage("request URL: " + finalURL)
resp, err := http.Get(finalURL)
if err != nil {
logError(err)
return nil
}
if resp.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(resp.Body)
logErrorMessage("bad response getting access_token: " + string(body))
return nil
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
logError(err)
return nil
}
var rawDonations DonationData
err = json.Unmarshal(data, &rawDonations)
if err != nil {
logError(err)
return nil
}
return &rawDonations.Data
}
func getLastDonationID(accessToken *AccessToken) *int {
if !isRunning {
return nil
}
numDonations := 1
donations := getStreamlabsDonations(accessToken, &numDonations, nil)
if len((*donations)) != 1 {
return nil
}
fmt.Printf("Last Donation:\n%+v\n", &(*donations)[0])
return &(*donations)[0].DonationID
}
//Compile templates on start
var templates = template.Must(template.ParseFiles(
"templates/notFound.html",
"templates/header.html",
"templates/footer.html",
"templates/index.html"))
//GrantType is the type of the request, refresh or get a new token
type GrantType string
const (
//RefreshToken used to refresh the token
RefreshToken GrantType = "refresh_token"
//AuthorizationCode used to get a new code
AuthorizationCode GrantType = "authorization_code"
)
func makeAccesTokenRequest(accessToken *AccessToken, grantType GrantType) {
app := getStreamlabsAppInfo()
logMessage("making request for access_code using grant_type " + string(grantType))
var grantTypeLabel string
var grantTypeVal string
if grantType == RefreshToken {
grantTypeLabel = "refresh_token"
grantTypeVal = accessToken.RefreshToken
} else if grantType == AuthorizationCode {
grantTypeLabel = "code"
grantTypeVal = appCode
}
message := map[string]interface{}{
"grant_type": grantType,
"client_id": app.ClientID,
"client_secret": app.ClientSecret,
"redirect_uri": app.RedirectURI,
grantTypeLabel: grantTypeVal,
}
bytesRepresentation, err := json.Marshal(message)
if err != nil {
logErrorMessage("error marshaling request body: " + err.Error())
}
resp, err := http.Post("https://streamlabs.com/api/v1.0/token",
"application/json", bytes.NewBuffer(bytesRepresentation))
if err != nil {
logError(err)
accessToken = nil
return
}
if resp.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(resp.Body)
logErrorMessage("bad response getting access_token: " + string(body))
accessToken = nil
return
}
accessToken.TimeBorn = time.Now()
err = json.NewDecoder(resp.Body).Decode(&accessToken)
if err != nil {
logErrorMessage("error decoding response for access token: " + err.Error())
accessToken = nil
return
}
}
func refreshAccessToken(accessToken *AccessToken) {
makeAccesTokenRequest(accessToken, RefreshToken)
}
func getAccessToken() *AccessToken {
var accessToken AccessToken
makeAccesTokenRequest(&accessToken, AuthorizationCode)
return &accessToken
}
func fire() {
logMessage("FIRE!!!")
signalToFire()
}
func saveToken(token string) {
logMessage("saving token: " + token)
}
//Display the named template
func display(w http.ResponseWriter, tmpl string, data interface{}) {
templates.ExecuteTemplate(w, tmpl, data)
}
func homeHandler(w http.ResponseWriter, r *http.Request) {
data := Page{
PageTitle: "Home",
}
display(w, "index", data)
}
func liveHandler(w http.ResponseWriter, r *http.Request) {
appCode = r.URL.Query().Get("code")
data := Page{
PageTitle: "Live",
}
if !isRunning && appCode != "" {
accessToken := getAccessToken()
if accessToken == nil {
logErrorMessage("error getting the access token")
}
isRunning = true
go listenAndHandleDonations(accessToken)
go handleFireing()
}
display(w, "index", data)
}
func randomPageHandler(w http.ResponseWriter, r *http.Request) {
// If empty show the home page
// If static page show the static package
// Else show the 404 page
if r.URL.Path == "/" {
homeHandler(w, r)
} else if r.URL.Path == "/fire" {
fireHandler(w, r)
} else if r.URL.Path == "/activate" {
activateHandler(w, r)
} else if r.URL.Path == "/live" {
liveHandler(w, r)
} else if r.URL.Path == "/stop" {
stopHandler(w, r)
} else if r.URL.Path == "/api/hopper" {
getHopperSize(w, r)
} else if r.URL.Path == "/api/add" {
addToHopper(w, r)
} else if r.URL.Path == "/api/pause" {
pauseHandler(w, r)
} else if r.URL.Path == "/api/pricepershot" {
pricePerShotHandler(w, r)
} else if r.URL.Path == "/api/isrunning" {
getIsRunning(w, r)
} else if strings.HasSuffix(r.URL.Path[1:], ".html") {
http.ServeFile(w, r, "static/html/"+r.URL.Path[1:])
} else {
logMessage("Sorry but it seems this page does not exist...")
errorHandler(w, r, http.StatusNotFound)
}
}
func fireHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
fire()
}
http.Redirect(w, r, "/", http.StatusSeeOther)
}
func pauseHandler(res http.ResponseWriter, req *http.Request) {
if req.Method == http.MethodPost {
if !isPaused {
if isRunning {
isRunning = false
} else {
isRunning = true
}
} else {
res.WriteHeader(http.StatusInternalServerError)
res.Write([]byte("500 - Not activated yet so cannot pause"))
}
}
}
func getHopperSize(res http.ResponseWriter, req *http.Request) {
json.NewEncoder(res).Encode(hopper)
}
func addToHopper(res http.ResponseWriter, req *http.Request) {
if req.Method == http.MethodPost {
hopper++
}
}
func getIsRunning(res http.ResponseWriter, req *http.Request) {
json.NewEncoder(res).Encode(isRunning)
}
func stopHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
isRunning = false
}
http.Redirect(w, r, "/", http.StatusSeeOther)
}
func pricePerShotHandler(res http.ResponseWriter, req *http.Request) {
if req.Method == "POST" {
req.ParseForm() //Parse url parameters passed, then parse the response packet for the POST body (request body)
// attention: If you do not call ParseForm method, the following data can not be obtained form
fmt.Println(req.Form) // print information on server side.
fmt.Println("path", req.URL.Path)
fmt.Println("scheme", req.URL.Scheme)
fmt.Println(req.Form["url_long"])
for k, v := range req.Form {
fmt.Println("key:", k)
fmt.Println("val:", strings.Join(v, ""))
}
costPerShotStr := (req.Form["pricepershot"])[0]
costPerShot, _ = strconv.ParseFloat(costPerShotStr, 64)
fmt.Println("costPerShotStr:", costPerShotStr)
http.Redirect(res, req, "/", http.StatusSeeOther)
} else if req.Method == "GET" {
json.NewEncoder(res).Encode(costPerShot)
}
}
func getStreamlabsAppInfo() StreamLabsApp {
var app StreamLabsApp
// pwd, _ := os.Getwd()
file, err := os.Open("StreamLabsAPI.json")
if err != nil {
logMessage("ERROR OPENING FILE: " + err.Error())
}
decoder := json.NewDecoder(file)
err = decoder.Decode(&app)
if err != nil {
logMessage("ERROR DECODING JSON: " + err.Error())
}
return app
}
func activateHandler(w http.ResponseWriter, r *http.Request) {
app := getStreamlabsAppInfo()
requestURL, err := url.Parse("https://streamlabs.com/api/v1.0/authorize")
if err != nil {
log.Fatal("error makinking redirection url: " + err.Error())
}
requestQuery := requestURL.Query()
requestQuery.Set("response_type", "code")
requestQuery.Set("client_id", app.ClientID)
requestQuery.Set("redirect_uri", app.RedirectURI)
requestQuery.Set("scope", "donations.read")
requestURL.RawQuery = requestQuery.Encode()
logMessage(requestURL.String())
http.Redirect(w, r, requestURL.String(), http.StatusSeeOther)
}
func errorHandler(w http.ResponseWriter, r *http.Request, status int) {
w.WriteHeader(status)
if status == http.StatusNotFound {
display(w, "404", &Page{PageTitle: "404"})
}
}
// Gets a random value from the low to high values. This will include the low and high values.
func randomValue(low int, high int) int {
var scaledInt = high - low + 1 // The +1 is to offset the values so it can be the high value.
return rand.Intn(scaledInt) + low
}
func getPort() string {
if value, ok := os.LookupEnv("PORT"); ok {
return ":" + value
}
return ":8080"
}
func main() {
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.HandleFunc("/", randomPageHandler)
var port = getPort()
logMessage("Now listening to port " + port)
log.Fatal(http.ListenAndServe(port, nil))
}
//A Page structure
type Page struct {
PageTitle string
}
//StreamLabsApp Object that holds the StreamLabs App info that comes from a JSON
type StreamLabsApp struct {
ClientID string
ClientSecret string
RedirectURI string
}
//AccessToken stores the value and timers
type AccessToken struct {
Val string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
LifeTime int `json:"expires_in"`
TokenType string `json:"token_type"`
TimeBorn time.Time
}
//DonationData object of the raw data returned from StreamLabs when getting donations
type DonationData struct {
Data []Donation `json:"data"`
}
//Donation object returned from StreamLabs
type Donation struct {
DonationID int `json:"donation_id"`
CreatedAt int `json:"created_at"`
Currency string `json:"currency"`
Amount float64 `json:"amount,string"`
Name string `json:"name"`
Message string `json:"message"`
Email string `json:"email"`
}