-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.go
158 lines (133 loc) · 2.67 KB
/
queue.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
package main
import (
"fmt"
"io"
"log"
"os"
"path/filepath"
"time"
"github.com/kycklingar/FurLoaderGO/dli"
)
type queue struct {
next chan dli.Submission
stop chan bool
threadCount int
inProgress int
}
func Queue() *queue {
return &queue{next: make(chan dli.Submission), stop: make(chan bool)}
}
var downloadPath = "downloads"
// TODO: improve this
func (q *queue) addIncDL(call func(int) []dli.Submission) {
var i = 0
for {
subs := call(i)
i++
fmt.Println(len(subs))
if len(subs) <= 0 {
break
}
for _, sub := range subs {
q.next <- sub
}
time.Sleep(time.Second * 2)
}
}
// stopThread will stop one downloading thread from the queue
func (q *queue) stopThread() {
q.stop <- true
q.threadCount--
}
// This will stop all running downloading threads from the queue
func (q *queue) stopAll() {
for q.threadCount > 0 {
q.stopThread()
}
}
// startThread will start one downloading thread on the queue
func (q *queue) startThread() {
// I'm still learning, no bully
q.threadCount++
for {
select {
case <-q.stop:
fmt.Println("Stopping the queue")
return
case s := <-q.next:
q.inProgress++
func() {
fmt.Print("Downloading: ", s.ID())
dbkey := s.SiteName() + s.ID()
str := db.Get(dbkey)
if str != "" {
fmt.Printf("\nFound %s in database\n", dbkey)
return
}
time.Sleep(time.Second * 2)
extra, err := s.GetDetails()
if err != nil {
log.Println(err)
return
}
fmt.Printf(" by %s\n", s.User().Name())
for _, esub := range extra {
fmt.Printf("Downloading extra submission %s\n", esub.ID())
if db.Get(esub.SiteName()+esub.ID()) != "" {
return
}
err = q.download(esub)
if err != nil {
log.Println(err)
return
}
db.Store(esub.SiteName()+esub.ID(), esub.FileURL())
time.Sleep(time.Second * 2)
}
err = q.download(s)
if err != nil {
log.Println(err)
return
}
db.Store(dbkey, s.FileURL())
}()
q.inProgress--
}
}
}
func (q *queue) download(sub dli.Submission) error {
body, err := sub.Download()
if err != nil {
log.Println(err)
return err
}
defer body.Close()
path := filepath.Join(
downloadPath,
sub.SiteName(),
sub.User().Name(),
sub.Folder(),
//filepath.Base(sub.Filename()),
)
err = os.MkdirAll(path, os.ModePerm)
if err != nil {
log.Println(err)
return err
}
path = filepath.Join(path, filepath.Base(sub.Filename()))
file, err := os.OpenFile(
path,
os.O_WRONLY|os.O_CREATE|os.O_TRUNC,
0666,
)
if err != nil {
log.Println(err)
return err
}
defer file.Close()
if _, err = io.Copy(file, body); err != nil {
log.Println(err)
return err
}
return nil
}