-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
882 lines (679 loc) · 21.6 KB
/
api.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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"math/rand"
"net/http"
"net/url"
"os"
"reflect"
"strings"
"time"
"github.com/Masterminds/squirrel"
"github.com/go-chi/chi/v5"
"github.com/gorilla/websocket"
_ "github.com/lib/pq"
"github.com/rs/cors"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"github.com/danielgtaylor/huma/v2"
"github.com/danielgtaylor/huma/v2/adapters/humachi"
)
type RefreshTokenStore struct {
RefreshToken string
CreatedAt time.Time
}
type Message struct {
Type int
Data []byte
}
type TwitchResponse struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
}
const secondViewAggregate = "ten_second_sum"
const minuteViewAggregate = "minute_sum"
const hourlyViewAggregate = "hourly_sum"
const dailyViewAggregate = "daily_sum"
const averageDailyViewAggregate = "avg_daily_sum"
const averageHourlyViewAggregate = "avg_hourly_sum"
type LiveStatus struct {
IsLive bool
}
type SpanQuery struct {
Span TimeRange `query:"span" default:"9 hours" enum:"30 minutes,1 hour,9 hours,1 week,1 month,1 year,all"`
}
func (ls *LiveStatus) setLiveStatus(liveStatusUpdate bool, db *gorm.DB) {
if !ls.IsLive && liveStatusUpdate {
// pog!
ls.IsLive = true
return
}
if ls.IsLive && !liveStatusUpdate {
// nl has logged off, refresh our aggregates to get the latest stream data
tomorrow := time.Now().AddDate(0, 0, 1).Format("2006-01-02")
yesterday := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
aggregates := []string{secondViewAggregate, minuteViewAggregate, hourlyViewAggregate, dailyViewAggregate, averageDailyViewAggregate, averageHourlyViewAggregate}
for _, agg := range aggregates {
query := fmt.Sprintf("CALL refresh_continuous_aggregate('%s', '%s', '%s')", agg, yesterday, tomorrow)
err := db.Exec(query).Error
if err != nil {
fmt.Println("error refreshing aggregate ", agg)
continue
}
}
ls.IsLive = false
fmt.Println("succesfully refreshed aggregates")
err := refreshTopClipsCache(db)
if err != nil {
fmt.Println("error refreshing top clips store", err)
return
}
}
}
// live scroller for emotes, via websocket client-sidep
// add small peak chart to show the spike
// :)
func main() {
env := GetConfig()
db, err := gorm.Open(postgres.Open(env.DatabaseUrl))
if len(os.Args) > 1 {
switch os.Args[1] {
case "migrate":
randomHue := rand.Float64()
color := hsvToRGB(HSV{
Hue: randomHue * 360,
Saturation: 0.6,
Value: 0.95,
})
fmt.Printf("#%02x%02x%02x", color.Red, color.Green, color.Blue)
return
}
}
if err != nil {
fmt.Println(err)
return
}
liveStatus := &LiveStatus{IsLive: false}
tokenManager := getTokenManager(db)
go doRegularBackup()
go connectToTwitchChat(
tokenManager,
db,
liveStatus,
)
router := chi.NewMux()
router.Use(cors.Default().Handler)
api := humachi.New(router, huma.DefaultConfig("NL chat dashboard API", "1.0.0"))
type ThumbnailInput struct {
ClipID string `query:"clip_id"`
}
huma.Get(api, "/api/thumbnail", func(ctx context.Context, input *ThumbnailInput) (*struct{ Body TwitchClip }, error) {
var clipInDb FetchedClip
fmt.Println("input clip id: ", input)
err := db.Where("clip_id = ?", input.ClipID).First(&clipInDb).Error
if err != nil {
fmt.Println("error getting clip from db", err)
return &struct{ Body TwitchClip }{Body: TwitchClip{}}, err
}
fmt.Println("clip in db: ", clipInDb)
if clipInDb.Thumbnail != "" {
return &struct{ Body TwitchClip }{Body: TwitchClip{ThumbnailURL: clipInDb.Thumbnail}}, nil
}
clip, err := fetchClipData(input.ClipID, *tokenManager)
if err != nil {
fmt.Println("error updating clip thumbnail", err)
return &struct{ Body TwitchClip }{Body: TwitchClip{}}, err
}
db.Model(&clipInDb).Select("thumbnail").Updates(map[string]interface{}{"thumbnail": clip.ThumbnailURL})
return &struct{ Body TwitchClip }{Body: *clip}, nil
})
huma.Get(api, "/api/initialize_top_clips", func(ctx context.Context, input *struct{}) (*struct{ Body bool }, error) {
err := initializeTopClips(db)
if err != nil {
fmt.Println("error initializing top clips", err)
return &struct{ Body bool }{Body: false}, err
}
return &struct{ Body bool }{Body: true}, nil
})
huma.Get(api, "/api/hero_all_time_clips", func(ctx context.Context, input *SpanQuery) (*AllTimeClipsOutput, error) {
return heroTopClips(input, db)
})
huma.Put(api, "/api/refresh_top_clips_store", func(ctx context.Context, input *struct{}) (*struct{ Body bool }, error) {
err := refreshTopClipsCache(db)
if err != nil {
fmt.Println("error refreshing top clips store", err)
return &struct{ Body bool }{Body: false}, err
}
return &struct{ Body bool }{Body: true}, nil
})
huma.Get(api, "/api/clip_counts", func(ctx context.Context, input *ClipCountsInput) (*ClipCountsOutput, error) {
return selectClipsFromEmotePeaks(*input, db)
})
huma.Get(api, "/api/series", func(ctx context.Context, input *SeriesInputForEmotes) (*TimeSeriesOutput, error) {
fmt.Println(input.EmoteIDs)
return selectSeries(*input, db)
})
huma.Get(api, "/api/series_greatest", func(ctx context.Context, input *SeriesInput) (*TimeSeriesOutput, error) {
return selectSeriesForGreatest(*input, db)
})
huma.Get(api, "/api/latest_series", func(ctx context.Context, input *SeriesInputForEmotes) (*TimeSeriesOutput, error) {
return selectLatestSeries(*input, db)
})
huma.Get(api, "/api/latest_greatest_series", func(ctx context.Context, input *SeriesInputForEmotes) (*TimeSeriesOutput, error) {
return selectLatestGreatestTimeSeries(*input, db)
})
huma.Get(api, "/api/latest_trendiest_series", func(ctx context.Context, input *SeriesInputForEmotes) (*TimeSeriesOutput, error) {
return selectLatestTrendiestTimeSeries(*input, db)
})
huma.Get(api, "/api/clip", func(ctx context.Context, input *NearestClipInput) (*NearestClipOutput, error) {
return selectNearestClip(*input, db)
})
huma.Get(api, "/api/is_live", func(ctx context.Context, input *struct{}) (*struct{ Body bool }, error) {
return &struct{ Body bool }{liveStatus.IsLive}, nil
})
huma.Get(api, "/api/emote_growth", func(ctx context.Context, input *EmotePerformanceInput) (*TopPerformingEmotesOutput, error) {
return selectPercentGrowthDay(*input, db)
})
huma.Get(api, "/api/latest_emote_growth", func(ctx context.Context, input *LatestEmotePerformanceInput) (*LatestEmotePerformanceOutput, error) {
return selectLatestPercentGrowth(*input, db)
})
huma.Get(api, "/api/emote_sums", func(ctx context.Context, input *EmoteSumInput) (*EmoteSumOutput, error) {
return selectSums(db, *input)
})
huma.Get(api, "/api/latest_emote_sums", func(ctx context.Context, input *LatestEmoteSumInput) (*EmoteSumOutput, error) {
return selectLatestSums(*input, db)
})
type PreviousStreamDateInput struct {
From time.Time `query:"from"`
}
// todo: eventually we should just include all data for a date in a single query
// reduce some round trips
huma.Get(api, "/api/previous_stream_date", func(ctx context.Context, input *PreviousStreamDateInput) (*struct{ Body time.Time }, error) {
psql := statementBuilder()
var query squirrel.SelectBuilder
if input.From.IsZero() {
query = psql.
Select("DATE(MAX(created_at))").
From("emote_counts").
Where(psql.
Select("DATE(max(created_at))").
From("emote_counts").
Prefix("created_at < (").
Suffix(")"))
} else {
query = psql.
Select("DATE(max(created_at))").
From("emote_counts").
Where(squirrel.Lt{"created_at": input.From})
}
var date time.Time
queryString, args, _ := query.ToSql()
err := db.Raw(queryString, args...).Scan(&date).Error
if err != nil {
return nil, err
}
return &struct{ Body time.Time }{Body: date}, nil
})
huma.Get(api, "/api/next_stream_date", func(ctx context.Context, input *PreviousStreamDateInput) (*struct{ Body time.Time }, error) {
if input.From.IsZero() {
return nil, fmt.Errorf("from date is required")
}
query := statementBuilder().
Select("DATE(min(created_at))").
From("emote_counts").
Where(squirrel.Gt{"created_at": input.From.Add(time.Hour * 24)})
var date time.Time
queryString, args, _ := query.ToSql()
err := db.Raw(queryString, args...).Scan(&date).Error
if err != nil {
return nil, err
}
return &struct{ Body time.Time }{Body: date}, nil
})
type EmoteOutput struct {
Body []Emote
}
huma.Get(api, "/api/emotes", func(ctx context.Context, input *struct{}) (*EmoteOutput, error) {
trackedEmotes, err := getEmotesInDB(db)
emotes := make([]Emote, 0, len(trackedEmotes))
for _, emote := range trackedEmotes {
emotes = append(emotes, emote)
}
if err != nil {
fmt.Println("Error getting emotes:", err)
return nil, err
}
return &EmoteOutput{Body: emotes}, nil
})
port := fmt.Sprintf(":%s", os.Getenv("PORT"))
fmt.Println("Listening on port", port)
listenError := http.ListenAndServe(port, router)
if listenError != nil {
fmt.Println(listenError)
}
}
func getChatCountEmotes() (map[string]bool, []string) {
val := reflect.ValueOf(ChatCounts{})
validColumnSet := make(map[string]bool, val.NumField())
emotes := make([]string, 0, val.NumField())
for i := 0; i < val.NumField(); i++ {
jsonTag := val.Type().Field(i).Tag.Get("json")
if jsonTag != "-" && jsonTag != "time" {
validColumnSet[jsonTag] = true
emotes = append(emotes, jsonTag)
}
}
return validColumnSet, emotes
}
func getTokenManager(db *gorm.DB) *TokenManager {
tokens, err := tryUseLatestRefreshToken(db)
if err != nil {
tokens, err = getTwitchWithAuthCode(db)
if err != nil {
panic("Error getting Twitch token!")
}
}
return &TokenManager{AccessToken: tokens.AccessToken, _refreshToken: tokens.RefreshToken}
}
type TokenManager struct {
AccessToken string
_refreshToken string
}
func (t *TokenManager) RefreshToken(db *gorm.DB) error {
response, err := refreshTwitchToken(db, t._refreshToken)
if err != nil {
fmt.Println("Error refreshing Twitch token:", err)
return err
}
t._refreshToken = response.RefreshToken
t.AccessToken = response.AccessToken
return nil
}
func connectToTwitchChat(tokenManager *TokenManager, db *gorm.DB, liveStatus *LiveStatus) {
env := GetConfig()
for {
ctx, cancel := context.WithCancel(context.Background())
conn, _, err := websocket.DefaultDialer.Dial("ws://irc-ws.chat.twitch.tv:80", nil)
if err != nil {
fmt.Println("Error connecting to Twitch IRC:", err)
cancel()
return
}
defer conn.Close()
conn.WriteMessage(websocket.TextMessage, []byte(fmt.Sprintf("PASS oauth:%s", tokenManager.AccessToken)))
conn.WriteMessage(websocket.TextMessage, []byte(fmt.Sprintf("NICK %s", env.Nickname)))
conn.WriteMessage(websocket.TextMessage, []byte("JOIN #northernlion"))
incomingMessages := make(chan Message)
go readChatMessages(conn, incomingMessages, cancel)
initEmotesToTrack, err := getTrackingEmotes(db)
if err != nil {
fmt.Println("Error getting initial emotes:", err)
return
}
latestEmotes := make(chan map[int]Emote)
go syncTrackingEmotes(db, latestEmotes, ctx)
go countEmotes(ctx, incomingMessages, db, initEmotesToTrack, tokenManager, liveStatus, latestEmotes)
<-ctx.Done()
cancel()
time.Sleep(5 * time.Second)
fmt.Println("Reconnecting to Twitch chat")
}
}
func getTrackingEmotes(db *gorm.DB) (map[int]Emote, error) {
emotes, err := getEmotesInDB(db)
if err != nil {
return nil, err
}
// delete the combined columns
for id, emote := range emotes {
if emote.Code == lul_kekw_icant || emote.Code == pog_pogcrazy_letsgo {
delete(emotes, id)
}
}
return emotes, nil
}
func getEmotesInDB(db *gorm.DB) (map[int]Emote, error) {
var emotes []Emote
err := db.Find(&emotes).Error
idToEmote := make(map[int]Emote)
for _, emote := range emotes {
idToEmote[int(emote.ID)] = emote
}
return idToEmote, err
}
func syncTrackingEmotes(db *gorm.DB, trackingEmotesOut chan<- map[int]Emote, ctx context.Context) {
refreshTimer := time.NewTicker(20 * time.Second)
defer refreshTimer.Stop()
for {
select {
case <-ctx.Done():
close(trackingEmotesOut)
return
case <-refreshTimer.C:
bttvEmotes, err := fetchNlEmotesFromBTTV()
if err != nil {
fmt.Println("Error getting latest BTTV emotes:", err)
continue
}
emotes, err := getTrackingEmotes(db)
if err != nil {
fmt.Println("Error getting current emotes in db:", err)
continue
}
currentTrackedCodes := make(map[string]bool)
for _, emote := range emotes {
currentTrackedCodes[emote.Code] = true
}
for _, bttvEmote := range bttvEmotes.Emotes {
if _, ok := currentTrackedCodes[bttvEmote.Code]; !ok {
fmt.Println("inserting new emote", bttvEmote.Code)
randomHue := rand.Float64()
color := hsvToRGB(HSV{
Hue: randomHue * 360,
Saturation: 0.6,
Value: 0.95,
})
newEmote := Emote{
Code: bttvEmote.Code,
Url: fmt.Sprintf("https://cdn.betterttv.net/emote/%s/2x.webp", bttvEmote.ID),
HexColor: fmt.Sprintf("#%02x%02x%02x", color.Red, color.Green, color.Blue),
}
err := db.Create(&newEmote).Error
if err != nil {
fmt.Println("Error creating new emote:", err)
continue
}
}
}
updatedEmotes, err := getTrackingEmotes(db)
if err != nil {
fmt.Println("Error getting updated db emotes:", err)
continue
}
trackingEmotesOut <- updatedEmotes
}
}
}
func readChatMessages(conn *websocket.Conn, incomingMessages chan Message, cancel context.CancelFunc) {
for {
messageType, messageData, err := conn.ReadMessage()
text := string(messageData)
if strings.Contains(text, "PING") {
conn.WriteMessage(websocket.TextMessage, []byte("PONG :tmi.twitch.tv"))
continue
}
if err != nil {
fmt.Println(text)
fmt.Println("Error reading message:", err)
cancel()
return
}
incomingMessages <- Message{Type: messageType, Data: messageData}
}
}
func countEmotes(
ctx context.Context,
message chan Message,
db *gorm.DB,
trackingEmotes map[int]Emote,
tokenManager *TokenManager,
liveStatus *LiveStatus,
emoteUpdates <-chan map[int]Emote,
) {
env := GetConfig()
postInterval := time.NewTicker(10 * time.Second)
defer postInterval.Stop()
var counter map[int]float64
resetCounter := func() {
counter = make(map[int]float64)
for _, emote := range trackingEmotes {
counter[int(emote.ID)] = 0
}
}
resetCounter()
for {
select {
case msg := <-message:
message := string(msg.Data)
messageText := splitAndGetLast(message, "#northernlion")
for _, emote := range trackingEmotes {
if emote.Code == "two" {
continue
}
if strings.Contains(messageText, emote.Code) {
if env.Debug {
fmt.Println("found emote", emote.Code)
}
counter[int(emote.ID)] += 1
}
}
twoEmoteId := -1
for _, emote := range trackingEmotes {
if emote.Code == "two" {
twoEmoteId = int(emote.ID)
break
}
}
if twoEmoteId == -1 {
panic("two code not found in trackingEmotes. Need to make this more generic at some point.")
}
_, ok := counter[twoEmoteId]
if !ok {
panic("two code not found in counter; it should have been initialized. ")
}
if contains_plus := strings.Contains(messageText, "+"); contains_plus {
counter[twoEmoteId] += parseVal(splitAndGetLast(messageText, "+"))
} else if contains_minus := strings.Contains(messageText, "-"); contains_minus {
counter[twoEmoteId] -= parseVal(splitAndGetLast(messageText, "-"))
}
case <-postInterval.C:
counts := make([]EmoteCount, 0, len(counter))
for emoteId, count := range counter {
emote := trackingEmotes[emoteId]
counts = append(counts, EmoteCount{
Count: int(count),
Emote: emote,
})
}
resetCounter()
go persistCountsIfLive(db, counts, tokenManager, liveStatus)
case refreshedEmotes := <-emoteUpdates:
for _, emote := range refreshedEmotes {
if _, ok := trackingEmotes[int(emote.ID)]; !ok {
fmt.Println("new emote", emote.Code)
}
}
trackingEmotes = refreshedEmotes
case <-ctx.Done():
return
}
}
}
func persistCountsIfLive(
db *gorm.DB,
counts []EmoteCount,
tokenManager *TokenManager,
liveStatus *LiveStatus,
retries ...int) {
env := GetConfig()
if env.Debug {
for _, row := range counts {
if row.Count != 0 {
fmt.Println("inserting", row.Emote.Code, row.Count)
}
}
}
clipResult := makeClip(tokenManager.AccessToken)
if clipResult.clipID != "" {
liveStatus.setLiveStatus(true, db)
countsWithClipIDs := make([]EmoteCount, 0, len(counts))
for _, count := range counts {
countsWithClipIDs = append(
countsWithClipIDs,
EmoteCount{
Count: count.Count,
Emote: count.Emote,
ClipID: clipResult.clipID,
})
}
err := db.Create(&FetchedClip{ClipID: clipResult.clipID}).Error
if err != nil {
fmt.Println("Error creating clip: ", clipResult.error)
liveStatus.setLiveStatus(false, db)
}
err = db.Create(&countsWithClipIDs).Error
if err != nil {
fmt.Println("Error inserting into db:", err)
}
if env.Debug {
fmt.Println("successfully inserted clip")
}
} else if clipResult.error == "unauthorized" {
fmt.Println("Unauthorized, refreshing token")
tokenManager.RefreshToken(db)
if len(retries) == 0 {
persistCountsIfLive(db, counts, tokenManager, liveStatus, 1)
}
} else {
fmt.Println("Error creating clip: ", clipResult.error)
liveStatus.setLiveStatus(false, db)
}
}
func splitAndGetLast(text string, splitter string) string {
split_text := strings.Split(text, splitter)
last := len(split_text) - 1
return split_text[last]
}
func parseVal(text string) float64 {
if strings.Contains(text, "2") {
return 2
}
return 0
}
type ClipResponse struct {
Data []struct {
Id string `json:"id"`
} `json:"data"`
}
type CreateClipResponse struct {
error string
clipID string
}
type TwitchCipResponse struct {
Data []TwitchClip `json:"data"`
}
type TwitchClip struct {
ID string `json:"id"`
BroadcasterID string `json:"broadcaster_id"`
CreatedAt string `json:"created_at"`
Duration float64 `json:"duration"`
ThumbnailURL string `json:"thumbnail_url"`
VideoID string `json:"video_id"`
}
func makeClip(authToken string) CreateClipResponse {
env := GetConfig()
requestBody := map[string]string{
"broadcaster_id": "14371185",
"has_delay": "false",
"duration": "90",
}
requestBodyBytes, err := json.Marshal(requestBody)
if err != nil {
return CreateClipResponse{error: "Error marshaling JSON"}
}
req, err := http.NewRequest("POST", "https://api.twitch.tv/helix/clips", bytes.NewBuffer(requestBodyBytes))
if err != nil {
return CreateClipResponse{error: "error creating request"}
}
auth := splitAndGetLast(authToken, ":")
bearer := fmt.Sprintf("Bearer %s", auth)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Client-Id", env.ClientId)
req.Header.Set("Authorization", bearer)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return CreateClipResponse{error: "error making request"}
}
defer resp.Body.Close()
if resp.StatusCode == 401 {
return CreateClipResponse{error: "unauthorized"}
}
if resp.StatusCode == 404 {
return CreateClipResponse{error: "not found"}
}
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
return CreateClipResponse{error: "error reading response body"}
}
var responseObject ClipResponse
err = json.Unmarshal(responseBody, &responseObject)
if err != nil {
return CreateClipResponse{error: "error unmarshaling response body"}
}
if len(responseObject.Data) > 0 {
return CreateClipResponse{clipID: responseObject.Data[0].Id}
}
return CreateClipResponse{error: "no clip found"}
}
func getTwitchWithAuthCode(db *gorm.DB) (TwitchResponse, error) {
env := GetConfig()
fmt.Println("Authorizing Twitch with client code")
data := url.Values{}
data.Set("client_id", env.ClientId)
data.Set("client_secret", env.ClientSecret)
data.Set("code", os.Getenv("CLIENT_CODE"))
data.Set("redirect_uri", "http://localhost:3000")
data.Set("grant_type", "authorization_code")
return getTwitchAuthResponse(data, db)
}
func refreshTwitchToken(db *gorm.DB, refreshToken string) (TwitchResponse, error) {
env := GetConfig()
fmt.Println("Refreshing Twitch auth with refresh token")
data := url.Values{}
data.Set("client_id", env.ClientId)
data.Set("client_secret", env.ClientSecret)
data.Set("grant_type", "refresh_token")
data.Set("refresh_token", refreshToken)
return getTwitchAuthResponse(data, db)
}
func getTwitchAuthResponse(data url.Values, db *gorm.DB) (TwitchResponse, error) {
req, err := http.NewRequest("POST", "https://id.twitch.tv/oauth2/token", strings.NewReader(data.Encode()))
if err != nil {
return TwitchResponse{}, err
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return TwitchResponse{}, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return TwitchResponse{}, err
}
var twitchResponse TwitchResponse
err = json.Unmarshal(body, &twitchResponse)
if err != nil {
fmt.Println(err)
return TwitchResponse{}, err
}
if twitchResponse.AccessToken == "" || twitchResponse.RefreshToken == "" {
fmt.Println("Tokens are empty. Body was:", string(body))
return TwitchResponse{}, fmt.Errorf("tokens are empty")
}
db.Create(&RefreshTokenStore{RefreshToken: twitchResponse.RefreshToken, CreatedAt: time.Now()})
return twitchResponse, nil
}
func tryUseLatestRefreshToken(db *gorm.DB) (TwitchResponse, error) {
var refreshTokenStore RefreshTokenStore
db.Order("created_at desc").First(&refreshTokenStore)
if refreshTokenStore.RefreshToken != "" {
return refreshTwitchToken(db, refreshTokenStore.RefreshToken)
}
return TwitchResponse{}, fmt.Errorf("no refresh token found")
}