-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
106 lines (86 loc) · 2.04 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
package clipboard_yt_dl
import (
"github.com/beeker1121/goque"
"log"
"net/url"
"time"
)
// create new ClipboardYtDl instance
func NewClipboardYtDl() *ClipboardYtDl {
return &ClipboardYtDl{queue: openQueue()}
}
// open queue database
func openQueue() *goque.Queue {
queue, err := goque.OpenQueue("data_dir")
if err != nil {
panic(err)
}
return queue
}
type ClipboardYtDl struct {
queue *goque.Queue
stopCh chan struct{}
}
// iterate over each item in queue if download is enabled
func (c *ClipboardYtDl) StartQueue(stopCh <-chan bool, callback func(video *Video, length uint64)) {
for {
select {
case <-stopCh:
return
default:
time.Sleep(time.Second)
if c.queue.Length() > 0 {
item, err := c.queue.Dequeue()
if err != nil {
panic(err)
}
copiedUrl, err := url.Parse(item.ToString())
if err != nil {
panic(err)
}
video := c.downloadVideo(copiedUrl)
if video != nil {
callback(video, c.queue.Length())
}
}
}
}
}
// stop processing queue
func (c *ClipboardYtDl) StopQueue(stopCh chan bool) {
stopCh <- true
}
// delete the queue database and open new queue
func (c *ClipboardYtDl) ClearQueue() {
c.queue.Drop()
c.queue = openQueue()
}
// add video object to queue
func (c *ClipboardYtDl) EnqueueVideo(url *url.URL) (*goque.Item, error) {
return c.queue.EnqueueString(url.String())
}
// retrieve amount of queued videos
func (c *ClipboardYtDl) VideoLength() uint64 {
return c.queue.Length()
}
// this method will download url
func (c *ClipboardYtDl) downloadVideo(url *url.URL) *Video {
log.Printf("INFO: %s downloading ... \n", url.String())
dl := YouTubeDl{}
video, err := dl.Download(url)
if err != nil {
switch err {
case UnsupportedError, UnknownServiceError, SSLCertificateVerifyFailedError:
log.Printf("ERROR: %s %s \n", url, err.Error())
return nil
default:
panic(err)
}
}
log.Printf("INFO: %s finished download to \"%s\" \n", url.String(), video.Filename)
return video
}
// close queue database
func (c *ClipboardYtDl) CloseQueue() {
c.queue.Close()
}