-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.go
183 lines (154 loc) · 3.73 KB
/
run.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
package cmd
import (
"YTDownloaderCli/pkg/worker"
"context"
cr "crypto/rand"
"fmt"
"github.com/kkdai/youtube/v2"
"github.com/spf13/cobra"
"github.com/vbauerster/mpb/v8"
"github.com/vbauerster/mpb/v8/decor"
"io"
"math/rand"
"os"
"sync"
"time"
)
func NewTextRun() *cobra.Command {
cmd := &cobra.Command{
Use: "test",
Short: "short test of how to use it",
Run: func(cmd *cobra.Command, args []string) {
main3()
},
}
return cmd
}
func main3() {
var wg sync.WaitGroup
client := &youtube.Client{}
wg.Add(3)
go down(client, &wg, "Xgs2WCHkmbQ")
go down(client, &wg, "1PxhTfmEyQ8")
go down(client, &wg, "BaW_jenozKc")
wg.Wait()
}
func down(client *youtube.Client, wg *sync.WaitGroup, s string) {
defer wg.Done()
videoID := s
video, err := client.GetVideo(videoID)
if err != nil {
panic(err)
}
formats := video.Formats.WithAudioChannels() // only get videos with audio
stream, _, err := client.GetStream(video, &formats[0])
if err != nil {
panic(err)
}
defer stream.Close()
file, err := os.Create(fmt.Sprintf("%s.mp4", videoID))
if err != nil {
panic(err)
}
defer file.Close()
_, err = io.Copy(file, stream)
if err != nil {
panic(err)
}
}
type testTask struct {
vtype int
p *mpb.Progress
}
func (t *testTask) Process(ctx context.Context) {
if t.vtype == 1 {
bar := t.p.New(0,
mpb.BarStyle().Rbound("|"),
mpb.PrependDecorators(
decor.Name("playlist-id", decor.WCSyncWidth, decor.WCSyncSpaceR),
decor.Name("task", decor.WC{C: decor.DindentRight | decor.DextraSpace}),
decor.OnCompleteOrOnAbort(
decor.Counters(
decor.SizeB1024(0), "% .2f / % .2f"),
"Done!",
),
),
)
simulateDownload(bar) // simulate video download
} else {
queue := make([]*mpb.Bar, 2)
queue[0] = t.p.New(0,
mpb.BarStyle().Rbound("|"),
mpb.PrependDecorators(
decor.Name("playlist-id", decor.WCSyncWidth, decor.WCSyncSpaceR),
decor.Name("task", decor.WC{C: decor.DindentRight | decor.DextraSpace}),
decor.OnCompleteOrOnAbort(
decor.Counters(
decor.SizeB1024(0), "% .2f / % .2f"),
"Done!",
),
),
)
simulateDownload(queue[0]) // simulate video download
queue[1] = t.p.New(0,
mpb.BarStyle().Rbound("|"),
mpb.BarQueueAfter(queue[0]),
mpb.PrependDecorators(
decor.Name("task", decor.WC{C: decor.DindentRight | decor.DextraSpace}),
decor.Name("Playlist-id", decor.WCSyncWidth, decor.WCSyncSpaceR),
decor.OnCompleteOrOnAbort(
decor.Counters(
decor.SizeB1024(0), "% .2f / % .2f"),
"Done!",
),
),
)
// Download audio if video format is chosen
simulateDownload(queue[1]) // simulate audio download j
}
}
// simulateDownload simulates a download task by updating progress.
func simulateDownload(bar *mpb.Bar) {
var total int64 = 64 * 1024 * 1024
bar.SetTotal(total, false)
defer bar.SetTotal(total, true)
r, w := io.Pipe()
go func() {
for i := 0; i < 1024; i++ {
_, _ = io.Copy(w, io.LimitReader(cr.Reader, 64*1024))
sleepDuration := time.Duration(rand.Intn(40)+10) * time.Millisecond
time.Sleep(sleepDuration)
}
w.Close()
}()
proxyReader := bar.ProxyReader(r)
defer proxyReader.Close()
// copy from proxyReader, ignoring errors
_, _ = io.Copy(io.Discard, proxyReader)
//total := 100
//
//bar.SetTotal(100, false)
//for i := 0; i < total; i++ {
// bar.Increment()
// time.Sleep(time.Duration(rand.Intn(80)) * time.Millisecond) // simulate variable download speed
//}
//bar.SetTotal(100, true)
}
func main2() {
var tasks []worker.Task
p := mpb.New(mpb.WithWidth(64), mpb.WithAutoRefresh())
for i := 0; i < 3; i++ {
tasks = append(
tasks,
&testTask{vtype: i, p: p},
)
}
pool := worker.Pool{
Tasks: tasks,
MaxConcurrent: 2,
}
pool.Run()
p.Wait()
}
func main4() {
}