-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
218 lines (191 loc) · 4.72 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
package main
import (
"encoding/json"
"flag"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"sync"
"time"
"github.com/nicklaw5/helix"
"github.com/schollz/progressbar/v2"
"github.com/sirupsen/logrus"
)
var (
userName string
userID string
clientID string
outputFolder string
maxWorkers int
)
func init() {
flag.StringVar(&userName, "user", "", "user name you want to download all vods from")
flag.StringVar(&userID, "userid", "", "user id you want to download all vods from")
flag.StringVar(&clientID, "clientid", "", "twitch client id to get vods and user information")
flag.IntVar(&maxWorkers, "workers", 4, "max parallel downloads")
flag.StringVar(&outputFolder, "output", "", "folder where everything gets saved to")
flag.Parse()
}
func main() {
if userName == "" && userID == "" {
logrus.Warn("missing user or user_id flag")
return
}
if userName != "" && userID != "" {
logrus.Warn("only use one, user or user_id")
return
}
h, err := createHelixClient(clientID)
if err != nil {
logrus.WithError(err).Error("failed creating twitch helix client")
return
}
if userName != "" {
user, err := getUserID(h, userName)
if err != nil {
logrus.WithError(err).Error("failed getting user id")
return
}
userID = user.ID
userName = user.DisplayName
}
if userID != "" {
user, err := getUserName(h, userID)
if err != nil {
logrus.WithError(err).Error("failed getting user id")
return
}
userID = user.ID
userName = user.DisplayName
}
if outputFolder != "" {
err := os.Chdir(outputFolder)
if err != nil {
logrus.WithError(err).Error("failed changing directory")
return
}
}
CreateDirIfNotExist(userName)
videos, err := getVideos(h, userID)
if err != nil {
logrus.WithError(err).Error("failed creating twitch helix client")
return
}
logrus.Infof("got %d VOD's", len(videos))
workChan := make(chan helix.Video, len(videos))
var wg sync.WaitGroup
wg.Add(len(videos))
logrus.Infof("starting %d workers", maxWorkers)
bar := progressbar.NewOptions(len(videos), progressbar.OptionSetRenderBlankState(true))
// Render the current state, which is 0% in this case
bar.RenderBlank()
for i := 0; i < maxWorkers; i++ {
go func(pb *progressbar.ProgressBar, wg *sync.WaitGroup) {
for vod := range workChan {
download(vod, userName)
wg.Done()
pb.Add(1)
}
}(bar, &wg)
}
for _, vod := range videos {
// download vod
workChan <- vod
}
wg.Wait()
}
// Config ...
type Config struct {
ClientID string `toml:"client_id"`
}
func createHelixClient(clientID string) (*helix.Client, error) {
return helix.NewClient(&helix.Options{
ClientID: clientID,
})
}
func getUserID(client *helix.Client, user string) (*helix.User, error) {
response, err := client.GetUsers(&helix.UsersParams{
Logins: []string{user},
})
if err != nil {
return nil, err
}
return &response.Data.Users[0], nil
}
func getUserName(client *helix.Client, userID string) (*helix.User, error) {
response, err := client.GetUsers(&helix.UsersParams{
IDs: []string{userID},
})
if err != nil {
return nil, err
}
return &response.Data.Users[0], nil
}
func getVideos(client *helix.Client, userID string) ([]helix.Video, error) {
logrus.Info("getting VOD's...")
curser := ""
var videos []helix.Video
for {
response, err := client.GetVideos(&helix.VideosParams{
First: 100,
After: curser,
UserID: userID,
})
if err != nil {
return nil, err
}
for _, vod := range response.Data.Videos {
if !containsVOD(videos, vod.ID) {
videos = append(videos, vod)
}
}
if response.Data.Pagination.Cursor == "" || response.Data.Pagination.Cursor == curser {
break
}
curser = response.Data.Pagination.Cursor
time.Sleep(1 * time.Second)
}
return videos, nil
}
func containsVOD(vods []helix.Video, id string) bool {
for _, v := range vods {
if id == v.ID {
return true
}
}
return false
}
func download(vod helix.Video, userName string) {
c, _ := os.Getwd()
// 2018-03-02T20:53:41Z
created, _ := time.Parse(time.RFC3339, vod.CreatedAt)
folder := filepath.Join(c, userName, created.Format("2006-01-02"))
CreateDirIfNotExist(folder)
args := []string{
"--no-part",
"-c",
"--write-info-json",
vod.URL,
}
cmd := exec.Command("youtube-dl", args...)
cmd.Dir = folder
if out, err := cmd.CombinedOutput(); err != nil {
logrus.WithError(err).Error("failed downloading vod")
logrus.Errorf("%s", out)
}
info, err := json.MarshalIndent(vod, "", "\t")
if err != nil {
return
}
ioutil.WriteFile(filepath.Join(userName, created.Format("2006-01-02"), "metadata.json"), info, 0755)
}
// CreateDirIfNotExist ...
func CreateDirIfNotExist(dir string) {
if _, err := os.Stat(dir); os.IsNotExist(err) {
err = os.MkdirAll(dir, 0755)
if err != nil {
panic(err)
}
}
}