This repository has been archived by the owner on Jan 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathciweimao.go
209 lines (194 loc) · 5.61 KB
/
ciweimao.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
package main
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/tidwall/gjson"
pb "gopkg.in/cheggaaa/pb.v1"
)
var downloadIndex int
var downIndexChan (chan int)
var downloadFailedChan (chan string)
var channelClosed = false
//获取书籍信息
func getBookInfo() {
if len(book.id) == 2 {
println("请至少输入书籍 ID")
os.Exit(1)
}
bookInfoURL := "https://app.hbooker.com/book/get_info_by_id"
paramsMap := map[string]string{"app_version": appVersion, "login_token": token.loginToken, "book_id": book.id, "account": token.account}
res, err := httpGet(bookInfoURL, paramsMap)
if err != nil {
panic(err)
}
body, err := getBody(res)
check(err)
body = decode(body, initEncryptKey)
code := gjson.Get(body, "code").String()
if code != "100000" {
tip := gjson.Get(body, "tip").String()
fmt.Println(tip)
os.Exit(1)
}
book.name = gjson.Get(body, "data.book_info.book_name").String()
book.author = gjson.Get(body, "data.book_info.author_name").String()
book.coverURL = gjson.Get(body, "data.book_info.cover").String()
book.tmpPath = path.tmp + pathSeparator + book.name + pathSeparator
chapterListURL := "https://www.ciweimao.com/chapter-list/" + book.id + "/book_detail"
chapterListRes, err := httpGet(chapterListURL, nil)
if err != nil {
panic(err)
}
doc, err := goquery.NewDocumentFromReader(chapterListRes.Body)
if err != nil {
panic(err)
}
doms := doc.Find("ul[class=book-chapter-list]>li>a")
book.chapterNum = doms.Length()
doms.Each(func(i int, selection *goquery.Selection) {
attr, _ := selection.Attr("href")
id := strings.Replace(attr, "https://www.ciweimao.com/chapter/", "", -1)
book.chapterIDs = append(book.chapterIDs, id)
})
fmt.Println("《" + book.name + "》," + "共" + strconv.Itoa(book.chapterNum) + "章")
}
//下载章节
func downloadChapters() {
downloadIndex = 0
downIndexChan = make(chan int)
downloadFailedChan = make(chan string)
bar = pb.StartNew(book.chapterNum)
bar.ShowTimeLeft = false
// bar.SetWidth(80)
for _, chapterID := range book.chapterIDs {
go writeChapterTemp(chapterID, book.chapterNum)
}
for {
if channelClosed {
return
}
select {
case ix, _ := <-downIndexChan:
if ix < 2 {
bar.Increment()
}
downloadIndex++
case df, _ := <-downloadFailedChan:
download.failedChapters = append(download.failedChapters, df)
}
watchChan(book.chapterNum)
}
}
func redownlodChapters() {
downloadIndex = 0
chapterIDs := download.failedChapters
download.failedChapters = download.failedChapters[:0]
for _, chapterID := range chapterIDs {
go writeChapterTemp(chapterID, len(chapterIDs))
}
for {
if channelClosed {
return
}
select {
case ix, _ := <-downIndexChan:
if ix < 2 {
bar.Increment()
}
downloadIndex++
case df, _ := <-downloadFailedChan:
download.failedChapters = append(download.failedChapters, df)
}
watchChan(len(chapterIDs))
}
}
func watchChan(num int) {
if channelClosed {
return
}
if downloadIndex == num {
if len(download.failedChapters) == 0 {
close(downIndexChan)
close(downloadFailedChan)
channelClosed = true
genBook()
}
redownlodChapters()
}
}
//获取章节内容
func getChapterContent(chapterID string) (string, int) {
contentKeyURL := "https://app.hbooker.com/chapter/get_chapter_cmd?app_version=" + appVersion + "&chapter_id=" + chapterID + "&login_token=" + token.loginToken + "&account=" + token.account
paramsMap := map[string]string{"app_version": appVersion, "chapter_id": chapterID, "login_token": token.loginToken, "account": token.account}
keyRes, err := httpGet(contentKeyURL, paramsMap)
if err != nil {
return "", 2
}
keyBody, err := getBody(keyRes)
if err != nil {
return "", 2
}
keyBody = decode(keyBody, initEncryptKey)
contentKey := gjson.Get(keyBody, "data.command").String()
contentURL := "https://app.hbooker.com/chapter/get_cpt_ifm"
paramsMap1 := map[string]string{"chapter_command": contentKey, "app_version": appVersion, "login_token": token.loginToken, "chapter_id": chapterID, "account": token.account}
contentRes, err := httpGet(contentURL, paramsMap1)
if err != nil {
return "", 2
}
contentBody, err := getBody(contentRes)
if err != nil {
return "", 2
}
contentBody = decode(contentBody, initEncryptKey)
chapterTitle := gjson.Get(contentBody, "data.chapter_info.chapter_title").String()
content := gjson.Get(contentBody, "data.chapter_info.txt_content").String()
auth := gjson.Get(contentBody, "data.chapter_info.auth_access").String()
if auth == "0" {
book.invalidChapters.Store(chapterID, nil)
return "", 1
}
content = decode(content, contentKey)
if book.format == "epub" {
titleElement := "<h2 id=\"title\" class=\"titlel2std\">" + chapterTitle + "</h2>"
content = strings.Replace(content, " ", "<p class=\"a\"> ", -1)
content = strings.Replace(content, "\n", "</p>", -1)
book.chapters.Store(chapterID, chapterTitle)
return contentHeader + "\n" + titleElement + "\n" + content + "\n" + contentFooter, 0
}
return chapterTitle + "\n\n" + content + "\n\n\n\n", 0
}
//写出章节缓存
func writeChapterTemp(chapterID string, num int) {
var tmpPath string
var fileName string
if book.format == "epub" {
tmpPath = book.tmpPath + "OEBPS" + pathSeparator
fileName = "chapter" + chapterID + ".html"
} else {
tmpPath = book.tmpPath
fileName = chapterID + ".txt"
}
content, code := getChapterContent(chapterID)
switch {
case code == 0:
writeOut(content, tmpPath, fileName)
case code == 1:
break
case code == 2:
downloadFailedChan <- chapterID
}
downIndexChan <- code
}
func genBook() {
switch book.format {
case "epub":
genEpub()
case "txt":
mergeTemp()
}
bar.Finish()
}