forked from missdeer/getnovel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dlutil.go
194 lines (178 loc) · 4.74 KB
/
dlutil.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
package main
import (
"context"
"fmt"
"io/ioutil"
"log"
"math"
"os"
"sync/atomic"
"github.com/dfordsoft/golib/ebook"
"github.com/google/uuid"
"golang.org/x/sync/semaphore"
)
type contentUtil struct {
index int
title string
link string
content string
}
type downloadUtil struct {
downloader func(string) []byte
generator ebook.IBook
tempDir string
currentPage int32
maxPage int32
quit chan bool
content chan contentUtil
buffer []contentUtil
startContent *contentUtil
endContent *contentUtil
ctx context.Context
semaphore *semaphore.Weighted
}
func newDownloadUtil(dl func(string) []byte, generator ebook.IBook) (du *downloadUtil) {
du = &downloadUtil{
downloader: dl,
generator: generator,
quit: make(chan bool),
ctx: context.TODO(),
semaphore: semaphore.NewWeighted(opts.ParallelCount),
content: make(chan contentUtil),
}
if opts.FromChapter != 0 {
du.startContent = &contentUtil{index: opts.FromChapter}
}
if opts.FromTitle != "" {
du.startContent = &contentUtil{title: opts.FromTitle, index: math.MaxInt32}
}
if opts.ToChapter != 0 {
du.endContent = &contentUtil{index: opts.ToChapter}
}
if opts.ToTitle != "" {
du.endContent = &contentUtil{title: opts.ToTitle}
}
var err error
du.tempDir, err = ioutil.TempDir("", uuid.New().String())
if err != nil {
log.Fatal("creating temporary directory failed", err)
}
return
}
func (du *downloadUtil) wait() {
<-du.quit
os.RemoveAll(du.tempDir)
}
func (du *downloadUtil) preprocessURL(index int, title string, link string) (returnImmediately bool, reachEnd bool) {
atomic.StoreInt32(&du.maxPage, int32(index))
if du.startContent != nil {
if du.startContent.index == index {
du.startContent.title = title
du.startContent.link = link
atomic.StoreInt32(&du.currentPage, int32(index-1))
}
if du.startContent.title == title {
du.startContent.index = index
du.startContent.link = link
atomic.StoreInt32(&du.currentPage, int32(index-1))
}
if du.startContent.index > index {
return true, false
}
}
if du.endContent != nil {
if du.endContent.index == index {
du.endContent.title = title
du.endContent.link = link
}
if du.endContent.title == title {
du.endContent.index = index
du.endContent.link = link
}
atomic.StoreInt32(&du.maxPage, int32(du.endContent.index))
if index > du.endContent.index && du.endContent.index != 0 {
return true, true
}
}
return false, false
}
func (du *downloadUtil) addURL(index int, title string, link string) (reachEnd bool) {
if r, e := du.preprocessURL(index, title, link); r == true {
return e
}
// semaphore
du.semaphore.Acquire(du.ctx, 1)
go func() {
filePath := fmt.Sprintf("%s/%d.txt", du.tempDir, index)
contentFd, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
log.Println("opening file", filePath, "for writing failed ", err)
return
}
contentFd.Write(du.downloader(link))
contentFd.Close()
du.content <- contentUtil{
index: index,
title: title,
link: link,
content: filePath,
}
du.semaphore.Release(1)
}()
return false
}
func (du *downloadUtil) bufferHandler(cu contentUtil) (exit bool) {
fmt.Println(cu.title, cu.link)
// insert into local buffer
if len(du.buffer) == 0 || du.buffer[0].index > cu.index {
// push front
du.buffer = append([]contentUtil{cu}, du.buffer...)
// check local buffer to pick items to generator
for ; len(du.buffer) > 0 && int32(du.buffer[0].index) == atomic.LoadInt32(&du.currentPage)+1; du.buffer = du.buffer[1:] {
contentFd, err := os.OpenFile(du.buffer[0].content, os.O_RDONLY, 0644)
if err != nil {
log.Println("opening file ", du.buffer[0].content, " for reading failed ", err)
continue
}
contentC, err := ioutil.ReadAll(contentFd)
contentFd.Close()
if err != nil {
log.Println("reading file ", du.buffer[0].content, " failed ", err)
continue
}
os.Remove(du.buffer[0].content)
du.generator.AppendContent(du.buffer[0].title, du.buffer[0].link, string(contentC))
atomic.AddInt32(&du.currentPage, 1)
}
if atomic.LoadInt32(&du.currentPage) == atomic.LoadInt32(&du.maxPage) {
du.quit <- true
return true
}
return false
}
if du.buffer[len(du.buffer)-1].index < cu.index {
// push back
du.buffer = append(du.buffer, cu)
return false
}
for i := 0; i < len(du.buffer)-1; i++ {
if du.buffer[i].index < cu.index && du.buffer[i+1].index > cu.index {
// insert at i+1
du.buffer = append(du.buffer[:i+1], append([]contentUtil{cu}, du.buffer[i+1:]...)...)
return false
}
}
return false
}
func (du *downloadUtil) process() {
go func() {
for {
select {
case cu := <-du.content:
if du.bufferHandler(cu) {
return
}
}
}
}()
}