forked from zsakvo/dhbooker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
202 lines (188 loc) · 6.97 KB
/
http.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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"github.com/schollz/progressbar"
"github.com/tidwall/gjson"
)
var quit = make(chan int)
func httpGet(url string) string {
resp, err := http.Get(url)
check(err)
defer resp.Body.Close()
s, err := ioutil.ReadAll(resp.Body)
check(err)
return string(s)
}
func httpPost(url string, content string) string {
resp, err := http.Post(url,
"application/x-www-form-urlencoded",
strings.NewReader(content))
check(err)
body, err := ioutil.ReadAll(resp.Body)
check(err)
return string(body)
}
//账号登入
func loginByPass(account accountSettings) {
name := account.username
pass := account.password
url := "https://app.hbooker.com/signup/login?login_name=" + name + "&app_version=" + appVersion + "&passwd=" + pass
body := httpGet(url)
body = decode(body, initEncryptKey)
code := gjson.Get(body, "code").String()
switch {
case code == "210002":
fmt.Println("用户不存在,请重写配置文件")
case code == "210003":
fmt.Println("密码错误,请重写配置文件")
case code == "100000":
localToken := gjson.Get(body, "data").String()
setConfig("token", "token", localToken)
writeConfig()
token.readerID = gjson.Get(localToken, "reader_info.reader_id").String()
token.loginToken = gjson.Get(localToken, "login_token").String()
token.account = gjson.Get(localToken, "reader_info.account").String()
clear()
}
}
//凭证登入
func loginByToken(localToken string) {
token.readerID = gjson.Get(localToken, "reader_info.reader_id").String()
token.loginToken = gjson.Get(localToken, "login_token").String()
token.account = gjson.Get(localToken, "reader_info.account").String()
url := "https://app.hbooker.com/reader/get_my_info?reader_id=" + token.readerID + "&app_version=" + appVersion + "&login_token=" + token.loginToken + "&account=" + token.account
body := httpGet(url)
body = decode(body, initEncryptKey)
code := gjson.Get(body, "code").String()
if code != "100000" {
fmt.Println(gjson.Get(body, "tip"))
fmt.Println("凭证登入失败,尝试使用用户密码登入")
account := getAccountSettings()
loginByPass(account)
} else {
clear()
}
}
//获取书籍名称
func getBookInfo() {
bookInfoURL := "https://app.hbooker.com/book/get_info_by_id?module_id=&tab_type=&app_version=" + appVersion + "&recommend=&carousel_position=&book_id=" + bookID + "&login_token=" + token.loginToken + "&book_id=" + bookID + "&account=" + token.account
body := httpGet(bookInfoURL)
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)
}
bookName = gjson.Get(body, "data.book_info.book_name").String()
bookAuthor = gjson.Get(body, "data.book_info.author_name").String()
bookCoverURL = gjson.Get(body, "data.book_info.cover").String()
}
//获取分卷信息
func getBookRolls() ([]gjson.Result, int) {
url := "https://app.hbooker.com/book/get_division_list?app_version=" + appVersion + "&login_token=" + token.loginToken + "&book_id=" + bookID + "&account=" + token.account
body := httpGet(url)
body = decode(body, initEncryptKey)
rolls := gjson.Get(body, "data.division_list.#.division_id").Array()
num := len(rolls)
return rolls, num
}
//获取章节信息
func getChapters(rolls []gjson.Result) ([]gjson.Result, int) {
for _, roll := range rolls {
content := "last_update_time=0&app_version=" + appVersion + "&division_id=" + roll.String() + "&login_token=" + token.loginToken + "&account=" + token.account
body := httpPost("https://app.hbooker.com/chapter/get_updated_chapter_by_division_id", content)
body = decode(body, initEncryptKey)
chapterIDs = append(chapterIDs, gjson.Get(body, "data.chapter_list.#.chapter_id").Array()...)
}
num := len(chapterIDs)
return chapterIDs, num
}
//获取章节内容
func getChapterContent(chapterID string) (string, string, bool) {
contentKeyURL := "https://app.hbooker.com/chapter/get_chapter_cmd?app_version=" + appVersion + "&chapter_id=" + chapterID + "&login_token=" + token.loginToken + "&account=" + token.account
keyBody := httpGet(contentKeyURL)
keyBody = decode(keyBody, initEncryptKey)
contentKey := gjson.Get(keyBody, "data.command").String()
contentURL := "https://app.hbooker.com/chapter/get_cpt_ifm?chapter_command=" + contentKey + "&app_version=" + appVersion + "&chapter_id=" + chapterID + "&login_token=" + token.loginToken + "&account=" + token.account
contentBody := httpGet(contentURL)
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 len(content) == 0 {
invalidChapters.Store(chapterID, "1")
return "", "", false
}
if auth == "0" {
invalidChapters.Store(chapterID, "1")
return "", "", false
}
validChapterNum++
content = decode(content, contentKey)
validChapterIDs = append(validChapterIDs, chapterID)
if downloadType == "epub" {
titleElement := "<h2 id=\"title\" class=\"titlel2std\">" + chapterTitle + "</h2>"
content = strings.Replace(content, " ", "<p class=\"a\"> ", -1)
content = strings.Replace(content, "\n", "</p>", -1)
chapterTitles = append(chapterTitles, chapterTitle)
// chapters[chapterID] = chapterTitle
chapters.Store(chapterID, chapterTitle)
return chapterTitle, contentHeader + "\n" + titleElement + "\n" + content + "\n" + contentFooter, true
}
return chapterTitle, chapterTitle + "\n\n" + content + "\n\n\n\n", true
}
//下载索引
var di int
//写出章节缓存
func writeChapterTemp(chapterID string) {
var tmpPath string
var fileName string
if downloadType == "epub" {
tmpPath = bookTmpPath + "OEBPS" + "/"
fileName = "chapter" + chapterID + ".html"
} else {
tmpPath = bookTmpPath + "/"
fileName = chapterID + ".txt"
}
_, content, result := getChapterContent(chapterID)
if result {
writeOut(content, tmpPath, fileName)
}
bar.Add(1)
if di == bookChapterNum {
quit <- 1
}
di++
}
//下载章节
func downloadChapters(chapterIDs []gjson.Result) {
di = 1
bar = *progressbar.New(len(chapterIDs))
for _, chapterID := range chapterIDs {
go writeChapterTemp(chapterID.String())
}
<-quit
if downloadType == "epub" {
coverElement := coverHeader + "\n" + "<img src=\"cover.jpg\" alt=\"" + bookName + "\" />" + coverFooter
coverBody := httpGet(bookCoverURL)
writeOut(mimetype, bookTmpPath, "mimetype")
writeOut(container, bookTmpPath+"/META-INF/", "container.xml")
writeOut(coverElement, bookTmpPath+"/OEBPS/", "cover.html")
writeOut(coverBody, bookTmpPath+"/OEBPS/", "cover.jpg")
writeOut(css, bookTmpPath+"/OEBPS/", "style.css")
writeOut(genBookToc(), bookTmpPath+"/OEBPS/", "book-toc.html")
writeOut(genContentOpf(), bookTmpPath+"/OEBPS/", "content.opf")
writeOut(genTocNcx(), bookTmpPath+"/OEBPS/", "toc.ncx")
}
if downloadType == "epub" {
compressEpub(bookTmpPath, path.out+"/"+bookName+".epub")
} else {
mergeTemp()
}
bar.Finish()
}