-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitch_download_vod.go
77 lines (67 loc) · 1.91 KB
/
twitch_download_vod.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
package main
import (
"errors"
"github.com/goldbattle/twitch_vods/algos"
"github.com/goldbattle/twitch_vods/helpers"
"github.com/goldbattle/twitch_vods/models"
"github.com/goldbattle/twitch_vods/twitch"
"github.com/nicklaw5/helix"
"log"
"os"
"sync"
"time"
)
func main() {
// Load the config
if len(os.Args) < 2 {
log.Fatalf("CONFIG: please pass path to config as argument\n")
}
log.Printf("CONFIG: loading %s\n", os.Args[1])
config := helpers.LoadConfigFile(os.Args[1])
// Create the client
client, err := helix.NewClient(&helix.Options{
ClientID: config.TwitchClientId,
ClientSecret: config.TwitchSecretId,
RateLimitFunc: twitch.RateLimitCallback,
})
if err != nil {
log.Fatalf("%v", err)
}
// Initialize methods responsible for refreshing oauth
waitForFirstAppAccessToken := make(chan struct{})
go twitch.InitAppAccessToken(client, waitForFirstAppAccessToken)
<-waitForFirstAppAccessToken
// Ensure we have channels
if len(config.ChannelsVideo) < 1 {
log.Fatalf("CONFIG: please specify at least one chat channel to watch\n")
}
// Get the user ids for this user
var usernameIds []string
for _, username := range config.ChannelsVideo {
user := helix.User{}
err := errors.New("startup")
for err != nil {
user, err = twitch.GetUser(client, username)
if err != nil {
log.Printf("ERROR: %s\n", err)
} else {
log.Printf("CLIENT: user %s -> %s\n", username, user.ID)
}
}
usernameIds = append(usernameIds, user.ID)
}
// Start group
var wg sync.WaitGroup
for i := range usernameIds {
wg.Add(1)
go func(client *helix.Client, username string, usernameId string, config models.ConfigurationFile) {
defer wg.Done()
for true {
algos.DownloadVodLatest(client, username, usernameId, config)
time.Sleep(time.Duration(config.QueryVodsMin) * time.Minute)
}
}(client, config.ChannelsVideo[i], usernameIds[i], config)
}
// Wait for all to complete
wg.Wait()
}