-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdlutil.go
215 lines (198 loc) · 6.64 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"context"
"fmt"
"io"
"log"
"math"
"net/http"
"net/url"
"os"
"sync/atomic"
"time"
"github.com/google/uuid"
"github.com/missdeer/getnovel/config"
"github.com/missdeer/getnovel/ebook"
"github.com/missdeer/golib/httputil"
"golang.org/x/sync/semaphore"
)
type ContentInfo struct {
Index int
Title string
LinkURL string
ContentFilePath string
}
type DownloadUtil struct {
ContentExtractor func(string, []byte) []byte
ContentLinkPreprocessor func(string) (string, http.Header)
Generator ebook.IBook
TempDir string
CurrentPage int32
MaxPage int32
Quit chan bool
Content chan ContentInfo
Buffer []ContentInfo
StartContent *ContentInfo
EndContent *ContentInfo
Ctx context.Context
Semaphore *semaphore.Weighted
}
func NewDownloadUtil(contentExtractor func(string, []byte) []byte, contentLinkPreprocessor func(string) (string, http.Header), generator ebook.IBook) (dlutil *DownloadUtil) {
dlutil = &DownloadUtil{
ContentExtractor: contentExtractor,
ContentLinkPreprocessor: contentLinkPreprocessor,
Generator: generator,
Quit: make(chan bool),
Ctx: context.TODO(),
Semaphore: semaphore.NewWeighted(config.Opts.ParallelCount),
Content: make(chan ContentInfo),
}
if config.Opts.FromChapter != 0 {
dlutil.StartContent = &ContentInfo{Index: config.Opts.FromChapter}
}
if config.Opts.FromTitle != "" {
dlutil.StartContent = &ContentInfo{Title: config.Opts.FromTitle, Index: math.MaxInt32}
}
if config.Opts.ToChapter != 0 {
dlutil.EndContent = &ContentInfo{Index: config.Opts.ToChapter}
}
if config.Opts.ToTitle != "" {
dlutil.EndContent = &ContentInfo{Title: config.Opts.ToTitle}
}
var err error
dlutil.TempDir, err = os.MkdirTemp("", uuid.New().String())
if err != nil {
log.Fatal("creating temporary directory failed", err)
}
return
}
func (dlutil *DownloadUtil) Wait() {
<-dlutil.Quit
os.RemoveAll(dlutil.TempDir)
}
func (dlutil *DownloadUtil) PreprocessURL(index int, title string, link string) (returnImmediately bool, reachEnd bool) {
atomic.StoreInt32(&dlutil.MaxPage, int32(index))
if dlutil.StartContent != nil {
if dlutil.StartContent.Index == index {
dlutil.StartContent.Title = title
dlutil.StartContent.LinkURL = link
atomic.StoreInt32(&dlutil.CurrentPage, int32(index-1))
}
if dlutil.StartContent.Title == title {
dlutil.StartContent.Index = index
dlutil.StartContent.LinkURL = link
atomic.StoreInt32(&dlutil.CurrentPage, int32(index-1))
}
if dlutil.StartContent.Index > index {
return true, false
}
}
if dlutil.EndContent != nil {
if dlutil.EndContent.Index == index {
dlutil.EndContent.Title = title
dlutil.EndContent.LinkURL = link
}
if dlutil.EndContent.Title == title {
dlutil.EndContent.Index = index
dlutil.EndContent.LinkURL = link
}
atomic.StoreInt32(&dlutil.MaxPage, int32(dlutil.EndContent.Index))
if index > dlutil.EndContent.Index && dlutil.EndContent.Index != 0 {
return true, true
}
}
return false, false
}
func (dlutil *DownloadUtil) AddURL(index int, title string, link string) (reachEnd bool) {
if r, e := dlutil.PreprocessURL(index, title, link); r == true {
return e
}
// semaphore
dlutil.Semaphore.Acquire(dlutil.Ctx, 1)
go func() {
filePath := fmt.Sprintf("%s/%d.txt", dlutil.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
}
theURL, _ := url.Parse(link)
headers := http.Header{
"Referer": []string{fmt.Sprintf("%s://%s", theURL.Scheme, theURL.Host)},
"User-Agent": []string{"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0"},
"Accept": []string{"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"},
"Accept-Language": []string{`en-US,en;q=0.8`},
"Upgrade-Insecure-Requests": []string{"1"},
}
if dlutil.ContentLinkPreprocessor != nil {
link, headers = dlutil.ContentLinkPreprocessor(link)
}
rawPageContent, err := httputil.GetBytes(link, headers, time.Duration(config.Opts.Timeout)*time.Second, config.Opts.RetryCount)
if err != nil {
log.Println("getting chapter content from", link, "failed ", err)
return
}
contentFd.Write(dlutil.ContentExtractor(link, rawPageContent))
contentFd.Close()
dlutil.Content <- ContentInfo{
Index: index,
Title: title,
LinkURL: link,
ContentFilePath: filePath,
}
dlutil.Semaphore.Release(1)
}()
return false
}
func (dlutil *DownloadUtil) BufferHandler(contentInfo ContentInfo) (exit bool) {
fmt.Println(contentInfo.Title, contentInfo.LinkURL)
// insert into local buffer
if len(dlutil.Buffer) == 0 || dlutil.Buffer[0].Index > contentInfo.Index {
// push front
dlutil.Buffer = append([]ContentInfo{contentInfo}, dlutil.Buffer...)
// check local buffer to pick items to generator
for ; len(dlutil.Buffer) > 0 && int32(dlutil.Buffer[0].Index) == atomic.LoadInt32(&dlutil.CurrentPage)+1; dlutil.Buffer = dlutil.Buffer[1:] {
contentFd, err := os.OpenFile(dlutil.Buffer[0].ContentFilePath, os.O_RDONLY, 0644)
if err != nil {
log.Println("opening file ", dlutil.Buffer[0].ContentFilePath, " for reading failed ", err)
continue
}
contentC, err := io.ReadAll(contentFd)
contentFd.Close()
if err != nil {
log.Println("reading file ", dlutil.Buffer[0].ContentFilePath, " failed ", err)
continue
}
os.Remove(dlutil.Buffer[0].ContentFilePath)
dlutil.Generator.AppendContent(dlutil.Buffer[0].Title, dlutil.Buffer[0].LinkURL, string(contentC))
atomic.AddInt32(&dlutil.CurrentPage, 1)
}
if atomic.LoadInt32(&dlutil.CurrentPage) == atomic.LoadInt32(&dlutil.MaxPage) {
dlutil.Quit <- true
return true
}
return false
}
if dlutil.Buffer[len(dlutil.Buffer)-1].Index < contentInfo.Index {
// push back
dlutil.Buffer = append(dlutil.Buffer, contentInfo)
return false
}
for i := 0; i < len(dlutil.Buffer)-1; i++ {
if dlutil.Buffer[i].Index < contentInfo.Index && dlutil.Buffer[i+1].Index > contentInfo.Index {
// insert at i+1
dlutil.Buffer = append(dlutil.Buffer[:i+1], append([]ContentInfo{contentInfo}, dlutil.Buffer[i+1:]...)...)
return false
}
}
return false
}
func (dlutil *DownloadUtil) Process() {
go func() {
for contentInfo := range dlutil.Content {
if dlutil.BufferHandler(contentInfo) {
return
}
}
}()
}